1 package org.objectweb.jonas.jtests.beans.relation.tier; 2 3 6 public class TierUtil { 7 8 private static Object lookupHome(java.util.Hashtable environment, String jndiName, Class narrowTo) 9 throws javax.naming.NamingException { 10 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (environment); 12 try { 13 Object objRef = initialContext.lookup(jndiName); 14 if (narrowTo.isInstance(java.rmi.Remote .class)) 16 return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo); 17 else 18 return objRef; 19 } finally { 20 initialContext.close(); 21 } 22 } 23 24 26 30 public static TierLocalHome getLocalHome() throws javax.naming.NamingException { 31 return (TierLocalHome) lookupHome(null, TierLocalHome.COMP_NAME, TierLocalHome.class); 32 } 33 34 35 private static String hexServerIP = null; 36 37 private static final java.security.SecureRandom seeder = new java.security.SecureRandom (); 39 40 47 public static final String generateGUID(Object o) { 48 StringBuffer tmpBuffer = new StringBuffer (16); 49 if (hexServerIP == null) { 50 java.net.InetAddress localInetAddress = null; 51 try { 52 54 localInetAddress = java.net.InetAddress.getLocalHost(); 55 } catch (java.net.UnknownHostException uhe) { 56 System.err.println("TierUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 57 uhe.printStackTrace(); 59 return null; 60 } 61 byte serverIP[] = localInetAddress.getAddress(); 62 hexServerIP = hexFormat(getInt(serverIP), 8); 63 } 64 65 String hashcode = hexFormat(System.identityHashCode(o), 8); 66 tmpBuffer.append(hexServerIP); 67 tmpBuffer.append(hashcode); 68 69 long timeNow = System.currentTimeMillis(); 70 int timeLow = (int) timeNow & 0xFFFFFFFF; 71 int node = seeder.nextInt(); 72 73 StringBuffer guid = new StringBuffer (32); 74 guid.append(hexFormat(timeLow, 8)); 75 guid.append(tmpBuffer.toString()); 76 guid.append(hexFormat(node, 8)); 77 return guid.toString(); 78 } 79 80 private static int getInt(byte bytes[]) { 81 int i = 0; 82 int j = 24; 83 for (int k = 0; j >= 0; k++) { 84 int l = bytes[k] & 0xff; 85 i += l << j; 86 j -= 8; 87 } 88 return i; 89 } 90 91 private static String hexFormat(int i, int j) { 92 String s = Integer.toHexString(i); 93 return padHex(s, j) + s; 94 } 95 96 private static String padHex(String s, int i) { 97 StringBuffer tmpBuffer = new StringBuffer (); 98 if (s.length() < i) { 99 for (int j = 0; j < i - s.length(); j++) { 100 tmpBuffer.append('0'); 101 } 102 } 103 return tmpBuffer.toString(); 104 } 105 106 } 107 108 | Popular Tags |