KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jms > JmsManagedConnectionFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * 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.io.PrintWriter JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.jms.ConnectionMetaData JavaDoc;
29 import javax.resource.ResourceException JavaDoc;
30 import javax.resource.spi.ConnectionManager JavaDoc;
31 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
32 import javax.resource.spi.ManagedConnection JavaDoc;
33 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
34 import javax.security.auth.Subject JavaDoc;
35
36 import org.jboss.jms.jndi.JMSProviderAdapter;
37 import org.jboss.logging.Logger;
38
39 /**
40  * Jms ManagedConectionFactory
41  *
42  * @author <a HREF="mailto:peter.antman@tim.se">Peter Antman </a>.
43  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 38315 $
45  */

46 public class JmsManagedConnectionFactory implements ManagedConnectionFactory JavaDoc
47 {
48    private static final long serialVersionUID = -923483284031773011L;
49
50    private static final Logger log = Logger.getLogger(JmsManagedConnection.class);
51
52    /** Settable attributes in ra.xml */
53    private JmsMCFProperties mcfProperties = new JmsMCFProperties();
54
55    /** Whether we are strict */
56    private boolean strict = true;
57
58    /** For local access. */
59    private JMSProviderAdapter adapter;
60
61    public JmsManagedConnectionFactory()
62    {
63       // empty
64
}
65
66    /**
67     * Create a "non managed" connection factory. No appserver involved
68     */

69    public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc
70    {
71       return createConnectionFactory(null);
72    }
73
74    /**
75     * Create a ConnectionFactory with appserver hook
76     */

77    public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cxManager) throws ResourceException JavaDoc
78    {
79       Object JavaDoc cf = new JmsConnectionFactoryImpl(this, cxManager);
80
81       if (log.isTraceEnabled())
82       {
83          log.trace("Created connection factory: " + cf + ", using connection manager: " + cxManager);
84       }
85
86       return cf;
87    }
88
89    /**
90     * Create a new connection to manage in pool
91     */

92    public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc info)
93          throws ResourceException JavaDoc
94    {
95       boolean trace = log.isTraceEnabled();
96
97       info = getInfo(info);
98       if (trace)
99          log.trace("connection request info: " + info);
100
101       JmsCred cred = JmsCred.getJmsCred(this, subject, info);
102       if (trace)
103          log.trace("jms credentials: " + cred);
104
105       // OK we got autentication stuff
106
JmsManagedConnection mc = new JmsManagedConnection(this, info, cred.name, cred.pwd);
107
108       if (trace)
109          log.trace("created new managed connection: " + mc);
110
111       // Set default logwriter according to spec
112

113       //
114
// jason: screw the logWriter stuff for now it sucks ass
115
//
116

117       return mc;
118    }
119
120    /**
121     * Match a set of connections from the pool
122     */

123    public ManagedConnection JavaDoc matchManagedConnections(Set JavaDoc connectionSet, Subject JavaDoc subject, ConnectionRequestInfo JavaDoc info)
124          throws ResourceException JavaDoc
125    {
126       boolean trace = log.isTraceEnabled();
127
128       // Get cred
129
info = getInfo(info);
130       JmsCred cred = JmsCred.getJmsCred(this, subject, info);
131
132       if (trace)
133          log.trace("Looking for connection matching credentials: " + cred);
134
135       // Traverse the pooled connections and look for a match, return first
136
// found
137
Iterator JavaDoc connections = connectionSet.iterator();
138
139       while (connections.hasNext())
140       {
141          Object JavaDoc obj = connections.next();
142
143          // We only care for connections of our own type
144
if (obj instanceof JmsManagedConnection)
145          {
146             // This is one from the pool
147
JmsManagedConnection mc = (JmsManagedConnection) obj;
148
149             // Check if we even created this on
150
ManagedConnectionFactory JavaDoc mcf = mc.getManagedConnectionFactory();
151
152             // Only admit a connection if it has the same username as our
153
// asked for creds
154

155             // FIXME, Here we have a problem, jms connection
156
// may be anonymous, have a user name
157

158             if ((mc.getUserName() == null || (mc.getUserName() != null && mc.getUserName().equals(cred.name)))
159                   && mcf.equals(this))
160             {
161                // Now check if ConnectionInfo equals
162
if (info.equals(mc.getInfo()))
163                {
164
165                   if (trace)
166                      log.trace("Found matching connection: " + mc);
167
168                   return mc;
169                }
170             }
171          }
172       }
173
174       if (trace)
175          log.trace("No matching connection was found");
176
177       return null;
178    }
179
180    public void setLogWriter(PrintWriter JavaDoc out) throws ResourceException JavaDoc
181    {
182       //
183
// jason: screw the logWriter stuff for now it sucks ass
184
//
185
}
186
187    public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc
188    {
189       //
190
// jason: screw the logWriter stuff for now it sucks ass
191
//
192

193       return null;
194    }
195
196    /**
197     * Checks for equality ower the configured properties.
198     */

