KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > interfaces > CustomerBMPUtil


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

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

13 public class CustomerBMPUtil
14 {
15    /** Cached remote home (EJBHome). Uses lazy loading to obtain its value (loaded by getHome() methods). */
16    private static test.interfaces.CustomerHome cachedRemoteHome = 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 remote home interface from default initial context
37     * @return Home interface for CustomerBMP. Lookup using COMP_NAME
38     */

39    public static test.interfaces.CustomerHome getHome() throws javax.naming.NamingException JavaDoc
40    {
41       if (cachedRemoteHome == null) {
42             cachedRemoteHome = (test.interfaces.CustomerHome) lookupHome(null, test.interfaces.CustomerHome.COMP_NAME, test.interfaces.CustomerHome.class);
43       }
44       return cachedRemoteHome;
45    }
46
47    /**
48     * Obtain remote home interface from parameterised initial context
49     * @param environment Parameters to use for creating initial context
50     * @return Home interface for CustomerBMP. Lookup using COMP_NAME
51     */

52    public static test.interfaces.CustomerHome getHome( java.util.Hashtable JavaDoc environment ) throws javax.naming.NamingException JavaDoc
53    {
54        return (test.interfaces.CustomerHome) lookupHome(environment, test.interfaces.CustomerHome.COMP_NAME, test.interfaces.CustomerHome.class);
55    }
56
57    /** Cached per JVM server IP. */
58    private static String JavaDoc hexServerIP = null;
59
60    // initialise the secure random instance
61
private static final java.security.SecureRandom JavaDoc seeder = new java.security.SecureRandom JavaDoc();
62
63    /**
64     * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
65     * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
66     *
67     * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
68     */

69    public static final String JavaDoc generateGUID(Object JavaDoc o) {
70        StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
71        if (hexServerIP == null) {
72            java.net.InetAddress JavaDoc localInetAddress = null;
73            try {
74                // get the inet address
75

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