KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > tier > TestFacadeUtil


1 package org.objectweb.jonas.jtests.beans.relation.tier;
2
3 import javax.naming.NamingException JavaDoc;
4 import javax.naming.InitialContext JavaDoc;
5 import javax.rmi.PortableRemoteObject JavaDoc;
6 import java.security.SecureRandom JavaDoc;
7
8 import java.util.Hashtable JavaDoc;
9 import java.net.InetAddress JavaDoc;
10 import java.net.UnknownHostException JavaDoc;
11
12 /**
13  * Utility class for TestFacade.
14  */

15
16 public class TestFacadeUtil {
17
18     private static String JavaDoc JNDI_NAME = "ejb/Test";
19
20     // Home interface lookup methods
21

22     /**
23      * Obtain remote home interface from default initial context
24      * @return Home interface for TestFacade. Lookup using JNDI_NAME
25      */

26     public static TestFacadeHome getHome() throws NamingException JavaDoc {
27         // Obtain initial context
28
InitialContext JavaDoc initialContext = new InitialContext JavaDoc();
29         try {
30             Object JavaDoc objRef = initialContext.lookup(JNDI_NAME);
31             return (TestFacadeHome) PortableRemoteObject.narrow(objRef, TestFacadeHome.class);
32         } finally {
33             initialContext.close();
34         }
35     }
36
37     /**
38      * Obtain remote home interface from parameterised initial context
39      * @param environment Parameters to use for creating initial context
40      * @return Home interface for TestFacade. Lookup using JNDI_NAME
41      */

42     public static TestFacadeHome getHome(Hashtable JavaDoc environment) throws NamingException JavaDoc {
43         // Obtain initial context
44
InitialContext JavaDoc initialContext = new InitialContext JavaDoc(environment);
45         try {
46             Object JavaDoc objRef = initialContext.lookup(JNDI_NAME);
47             return (TestFacadeHome) PortableRemoteObject.narrow(objRef, TestFacadeHome.class);
48         } finally {
49             initialContext.close();
50         }
51     }
52
53     /** Cached per JVM server IP. */
54     private static String JavaDoc hexServerIP = null;
55
56     // initialise the secure random instance
57
private static final SecureRandom JavaDoc seeder = new SecureRandom JavaDoc();
58
59     /**
60      * A 32 byte GUID generator (Globally Unique ID). These artificial keys
61      * SHOULD <strong>NOT </strong> be seen by the user, not even touched by the
62      * DBA but with very rare exceptions, just manipulated by the database and
63      * the programs. Usage: Add an id field (type java.lang.String) to your EJB,
64      * and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
65      */

66     public static final String JavaDoc generateGUID(Object JavaDoc o) {
67         StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
68         if (hexServerIP == null) {
69             InetAddress JavaDoc localInetAddress = null;
70             try {
71                 // get the inet address
72
localInetAddress = InetAddress.getLocalHost();
73             } catch (UnknownHostException JavaDoc uhe) {
74                 System.err
75                         .println("TestFacadeUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
76                 // todo: find better way to get around this...
77
uhe.printStackTrace();
78                 return null;
79             }
80             byte serverIP[] = localInetAddress.getAddress();
81             hexServerIP = hexFormat(getInt(serverIP), 8);
82         }
83         String JavaDoc 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 JavaDoc guid = new StringBuffer JavaDoc(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 JavaDoc hexFormat(int i, int j) {
110         String JavaDoc s = Integer.toHexString(i);
111         return padHex(s, j) + s;
112     }
113
114     private static String JavaDoc padHex(String JavaDoc s, int i) {
115         StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc();
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