KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > interfaces > AccountUtil


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

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

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

37    /**
38     * Obtain remote home interface from default initial context
39     * @return Home interface for Account. Lookup using COMP_NAME
40     */

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

54    public static test.interfaces.AccountHome getHome( java.util.Hashtable JavaDoc environment ) throws javax.naming.NamingException JavaDoc
55    {
56        return (test.interfaces.AccountHome) lookupHome(environment, test.interfaces.AccountHome.COMP_NAME, test.interfaces.AccountHome.class);
57    }
58
59    /**
60     * Obtain local home interface from default initial context
61     * @return Local home interface for Account. Lookup using COMP_NAME
62     */

63    public static test.interfaces.AccountLocalHome getLocalHome() throws javax.naming.NamingException JavaDoc
64    {
65       if (cachedLocalHome == null) {
66             cachedLocalHome = (test.interfaces.AccountLocalHome) lookupHome(null, test.interfaces.AccountLocalHome.COMP_NAME, test.interfaces.AccountLocalHome.class);
67       }
68       return cachedLocalHome;
69    }
70
71    /** Cached per JVM server IP. */
72    private static String JavaDoc hexServerIP = null;
73
74    // initialise the secure random instance
75
private static final java.security.SecureRandom JavaDoc seeder = new java.security.SecureRandom JavaDoc();
76
77    /**
78     * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
79     * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
80     *
81     * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
82     */

83    public static final String JavaDoc generateGUID(Object JavaDoc o) {
84        StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
85        if (hexServerIP == null) {
86            java.net.InetAddress JavaDoc localInetAddress = null;
87            try {
88                // get the inet address
89

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