KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > connector > ManagedQueueConnectionFactoryImpl


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 2004 - 2006 Bull SA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (Bull SA)
22  * Contributor(s): ScalAgent Distributed Technologies
23  * Benoit Pelletier (Bull SA)
24  */

25 package org.objectweb.joram.client.connector;
26
27 import org.objectweb.joram.client.jms.ha.local.XAHALocalConnectionFactory;
28 import org.objectweb.joram.client.jms.ha.local.XAQueueHALocalConnectionFactory;
29 import org.objectweb.joram.client.jms.ha.tcp.XAHATcpConnectionFactory;
30 import org.objectweb.joram.client.jms.ha.tcp.XAQueueHATcpConnectionFactory;
31 import org.objectweb.joram.client.jms.local.XALocalConnectionFactory;
32 import org.objectweb.joram.client.jms.local.XAQueueLocalConnectionFactory;
33 import org.objectweb.joram.client.jms.tcp.QueueTcpConnectionFactory;
34 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory;
35 import org.objectweb.joram.client.jms.tcp.XATcpConnectionFactory;
36 import org.objectweb.joram.client.jms.tcp.XAQueueTcpConnectionFactory;
37
38 import javax.jms.ConnectionFactory JavaDoc;
39 import javax.jms.JMSException JavaDoc;
40 import javax.jms.JMSSecurityException JavaDoc;
41 import javax.jms.IllegalStateException JavaDoc;
42 import javax.jms.QueueConnectionFactory JavaDoc;
43 import javax.jms.XAConnection JavaDoc;
44 import javax.jms.XAQueueConnection JavaDoc;
45 import javax.jms.XAConnectionFactory JavaDoc;
46 import javax.jms.XAQueueConnectionFactory JavaDoc;
47 import javax.naming.StringRefAddr JavaDoc;
48 import javax.naming.Reference JavaDoc;
49 import javax.resource.ResourceException JavaDoc;
50 import javax.resource.spi.CommException JavaDoc;
51 import javax.resource.spi.ConnectionManager JavaDoc;
52 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
53 import javax.resource.spi.ManagedConnection JavaDoc;
54 import javax.resource.spi.ResourceAdapter JavaDoc;
55 import javax.resource.spi.SecurityException JavaDoc;
56 import javax.security.auth.Subject JavaDoc;
57
58 import java.io.PrintWriter JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.Set JavaDoc;
61 import java.util.Vector JavaDoc;
62
63 import org.objectweb.util.monolog.api.BasicLevel;
64
65 /**
66  * A <code>ManagedQueueConnectionFactoryImpl</code> instance manages
67  * PTP outbound connectivity to a given JORAM server.
68  */

69 public class ManagedQueueConnectionFactoryImpl
70              extends ManagedConnectionFactoryImpl
71              implements javax.resource.spi.ManagedConnectionFactory JavaDoc,
72                         javax.resource.spi.ResourceAdapterAssociation JavaDoc,
73                         javax.resource.spi.ValidatingManagedConnectionFactory JavaDoc,
74                         java.io.Serializable JavaDoc
75 {
76   /**
77    * Constructs a <code>ManagedQueueConnectionFactoryImpl</code> instance.
78    */

79   public ManagedQueueConnectionFactoryImpl()
80   {}
81
82
83   /**
84    * Method called by an application server (managed case) for creating an
85    * <code>OutboundQueueConnectionFactory</code> instance.
86    *
87    * @param cxManager Application server's connections pooling manager.
88    *
89    * @exception ResourceException Never thrown.
90    */

91   public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cxManager)
92     throws ResourceException JavaDoc {
93     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
94       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConnectionFactory(" + cxManager + ")");
95
96     return new OutboundQueueConnectionFactory(this, cxManager);
97   }
98
99   /**
100    * Method called in the non managed case for creating an
101    * <code>OutboundQueueConnectionFactory</code> instance.
102    *
103    * @exception ResourceException Never thrown.
104    */

