Disable Ruby On Rails Sessions

Just a reminder to myself, but maybe it’s usefull for others as well.

Some days ago i stumpled upon the fact, that one can turn off session support in controllers in rails: HowtoChangeSessionOptions (see Disabling Sessions) or HowtoPerActionSessionOptions.

I found it particularly useful and convenient for my Feed controller, which is dedicated entirely to feed generation (yeah, i know about about REST and especially respond_to, but i wouldn’t get rid of thousands of useless sessions without an explicit controller:

class FeedsController < ApplicationController
  session :disabled => true
end

| Comments (0) »

20-Nov-07



Upgrading Mac OS X 10.4 to 10.5

Some more or less unordered thoughts and impressions of the latest iteration of Apples Mac OS X, nicknamed Leopard:

Despite all the naysayers who warn you “don’t upgrade, make a clean install”, i’ve updated two Intel Macs to today without much hassle.

In both cases, the famous “Welcome Movie Thingy” crashed 🙁 Hurray, good first impression.

In a nutshell: Leopard boosts Apache finally to Apache 2 which forced me to reconfigure my development webserver. Timemachine sucks bigtime as it doesn’t work over samba shares at all and over afp shares only with an intermediate disk image which is dead slow.

Parallels works fine, my Pureftpd Manager doesn’t. Share Points is no longer needed as the sharing tab has an option to choose the folders that are shared via samba or afp. Also, all input managers have stopped working (like the great InquisitorX). I guess hacks exists, but i don’t wanna run into trouble the next system upgrade.

I need to adjust myself to the new look and feel. It’s nice, that brushed metal is gone and the widgets are somewhat consistent, but I don’t like the colors: Inactive is too white, active to dark. The new front row app is odd. I like the old one better. The effects in photo booth and iChat feel like an “aaah, nice” and then never use it again feature to me. Muhaha, how funny, standing in a fishbowl.

Some things are nice and handy, spaces for instance and the pimped, system wide dictionary, that lets you search wikipedia. OS X comes bundled with rails now, which is nice. Also very cool is the new screen sharing core service, that lets you remote control other macs. I used Chicken of the VNC before, but that tool didn’t like german chars and right mouse clicks at all. The screen sharing thing is way better.

I’m no benchmarker, but the system feels fast and smooth on a Core Solo Mac Mini and a Core 2 Duo Mac Book, haven’t run it on a G5 yet.

I’m curious, if and when something bad happens to the new system… I did some extensive testing the last 2 days but until now, everything works as it used, it just looks different. I’m wondering if that was money well spent, i’m not sure after all.

The new firewall settings are a joke, aren’t they? I have the terrible impression of a Zone Alarm clone… So now then, it’s time for me to write the ipfw rules per hand, once again. Also the new front row isn’t just odd, it’s crap. iTunes store everywhere… But the funniest thing is, it shows me the top 10 french songs bought on the very day. Not that i was interessted in the german ones, but thats just stupid commercials within a product i already payed for.

| Comments (1) »

27-Oct-07


A fistfull of readers

After presenting a working InputStream on a ByteBuffer, i have to more readers for you out there.

First, the StringBufferReader to efficient read data from a StringBuffer. One can use new java.io.StringReader(sb.toString()) but that would convert the whole StringBuffer (sb) to a string, loosing a whole lotta memory if the string is just big enough. If you can assure that the StringBuffer don’t need to be modified, use the following code:

import java.io.IOException;
import java.io.Reader;
 
public class StringBufferReader extends Reader {
	private int pos;
	private final StringBuffer sb;	
	private boolean closed;
 
	public StringBufferReader(final StringBuffer sb) {
		this.sb = sb;
		this.pos = 0;
		this.closed = false;
	}
 
	@Override
	public void close() throws IOException {
		this.closed = true;
	}
 
	@Override
	public int read(char[] cbuf, int off, int len) throws IOException {
		if(closed)
			throw new IOException("Reader is closed");
		int _len = Math.min(len, sb.length() - pos);		
		sb.getChars(pos, pos + _len, cbuf, off);
		pos += _len;
		return _len == 0 ? -1 : _len;
	}
}

It uses no intermediate buffer.

In the same context i stumbled upon the Byte Order Mark (BOM) in some UTF8 files (especially ones that were created with tools under Microsoft Windows).

The InputReaders and Streams in the JRE don’t skip the BOM, neither does the org.dom4j.io.SAXReader so parsing of such XML files or strings fails with something like “Content not allowed in prolog”. Enter my simple BOMSkippingReader:

import java.io.IOException;
import java.io.Reader;
 
/**
 * This reader skips a possible Byte Order Marker at the 
 * start of UTF8 files which java doesn't.
 * @author michael
 *
 */
public class BOMSkippingReader extends Reader {
	private final Reader decorated;	
	private final boolean rewrite;
	private final int firstchar;
	private int pos = 0;
 
	public BOMSkippingReader(final Reader decorated) throws IOException {
		this.decorated = decorated;
 
		this.firstchar = decorated.read();						
		this.rewrite = firstchar != 65279;			
	}
 
	@Override
	public void close() throws IOException {
		decorated.close();
	}
 
	@Override
	public int read(char[] cbuf, int off, int len) throws IOException {					
		int redone = 0;					
		while(rewrite && pos < 1) {
			cbuf[off + pos++] = (char) firstchar;
			++redone;
		}
		return redone + decorated.read(cbuf, off + redone, len - redone);				
	}
}

If there’s a BOM in the underlying reader, it’s skipped, otherwise written back to the reader. Works well for me and the small while loop doesn’t have much impact on the performance in my app.

| Comments (0) »

23-Oct-07


Java: Creating a Stream on a ByteBuffer

This example is plain wrong. The InputStream will cause an endless loop as it always returns a value >= 0. Working code as follows:

private static InputStream _newInputStream(final ByteBuffer buf) {
	return new InputStream() {
		public synchronized int read() throws IOException {				
			return buf.hasRemaining() ? buf.get() : -1;
		}
 
		public synchronized int read(byte[] bytes, int off, int len) throws IOException {			
			int rv = Math.min(len, buf.remaining());				
			buf.get(bytes, off, rv);
			return rv == 0 ? -1 : rv;
		}
	};
}

| Comments (1) »

18-Oct-07