1 4 package org.enhydra.pim.ejb.session; 5 6 11 public class NoteManagerUtil 12 { 13 14 15 private static org.enhydra.pim.ejb.session.NoteManagerLocalHome 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 38 public static org.enhydra.pim.ejb.session.NoteManagerLocalHome getLocalHome() throws javax.naming.NamingException 39 { 40 if (cachedLocalHome == null) { 41 cachedLocalHome = (org.enhydra.pim.ejb.session.NoteManagerLocalHome) lookupHome(null, org.enhydra.pim.ejb.session.NoteManagerLocalHome.JNDI_NAME, org.enhydra.pim.ejb.session.NoteManagerLocalHome.class); 42 } 43 return cachedLocalHome; 44 } 45 46 47 private static String hexServerIP = null; 48 49 private static final java.security.SecureRandom seeder = new java.security.SecureRandom (); 51 52 58 public static final String generateGUID(Object o) { 59 StringBuffer tmpBuffer = new StringBuffer (16); 60 if (hexServerIP == null) { 61 java.net.InetAddress localInetAddress = null; 62 try { 63 localInetAddress = java.net.InetAddress.getLocalHost(); 65 } 66 catch (java.net.UnknownHostException uhe) { 67 System.err.println("NoteManagerUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 68 uhe.printStackTrace(); 70 return null; 71 } 72 byte serverIP[] = localInetAddress.getAddress(); 73 hexServerIP = hexFormat(getInt(serverIP), 8); 74 } 75 String hashcode = hexFormat(System.identityHashCode(o), 8); 76 tmpBuffer.append(hexServerIP); 77 tmpBuffer.append(hashcode); 78 79 long timeNow = System.currentTimeMillis(); 80 int timeLow = (int)timeNow & 0xFFFFFFFF; 81 int node = seeder.nextInt(); 82 83 StringBuffer guid = new StringBuffer (32); 84 guid.append(hexFormat(timeLow, 8)); 85 guid.append(tmpBuffer.toString()); 86 guid.append(hexFormat(node, 8)); 87 return guid.toString(); 88 } 89 90 private static int getInt(byte bytes[]) { 91 int i = 0; 92 int j = 24; 93 for (int k = 0; j >= 0; k++) { 94 int l = bytes[k] & 0xff; 95 i += l << j; 96 j -= 8; 97 } 98 return i; 99 } 100 101 private static String hexFormat(int i, int j) { 102 String s = Integer.toHexString(i); 103 return padHex(s, j) + s; 104 } 105 106 private static String padHex(String s, int i) { 107 StringBuffer tmpBuffer = new StringBuffer (); 108 if (s.length() < i) { 109 for (int j = 0; j < i - s.length(); j++) { 110 tmpBuffer.append('0'); 111 } 112 } 113 return tmpBuffer.toString(); 114 } 115 116 } | Popular Tags |