KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > interfaces > QueueBeanUtil


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

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

13 public class QueueBeanUtil
14 {
15
16    /** Cached queue (javax.jms.Queue). Uses lazy loading to obtain its value (loaded by getQueue() methods). */
17    private static javax.jms.Queue JavaDoc cachedQueue = null;
18    /** Cached connection factory. Uses lazy loading to obtain its value. */
19    private static javax.jms.QueueConnectionFactory JavaDoc cachedConnectionFactory = null;
20
21   private static final java.lang.String JavaDoc DESTINATION_JNDI_NAME="";
22   private static final java.lang.String JavaDoc CONNECTION_FACTORY_JNDI_NAME="";
23
24    /**
25     * Obtain destination queue from default initial context
26     * @return Destination JMS Queue for QueueBean. Lookup using COMP_NAME
27     */

28    public static javax.jms.Queue JavaDoc getQueue() throws javax.naming.NamingException JavaDoc
29    {
30       if (cachedQueue == null) {
31          // Obtain initial context
32
javax.naming.InitialContext JavaDoc initialContext = new javax.naming.InitialContext JavaDoc();
33          try {
34             java.lang.Object JavaDoc objRef = initialContext.lookup(DESTINATION_JNDI_NAME);
35             cachedQueue = (javax.jms.Queue JavaDoc) objRef;
36          } finally {
37             initialContext.close();
38          }
39       }
40       return cachedQueue;
41    }
42
43    /**
44     * Obtain destination queue from parameterised initial context
45     * @param environment Parameters to use for creating initial context
46     * @return Destination JMS Queue for QueueBean. Lookup using COMP_NAME
47     */

48    public static javax.jms.Queue JavaDoc getQueue( java.util.Hashtable JavaDoc environment ) throws javax.naming.NamingException JavaDoc
49    {
50       // Obtain initial context
51
javax.naming.InitialContext JavaDoc initialContext = new javax.naming.InitialContext JavaDoc(environment);
52       try {
53          java.lang.Object JavaDoc objRef = initialContext.lookup(DESTINATION_JNDI_NAME);
54          return (javax.jms.Queue JavaDoc) objRef;
55       } finally {
56          initialContext.close();
57       }
58    }
59
60    /**
61     * Obtain destination queue from default initial context
62     * @return Destination JMS Connection Factory for QueueBean. Lookup using COMP_NAME
63     */

64    public static javax.jms.QueueConnection JavaDoc getQueueConnection() throws javax.naming.NamingException JavaDoc, javax.jms.JMSException JavaDoc
65    {
66       if (cachedConnectionFactory == null) {
67          // Obtain initial context
68
javax.naming.InitialContext JavaDoc initialContext = new javax.naming.InitialContext JavaDoc();
69          try {
70             java.lang.Object JavaDoc objRef = initialContext.lookup(CONNECTION_FACTORY_JNDI_NAME);
71             cachedConnectionFactory = (javax.jms.QueueConnectionFactory JavaDoc) objRef;
72          } finally {
73             initialContext.close();
74          }
75       }
76       return cachedConnectionFactory.createQueueConnection();
77    }
78
79    /**
80     * Obtain destination queue from parameterised initial context
81     * @param environment Parameters to use for creating initial context
82     * @return Destination JMS Connection Factory for QueueBean. Lookup using COMP_NAME
83     */

84    public static javax.jms.QueueConnection JavaDoc getQueueConnection( java.util.Hashtable JavaDoc environment ) throws javax.naming.NamingException JavaDoc, javax.jms.JMSException JavaDoc
85    {
86       // Obtain initial context
87
javax.naming.InitialContext JavaDoc initialContext = new javax.naming.InitialContext JavaDoc(environment);
88       try {
89          java.lang.Object JavaDoc objRef = initialContext.lookup(CONNECTION_FACTORY_JNDI_NAME);
90          return ((javax.jms.QueueConnectionFactory JavaDoc) objRef).createQueueConnection();
91       } finally {
92          initialContext.close();
93       }
94    }
95
96    /** Cached per JVM server IP. */
97    private static String JavaDoc hexServerIP = null;
98
99    // initialise the secure random instance
100
private static final java.security.SecureRandom JavaDoc seeder = new java.security.SecureRandom JavaDoc();
101
102    /**
103     * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
104     * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
105     *
106     * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
107     */

108    public static final String JavaDoc generateGUID(Object JavaDoc o) {
109        StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
110        if (hexServerIP == null) {
111            java.net.InetAddress JavaDoc localInetAddress = null;
112            try {
113                // get the inet address
114

115                localInetAddress = java.net.InetAddress.getLocalHost();
116            }
117            catch (java.net.UnknownHostException JavaDoc uhe) {
118                System.err.println("QueueBeanUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
119                // todo: find better way to get around this...
120
uhe.printStackTrace();
121                return null;
122            }
123            byte serverIP[] = localInetAddress.getAddress();
124            hexServerIP = hexFormat(getInt(serverIP), 8);
125        }
126
127        String JavaDoc hashcode = hexFormat(System.identityHashCode(o), 8);
128        tmpBuffer.append(hexServerIP);
129        tmpBuffer.append(hashcode);
130
131        long timeNow = System.currentTimeMillis();
132        int timeLow = (int)timeNow & 0xFFFFFFFF;
133        int node = seeder.nextInt();
134
135        StringBuffer JavaDoc guid = new StringBuffer JavaDoc(32);
136        guid.append(hexFormat(timeLow, 8));
137        guid.append(tmpBuffer.toString());
138        guid.append(hexFormat(node, 8));
139        return guid.toString();
140    }
141
142    private static int getInt(byte bytes[]) {
143        int i = 0;
144        int j = 24;
145        for (int k = 0; j >= 0; k++) {
146            int l = bytes[k] & 0xff;
147            i += l << j;
148            j -= 8;
149        }
150        return i;
151    }
152
153    private static String JavaDoc hexFormat(int i, int j) {
154        String JavaDoc s = Integer.toHexString(i);
155        return padHex(s, j) + s;
156    }
157
158    private static String JavaDoc padHex(String JavaDoc s, int i) {
159        StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc();
160        if (s.length() < i) {
161            for (int j = 0; j < i - s.length(); j++) {
162                tmpBuffer.append('0');
163            }
164        }
165        return tmpBuffer.toString();
166    }
167
168 }
169
170
Popular Tags