1 4 package test.interfaces; 5 6 13 public class AccountUtil 14 { 15 16 private static test.interfaces.AccountHome cachedRemoteHome = null; 17 18 private static test.interfaces.AccountLocalHome cachedLocalHome = null; 19 20 private static Object lookupHome(java.util.Hashtable environment, String jndiName, Class narrowTo) throws javax.naming.NamingException { 21 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (environment); 23 try { 24 Object objRef = initialContext.lookup(jndiName); 25 if (java.rmi.Remote .class.isAssignableFrom(narrowTo)) 27 return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo); 28 else 29 return objRef; 30 } finally { 31 initialContext.close(); 32 } 33 } 34 35 37 41 public static test.interfaces.AccountHome getHome() throws javax.naming.NamingException 42 { 43 if (cachedRemoteHome == null) { 44 cachedRemoteHome = (test.interfaces.AccountHome) lookupHome(null, test.interfaces.AccountHome.COMP_NAME, test.interfaces.AccountHome.class); 45 } 46 return cachedRemoteHome; 47 } 48 49 54 public static test.interfaces.AccountHome getHome( java.util.Hashtable environment ) throws javax.naming.NamingException 55 { 56 return (test.interfaces.AccountHome) lookupHome(environment, test.interfaces.AccountHome.COMP_NAME, test.interfaces.AccountHome.class); 57 } 58 59 63 public static test.interfaces.AccountLocalHome getLocalHome() throws javax.naming.NamingException 64 { 65 if (cachedLocalHome == null) { 66 cachedLocalHome = (test.interfaces.AccountLocalHome) lookupHome(null, test.interfaces.AccountLocalHome.COMP_NAME, test.interfaces.AccountLocalHome.class); 67 } 68 return cachedLocalHome; 69 } 70 71 72 private static String hexServerIP = null; 73 74 private static final java.security.SecureRandom seeder = new java.security.SecureRandom (); 76 77 83 public static final String generateGUID(Object o) { 84 StringBuffer tmpBuffer = new StringBuffer (16); 85 if (hexServerIP == null) { 86 java.net.InetAddress localInetAddress = null; 87 try { 88 90 localInetAddress = java.net.InetAddress.getLocalHost(); 91 } 92 catch (java.net.UnknownHostException uhe) { 93 System.err.println("AccountUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 94 uhe.printStackTrace(); 96 return null; 97 } 98 byte serverIP[] = localInetAddress.getAddress(); 99 hexServerIP = hexFormat(getInt(serverIP), 8); 100 } 101 102 String hashcode = hexFormat(System.identityHashCode(o), 8); 103 tmpBuffer.append(hexServerIP); 104 tmpBuffer.append(hashcode); 105 106 long timeNow = System.currentTimeMillis(); 107 int timeLow = (int)timeNow & 0xFFFFFFFF; 108 int node = seeder.nextInt(); 109 110 StringBuffer guid = new StringBuffer (32); 111 guid.append(hexFormat(timeLow, 8)); 112 guid.append(tmpBuffer.toString()); 113 guid.append(hexFormat(node, 8)); 114 return guid.toString(); 115 } 116 117 private static int getInt(byte bytes[]) { 118 int i = 0; 119 int j = 24; 120 for (int k = 0; j >= 0; k++) { 121 int l = bytes[k] & 0xff; 122 i += l << j; 123 j -= 8; 124 } 125 return i; 126 } 127 128 private static String hexFormat(int i, int j) { 129 String s = Integer.toHexString(i); 130 return padHex(s, j) + s; 131 } 132 133 private static String padHex(String s, int i) { 134 StringBuffer tmpBuffer = new StringBuffer (); 135 if (s.length() < i) { 136 for (int j = 0; j < i - s.length(); j++) { 137 tmpBuffer.append('0'); 138 } 139 } 140 return tmpBuffer.toString(); 141 } 142 143 } 144 145 | Popular Tags |