1 package org.objectweb.jonas.jtests.beans.relation.tier; 2 3 import javax.naming.NamingException ; 4 import javax.naming.InitialContext ; 5 import javax.rmi.PortableRemoteObject ; 6 import java.security.SecureRandom ; 7 8 import java.util.Hashtable ; 9 import java.net.InetAddress ; 10 import java.net.UnknownHostException ; 11 12 15 16 public class TestFacadeUtil { 17 18 private static String JNDI_NAME = "ejb/Test"; 19 20 22 26 public static TestFacadeHome getHome() throws NamingException { 27 InitialContext initialContext = new InitialContext (); 29 try { 30 Object objRef = initialContext.lookup(JNDI_NAME); 31 return (TestFacadeHome) PortableRemoteObject.narrow(objRef, TestFacadeHome.class); 32 } finally { 33 initialContext.close(); 34 } 35 } 36 37 42 public static TestFacadeHome getHome(Hashtable environment) throws NamingException { 43 InitialContext initialContext = new InitialContext (environment); 45 try { 46 Object objRef = initialContext.lookup(JNDI_NAME); 47 return (TestFacadeHome) PortableRemoteObject.narrow(objRef, TestFacadeHome.class); 48 } finally { 49 initialContext.close(); 50 } 51 } 52 53 54 private static String hexServerIP = null; 55 56 private static final SecureRandom seeder = new SecureRandom (); 58 59 66 public static final String generateGUID(Object o) { 67 StringBuffer tmpBuffer = new StringBuffer (16); 68 if (hexServerIP == null) { 69 InetAddress localInetAddress = null; 70 try { 71 localInetAddress = InetAddress.getLocalHost(); 73 } catch (UnknownHostException uhe) { 74 System.err 75 .println("TestFacadeUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 76 uhe.printStackTrace(); 78 return null; 79 } 80 byte serverIP[] = localInetAddress.getAddress(); 81 hexServerIP = hexFormat(getInt(serverIP), 8); 82 } 83 String hashcode = hexFormat(System.identityHashCode(o), 8); 84 tmpBuffer.append(hexServerIP); 85 tmpBuffer.append(hashcode); 86 87 long timeNow = System.currentTimeMillis(); 88 int timeLow = (int) timeNow & 0xFFFFFFFF; 89 int node = seeder.nextInt(); 90 91 StringBuffer guid = new StringBuffer (32); 92 guid.append(hexFormat(timeLow, 8)); 93 guid.append(tmpBuffer.toString()); 94 guid.append(hexFormat(node, 8)); 95 return guid.toString(); 96 } 97 98 private static int getInt(byte bytes[]) { 99 int i = 0; 100 int j = 24; 101 for (int k = 0; j >= 0; k++) { 102 int l = bytes[k] & 0xff; 103 i += l << j; 104 j -= 8; 105 } 106 return i; 107 } 108 109 private static String hexFormat(int i, int j) { 110 String s = Integer.toHexString(i); 111 return padHex(s, j) + s; 112 } 113 114 private static String padHex(String s, int i) { 115 StringBuffer tmpBuffer = new StringBuffer (); 116 if (s.length() < i) { 117 for (int j = 0; j < i - s.length(); j++) { 118 tmpBuffer.append('0'); 119 } 120 } 121 return tmpBuffer.toString(); 122 } 123 124 } | Popular Tags |