KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > ConnectionFactoryHelper


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.jms;
23
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.ConnectionFactory JavaDoc;
27 import javax.jms.QueueConnection JavaDoc;
28 import javax.jms.QueueConnectionFactory JavaDoc;
29 import javax.jms.TopicConnection JavaDoc;
30 import javax.jms.TopicConnectionFactory JavaDoc;
31 import javax.jms.XAConnectionFactory JavaDoc;
32 import javax.jms.XAQueueConnectionFactory JavaDoc;
33 import javax.jms.XATopicConnectionFactory JavaDoc;
34
35 import org.jboss.logging.Logger;
36
37 /**
38  * A helper for creating connections from jms connection factories.
39  *
40  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
41  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 38366 $
43  */

44 public class ConnectionFactoryHelper
45 {
46    /** Class logger. */
47    private static Logger log = Logger.getLogger(ConnectionFactoryHelper.class);
48
49    /**
50     * Create a connection from the given factory. An XA connection will
51     * be created if possible.
52     *
53     * @param factory An object that implements ConnectionFactory,
54     * XAQConnectionFactory
55     * @param username The username to use or null for no user.
56     * @param password The password for the given username or null if no
57     * username was specified.
58     * @return A queue connection.
59     *
60     * @throws JMSException Failed to create connection.
61     * @throws IllegalArgumentException Factory is null or invalid.
62     */

63    public static Connection JavaDoc createConnection(final Object JavaDoc factory, final String JavaDoc username, final String JavaDoc password)
64          throws JMSException JavaDoc
65    {
66       if (factory == null)
67          throw new IllegalArgumentException JavaDoc("factory is null");
68
69       log.debug("using connection factory: " + factory);
70       log.debug("using username/password: " + String.valueOf(username) + "/-- not shown --");
71
72       Connection JavaDoc connection;
73
74       if (factory instanceof XAConnectionFactory JavaDoc)
75       {
76          XAConnectionFactory JavaDoc qFactory = (XAConnectionFactory JavaDoc) factory;
77          if (username != null)
78             connection = qFactory.createXAConnection(username, password);
79          else
80             connection = qFactory.createXAConnection();
81
82          log.debug("created XAConnection: " + connection);
83       }
84       else if (factory instanceof ConnectionFactory JavaDoc)
85       {
86          ConnectionFactory JavaDoc qFactory = (ConnectionFactory JavaDoc) factory;
87          if (username != null)
88             connection = qFactory.createConnection(username, password);
89          else
90             connection = qFactory.createConnection();
91
92          log.debug("created Connection: " + connection);
93       }
94       else
95       {
96          throw new IllegalArgumentException JavaDoc("factory is invalid");
97       }
98
99       return connection;
100    }
101
102    /**
103     * Create a connection from the given factory. An XA connection will
104     * be created if possible.
105     *
106     * @param factory An object that implements QueueConnectionFactory,
107     * XAQueueConnectionFactory
108     * @return A queue connection.
109     *
110     * @throws JMSException Failed to create connection.
111     * @throws IllegalArgumentException Factory is null or invalid.
112     */

113    public static Connection JavaDoc createConnection(final Object JavaDoc factory) throws JMSException JavaDoc
114    {
115       return createConnection(factory, null, null);
116    }
117
118    /**
119     * Create a queue connection from the given factory. An XA connection will
120     * be created if possible.
121     *
122     * @param factory An object that implements QueueConnectionFactory,
123     * XAQueueConnectionFactory
124     * @param username The username to use or null for no user.
125     * @param password The password for the given username or null if no
126     * username was specified.
127     * @return A queue connection.
128     *
129     * @throws JMSException Failed to create connection.
130     * @throws IllegalArgumentException Factory is null or invalid.
131     */

132    public static QueueConnection JavaDoc createQueueConnection(final Object JavaDoc factory, final String JavaDoc username,
133          final String JavaDoc password) throws JMSException JavaDoc
134    {
135       if (factory == null)
136          throw new IllegalArgumentException JavaDoc("factory is null");
137
138       log.debug("using connection factory: " + factory);
139       log.debug("using username/password: " + String.valueOf(username) + "/-- not shown --");
140
141       QueueConnection JavaDoc connection;
142
143       if (factory instanceof XAQueueConnectionFactory JavaDoc)
144       {
145          XAQueueConnectionFactory JavaDoc qFactory = (XAQueueConnectionFactory JavaDoc) factory;
146          if (username != null)
147             connection = qFactory.createXAQueueConnection(username, password);
148          else
149             connection = qFactory.createXAQueueConnection();
150
151          log.debug("created XAQueueConnection: " + connection);
152       }
153       else if (factory instanceof QueueConnectionFactory JavaDoc)
154       {
155          QueueConnectionFactory JavaDoc qFactory = (QueueConnectionFactory JavaDoc) factory;
156          if (username != null)
157             connection = qFactory.createQueueConnection(username, password);
158          else
159             connection = qFactory.createQueueConnection();
160
161          log.debug("created QueueConnection: " + connection);
162       }
163       else
164          throw new IllegalArgumentException JavaDoc("factory is invalid");
165
166       return connection;
167    }
168
169    /**
170     * Create a queue connection from the given factory. An XA connection will
171     * be created if possible.
172     *
173     * @param factory An object that implements QueueConnectionFactory,
174     * XAQueueConnectionFactory
175     * @return A queue connection.
176     *
177     * @throws JMSException Failed to create connection.
178     * @throws IllegalArgumentException Factory is null or invalid.
179     */

180    public static QueueConnection JavaDoc createQueueConnection(final Object JavaDoc factory) throws JMSException JavaDoc
181    {
182       return createQueueConnection(factory, null, null);
183    }
184
185    /**
186     * Create a topic connection from the given factory. An XA connection will
187     * be created if possible.
188     *
189     * @param factory An object that implements TopicConnectionFactory,
190     * XATopicConnectionFactory
191     * @param username The username to use or null for no user.
192     * @param password The password for the given username or null if no
193     * username was specified.
194     * @return A topic connection.
195     *
196     * @throws JMSException Failed to create connection.
197     * @throws IllegalArgumentException Factory is null or invalid.
198     */

199    public static TopicConnection JavaDoc createTopicConnection(final Object JavaDoc factory, final String JavaDoc username,
200          final String JavaDoc password) throws JMSException JavaDoc
201    {
202       if (factory == null)
203          throw new IllegalArgumentException JavaDoc("factory is null");
204
205       log.debug("using connection factory: " + factory);
206       log.debug("using username/password: " + String.valueOf(username) + "/-- not shown --");
207
208       TopicConnection JavaDoc connection;
209
210       if (factory instanceof XATopicConnectionFactory JavaDoc)
211       {
212          XATopicConnectionFactory JavaDoc tFactory = (XATopicConnectionFactory JavaDoc) factory;
213          if (username != null)
214             connection = tFactory.createXATopicConnection(username, password);
215          else
216             connection = tFactory.createXATopicConnection();
217
218          log.debug("created XATopicConnection: " + connection);
219       }
220       else if (factory instanceof TopicConnectionFactory JavaDoc)
221       {
222          TopicConnectionFactory JavaDoc tFactory = (TopicConnectionFactory JavaDoc) factory;
223          if (username != null)
224             connection = tFactory.createTopicConnection(username, password);
225          else
226             connection = tFactory.createTopicConnection();
227
228          log.debug("created TopicConnection: " + connection);
229       }
230       else
231          throw new IllegalArgumentException JavaDoc("factory is invalid");
232
233       return connection;
234    }
235
236    /**
237     * Create a topic connection from the given factory. An XA connection will
238     * be created if possible.
239     *
240     * @param factory An object that implements TopicConnectionFactory,
241     * XATopicConnectionFactory
242     * @return A topic connection.
243     *
244     * @throws JMSException Failed to create connection.
245     * @throws IllegalArgumentException Factory is null or invalid.
246     */

247    public static TopicConnection JavaDoc createTopicConnection(final Object JavaDoc factory) throws JMSException JavaDoc
248    {
249       return createTopicConnection(factory, null, null);
250    }
251 }
252
Popular Tags