1 4 package org.enhydra.pim.ejb.session; 5 6 11 public class ContactManagerUtil 12 { 13 14 15 private static org.enhydra.pim.ejb.session.ContactManagerLocalHome cachedLocalHome = null; 16 17 private static Object lookupHome(java.util.Hashtable environment, String jndiName, Class narrowTo) throws javax.naming.NamingException { 18 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (environment); 20 try { 21 Object objRef = initialContext.lookup(jndiName); 22 if (narrowTo.isInstance(java.rmi.Remote .class)) 24 return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo); 25 else 26 return objRef; 27 } finally { 28 initialContext.close(); 29 } 30 } 31 32 34 35 39 public static org.enhydra.pim.ejb.session.ContactManagerLocalHome getLocalHome() throws javax.naming.NamingException 40 { 41 if (cachedLocalHome == null) { 42 cachedLocalHome = (org.enhydra.pim.ejb.session.ContactManagerLocalHome) lookupHome(null, org.enhydra.pim.ejb.session.ContactManagerLocalHome.JNDI_NAME, org.enhydra.pim.ejb.session.ContactManagerLocalHome.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 localInetAddress = java.net.InetAddress.getLocalHost(); 66 } 67 catch (java.net.UnknownHostException uhe) { 68 System.err.println("ContactManagerUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 69 uhe.printStackTrace(); 71 return null; 72 } 73 byte serverIP[] = localInetAddress.getAddress(); 74 hexServerIP = hexFormat(getInt(serverIP), 8); 75 } 76 String hashcode = hexFormat(System.identityHashCode(o), 8); 77 tmpBuffer.append(hexServerIP); 78 tmpBuffer.append(hashcode); 79 80 long timeNow = System.currentTimeMillis(); 81 int timeLow = (int)timeNow & 0xFFFFFFFF; 82 int node = seeder.nextInt(); 83 84 StringBuffer guid = new StringBuffer (32); 85 guid.append(hexFormat(timeLow, 8)); 86 guid.append(tmpBuffer.toString()); 87 guid.append(hexFormat(node, 8)); 88 return guid.toString(); 89 } 90 91 private static int getInt(byte bytes[]) { 92 int i = 0; 93 int j = 24; 94 for (int k = 0; j >= 0; k++) { 95 int l = bytes[k] & 0xff; 96 i += l << j; 97 j -= 8; 98 } 99 return i; 100 } 101 102 private static String hexFormat(int i, int j) { 103 String s = Integer.toHexString(i); 104 return padHex(s, j) + s; 105 } 106 107 private static String padHex(String s, int i) { 108 StringBuffer tmpBuffer = new StringBuffer (); 109 if (s.length() < i) { 110 for (int j = 0; j < i - s.length(); j++) { 111 tmpBuffer.append('0'); 112 } 113 } 114 return tmpBuffer.toString(); 115 } 116 117 } | Popular Tags |