FileDocCategorySizeDatePackage
IDGenerator.javaAPI DocApache Lucene 2.1.05852Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.storage

IDGenerator

public class IDGenerator extends Object
This is the main entry ID generator to generate unique ids for each entry. The Generator uses {@link java.security.SecureRandom} Numbers and the {@link java.lang.System#currentTimeMillis()} to create a semi-unique sting; The string will be digested by a {@link java.security.MessageDigest} which returns a byte array. The generator encodes the byte array as a hex string.

The generated Id's will cached in a {@link java.util.concurrent.BlockingQueue} and reproduced if an id has been removed.

author
Simon Willnauer

Fields Summary
final AtomicBoolean
stopped
private final SecureRandom
secureRandom
private final MessageDigest
mdigest
private final BlockingQueue
blockingQueue
private Thread
runner
private static final int
DEFAULT_CAPACITY
protected static final Log
LOGGER
private static final String
RUNNER_THREAD_NAME
Constructors Summary
public IDGenerator(int capacity)
Constructs a new ID generator. with a fixed capacity of prebuild ids. The default capacity is 10. Every given parameter less than 10 will be ignored.

param
capacity - capacity of the prebuild id queue
throws
NoSuchAlgorithmException - if the algorithm does not exist

 
 
                                                                                       
          
 
        this.secureRandom = SecureRandom.getInstance("SHA1PRNG"); 
        this.mdigest = MessageDigest.getInstance("SHA-1"); 
        this.blockingQueue = new ArrayBlockingQueue<String>( 
                (capacity < DEFAULT_CAPACITY ? DEFAULT_CAPACITY : capacity), 
                false); 
        startIDProducer(); 
 
    
Methods Summary
public intgetQueueSize()

return
the current size of the queue

 
        return this.blockingQueue.size(); 
    
public java.lang.StringgetUID()
This method takes a gnerated id from the IDProducer queue and retruns it. If no ID is available this method will wait until an ID is produced. This implementation is thread-safe.

return
a UID
throws
InterruptedException - if interrupted while waiting

 
        return this.blockingQueue.take(); 
    
static java.lang.StringhexEncode(byte[] input)
Encodes a given byte array into a hex string.

param
input - the byte array to encode
return
hex string representation of the given byte array

 
        StringBuffer result = new StringBuffer(); 
        char[] digits = { '0", '1", '2", '3", '4", '5", '6", '7", '8", '9", 
                'a", 'b", 'c", 'd", 'e", 'f" }; 
        for (int idx = 0; idx < input.length; ++idx) { 
            byte b = input[idx]; 
            result.append(digits[(b & 0xf0) >> 4]); 
            result.append(digits[b & 0x0f]); 
        } 
        return result.toString(); 
    
private voidstartIDProducer()

 
        if (this.runner == null) { 
            UIDProducer producer = new UIDProducer(this.blockingQueue, 
                    this.secureRandom, this.mdigest); 
            this.runner = new Thread(producer);
            this.runner.setDaemon(true);
            this.runner.setName(RUNNER_THREAD_NAME);
            this.runner.start(); 
        } 
    
public voidstopIDGenerator()
Stops the id-producer

        this.stopped.set(true);
        this.runner.interrupt();