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