105   public Object JavaDoc createConnectionFactory()
106     throws ResourceException JavaDoc {
107     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
108       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConnectionFactory()");
109
110     OutboundConnectionFactory factory =
111       new OutboundQueueConnectionFactory(this,
112                                          DefaultConnectionManager.getRef());
113
114     Reference JavaDoc ref =
115       new Reference JavaDoc(factory.getClass().getName(),
116                     "org.objectweb.joram.client.connector.ObjectFactoryImpl",
117                     null);
118     ref.add(new StringRefAddr JavaDoc("hostName", hostName));
119     ref.add(new StringRefAddr JavaDoc("serverPort", "" + serverPort));
120     ref.add(new StringRefAddr JavaDoc("userName", userName));
121     ref.add(new StringRefAddr JavaDoc("password", password));
122
123     factory.setReference(ref);
124     return factory;
125   }
126
127   /**
128    * Creates a new PTP physical connection to the underlying JORAM server,
129    * and returns a <code>ManagedConnectionImpl</code> instance for a
130    * managed environment.
131    *
132    * @param subject Security data, not taken into account.
133    * @param cxRequest User identification data, may be <code>null</code>.
134    *
135    * @exception CommException If the JORAM server is not reachable.
136    * @exception SecurityException If the connecting is not allowed.
137    * @exception IllegalStateException If the central Joram adapter state is
138    * invalid.
139    * @exception ResourceException If the provided user info is invalid,
140    * or if connecting fails for any other
141    * reason.
142    */

143   public ManagedConnection JavaDoc
144       createManagedConnection(Subject JavaDoc subject,
145                               ConnectionRequestInfo JavaDoc cxRequest)
146     throws ResourceException JavaDoc {
147
148     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
149       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
150                                     this + " createManagedConnection(" + subject +
151                                     ", " + cxRequest + ")");
152
153     String JavaDoc userName;
154     String JavaDoc password;
155
156     String JavaDoc hostName = this.hostName;
157     int serverPort = this.serverPort;
158
159     // For XA recovery, connecting to the JORAM server with the default user
160
// identity.
161
if (cxRequest == null) {
162       userName = this.userName;
163       password = this.password;
164     }
165     else {
166       if (! (cxRequest instanceof ConnectionRequest)) {
167           if (out != null)
168               out.print("Provided ConnectionRequestInfo instance is not a JORAM object.");
169         throw new ResourceException JavaDoc("Provided ConnectionRequestInfo instance "
170                                     + "is not a JORAM object.");
171       }
172
173       userName = ((ConnectionRequest) cxRequest).getUserName();
174       password = ((ConnectionRequest) cxRequest).getPassword();
175     }
176
177     XAConnection JavaDoc cnx = null;
178
179     if (collocated) {
180         hostName = "localhost";
181         serverPort = -1;
182     }
183
184     try {
185
186         if (isHa) {
187             if (collocated) {
188                 if (cxRequest instanceof QueueConnectionRequest) {
189                     XAQueueConnectionFactory JavaDoc factory = XAQueueHALocalConnectionFactory.create();
190                     setParameters(factory);
191                     cnx = factory.createXAQueueConnection(userName, password);
192                 } else {
193                     XAConnectionFactory JavaDoc factory = XAHALocalConnectionFactory.create();
194                     setParameters(factory);
195                     cnx = factory.createXAConnection(userName, password);
196                 }
197             } else {
198                 String JavaDoc urlHa = "hajoram://" + hostName + ":" + serverPort;
199                 if (cxRequest instanceof QueueConnectionRequest) {
200                     XAQueueConnectionFactory JavaDoc factory = XAQueueHATcpConnectionFactory.create(urlHa);
201                     setParameters(factory);
202                     cnx = factory.createXAQueueConnection(userName, password);
203                 } else {
204                     XAConnectionFactory JavaDoc factory = XAHATcpConnectionFactory.create(urlHa);
205                     setParameters(factory);
206                     cnx = factory.createXAConnection(userName, password);
207                 }
208             }
209         } else {
210             if (collocated) {
211                 if (cxRequest instanceof QueueConnectionRequest) {
212                     XAQueueConnectionFactory JavaDoc factory = XAQueueLocalConnectionFactory.create();
213                     setParameters(factory);
214                     cnx = factory.createXAQueueConnection(userName, password);
215                 } else {
216                     XAConnectionFactory JavaDoc factory = XALocalConnectionFactory.create();
217                     setParameters(factory);
218                     cnx = factory.createXAConnection(userName, password);
219                 }
220             } else {
221                 if (cxRequest instanceof QueueConnectionRequest) {
222                     XAQueueConnectionFactory JavaDoc factory = XAQueueTcpConnectionFactory.create(hostName, serverPort);
223                     setParameters(factory);
224                     cnx = factory.createXAQueueConnection(userName, password);
225                 } else {
226                     XAConnectionFactory JavaDoc factory = XATcpConnectionFactory.create(hostName, serverPort);
227                     setParameters(factory);
228                     cnx = factory.createXAConnection(userName, password);
229                 }
230             }
231         }
232
233         if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
234             AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createManagedConnection cnx = " + cnx);
235
236     } catch (IllegalStateException JavaDoc exc) {
237         if (out != null)
238             out.print("Could not access the JORAM server: " + exc);
239       throw new CommException JavaDoc("Could not access the JORAM server: " + exc);
240     } catch (JMSSecurityException JavaDoc exc) {
241         if (out != null)
242             out.print("Invalid user identification: " + exc);
243       throw new SecurityException JavaDoc("Invalid user identification: " + exc);
244     } catch (JMSException JavaDoc exc) {
245         if (out != null)
246             out.print("Failed connecting process: " + exc);
247       throw new ResourceException JavaDoc("Failed connecting process: " + exc);
248     }
249
250     ManagedConnection JavaDoc managedCx = new ManagedConnectionImpl(ra,
251                                                             cnx,
252                                                             hostName,
253                                                             serverPort,
254                                                             userName);
255     managedCx.setLogWriter(out);
256
257     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
258       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
259                                     this + " createManagedConnection managedCx = " + managedCx);
260
261     return managedCx;
262   }
263
264   /**
265    * Finds a matching connection from the candidate set of connections and
266    * returns a <code>ManagedConnectionImpl</code> instance.
267    *
268    * @param connectionSet Set of connections to test.
269    * @param subject Security data, not taken into account.
270    * @param cxRequest User identification data, may be <code>null</code>.
271    *
272    * @exception ResourceException If the provided connection request info is
273    * invalid.
274    */

