KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > pim > ejb > session > DataManagerUtil


1 /*
2  * Generated file - Do not edit!
3  */

4 package org.enhydra.pim.ejb.session;
5
6 /**
7  * Utility class for DataManager.
8  * @generated
9  * @lomboz generated
10  */

11 public class DataManagerUtil
12 {
13
14    /** Cached local home (EJBLocalHome). Uses lazy loading to obtain its value (loaded by getLocalHome() methods). */
15    private static org.enhydra.pim.ejb.session.DataManagerLocalHome cachedLocalHome = null;
16
17    private static Object JavaDoc lookupHome(java.util.Hashtable JavaDoc environment, String JavaDoc jndiName, Class JavaDoc narrowTo) throws javax.naming.NamingException JavaDoc {
18       // Obtain initial context
19
javax.naming.InitialContext JavaDoc initialContext = new javax.naming.InitialContext JavaDoc(environment);
20       try {
21          Object JavaDoc objRef = initialContext.lookup(jndiName);
22          // only narrow if necessary
23
if (narrowTo.isInstance(java.rmi.Remote JavaDoc.class))
24             return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo);
25          else
26             return objRef;
27       } finally {
28          initialContext.close();
29       }
30    }
31
32    // Home interface lookup methods
33

34   /**
35     * Obtain local home interface from default initial context
36     * @return Local home interface for DataManager. Lookup using JNDI_NAME
37     */

38    public static org.enhydra.pim.ejb.session.DataManagerLocalHome getLocalHome() throws javax.naming.NamingException JavaDoc
39    {
40       if (cachedLocalHome == null) {
41             cachedLocalHome = (org.enhydra.pim.ejb.session.DataManagerLocalHome) lookupHome(null, org.enhydra.pim.ejb.session.DataManagerLocalHome.JNDI_NAME, org.enhydra.pim.ejb.session.DataManagerLocalHome.class);
42       }
43       return cachedLocalHome;
44    }
45
46    /** Cached per JVM server IP. */
47    private static String JavaDoc hexServerIP = null;
48
49    // initialise the secure random instance
50
private static final java.security.SecureRandom JavaDoc seeder = new java.security.SecureRandom JavaDoc();
51
52    /**
53     * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
54     * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
55     *
56     * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
57     */

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