1 22 package org.jboss.test.cmp2.batchcascadedelete.ejb; 23 24 25 26 public class ParentUtil 27 { 28 31 private static ParentHome cachedRemoteHome = null; 32 33 36 private static ParentLocalHome cachedLocalHome = null; 37 38 40 45 public static ParentHome getHome() throws javax.naming.NamingException 46 { 47 if(cachedRemoteHome == null) 48 { 49 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (); 51 try 52 { 53 java.lang.Object objRef = initialContext.lookup(ParentHome.JNDI_NAME); 54 cachedRemoteHome = (ParentHome)javax.rmi.PortableRemoteObject.narrow(objRef, ParentHome.class); 55 } 56 finally 57 { 58 initialContext.close(); 59 } 60 } 61 return cachedRemoteHome; 62 } 63 64 70 public static ParentHome getHome(java.util.Hashtable environment) throws javax.naming.NamingException 71 { 72 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (environment); 74 try 75 { 76 java.lang.Object objRef = initialContext.lookup(ParentHome.JNDI_NAME); 77 return (ParentHome)javax.rmi.PortableRemoteObject.narrow(objRef, ParentHome.class); 78 } 79 finally 80 { 81 initialContext.close(); 82 } 83 } 84 85 90 public static ParentLocalHome getLocalHome() throws javax.naming.NamingException 91 { 92 if(cachedLocalHome == null) 94 { 95 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (); 97 try 98 { 99 cachedLocalHome = (ParentLocalHome)initialContext.lookup(ParentLocalHome.JNDI_NAME); 100 } 101 finally 102 { 103 initialContext.close(); 104 } 105 } 106 return cachedLocalHome; 107 } 108 109 112 private static String hexServerIP = null; 113 114 private static final java.security.SecureRandom seeder = new java.security.SecureRandom (); 116 117 123 public static final String generateGUID(Object o) 124 { 125 StringBuffer tmpBuffer = new StringBuffer (16); 126 if(hexServerIP == null) 127 { 128 java.net.InetAddress localInetAddress = null; 129 try 130 { 131 localInetAddress = java.net.InetAddress.getLocalHost(); 133 } 134 catch(java.net.UnknownHostException uhe) 135 { 136 System.err.println("ParentUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 137 uhe.printStackTrace(); 139 return null; 140 } 141 byte serverIP[] = localInetAddress.getAddress(); 142 hexServerIP = hexFormat(getInt(serverIP), 8); 143 } 144 String hashcode = hexFormat(System.identityHashCode(o), 8); 145 tmpBuffer.append(hexServerIP); 146 tmpBuffer.append(hashcode); 147 148 long timeNow = System.currentTimeMillis(); 149 int timeLow = (int)timeNow & 0xFFFFFFFF; 150 int node = seeder.nextInt(); 151 152 StringBuffer guid = new StringBuffer (32); 153 guid.append(hexFormat(timeLow, 8)); 154 guid.append(tmpBuffer.toString()); 155 guid.append(hexFormat(node, 8)); 156 return guid.toString(); 157 } 158 159 private static int getInt(byte bytes[]) 160 { 161 int i = 0; 162 int j = 24; 163 for(int k = 0; j >= 0; k++) 164 { 165 int l = bytes[k] & 0xff; 166 i += l << j; 167 j -= 8; 168 } 169 return i; 170 } 171 172 private static String hexFormat(int i, int j) 173 { 174 String s = Integer.toHexString(i); 175 return padHex(s, j) + s; 176 } 177 178 private static String padHex(String s, int i) 179 { 180 StringBuffer tmpBuffer = new StringBuffer (); 181 if(s.length() < i) 182 { 183 for(int j = 0; j < i - s.length(); j++) 184 { 185 tmpBuffer.append('0'); 186 } 187 } 188 return tmpBuffer.toString(); 189 } 190 191 } | Popular Tags |