KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > util > jms > WSIFJMSFinderForJndi


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.util.jms;
59
60 import java.util.Hashtable JavaDoc;
61
62 import javax.jms.Destination JavaDoc;
63 import javax.jms.Queue JavaDoc;
64 import javax.jms.QueueConnectionFactory JavaDoc;
65 import javax.naming.Context JavaDoc;
66 import javax.naming.NamingException JavaDoc;
67 import javax.naming.directory.InitialDirContext JavaDoc;
68 import org.apache.wsif.WSIFException;
69 import org.apache.wsif.logging.Trc;
70
71 /**
72  * Finds JMS objects by looking them up in JNDI.
73  * @author Mark Whitlock <whitlock@apache.org>
74  */

75 public class WSIFJMSFinderForJndi extends WSIFJMSFinder {
76
77     private InitialDirContext JavaDoc namedJndiContext = null;
78     private InitialDirContext JavaDoc containersJndiContext = null;
79     private QueueConnectionFactory JavaDoc factory;
80     private Destination JavaDoc initialDestination = null;
81     private String JavaDoc style;
82     private String JavaDoc portName;
83
84     /**
85      * Package private constructor.
86      * @param jmsVendorURL uniquely identifies the JMS implementation. Unused at present.
87      * @param initialContextFactory used to get JNDI's InitialContext in a non-managed environment
88      * @param jndiProviderURL of the JNDI repository
89      * @param style is either queue or topic
90      * @param jndiConnectionFactory is the JNDI name of the qcf
91      * @param jndiDestinationName is the JNDI name of the initial queue.
92      * @param jmsproviderDestinationName is the JMS implementation's name of the initial queue.
93      */

94     public WSIFJMSFinderForJndi(
95         String JavaDoc jmsVendorURL,
96         String JavaDoc initialContextFactory,
97         String JavaDoc jndiProviderURL,
98         String JavaDoc style,
99         String JavaDoc jndiConnectionFactory,
100         String JavaDoc jndiDestinationName,
101         String JavaDoc 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         /*
128          * Go find the container's JNDI and the JNDI specified in the WSDL.
129          */

130         if (initialContextFactory != null && jndiProviderURL != null) {
131             Hashtable JavaDoc environment = new Hashtable JavaDoc();
132             environment.put(
133                 Context.INITIAL_CONTEXT_FACTORY,
134                 initialContextFactory);
135             environment.put(Context.PROVIDER_URL, jndiProviderURL);
136
137             try {
138                 namedJndiContext = new InitialDirContext JavaDoc(environment);
139             } catch (NamingException JavaDoc 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 JavaDoc();
155         } catch (NamingException JavaDoc 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         /*
175          * Go find the queue connection factory.
176          */

177         if (jndiConnectionFactory == null)
178             throw new WSIFException(
179                 "jndiConnectionFactory must be specified in port " + portName);
180         try {
181             factory = (QueueConnectionFactory JavaDoc) 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 JavaDoc 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 JavaDoc 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         /*
209          * Go find the initial destination.
210          */

211         if (jndiDestinationName != null)
212             try {
213                 initialDestination = (Destination JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc getFactory() {
244         Trc.entry(this);
245         Trc.exit(factory);
246         return factory;
247     }
248
249     public Destination JavaDoc getInitialDestination() {
250         Trc.entry(this);
251         Trc.exit(initialDestination);
252         return initialDestination;
253     }
254
255     String JavaDoc getStyle() {
256         Trc.entry(this);
257         Trc.exit(style);
258         return style;
259     }
260
261     Queue JavaDoc findQueue(String JavaDoc name) throws WSIFException {
262         Trc.entry(this, name);
263         Queue JavaDoc q = null;
264         try {
265             q = (Queue JavaDoc) 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 JavaDoc 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 JavaDoc 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     /**
296      * There is at least one and at most two JNDI databases to look
297      * objects up in :- the container's default JNDI database and the
298      * one specified in the WSDL. This code looks objects up in the
299      * container's JNDI first (if running in a container), and then
300      * looks the object up in the JNDI pointed at by the WSDL (assuming
301      * there is a JNDI mentioned in the WSDL). This allows a client
302      * administrator to override the JNDI specified in the WSDL. If
303      * both fail, then the exception from the WSDL's JNDI is returned.
304      */

305     private Object JavaDoc lookupJNDIName(String JavaDoc name) throws NamingException JavaDoc {
306         Trc.entry(name);
307         Object JavaDoc result = null;
308         if (containersJndiContext != null) {
309             try {
310                 result = containersJndiContext.lookup(name);
311             } catch (NamingException JavaDoc 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     /**
325      * The argument name is prefixed with "java:comp/env/" context
326      * prior to the JNDI lookup. If an exception is thrown, the
327      * JNDI lookup is done on the argument name without the prefix.
328      * If the lookup fails, the error based off the lookup of the
329      * argument is returned.
330      */

331     private Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
332         Trc.entry(name);
333
334         Object JavaDoc result = null;
335         try {
336             result = lookupJNDIName("java:comp/env/" + name);
337         } catch (NamingException JavaDoc ignored) {
338             Trc.exception(ignored);
339             result = lookupJNDIName(name);
340         }
341         Trc.exit(result);
342         return result;
343     }
344
345     public String JavaDoc deep() {
346         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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 JavaDoc e) {
355             Trc.exceptionInTrace(e);
356         }
357         return buff.toString();
358     }
359 }
Popular Tags