1 57 58 package org.apache.wsif.util.jms; 59 60 import java.util.Hashtable ; 61 62 import javax.jms.Destination ; 63 import javax.jms.Queue ; 64 import javax.jms.QueueConnectionFactory ; 65 import javax.naming.Context ; 66 import javax.naming.NamingException ; 67 import javax.naming.directory.InitialDirContext ; 68 import org.apache.wsif.WSIFException; 69 import org.apache.wsif.logging.Trc; 70 71 75 public class WSIFJMSFinderForJndi extends WSIFJMSFinder { 76 77 private InitialDirContext namedJndiContext = null; 78 private InitialDirContext containersJndiContext = null; 79 private QueueConnectionFactory factory; 80 private Destination initialDestination = null; 81 private String style; 82 private String portName; 83 84 94 public WSIFJMSFinderForJndi( 95 String jmsVendorURL, 96 String initialContextFactory, 97 String jndiProviderURL, 98 String style, 99 String jndiConnectionFactory, 100 String jndiDestinationName, 101 String portName) 102 throws WSIFException { 103 Trc.entry( 104 this, 105 jmsVendorURL, 106 initialContextFactory, 107 jndiProviderURL, 108 style, 109 jndiConnectionFactory, 110 jndiDestinationName, 111 portName); 112 113 if (!allStyles.contains(style)) 114 throw new WSIFException("Style must either be queue or topic"); 115 this.style = style; 116 if (portName == null) 117 portName = "<null>"; 118 this.portName = portName; 119 120 if (((initialContextFactory == null) && (jndiProviderURL != null)) 121 || ((initialContextFactory != null) && (jndiProviderURL == null))) 122 throw new WSIFException( 123 "Either both initialContextFactory and jndiProviderURL " 124 + "must be specified or neither of them must be specified. Port=" 125 + portName); 126 127 130 if (initialContextFactory != null && jndiProviderURL != null) { 131 Hashtable environment = new Hashtable (); 132 environment.put( 133 Context.INITIAL_CONTEXT_FACTORY, 134 initialContextFactory); 135 environment.put(Context.PROVIDER_URL, jndiProviderURL); 136 137 try { 138 namedJndiContext = new InitialDirContext (environment); 139 } catch (NamingException ne) { 140 Trc.exception(ne); 141 throw new WSIFException( 142 "WSIFJMSFinderForJndi caught '" 143 + ne 144 + "'. InitialContextFactory was '" 145 + initialContextFactory 146 + "' ProviderUrl was '" 147 + jndiProviderURL 148 + "'. Port=" 149 + portName); 150 } 151 } 152 153 try { 154 containersJndiContext = new InitialDirContext (); 155 } catch (NamingException ne) { 156 Trc.exception(ne); 157 if (initialContextFactory == null && jndiProviderURL == null) 158 throw new WSIFException( 159 "WSIFJMSFinderForJndi caught '" 160 + ne 161 + "' using the default JNDI repository. Port=" 162 + portName); 163 } 164 165 if (STYLE_TOPIC.equals(style)) 166 throw new WSIFException("Topics not implemented. Port=" + portName); 167 else if (!STYLE_QUEUE.equals(style)) 168 throw new WSIFException( 169 "jms:address must either be a queue or a topic not a '" 170 + (style == null ? "null" : style) 171 + "'. Port=" 172 + portName); 173 174 177 if (jndiConnectionFactory == null) 178 throw new WSIFException( 179 "jndiConnectionFactory must be specified in port " + portName); 180 try { 181 factory = (QueueConnectionFactory ) lookup(jndiConnectionFactory); 182 if (factory == null) 183 throw new WSIFException( 184 "WSIFJMSFinderForJndi was not able to lookup the ConnectionFactory " 185 + jndiConnectionFactory 186 + " in JNDI. Port=" 187 + portName); 188 } catch (ClassCastException cce) { 189 Trc.exception(cce); 190 throw new WSIFException( 191 "WSIFJMSFinderForJndi caught ClassCastException. The ConnectionFactory " 192 + jndiConnectionFactory 193 + " in JNDI was not defined to be a connection factory. Port=" 194 + portName 195 + " " 196 + cce); 197 } catch (NamingException ne) { 198 Trc.exception(ne); 199 throw new WSIFException( 200 "WSIFJMSFinderForJndi caught NamingException. The ConnectionFactory " 201 + jndiConnectionFactory 202 + " in JNDI was not defined to be a connection factory. Port=" 203 + portName 204 + " " 205 + ne); 206 } 207 208 211 if (jndiDestinationName != null) 212 try { 213 initialDestination = (Destination ) lookup(jndiDestinationName); 214 if (initialDestination == null) 215 throw new WSIFException( 216 "WSIFJMSFinderForJndi was not able to lookup the Destination " 217 + jndiDestinationName 218 + " in JNDI. Port=" 219 + portName); 220 } catch (ClassCastException cce) { 221 Trc.exception(cce); 222 throw new WSIFException( 223 "WSIFJMSFinderForJndi caught ClassCastException. The Destination " 224 + jndiDestinationName 225 + " in JNDI was not defined to be a destination. Port=" 226 + portName 227 + " " 228 + cce); 229 } catch (NamingException cce) { 230 Trc.exception(cce); 231 throw new WSIFException( 232 "WSIFJMSFinderForJndi caught NamingException. The Destination " 233 + jndiDestinationName 234 + " in JNDI was not defined to be a destination. Port=" 235 + portName 236 + " " 237 + cce); 238 } 239 if (Trc.ON) 240 Trc.exit(deep()); 241 } 242 243 public QueueConnectionFactory getFactory() { 244 Trc.entry(this); 245 Trc.exit(factory); 246 return factory; 247 } 248 249 public Destination getInitialDestination() { 250 Trc.entry(this); 251 Trc.exit(initialDestination); 252 return initialDestination; 253 } 254 255 String getStyle() { 256 Trc.entry(this); 257 Trc.exit(style); 258 return style; 259 } 260 261 Queue findQueue(String name) throws WSIFException { 262 Trc.entry(this, name); 263 Queue q = null; 264 try { 265 q = (Queue ) lookup(name); 266 if (q == null) 267 throw new WSIFException( 268 "WSIFJMSFinderForJndi was not able to lookup the Destination " 269 + name 270 + " in JNDI.Port=" 271 + portName); 272 } catch (ClassCastException cce) { 273 Trc.exception(cce); 274 throw new WSIFException( 275 "WSIFJMSFinderForJndi caught ClassCastException. The Queue " 276 + name 277 + " in JNDI was not defined to be a queue. Port=" 278 + portName 279 + " " 280 + cce); 281 } catch (NamingException ne) { 282 Trc.exception(ne); 283 throw new WSIFException( 284 "WSIFJMSFinderForJndi caught NamingException. The Queue " 285 + name 286 + " in JNDI was not defined to be a queue. Port=" 287 + portName 288 + " " 289 + ne); 290 } 291 Trc.exit(q); 292 return q; 293 } 294 295 305 private Object lookupJNDIName(String name) throws NamingException { 306 Trc.entry(name); 307 Object result = null; 308 if (containersJndiContext != null) { 309 try { 310 result = containersJndiContext.lookup(name); 311 } catch (NamingException ne) { 312 Trc.exception(ne); 313 if (namedJndiContext != null) 314 result = namedJndiContext.lookup(name); 315 else 316 throw ne; 317 } 318 } else 319 result = namedJndiContext.lookup(name); 320 Trc.exit(result); 321 return result; 322 } 323 324 331 private Object lookup(String name) throws NamingException { 332 Trc.entry(name); 333 334 Object result = null; 335 try { 336 result = lookupJNDIName("java:comp/env/" + name); 337 } catch (NamingException ignored) { 338 Trc.exception(ignored); 339 result = lookupJNDIName(name); 340 } 341 Trc.exit(result); 342 return result; 343 } 344 345 public String deep() { 346 StringBuffer buff = new StringBuffer (); 347 try { 348 buff.append(this.toString() + "\n"); 349 buff.append("containersJndiContext: " + containersJndiContext); 350 buff.append(" namedJndiContext: " + namedJndiContext); 351 buff.append(" factory: " + factory); 352 buff.append(" initialDestination: " + initialDestination); 353 buff.append(" style: " + style); 354 } catch (Exception e) { 355 Trc.exceptionInTrace(e); 356 } 357 return buff.toString(); 358 } 359 } | Popular Tags |