1 4 package test.interfaces; 5 6 13 public class CustomerUtil 14 { 15 16 private static test.interfaces.CustomerHome cachedRemoteHome = 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.CustomerHome getHome() throws javax.naming.NamingException 40 { 41 if (cachedRemoteHome == null) { 42 cachedRemoteHome = (test.interfaces.CustomerHome) lookupHome(null, test.interfaces.CustomerHome.COMP_NAME, test.interfaces.CustomerHome.class); 43 } 44 return cachedRemoteHome; 45 } 46 47 52 public static test.interfaces.CustomerHome getHome( java.util.Hashtable environment ) throws javax.naming.NamingException 53 { 54 return (test.interfaces.CustomerHome) lookupHome(environment, test.interfaces.CustomerHome.COMP_NAME, test.interfaces.CustomerHome.class); 55 } 56 57 58 private static String hexServerIP = null; 59 60 private static final java.security.SecureRandom seeder = new java.security.SecureRandom (); 62 63 69 public static final String generateGUID(Object o) { 70 StringBuffer tmpBuffer = new StringBuffer (16); 71 if (hexServerIP == null) { 72 java.net.InetAddress localInetAddress = null; 73 try { 74 76 localInetAddress = java.net.InetAddress.getLocalHost(); 77 } 78 catch (java.net.UnknownHostException uhe) { 79 System.err.println("CustomerUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 80 uhe.printStackTrace(); 82 return null; 83 } 84 byte serverIP[] = localInetAddress.getAddress(); 85 hexServerIP = hexFormat(getInt(serverIP), 8); 86 } 87 88 String hashcode = hexFormat(System.identityHashCode(o), 8); 89 tmpBuffer.append(hexServerIP); 90 tmpBuffer.append(hashcode); 91 92 long timeNow = System.currentTimeMillis(); 93 int timeLow = (int)timeNow & 0xFFFFFFFF; 94 int node = seeder.nextInt(); 95 96 StringBuffer guid = new StringBuffer (32); 97 guid.append(hexFormat(timeLow, 8)); 98 guid.append(tmpBuffer.toString()); 99 guid.append(hexFormat(node, 8)); 100 return guid.toString(); 101 } 102 103 private static int getInt(byte bytes[]) { 104 int i = 0; 105 int j = 24; 106 for (int k = 0; j >= 0; k++) { 107 int l = bytes[k] & 0xff; 108 i += l << j; 109 j -= 8; 110 } 111 return i; 112 } 113 114 private static String hexFormat(int i, int j) { 115 String s = Integer.toHexString(i); 116 return padHex(s, j) + s; 117 } 118 119 private static String padHex(String s, int i) { 120 StringBuffer tmpBuffer = new StringBuffer (); 121 if (s.length() < i) { 122 for (int j = 0; j < i - s.length(); j++) { 123 tmpBuffer.append('0'); 124 } 125 } 126 return tmpBuffer.toString(); 127 } 128 129 } 130 131 | Popular Tags |