KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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