View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   */
43  
44  package org.exolab.jms.server;
45  
46  import java.util.Hashtable;
47  
48  import javax.naming.Context;
49  import javax.naming.NamingException;
50  
51  import org.apache.commons.logging.Log;
52  import org.apache.commons.logging.LogFactory;
53  
54  import org.exolab.jms.client.JmsConnectionFactory;
55  import org.exolab.jms.client.JmsQueueConnectionFactory;
56  import org.exolab.jms.client.JmsServerStubIfc;
57  import org.exolab.jms.client.JmsTopicConnectionFactory;
58  import org.exolab.jms.client.JmsXAQueueConnectionFactory;
59  import org.exolab.jms.client.JmsXATopicConnectionFactory;
60  import org.exolab.jms.config.ConnectionFactories;
61  import org.exolab.jms.config.ConnectionFactory;
62  import org.exolab.jms.config.QueueConnectionFactory;
63  import org.exolab.jms.config.TopicConnectionFactory;
64  import org.exolab.jms.config.XAQueueConnectionFactory;
65  import org.exolab.jms.config.XATopicConnectionFactory;
66  
67  
68  /***
69   * Helper class for binding connection factories in JNDI
70   *
71   * @version     $Revision: 1.3 $ $Date: 2003/08/07 13:33:08 $
72   * @author      <a href="mailto:tima@intalio.com">Tim Anderson</a>
73   */
74  public class ConnectionFactoryHelper {
75  
76      /***
77       * The logger
78       */
79      private static final Log _log =
80          LogFactory.getLog(ConnectionFactoryHelper.class);
81  
82  
83      /***
84       * Bind the connection factories to the supplied context
85       *
86       * @param context the context to bind factories to
87       * @param factories the connection factories to bind
88       * @param implementation a class implementing the {@link JmsServerIfc}
89       * interface
90       * @param environment parameters to pass to the associated
91       * {@link JmsConnectionFactory} implementation
92       * @throws NamingException if a factory cannot be bound
93       */
94      public static void bind(Context context, ConnectionFactories factories,
95                              Class implementation, Hashtable environment)
96          throws NamingException {
97          if (context == null) {
98              throw new IllegalArgumentException("Argument context is null");
99          }
100         if (factories == null) {
101             throw new IllegalArgumentException("Argument factories is null");
102         }
103         if (implementation == null) {
104             throw new IllegalArgumentException(
105                 "Argument implementation is null");
106         }
107         if (!JmsServerStubIfc.class.isAssignableFrom(implementation)) {
108             throw new IllegalArgumentException(
109                 "Class " + implementation.getName() + " does not implement " +
110                 JmsServerStubIfc.class.getName());
111         }
112         if (environment == null) {
113             throw new IllegalArgumentException("Argument environment is null");
114         }
115 
116         ConnectionFactory[] queue = factories.getQueueConnectionFactory();
117         ConnectionFactory[] topic = factories.getTopicConnectionFactory();
118         ConnectionFactory[] xaqueue = factories.getXAQueueConnectionFactory();
119         ConnectionFactory[] xatopic = factories.getXATopicConnectionFactory();
120         for (int i = 0; i < queue.length; ++i) {
121             bind(context, queue[i], implementation, environment);
122         }
123         for (int i = 0; i < topic.length; ++i) {
124             bind(context, topic[i], implementation, environment);
125         }
126         for (int i = 0; i < xaqueue.length; ++i) {
127             bind(context, xaqueue[i], implementation, environment);
128         }
129         for (int i = 0; i < xatopic.length; ++i) {
130             bind(context, xatopic[i], implementation, environment);
131         }
132     }
133 
134     /***
135      * Bind a connection factory to the supplied context
136      *
137      * @param context the context to bind factories to
138      * @param factory the connection factory to bind
139      * @param implementation a class implementing the {@link JmsServerIfc}
140      * interface
141      * @param environment parameters to pass to the associated
142      * {@link JmsConnectionFactory} implementation
143      * @throws NamingException if the factory cannot be bound
144      */
145     private static void bind(Context context, ConnectionFactory factory,
146                              Class implementation, Hashtable environment)
147         throws NamingException {
148         JmsConnectionFactory instance = null;
149         if (factory instanceof QueueConnectionFactory) {
150             instance = new JmsQueueConnectionFactory(implementation.getName(),
151                 environment);
152         } else if (factory instanceof TopicConnectionFactory) {
153             instance = new JmsTopicConnectionFactory(implementation.getName(),
154                 environment);
155         } else if (factory instanceof XAQueueConnectionFactory) {
156             instance = new JmsXAQueueConnectionFactory(
157                 implementation.getName(), environment);
158         } else if (factory instanceof XATopicConnectionFactory) {
159             instance = new JmsXATopicConnectionFactory(
160                 implementation.getName(), environment);
161         } else {
162             throw new IllegalArgumentException(
163                 "Unknown connection factory type: " +
164                 factory.getClass().getName());
165         }
166 
167         context.rebind(factory.getName(), instance);
168         _log.debug("Bound connection factory " + factory.getName());
169     }
170 
171 } // -- ConnectionFactoryHelper