275   public ManagedConnection JavaDoc
276       matchManagedConnections(Set JavaDoc connectionSet,
277                               Subject JavaDoc subject,
278                               ConnectionRequestInfo JavaDoc cxRequest)
279     throws ResourceException JavaDoc {
280
281     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
282       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
283                                     this + " matchManagedConnections(" + connectionSet +
284                                     ", " + subject + ", " + cxRequest + ")");
285
286     String JavaDoc userName;
287     String JavaDoc mode = "Unified";
288
289     // No user identification provided, using the default one.
290
if (cxRequest == null)
291       userName = this.userName;
292     else {
293       if (! (cxRequest instanceof ConnectionRequest)) {
294           if (out != null)
295               out.print("Provided ConnectionRequestInfo instance is not a JORAM object.");
296         throw new ResourceException JavaDoc("Provided ConnectionRequestInfo instance "
297                                     + "is not a JORAM object.");
298       }
299
300       userName = ((ConnectionRequest) cxRequest).getUserName();
301
302       if (cxRequest instanceof QueueConnectionRequest)
303         mode = "PTP";
304     }
305
306     String JavaDoc hostName = this.hostName;
307     int serverPort = this.serverPort;
308
309     if (collocated) {
310         hostName = "localhost";
311         serverPort = -1;
312     }
313
314     ManagedConnectionImpl managedCx = null;
315     boolean matching = false;
316
317     Iterator JavaDoc it = connectionSet.iterator();
318     while (! matching && it.hasNext()) {
319       try {
320         managedCx = (ManagedConnectionImpl) it.next();
321         matching = managedCx.matches(hostName, serverPort, userName, mode);
322       }
323       catch (ClassCastException JavaDoc exc) {}
324     }
325
326     if (matching) {
327       if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
328         AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
329                                       this + " matchManagedConnections match " + managedCx);
330       managedCx.setLogWriter(out);
331       return managedCx;
332     }
333     return null;
334   }
335
336   /** Returns a code depending on the managed factory configuration. */
337   public int hashCode()
338   {
339     return ("PTP:"
340             + hostName
341             + ":"
342             + serverPort
343             + "-"
344             + userName).hashCode();
345   }
346
347   /** Compares managed factories according to their configuration. */
348   public boolean equals(Object JavaDoc o)
349   {
350     if (! (o instanceof ManagedQueueConnectionFactoryImpl))
351       return false;
352
353     ManagedConnectionFactoryImpl other = (ManagedConnectionFactoryImpl) o;
354
355     boolean res =
356       hostName.equals(other.hostName)
357       && serverPort == other.serverPort
358       && userName.equals(other.userName);
359
360     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
361       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
362                                     this + " equals = " + res);
363     return res;
364   }
365 }
366
Popular Tags