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-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   */
43  package org.exolab.jms.server.net;
44  
45  import java.util.Map;
46  
47  import javax.naming.Context;
48  import javax.naming.NamingException;
49  import javax.jms.XAConnectionFactory;
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.JmsServerStubIfc;
56  import org.exolab.jms.client.JmsXAConnectionFactory;
57  import org.exolab.jms.config.ConnectionFactories;
58  import org.exolab.jms.config.ConnectionFactory;
59  import org.exolab.jms.config.QueueConnectionFactory;
60  import org.exolab.jms.config.TopicConnectionFactory;
61  import org.exolab.jms.config.XAQueueConnectionFactory;
62  import org.exolab.jms.config.XATopicConnectionFactory;
63  import org.exolab.jms.config.ConnectionFactoryType;
64  import org.exolab.jms.server.ServerConnector;
65  
66  
67  /***
68   * Helper class for binding connection factories in JNDI.
69   *
70   * @version     $Revision: 1.2 $ $Date: 2005/04/07 02:42:49 $
71   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
72   */
73  public class ConnectionFactoryHelper {
74  
75      /***
76       * The logger.
77       */
78      private static final Log _log =
79          LogFactory.getLog(ConnectionFactoryHelper.class);
80  
81  
82      /***
83       * Bind the connection factories to the supplied context.
84       *
85       * @param context the context to bind factories to
86       * @param factories the connection factories to bind
87       * @param implementation a class implementing the {@link ServerConnector}
88       * interface
89       * @param properties parameters to pass to the associated
90       * {@link JmsConnectionFactory} implementation
91       * @throws NamingException if a factory cannot be bound
92       */
93      public static void bind(Context context, ConnectionFactories factories,
94                              Class implementation, Map properties)
95          throws NamingException {
96          if (context == null) {
97              throw new IllegalArgumentException("Argument 'context' is null");
98          }
99          if (factories == null) {
100             throw new IllegalArgumentException("Argument 'factories' is null");
101         }
102         if (implementation == null) {
103             throw new IllegalArgumentException(
104                 "Argument 'implementation' is null");
105         }
106         if (!JmsServerStubIfc.class.isAssignableFrom(implementation)) {
107             throw new IllegalArgumentException(
108                 "Class " + implementation.getName() + " does not implement " +
109                 JmsServerStubIfc.class.getName());
110         }
111         if (properties == null) {
112             throw new IllegalArgumentException("Argument properties is null");
113         }
114 
115         ConnectionFactoryType[] type = factories.getConnectionFactory();
116         ConnectionFactoryType[] queue = factories.getQueueConnectionFactory();
117         ConnectionFactoryType[] topic = factories.getTopicConnectionFactory();
118         ConnectionFactoryType[] xatype = factories.getXAConnectionFactory();
119         ConnectionFactoryType[] xaqueue =
120                 factories.getXAQueueConnectionFactory();
121         ConnectionFactoryType[] xatopic =
122                 factories.getXATopicConnectionFactory();
123         for (int i = 0; i < type.length; ++i) {
124             bind(context, type[i], implementation, properties);
125         }
126         for (int i = 0; i < queue.length; ++i) {
127             bind(context, queue[i], implementation, properties);
128         }
129         for (int i = 0; i < topic.length; ++i) {
130             bind(context, topic[i], implementation, properties);
131         }
132         for (int i = 0; i < xatype.length; ++i) {
133             bind(context, xatype[i], implementation, properties);
134         }
135         for (int i = 0; i < xaqueue.length; ++i) {
136             bind(context, xaqueue[i], implementation, properties);
137         }
138         for (int i = 0; i < xatopic.length; ++i) {
139             bind(context, xatopic[i], implementation, properties);
140         }
141     }
142 
143     /***
144      * Bind a connection factory to the supplied context.
145      *
146      * @param context the context to bind factories to
147      * @param factory the connection factory to bind
148      * @param implementation a class implementing the {@link ServerConnector}
149      * interface
150      * @param properties parameters to pass to the associated
151      * {@link JmsConnectionFactory} implementation
152      * @throws NamingException if the factory cannot be bound
153      */
154     private static void bind(Context context, ConnectionFactoryType factory,
155                              Class implementation, Map properties)
156         throws NamingException {
157         JmsConnectionFactory instance = null;
158         if (factory instanceof ConnectionFactory
159         || factory instanceof QueueConnectionFactory
160         || factory instanceof TopicConnectionFactory) {
161             instance = new JmsConnectionFactory(implementation.getName(),
162                                                      properties, null);
163         } else if (factory instanceof XAConnectionFactory
164             || factory instanceof XAQueueConnectionFactory
165             || factory instanceof XATopicConnectionFactory) {
166             instance = new JmsXAConnectionFactory(
167                 implementation.getName(), properties);
168         } else {
169             throw new IllegalArgumentException(
170                 "Unknown connection factory type: " +
171                 factory.getClass().getName());
172         }
173 
174         context.rebind(factory.getName(), instance);
175         _log.debug("Bound connection factory " + factory.getName());
176     }
177 
178 }