1 4 package test.interfaces; 5 6 13 public class QueueBeanUtil 14 { 15 16 17 private static javax.jms.Queue cachedQueue = null; 18 19 private static javax.jms.QueueConnectionFactory cachedConnectionFactory = null; 20 21 private static final java.lang.String DESTINATION_JNDI_NAME=""; 22 private static final java.lang.String CONNECTION_FACTORY_JNDI_NAME=""; 23 24 28 public static javax.jms.Queue getQueue() throws javax.naming.NamingException 29 { 30 if (cachedQueue == null) { 31 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (); 33 try { 34 java.lang.Object objRef = initialContext.lookup(DESTINATION_JNDI_NAME); 35 cachedQueue = (javax.jms.Queue ) objRef; 36 } finally { 37 initialContext.close(); 38 } 39 } 40 return cachedQueue; 41 } 42 43 48 public static javax.jms.Queue getQueue( java.util.Hashtable environment ) throws javax.naming.NamingException 49 { 50 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (environment); 52 try { 53 java.lang.Object objRef = initialContext.lookup(DESTINATION_JNDI_NAME); 54 return (javax.jms.Queue ) objRef; 55 } finally { 56 initialContext.close(); 57 } 58 } 59 60 64 public static javax.jms.QueueConnection getQueueConnection() throws javax.naming.NamingException , javax.jms.JMSException 65 { 66 if (cachedConnectionFactory == null) { 67 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (); 69 try { 70 java.lang.Object objRef = initialContext.lookup(CONNECTION_FACTORY_JNDI_NAME); 71 cachedConnectionFactory = (javax.jms.QueueConnectionFactory ) objRef; 72 } finally { 73 initialContext.close(); 74 } 75 } 76 return cachedConnectionFactory.createQueueConnection(); 77 } 78 79 84 public static javax.jms.QueueConnection getQueueConnection( java.util.Hashtable environment ) throws javax.naming.NamingException , javax.jms.JMSException 85 { 86 javax.naming.InitialContext initialContext = new javax.naming.InitialContext (environment); 88 try { 89 java.lang.Object objRef = initialContext.lookup(CONNECTION_FACTORY_JNDI_NAME); 90 return ((javax.jms.QueueConnectionFactory ) objRef).createQueueConnection(); 91 } finally { 92 initialContext.close(); 93 } 94 } 95 96 97 private static String hexServerIP = null; 98 99 private static final java.security.SecureRandom seeder = new java.security.SecureRandom (); 101 102 108 public static final String generateGUID(Object o) { 109 StringBuffer tmpBuffer = new StringBuffer (16); 110 if (hexServerIP == null) { 111 java.net.InetAddress localInetAddress = null; 112 try { 113 115 localInetAddress = java.net.InetAddress.getLocalHost(); 116 } 117 catch (java.net.UnknownHostException uhe) { 118 System.err.println("QueueBeanUtil: Could not get the local IP address using InetAddress.getLocalHost()!"); 119 uhe.printStackTrace(); 121 return null; 122 } 123 byte serverIP[] = localInetAddress.getAddress(); 124 hexServerIP = hexFormat(getInt(serverIP), 8); 125 } 126 127 String 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 guid = new StringBuffer (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 hexFormat(int i, int j) { 154 String s = Integer.toHexString(i); 155 return padHex(s, j) + s; 156 } 157 158 private static String padHex(String s, int i) { 159 StringBuffer tmpBuffer = new StringBuffer (); 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 |