001: package org.objectweb.jonas.jtests.beans.relation.tier;
002:
003: /**
004: * Utility class for Tiemor.
005: */
006: public class TiemorUtil {
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 Tiemor. Lookup using COMP_NAME
032: */
033: public static TiemorLocalHome getLocalHome()
034: throws javax.naming.NamingException {
035: return (TiemorLocalHome) lookupHome(null,
036: TiemorLocalHome.COMP_NAME, TiemorLocalHome.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
047: * SHOULD <strong>NOT </strong> be seen by the user, not even touched by the
048: * DBA but with very rare exceptions, just manipulated by the database and
049: * the programs. Usage: Add an id field (type java.lang.String) to your EJB,
050: * and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
051: */
052: public static final String generateGUID(Object o) {
053: StringBuffer tmpBuffer = new StringBuffer(16);
054: if (hexServerIP == null) {
055: java.net.InetAddress localInetAddress = null;
056: try {
057: // get the inet address
058:
059: localInetAddress = java.net.InetAddress.getLocalHost();
060: } catch (java.net.UnknownHostException uhe) {
061: System.err
062: .println("TiemorUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
063: // todo: find better way to get around this...
064: uhe.printStackTrace();
065: return null;
066: }
067: byte serverIP[] = localInetAddress.getAddress();
068: hexServerIP = hexFormat(getInt(serverIP), 8);
069: }
070:
071: String hashcode = hexFormat(System.identityHashCode(o), 8);
072: tmpBuffer.append(hexServerIP);
073: tmpBuffer.append(hashcode);
074:
075: long timeNow = System.currentTimeMillis();
076: int timeLow = (int) timeNow & 0xFFFFFFFF;
077: int node = seeder.nextInt();
078:
079: StringBuffer guid = new StringBuffer(32);
080: guid.append(hexFormat(timeLow, 8));
081: guid.append(tmpBuffer.toString());
082: guid.append(hexFormat(node, 8));
083: return guid.toString();
084: }
085:
086: private static int getInt(byte bytes[]) {
087: int i = 0;
088: int j = 24;
089: for (int k = 0; j >= 0; k++) {
090: int l = bytes[k] & 0xff;
091: i += l << j;
092: j -= 8;
093: }
094: return i;
095: }
096:
097: private static String hexFormat(int i, int j) {
098: String s = Integer.toHexString(i);
099: return padHex(s, j) + s;
100: }
101:
102: private static String padHex(String s, int i) {
103: StringBuffer tmpBuffer = new StringBuffer();
104: if (s.length() < i) {
105: for (int j = 0; j < i - s.length(); j++) {
106: tmpBuffer.append('0');
107: }
108: }
109: return tmpBuffer.toString();
110: }
111:
112: }
|