1 4 package test.interfaces; 5 6 13 public class AddressUtil 14 { 15 16 private static test.interfaces.AddressLocalHome cachedLocalHome = null; 17 18 private static Object lookupHome(java.util.Hashtable environment, String jndiName, Class narrowTo) throws javax.naming.NamingException { 19 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (environment); 21 try { 22 Object objRef = initialContext.lookup(jndiName); 23 if (java.rmi.Remote .class.isAssignableFrom(narrowTo)) 25 return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo); 26 else 27 return objRef; 28 } finally { 29 initialContext.close(); 30 } 31 } 32 33 35 39 public static test.interfaces.AddressLocalHome getLocalHome() throws javax.naming.NamingException 40 { 41 if (cachedLocalHome == null) { 42 cachedLocalHome = (test.interfaces.AddressLocalHome) lookupHome(null, test.interfaces.AddressLocalHome.COMP_NAME, test.interfaces.AddressLocalHome.class); 43 } 44 return cachedLocalHome; 45 } 46 47 48 private static String hexServerIP = null; 49 50 private static final java.security.SecureRandom seeder = new java.security.SecureRandom (); 52 53 59 public static final String generateGUID(Object o) { 60 StringBuffer tmpBuffer = new StringBuffer (16); 61 if (hexServerIP == null) { 62 java.net.InetAddress localInetAddress = null; 63 try { 64 66 localInetAddress = java.net.InetAddress.getLocalHost(); 67 } 68 catch (java.net.UnknownHostException uhe) { 69 System.err.println("AddressUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 70 uhe.printStackTrace(); 72 return null; 73 } 74 byte serverIP[] = localInetAddress.getAddress(); 75 hexServerIP = hexFormat(getInt(serverIP), 8); 76 } 77 78 String hashcode = hexFormat(System.identityHashCode(o), 8); 79 tmpBuffer.append(hexServerIP); 80 tmpBuffer.append(hashcode); 81 82 long timeNow = System.currentTimeMillis(); 83 int timeLow = (int)timeNow & 0xFFFFFFFF; 84 int node = seeder.nextInt(); 85 86 StringBuffer guid = new StringBuffer (32); 87 guid.append(hexFormat(timeLow, 8)); 88 guid.append(tmpBuffer.toString()); 89 guid.append(hexFormat(node, 8)); 90 return guid.toString(); 91 } 92 93 private static int getInt(byte bytes[]) { 94 int i = 0; 95 int j = 24; 96 for (int k = 0; j >= 0; k++) { 97 int l = bytes[k] & 0xff; 98 i += l << j; 99 j -= 8; 100 } 101 return i; 102 } 103 104 private static String hexFormat(int i, int j) { 105 String s = Integer.toHexString(i); 106 return padHex(s, j) + s; 107 } 108 109 private static String padHex(String s, int i) { 110 StringBuffer tmpBuffer = new StringBuffer (); 111 if (s.length() < i) { 112 for (int j = 0; j < i - s.length(); j++) { 113 tmpBuffer.append('0'); 114 } 115 } 116 return tmpBuffer.toString(); 117 } 118 119 } 120 121 | Popular Tags |