FileDocCategorySizeDatePackage
SimpleKeyGenerator.javaAPI DocGlassfish v2 API5114Fri May 04 22:32:56 BST 2007com.sun.ejb.base.sfsb

SimpleKeyGenerator

public class SimpleKeyGenerator extends Object implements com.sun.ejb.spi.sfsb.SFSBUUIDUtil
A utility class that generates stateful session keys using two longs The session id generated by this class is guarenteed to be unique as long as the system clock is never reset to a previous value The hashCode of the SessionKey generated by SimpleKeyGenerator also allows uniform distribution of keys when hashed in a HashMap
author
Mahesh Kannan

Fields Summary
private int
idCounter
private long
time
private long
prefix
Constructors Summary
public SimpleKeyGenerator()

        //Inital isCounter value
        this.idCounter = 0;

        //Set the default time value
        this.time = System.currentTimeMillis();

        //Default value for prefix
        this.prefix = 0;
    
Methods Summary
public java.lang.ObjectbyteArrayToKey(byte[] array, int startIndex, int len)
Return the sessionKey that represents the sessionKey. This has to be super efficient as the container calls this method on every invocation. Two objects obtained from identical byte[] must satisfy both o1.equals(o2) and o1.hashCode() == o2.hashCode()

return
the sessionKey object

        long myTime = Utility.bytesToLong(array, startIndex);
        long myId = Utility.bytesToLong(array, startIndex+8);

        return new SimpleSessionKey(myTime, myId);
    
public java.lang.ObjectcreateSessionKey()
Create and return the sessionKey.

return
the sessionKey object

        int id = 0;
        synchronized (this) {
            id = ++idCounter;
            if (id < 0) {
                //idCounter wrapped around!!
                id = idCounter = 0;
                time = System.currentTimeMillis();
            }
        }

        return new SimpleSessionKey(time, prefix + id);
    
public byte[]keyToByteArray(java.lang.Object sessionKey)
Called from the Container before publishing an IOR. The method must convert the sessionKey into a byte[]

return
A byte[] representation of the key. The byte[] could be created using serialization.

        SimpleSessionKey key = (SimpleSessionKey) sessionKey;
        byte[] array = new byte[16];

        Utility.longToBytes(key.time, array, 0);
        Utility.longToBytes(key.id, array, 8);

        return array;
    
public voidsetPrefix(int val)

        this.prefix = val;