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