001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.keygenerator.uuid;
023:
024: import org.jboss.ejb.plugins.keygenerator.KeyGenerator;
025:
026: import java.net.InetAddress;
027: import java.security.SecureRandom;
028:
029: /**
030: * The implementation of UUID key generator
031: * based on the algorithm from Floyd Marinescu's EJB Design Patterns.
032: *
033: * @author <a href="mailto:loubyansky@ukr.net">Alex Loubyansky</a>
034: *
035: * @version $Revision: 57209 $
036: */
037: public class UUIDKeyGenerator implements KeyGenerator {
038:
039: // Attributes ---------------------------------------------------
040:
041: /** secure random to provide nonrepeating seed */
042: SecureRandom seeder;
043:
044: /** cached middle value */
045: private String midValue;
046:
047: // Constructor --------------------------------------------------
048:
049: public UUIDKeyGenerator() throws Exception {
050: // cache the middle part for UUID
051:
052: StringBuffer buffer = new StringBuffer(16);
053:
054: // construct host part of the uuid (8 hex digits)
055: byte[] addr = InetAddress.getLocalHost().getAddress();
056: buffer.append(toHex(toInt(addr), 8));
057:
058: // append the hash code for this object (8 hex digits)
059: buffer.append(toHex(System.identityHashCode(this ), 8));
060:
061: // set up midValue
062: midValue = buffer.toString();
063:
064: // load up the randomizer
065: seeder = new SecureRandom();
066: int node = seeder.nextInt();
067: }
068:
069: // KeyGenerator implementation ----------------------------------
070:
071: public Object generateKey() {
072: StringBuffer buffer = new StringBuffer(32);
073:
074: // append current time as unsigned int value
075: buffer.append(toHex(
076: (int) (System.currentTimeMillis() & 0xFFFFFFFF), 8));
077:
078: // append cached midValue
079: buffer.append(midValue);
080:
081: // append the next random int
082: buffer.append(toHex(seeder.nextInt(), 8));
083:
084: // return the result
085: return buffer.toString();
086: }
087:
088: // Private ------------------------------------------------------
089:
090: /**
091: * Converts int value to string hex representation
092: */
093: private String toHex(int value, int length) {
094: // hex digits
095: char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7',
096: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
097:
098: StringBuffer buffer = new StringBuffer(length);
099: int shift = (length - 1) << 2;
100: int i = -1;
101: while (++i < length) {
102: buffer.append(hexDigits[(value >> shift) & 0x0000000F]);
103: value <<= 4;
104: }
105: return buffer.toString();
106: }
107:
108: /**
109: * Constructs int value from byte array
110: */
111: private static int toInt(byte[] bytes) {
112: int value = 0;
113: int i = -1;
114: while (++i < bytes.length) {
115: value <<= 8;
116: int b = bytes[i] & 0xff;
117: value |= b;
118: }
119: return value;
120: }
121: }
|