001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jms;
023:
024: import javax.jms.JMSException;
025: import javax.jms.Connection;
026: import javax.jms.ConnectionFactory;
027: import javax.jms.QueueConnection;
028: import javax.jms.QueueConnectionFactory;
029: import javax.jms.TopicConnection;
030: import javax.jms.TopicConnectionFactory;
031: import javax.jms.XAConnectionFactory;
032: import javax.jms.XAQueueConnectionFactory;
033: import javax.jms.XATopicConnectionFactory;
034:
035: import org.jboss.logging.Logger;
036:
037: /**
038: * A helper for creating connections from jms connection factories.
039: *
040: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
041: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
042: * @version $Revision: 57209 $
043: */
044: public class ConnectionFactoryHelper {
045: /** Class logger. */
046: private static Logger log = Logger
047: .getLogger(ConnectionFactoryHelper.class);
048:
049: /**
050: * Create a connection from the given factory. An XA connection will
051: * be created if possible.
052: *
053: * @param factory An object that implements ConnectionFactory,
054: * XAQConnectionFactory
055: * @param username The username to use or null for no user.
056: * @param password The password for the given username or null if no
057: * username was specified.
058: * @return A queue connection.
059: *
060: * @throws JMSException Failed to create connection.
061: * @throws IllegalArgumentException Factory is null or invalid.
062: */
063: public static Connection createConnection(final Object factory,
064: final String username, final String password)
065: throws JMSException {
066: if (factory == null)
067: throw new IllegalArgumentException("factory is null");
068:
069: log.debug("using connection factory: " + factory);
070: log.debug("using username/password: "
071: + String.valueOf(username) + "/-- not shown --");
072:
073: Connection connection;
074:
075: if (factory instanceof XAConnectionFactory) {
076: XAConnectionFactory qFactory = (XAConnectionFactory) factory;
077: if (username != null)
078: connection = qFactory.createXAConnection(username,
079: password);
080: else
081: connection = qFactory.createXAConnection();
082:
083: log.debug("created XAConnection: " + connection);
084: } else if (factory instanceof ConnectionFactory) {
085: ConnectionFactory qFactory = (ConnectionFactory) factory;
086: if (username != null)
087: connection = qFactory.createConnection(username,
088: password);
089: else
090: connection = qFactory.createConnection();
091:
092: log.debug("created Connection: " + connection);
093: } else {
094: throw new IllegalArgumentException("factory is invalid");
095: }
096:
097: return connection;
098: }
099:
100: /**
101: * Create a connection from the given factory. An XA connection will
102: * be created if possible.
103: *
104: * @param factory An object that implements QueueConnectionFactory,
105: * XAQueueConnectionFactory
106: * @return A queue connection.
107: *
108: * @throws JMSException Failed to create connection.
109: * @throws IllegalArgumentException Factory is null or invalid.
110: */
111: public static Connection createConnection(final Object factory)
112: throws JMSException {
113: return createConnection(factory, null, null);
114: }
115:
116: /**
117: * Create a queue connection from the given factory. An XA connection will
118: * be created if possible.
119: *
120: * @param factory An object that implements QueueConnectionFactory,
121: * XAQueueConnectionFactory
122: * @param username The username to use or null for no user.
123: * @param password The password for the given username or null if no
124: * username was specified.
125: * @return A queue connection.
126: *
127: * @throws JMSException Failed to create connection.
128: * @throws IllegalArgumentException Factory is null or invalid.
129: */
130: public static QueueConnection createQueueConnection(
131: final Object factory, final String username,
132: final String password) throws JMSException {
133: if (factory == null)
134: throw new IllegalArgumentException("factory is null");
135:
136: log.debug("using connection factory: " + factory);
137: log.debug("using username/password: "
138: + String.valueOf(username) + "/-- not shown --");
139:
140: QueueConnection connection;
141:
142: if (factory instanceof XAQueueConnectionFactory) {
143: XAQueueConnectionFactory qFactory = (XAQueueConnectionFactory) factory;
144: if (username != null)
145: connection = qFactory.createXAQueueConnection(username,
146: password);
147: else
148: connection = qFactory.createXAQueueConnection();
149:
150: log.debug("created XAQueueConnection: " + connection);
151: } else if (factory instanceof QueueConnectionFactory) {
152: QueueConnectionFactory qFactory = (QueueConnectionFactory) factory;
153: if (username != null)
154: connection = qFactory.createQueueConnection(username,
155: password);
156: else
157: connection = qFactory.createQueueConnection();
158:
159: log.debug("created QueueConnection: " + connection);
160: } else
161: throw new IllegalArgumentException("factory is invalid");
162:
163: return connection;
164: }
165:
166: /**
167: * Create a queue connection from the given factory. An XA connection will
168: * be created if possible.
169: *
170: * @param factory An object that implements QueueConnectionFactory,
171: * XAQueueConnectionFactory
172: * @return A queue connection.
173: *
174: * @throws JMSException Failed to create connection.
175: * @throws IllegalArgumentException Factory is null or invalid.
176: */
177: public static QueueConnection createQueueConnection(
178: final Object factory) throws JMSException {
179: return createQueueConnection(factory, null, null);
180: }
181:
182: /**
183: * Create a topic connection from the given factory. An XA connection will
184: * be created if possible.
185: *
186: * @param factory An object that implements TopicConnectionFactory,
187: * XATopicConnectionFactory
188: * @param username The username to use or null for no user.
189: * @param password The password for the given username or null if no
190: * username was specified.
191: * @return A topic connection.
192: *
193: * @throws JMSException Failed to create connection.
194: * @throws IllegalArgumentException Factory is null or invalid.
195: */
196: public static TopicConnection createTopicConnection(
197: final Object factory, final String username,
198: final String password) throws JMSException {
199: if (factory == null)
200: throw new IllegalArgumentException("factory is null");
201:
202: log.debug("using connection factory: " + factory);
203: log.debug("using username/password: "
204: + String.valueOf(username) + "/-- not shown --");
205:
206: TopicConnection connection;
207:
208: if (factory instanceof XATopicConnectionFactory) {
209: XATopicConnectionFactory tFactory = (XATopicConnectionFactory) factory;
210: if (username != null)
211: connection = tFactory.createXATopicConnection(username,
212: password);
213: else
214: connection = tFactory.createXATopicConnection();
215:
216: log.debug("created XATopicConnection: " + connection);
217: } else if (factory instanceof TopicConnectionFactory) {
218: TopicConnectionFactory tFactory = (TopicConnectionFactory) factory;
219: if (username != null)
220: connection = tFactory.createTopicConnection(username,
221: password);
222: else
223: connection = tFactory.createTopicConnection();
224:
225: log.debug("created TopicConnection: " + connection);
226: } else
227: throw new IllegalArgumentException("factory is invalid");
228:
229: return connection;
230: }
231:
232: /**
233: * Create a topic connection from the given factory. An XA connection will
234: * be created if possible.
235: *
236: * @param factory An object that implements TopicConnectionFactory,
237: * XATopicConnectionFactory
238: * @return A topic connection.
239: *
240: * @throws JMSException Failed to create connection.
241: * @throws IllegalArgumentException Factory is null or invalid.
242: */
243: public static TopicConnection createTopicConnection(
244: final Object factory) throws JMSException {
245: return createTopicConnection(factory, null, null);
246: }
247: }
|