1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a 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.resource.adapter.jms;
23
24 import java.sql.SQLException;
25
26 import javax.jms.Connection;
27 import javax.jms.JMSException;
28 import javax.jms.QueueConnection;
29 import javax.jms.TopicConnection;
30 import javax.naming.Reference;
31 import javax.resource.Referenceable;
32 import javax.resource.spi.ConnectionManager;
33 import javax.resource.spi.ManagedConnectionFactory;
34
35 import org.jboss.logging.Logger;
36 import org.jboss.resource.connectionmanager.JTATransactionChecker;
37 import org.jboss.util.NestedSQLException;
38
39 /**
40 * The the connection factory implementation for the JMS RA.
41 *
42 * <p>
43 * This object will be the QueueConnectionFactory or TopicConnectionFactory
44 * which clients will use to create connections.
45 *
46 * @author <a href="mailto:peter.antman@tim.se">Peter Antman</a>.
47 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
48 * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
49 * @version <tt>$Revision: 71790 $</tt>
50 */
51 public class JmsConnectionFactoryImpl
52 implements JmsConnectionFactory, Referenceable
53 {
54 private static final long serialVersionUID = -5135366013101194277L;
55
56 private static final Logger log = Logger.getLogger(JmsConnectionFactoryImpl.class);
57
58 private ManagedConnectionFactory mcf;
59
60 private ConnectionManager cm;
61
62 private Reference reference;
63
64 public JmsConnectionFactoryImpl(final ManagedConnectionFactory mcf,
65 final ConnectionManager cm)
66 {
67 this.mcf = mcf;
68
69 boolean trace = log.isTraceEnabled();
70 if (cm == null)
71 {
72 // This is standalone usage, no appserver
73 this.cm = new JmsConnectionManager();
74 if (trace)
75 log.trace("Created new connection manager");
76 }
77 else
78 this.cm = cm;
79
80 if (trace)
81 log.trace("Using ManagedConnectionFactory=" + mcf + ", ConnectionManager=" + cm);
82 }
83
84 public void setReference(final Reference reference)
85 {
86 this.reference = reference;
87
88 if (log.isTraceEnabled())
89 log.trace("Using Reference=" + reference);
90 }
91
92 public Reference getReference()
93 {
94 return reference;
95 }
96
97 // --- QueueConnectionFactory
98
99 public QueueConnection createQueueConnection() throws JMSException
100 {
101 QueueConnection qc = new JmsSessionFactoryImpl(mcf, cm, QUEUE);
102
103 if (log.isTraceEnabled())
104 log.trace("Created queue connection: " + qc);
105
106 return qc;
107 }
108
109 public QueueConnection createQueueConnection(String userName, String password)
110 throws JMSException
111 {
112 JmsSessionFactoryImpl s = new JmsSessionFactoryImpl(mcf, cm, QUEUE);
113 s.setUserName(userName);
114 s.setPassword(password);
115
116 if (log.isTraceEnabled())
117 log.trace("Created queue connection: " + s);
118
119 return s;
120 }
121
122 // --- TopicConnectionFactory
123
124 public TopicConnection createTopicConnection() throws JMSException
125 {
126 TopicConnection tc = new JmsSessionFactoryImpl(mcf, cm, TOPIC);
127
128 if (log.isTraceEnabled())
129 log.trace("Created topic connection: " + tc);
130
131 return tc;
132 }
133
134 public TopicConnection createTopicConnection(String userName, String password)
135 throws JMSException
136 {
137 JmsSessionFactoryImpl s = new JmsSessionFactoryImpl(mcf, cm, TOPIC);
138 s.setUserName(userName);
139 s.setPassword(password);
140
141 if (log.isTraceEnabled())
142 log.trace("Created topic connection: " + s);
143
144 return s;
145 }
146
147 // --- JMS 1.1
148
149 public Connection createConnection()
150 throws JMSException
151 {
152 Connection c = new JmsSessionFactoryImpl(mcf, cm, BOTH);
153
154 if (log.isTraceEnabled())
155 log.trace("Created connection: " + c);
156
157 return c;
158 }
159
160 public Connection createConnection(String userName, String password)
161 throws JMSException
162 {
163 JmsSessionFactoryImpl s = new JmsSessionFactoryImpl(mcf, cm, BOTH);
164 s.setUserName(userName);
165 s.setPassword(password);
166
167 if (log.isTraceEnabled())
168 log.trace("Created connection: " + s);
169
170 return s;
171 }
172 }