1 22 package org.jboss.jms; 23 24 import javax.jms.JMSException ; 25 import javax.jms.Connection ; 26 import javax.jms.ConnectionFactory ; 27 import javax.jms.QueueConnection ; 28 import javax.jms.QueueConnectionFactory ; 29 import javax.jms.TopicConnection ; 30 import javax.jms.TopicConnectionFactory ; 31 import javax.jms.XAConnectionFactory ; 32 import javax.jms.XAQueueConnectionFactory ; 33 import javax.jms.XATopicConnectionFactory ; 34 35 import org.jboss.logging.Logger; 36 37 44 public class ConnectionFactoryHelper 45 { 46 47 private static Logger log = Logger.getLogger(ConnectionFactoryHelper.class); 48 49 63 public static Connection createConnection(final Object factory, final String username, final String password) 64 throws JMSException 65 { 66 if (factory == null) 67 throw new IllegalArgumentException ("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 connection; 73 74 if (factory instanceof XAConnectionFactory ) 75 { 76 XAConnectionFactory qFactory = (XAConnectionFactory ) 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 ) 85 { 86 ConnectionFactory qFactory = (ConnectionFactory ) 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 ("factory is invalid"); 97 } 98 99 return connection; 100 } 101 102 113 public static Connection createConnection(final Object factory) throws JMSException 114 { 115 return createConnection(factory, null, null); 116 } 117 118 132 public static QueueConnection createQueueConnection(final Object factory, final String username, 133 final String password) throws JMSException 134 { 135 if (factory == null) 136 throw new IllegalArgumentException ("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 connection; 142 143 if (factory instanceof XAQueueConnectionFactory ) 144 { 145 XAQueueConnectionFactory qFactory = (XAQueueConnectionFactory ) 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 ) 154 { 155 QueueConnectionFactory qFactory = (QueueConnectionFactory ) 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 ("factory is invalid"); 165 166 return connection; 167 } 168 169 180 public static QueueConnection createQueueConnection(final Object factory) throws JMSException 181 { 182 return createQueueConnection(factory, null, null); 183 } 184 185 199 public static TopicConnection createTopicConnection(final Object factory, final String username, 200 final String password) throws JMSException 201 { 202 if (factory == null) 203 throw new IllegalArgumentException ("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 connection; 209 210 if (factory instanceof XATopicConnectionFactory ) 211 { 212 XATopicConnectionFactory tFactory = (XATopicConnectionFactory ) 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 ) 221 { 222 TopicConnectionFactory tFactory = (TopicConnectionFactory ) 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 ("factory is invalid"); 232 233 return connection; 234 } 235 236 247 public static TopicConnection createTopicConnection(final Object factory) throws JMSException 248 { 249 return createTopicConnection(factory, null, null); 250 } 251 } 252 | Popular Tags |