package org.jboss.jms;
import javax.jms.JMSException;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.XAConnectionFactory;
import javax.jms.XAQueueConnectionFactory;
import javax.jms.XATopicConnectionFactory;
import org.jboss.logging.Logger;
public class ConnectionFactoryHelper
{
private static Logger log = Logger.getLogger(ConnectionFactoryHelper.class);
public static Connection createConnection(final Object factory,
final String username,
final String password)
throws JMSException
{
if (factory == null)
throw new IllegalArgumentException("factory is null");
boolean debug = log.isDebugEnabled();
if (debug)
{
log.debug("using connection factory: " + factory);
log.debug("using username/password: " +
String.valueOf(username) + "/" +
String.valueOf(password));
}
Connection connection;
if (factory instanceof XAConnectionFactory)
{
XAConnectionFactory qFactory = (XAConnectionFactory)factory;
if (username != null)
connection = qFactory.createXAConnection(username, password);
else
connection = qFactory.createXAConnection();
if (debug)
log.debug("created XAConnection: " + connection);
}
else if (factory instanceof ConnectionFactory)
{
ConnectionFactory qFactory = (ConnectionFactory)factory;
if (username != null)
connection = qFactory.createConnection(username, password);
else
connection = qFactory.createConnection();
if (debug)
log.debug("created Connection: " + connection);
}
else {
throw new IllegalArgumentException("factory is invalid");
}
return connection;
}
public static Connection createConnection(final Object factory)
throws JMSException
{
return createConnection(factory, null, null);
}
public static QueueConnection createQueueConnection(final Object factory,
final String username,
final String password)
throws JMSException
{
if (factory == null)
throw new IllegalArgumentException("factory is null");
boolean debug = log.isDebugEnabled();
if (debug)
{
log.debug("using connection factory: " + factory);
log.debug("using username/password: " +
String.valueOf(username) + "/" +
String.valueOf(password));
}
QueueConnection connection;
if (factory instanceof XAQueueConnectionFactory)
{
XAQueueConnectionFactory qFactory = (XAQueueConnectionFactory)factory;
if (username != null)
connection = qFactory.createXAQueueConnection(username, password);
else
connection = qFactory.createXAQueueConnection();
if (debug)
log.debug("created XAQueueConnection: " + connection);
}
else if (factory instanceof QueueConnectionFactory)
{
QueueConnectionFactory qFactory = (QueueConnectionFactory)factory;
if (username != null)
connection = qFactory.createQueueConnection(username, password);
else
connection = qFactory.createQueueConnection();
if (debug)
log.debug("created QueueConnection: " + connection);
}
else
throw new IllegalArgumentException("factory is invalid");
return connection;
}
public static QueueConnection createQueueConnection(final Object factory)
throws JMSException
{
return createQueueConnection(factory, null, null);
}
public static TopicConnection createTopicConnection(final Object factory,
final String username,
final String password)
throws JMSException
{
if (factory == null)
throw new IllegalArgumentException("factory is null");
boolean debug = log.isDebugEnabled();
if (debug)
{
log.debug("using connection factory: " + factory);
log.debug("using username/password: " +
String.valueOf(username) + "/" +
String.valueOf(password));
}
TopicConnection connection;
if (factory instanceof XATopicConnectionFactory)
{
XATopicConnectionFactory tFactory = (XATopicConnectionFactory)factory;
if (username != null)
connection = tFactory.createXATopicConnection(username, password);
else
connection = tFactory.createXATopicConnection();
if (debug)
log.debug("created XATopicConnection: " + connection);
}
else if (factory instanceof TopicConnectionFactory)
{
TopicConnectionFactory tFactory = (TopicConnectionFactory)factory;
if (username != null)
connection = tFactory.createTopicConnection(username, password);
else
connection = tFactory.createTopicConnection();
if (debug)
log.debug("created TopicConnection: " + connection);
}
else
throw new IllegalArgumentException("factory is invalid");
return connection;
}
public static TopicConnection createTopicConnection(final Object factory)
throws JMSException
{
return createTopicConnection(factory, null, null);
}
}