KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.jonas.jtests.beans.relation.tier;
2
3 /**
4  * Utility class for Tier.
5  */

6 public class TierUtil {
7
8     private static Object JavaDoc lookupHome(java.util.Hashtable JavaDoc environment, String JavaDoc jndiName, Class JavaDoc narrowTo)
9             throws javax.naming.NamingException JavaDoc {
10         // Obtain initial context
11
javax.naming.InitialContext JavaDoc initialContext = new javax.naming.InitialContext JavaDoc(environment);
12         try {
13             Object JavaDoc objRef = initialContext.lookup(jndiName);
14             // only narrow if necessary
15
if (narrowTo.isInstance(java.rmi.Remote JavaDoc.class))
16                 return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo);
17             else
18                 return objRef;
19         } finally {
20             initialContext.close();
21         }
22     }
23
24     // Home interface lookup methods
25

26     /**
27      * Obtain local home interface from default initial context
28      * @return Local home interface for Tier. Lookup using COMP_NAME
29      */

30     public static TierLocalHome getLocalHome() throws javax.naming.NamingException JavaDoc {
31         return (TierLocalHome) lookupHome(null, TierLocalHome.COMP_NAME, TierLocalHome.class);
32     }
33
34     /** Cached per JVM server IP. */
35     private static String JavaDoc hexServerIP = null;
36
37     // initialise the secure random instance
38
private static final java.security.SecureRandom JavaDoc seeder = new java.security.SecureRandom JavaDoc();
39
40     /**
41      * A 32 byte GUID generator (Globally Unique ID). These artificial keys
42      * SHOULD <strong>NOT </strong> be seen by the user, not even touched by the
43      * DBA but with very rare exceptions, just manipulated by the database and
44      * the programs. Usage: Add an id field (type java.lang.String) to your EJB,
45      * and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
46      */

47     public static final String JavaDoc generateGUID(Object JavaDoc o) {
48         StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
49         if (hexServerIP == null) {
50             java.net.InetAddress JavaDoc localInetAddress = null;
51             try {
52                 // get the inet address
53

54                 localInetAddress = java.net.InetAddress.getLocalHost();
55             } catch (java.net.UnknownHostException JavaDoc uhe) {
56                 System.err.println("TierUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
57                 // todo: find better way to get around this...
58
uhe.printStackTrace();
59                 return null;
60             }
61             byte serverIP[] = localInetAddress.getAddress();
62             hexServerIP = hexFormat(getInt(serverIP), 8);
63         }
64
65         String JavaDoc hashcode = hexFormat(System.identityHashCode(o), 8);
66         tmpBuffer.append(hexServerIP);
67         tmpBuffer.append(hashcode);
68
69         long timeNow = System.currentTimeMillis();
70         int timeLow = (int) timeNow & 0xFFFFFFFF;
71         int node = seeder.nextInt();
72
73         StringBuffer JavaDoc guid = new StringBuffer JavaDoc(32);
74         guid.append(hexFormat(timeLow, 8));
75         guid.append(tmpBuffer.toString());
76         guid.append(hexFormat(node, 8));
77         return guid.toString();
78     }
79
80     private static int getInt(byte bytes[]) {
81         int i = 0;
82         int j = 24;
83         for (int k = 0; j >= 0; k++) {
84             int l = bytes[k] & 0xff;
85             i += l << j;
86             j -= 8;
87         }
88         return i;
89     }
90
91     private static String JavaDoc hexFormat(int i, int j) {
92         String JavaDoc s = Integer.toHexString(i);
93         return padHex(s, j) + s;
94     }
95
96     private static String JavaDoc padHex(String JavaDoc s, int i) {
97         StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc();
98         if (s.length() < i) {
99             for (int j = 0; j < i - s.length(); j++) {
100                 tmpBuffer.append('0');
101             }
102         }
103         return tmpBuffer.toString();
104     }
105
106 }
107
108
Popular Tags