001: package org.objectweb.jonas.jtests.beans.relation.tier;
002:
003: /**
004: * Utility class for Sequence.
005: */
006: public class SequenceUtil {
007:
008: private static Object lookupHome(java.util.Hashtable environment,
009: String jndiName, Class narrowTo)
010: throws javax.naming.NamingException {
011: // Obtain initial context
012: javax.naming.InitialContext initialContext = new javax.naming.InitialContext(
013: environment);
014: try {
015: Object objRef = initialContext.lookup(jndiName);
016: // only narrow if necessary
017: if (narrowTo.isInstance(java.rmi.Remote.class))
018: return javax.rmi.PortableRemoteObject.narrow(objRef,
019: narrowTo);
020: else
021: return objRef;
022: } finally {
023: initialContext.close();
024: }
025: }
026:
027: // Home interface lookup methods
028:
029: /**
030: * Obtain local home interface from default initial context
031: * @return Local home interface for Sequence. Lookup using COMP_NAME
032: */
033: public static SequenceLocalHome getLocalHome()
034: throws javax.naming.NamingException {
035: return (SequenceLocalHome) lookupHome(null,
036: SequenceLocalHome.COMP_NAME, SequenceLocalHome.class);
037: }
038:
039: /** Cached per JVM server IP. */
040: private static String hexServerIP = null;
041:
042: // initialise the secure random instance
043: private static final java.security.SecureRandom seeder = new java.security.SecureRandom();
044:
045: /**
046: * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
047: * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
048: *
049: * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
050: */
051: public static final String generateGUID(Object o) {
052: StringBuffer tmpBuffer = new StringBuffer(16);
053: if (hexServerIP == null) {
054: java.net.InetAddress localInetAddress = null;
055: try {
056: // get the inet address
057:
058: localInetAddress = java.net.InetAddress.getLocalHost();
059: } catch (java.net.UnknownHostException uhe) {
060: System.err
061: .println("SequenceUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
062: // todo: find better way to get around this...
063: uhe.printStackTrace();
064: return null;
065: }
066: byte serverIP[] = localInetAddress.getAddress();
067: hexServerIP = hexFormat(getInt(serverIP), 8);
068: }
069:
070: String hashcode = hexFormat(System.identityHashCode(o), 8);
071: tmpBuffer.append(hexServerIP);
072: tmpBuffer.append(hashcode);
073:
074: long timeNow = System.currentTimeMillis();
075: int timeLow = (int) timeNow & 0xFFFFFFFF;
076: int node = seeder.nextInt();
077:
078: StringBuffer guid = new StringBuffer(32);
079: guid.append(hexFormat(timeLow, 8));
080: guid.append(tmpBuffer.toString());
081: guid.append(hexFormat(node, 8));
082: return guid.toString();
083: }
084:
085: private static int getInt(byte bytes[]) {
086: int i = 0;
087: int j = 24;
088: for (int k = 0; j >= 0; k++) {
089: int l = bytes[k] & 0xff;
090: i += l << j;
091: j -= 8;
092: }
093: return i;
094: }
095:
096: private static String hexFormat(int i, int j) {
097: String s = Integer.toHexString(i);
098: return padHex(s, j) + s;
099: }
100:
101: private static String padHex(String s, int i) {
102: StringBuffer tmpBuffer = new StringBuffer();
103: if (s.length() < i) {
104: for (int j = 0; j < i - s.length(); j++) {
105: tmpBuffer.append('0');
106: }
107: }
108: return tmpBuffer.toString();
109: }
110:
111: }
|