199    public boolean equals(Object JavaDoc obj)
200    {
201       if (obj == null)
202          return false;
203       if (obj instanceof JmsManagedConnectionFactory)
204       {
205          return mcfProperties.equals(((JmsManagedConnectionFactory) obj).getProperties());
206       }
207       else
208       {
209          return false;
210       }
211    }
212
213    public int hashCode()
214    {
215       return mcfProperties.hashCode();
216    }
217
218    // --- Connfiguration API ---
219

220    public void setJmsProviderAdapterJNDI(String JavaDoc jndi)
221    {
222       mcfProperties.setProviderJNDI(jndi);
223    }
224
225    public String JavaDoc getJmsProviderAdapterJNDI()
226    {
227       return mcfProperties.getProviderJNDI();
228    }
229
230    /**
231     * Set userName, null by default.
232     */

233    public void setUserName(String JavaDoc userName)
234    {
235       mcfProperties.setUserName(userName);
236    }
237
238    /**
239     * Get userName, may be null.
240     */

241    public String JavaDoc getUserName()
242    {
243       return mcfProperties.getUserName();
244    }
245
246    /**
247     * Set password, null by default.
248     */

249    public void setPassword(String JavaDoc password)
250    {
251       mcfProperties.setPassword(password);
252    }
253
254    /**
255     * Get password, may be null.
256     */

257    public String JavaDoc getPassword()
258    {
259       return mcfProperties.getPassword();
260    }
261
262    /**
263     * Get client id, may be null.
264     */

265    public String JavaDoc getClientID()
266    {
267       return mcfProperties.getClientID();
268    }
269
270    /**
271     * Set client id, null by default.
272     */

273    public void setClientID(final String JavaDoc clientID)
274    {
275       mcfProperties.setClientID(clientID);
276    }
277
278    public boolean isStrict()
279    {
280       return strict;
281    }
282
283    public void setStrict(boolean strict)
284    {
285       this.strict = strict;
286    }
287
288    public void setStrict(Boolean JavaDoc strict)
289    {
290       this.strict = strict.booleanValue();
291    }
292
293    /**
294     * Set the default session typ
295     *
296     * @param type either javax.jms.Topic or javax.jms.Queue
297     *
298     * @exception ResourceException if type was not a valid type.
299     */

300    public void setSessionDefaultType(String JavaDoc type) throws ResourceException JavaDoc
301    {
302       mcfProperties.setSessionDefaultType(type);
303    }
304
305    public String JavaDoc getSessionDefaultType()
306    {
307       return mcfProperties.getSessionDefaultType();
308    }
309
310    /**
311     * For local access
312     */

313    public void setJmsProviderAdapter(final JMSProviderAdapter adapter)
314    {
315       this.adapter = adapter;
316    }
317
318    public JMSProviderAdapter getJmsProviderAdapter()
319    {
320       return adapter;
321    }
322
323    private ConnectionRequestInfo JavaDoc getInfo(ConnectionRequestInfo JavaDoc info)
324    {
325       if (info == null)
326       {
327          // Create a default one
328
return new JmsConnectionRequestInfo(mcfProperties);
329       }
330       else
331       {
332          // Fill the one with any defaults
333
((JmsConnectionRequestInfo) info).setDefaults(mcfProperties);
334          return info;
335       }
336    }
337
338    public ConnectionMetaData JavaDoc getMetaData()
339    {
340       return new JmsConnectionMetaData();
341    }
342
343    //---- MCF to MCF API
344

345    protected JmsMCFProperties getProperties()
346    {
347       return mcfProperties;
348    }
349 }
350
Popular Tags