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.resource.adapter.jms;
023:
024: import java.io.PrintWriter;
025: import java.util.Iterator;
026: import java.util.Set;
027:
028: import javax.jms.ConnectionMetaData;
029: import javax.resource.ResourceException;
030: import javax.resource.spi.ConnectionManager;
031: import javax.resource.spi.ConnectionRequestInfo;
032: import javax.resource.spi.ManagedConnection;
033: import javax.resource.spi.ManagedConnectionFactory;
034: import javax.security.auth.Subject;
035:
036: import org.jboss.jms.jndi.JMSProviderAdapter;
037: import org.jboss.logging.Logger;
038:
039: /**
040: * Jms ManagedConectionFactory
041: *
042: * @author <a href="mailto:peter.antman@tim.se">Peter Antman </a>.
043: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
044: * @version $Revision: 57189 $
045: */
046: public class JmsManagedConnectionFactory implements
047: ManagedConnectionFactory {
048: private static final long serialVersionUID = -923483284031773011L;
049:
050: private static final Logger log = Logger
051: .getLogger(JmsManagedConnection.class);
052:
053: /** Settable attributes in ra.xml */
054: private JmsMCFProperties mcfProperties = new JmsMCFProperties();
055:
056: /** Whether we are strict */
057: private boolean strict = true;
058:
059: /** For local access. */
060: private JMSProviderAdapter adapter;
061:
062: public JmsManagedConnectionFactory() {
063: // empty
064: }
065:
066: /**
067: * Create a "non managed" connection factory. No appserver involved
068: */
069: public Object createConnectionFactory() throws ResourceException {
070: return createConnectionFactory(null);
071: }
072:
073: /**
074: * Create a ConnectionFactory with appserver hook
075: */
076: public Object createConnectionFactory(ConnectionManager cxManager)
077: throws ResourceException {
078: Object cf = new JmsConnectionFactoryImpl(this , cxManager);
079:
080: if (log.isTraceEnabled()) {
081: log.trace("Created connection factory: " + cf
082: + ", using connection manager: " + cxManager);
083: }
084:
085: return cf;
086: }
087:
088: /**
089: * Create a new connection to manage in pool
090: */
091: public ManagedConnection createManagedConnection(Subject subject,
092: ConnectionRequestInfo info) throws ResourceException {
093: boolean trace = log.isTraceEnabled();
094:
095: info = getInfo(info);
096: if (trace)
097: log.trace("connection request info: " + info);
098:
099: JmsCred cred = JmsCred.getJmsCred(this , subject, info);
100: if (trace)
101: log.trace("jms credentials: " + cred);
102:
103: // OK we got autentication stuff
104: JmsManagedConnection mc = new JmsManagedConnection(this , info,
105: cred.name, cred.pwd);
106:
107: if (trace)
108: log.trace("created new managed connection: " + mc);
109:
110: return mc;
111: }
112:
113: /**
114: * Match a set of connections from the pool
115: */
116: public ManagedConnection matchManagedConnections(Set connectionSet,
117: Subject subject, ConnectionRequestInfo info)
118: throws ResourceException {
119: boolean trace = log.isTraceEnabled();
120:
121: // Get cred
122: info = getInfo(info);
123: JmsCred cred = JmsCred.getJmsCred(this , subject, info);
124:
125: if (trace)
126: log.trace("Looking for connection matching credentials: "
127: + cred);
128:
129: // Traverse the pooled connections and look for a match, return first
130: // found
131: Iterator connections = connectionSet.iterator();
132:
133: while (connections.hasNext()) {
134: Object obj = connections.next();
135:
136: // We only care for connections of our own type
137: if (obj instanceof JmsManagedConnection) {
138: // This is one from the pool
139: JmsManagedConnection mc = (JmsManagedConnection) obj;
140:
141: // Check if we even created this on
142: ManagedConnectionFactory mcf = mc
143: .getManagedConnectionFactory();
144:
145: // Only admit a connection if it has the same username as our
146: // asked for creds
147:
148: // FIXME, Here we have a problem, jms connection
149: // may be anonymous, have a user name
150:
151: if ((mc.getUserName() == null || (mc.getUserName() != null && mc
152: .getUserName().equals(cred.name)))
153: && mcf.equals(this )) {
154: // Now check if ConnectionInfo equals
155: if (info.equals(mc.getInfo())) {
156:
157: if (trace)
158: log.trace("Found matching connection: "
159: + mc);
160:
161: return mc;
162: }
163: }
164: }
165: }
166:
167: if (trace)
168: log.trace("No matching connection was found");
169:
170: return null;
171: }
172:
173: public void setLogWriter(PrintWriter out) throws ResourceException {
174: //
175: // jason: screw the logWriter stuff for now it sucks ass
176: //
177: }
178:
179: public PrintWriter getLogWriter() throws ResourceException {
180: //
181: // jason: screw the logWriter stuff for now it sucks ass
182: //
183:
184: return null;
185: }
186:
187: /**
188: * Checks for equality ower the configured properties.
189: */
190: public boolean equals(Object obj) {
191: if (obj == null)
192: return false;
193: if (obj instanceof JmsManagedConnectionFactory) {
194: return mcfProperties
195: .equals(((JmsManagedConnectionFactory) obj)
196: .getProperties());
197: } else {
198: return false;
199: }
200: }
201:
202: public int hashCode() {
203: return mcfProperties.hashCode();
204: }
205:
206: // --- Connfiguration API ---
207:
208: public void setJmsProviderAdapterJNDI(String jndi) {
209: mcfProperties.setProviderJNDI(jndi);
210: }
211:
212: public String getJmsProviderAdapterJNDI() {
213: return mcfProperties.getProviderJNDI();
214: }
215:
216: /**
217: * Set userName, null by default.
218: */
219: public void setUserName(String userName) {
220: mcfProperties.setUserName(userName);
221: }
222:
223: /**
224: * Get userName, may be null.
225: */
226: public String getUserName() {
227: return mcfProperties.getUserName();
228: }
229:
230: /**
231: * Set password, null by default.
232: */
233: public void setPassword(String password) {
234: mcfProperties.setPassword(password);
235: }
236:
237: /**
238: * Get password, may be null.
239: */
240: public String getPassword() {
241: return mcfProperties.getPassword();
242: }
243:
244: /**
245: * Get client id, may be null.
246: */
247: public String getClientID() {
248: return mcfProperties.getClientID();
249: }
250:
251: /**
252: * Set client id, null by default.
253: */
254: public void setClientID(final String clientID) {
255: mcfProperties.setClientID(clientID);
256: }
257:
258: public boolean isStrict() {
259: return strict;
260: }
261:
262: public void setStrict(boolean strict) {
263: this .strict = strict;
264: }
265:
266: public void setStrict(Boolean strict) {
267: this .strict = strict.booleanValue();
268: }
269:
270: /**
271: * Set the default session typ
272: *
273: * @param type either javax.jms.Topic or javax.jms.Queue
274: *
275: * @exception ResourceException if type was not a valid type.
276: */
277: public void setSessionDefaultType(String type)
278: throws ResourceException {
279: mcfProperties.setSessionDefaultType(type);
280: }
281:
282: public String getSessionDefaultType() {
283: return mcfProperties.getSessionDefaultType();
284: }
285:
286: /**
287: * For local access
288: */
289: public void setJmsProviderAdapter(final JMSProviderAdapter adapter) {
290: this .adapter = adapter;
291: }
292:
293: public JMSProviderAdapter getJmsProviderAdapter() {
294: return adapter;
295: }
296:
297: private ConnectionRequestInfo getInfo(ConnectionRequestInfo info) {
298: if (info == null) {
299: // Create a default one
300: return new JmsConnectionRequestInfo(mcfProperties);
301: } else {
302: // Fill the one with any defaults
303: ((JmsConnectionRequestInfo) info)
304: .setDefaults(mcfProperties);
305: return info;
306: }
307: }
308:
309: public ConnectionMetaData getMetaData() {
310: return new JmsConnectionMetaData();
311: }
312:
313: //---- MCF to MCF API
314:
315: protected JmsMCFProperties getProperties() {
316: return mcfProperties;
317: }
318: }
|