KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > bea > cmr > SubscriberUtil


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

4 package test.bea.cmr;
5
6 /**
7  * Utility class for Subscriber.
8  * @xdoclet-generated at 16-04-05
9  * @copyright The XDoclet Team
10  * @author XDoclet
11  * @version 1.2.3
12  */

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

35    /**
36     * Obtain local home interface from default initial context
37     * @return Local home interface for Subscriber. Lookup using COMP_NAME
38     */

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

59    public static final String JavaDoc generateGUID(Object JavaDoc o) {
60        StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
61        if (hexServerIP == null) {
62            java.net.InetAddress JavaDoc localInetAddress = null;
63            try {
64                // get the inet address
65

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