KickJava   Java API By Example, From Geeks To Geeks.

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


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.XATopicHALocalConnectionFactory;
29 import org.objectweb.joram.client.jms.ha.tcp.XAHATcpConnectionFactory;
30 import org.objectweb.joram.client.jms.ha.tcp.XATopicHATcpConnectionFactory;
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.local.XATopicLocalConnectionFactory;
34 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory;
35 import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory;
36 import org.objectweb.joram.client.jms.tcp.XAQueueTcpConnectionFactory;
37 import org.objectweb.joram.client.jms.tcp.XATcpConnectionFactory;
38 import org.objectweb.joram.client.jms.tcp.XATopicTcpConnectionFactory;
39
40 import javax.jms.ConnectionFactory JavaDoc;
41 import javax.jms.JMSException JavaDoc;
42 import javax.jms.JMSSecurityException JavaDoc;
43 import javax.jms.IllegalStateException JavaDoc;
44 import javax.jms.TopicConnectionFactory JavaDoc;
45 import javax.jms.XAConnection JavaDoc;
46 import javax.jms.XATopicConnection JavaDoc;
47 import javax.jms.XAConnectionFactory JavaDoc;
48 import javax.jms.XATopicConnectionFactory JavaDoc;
49 import javax.naming.StringRefAddr JavaDoc;
50 import javax.naming.Reference JavaDoc;
51 import javax.resource.ResourceException JavaDoc;
52 import javax.resource.spi.CommException JavaDoc;
53 import javax.resource.spi.ConnectionManager JavaDoc;
54 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
55 import javax.resource.spi.ManagedConnection JavaDoc;
56 import javax.resource.spi.ResourceAdapter JavaDoc;
57 import javax.resource.spi.SecurityException JavaDoc;
58 import javax.security.auth.Subject JavaDoc;
59
60 import java.io.PrintWriter JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.Set JavaDoc;
63 import java.util.Vector JavaDoc;
64
65 import org.objectweb.util.monolog.api.BasicLevel;
66
67 /**
68  * A <code>ManagedTopicConnectionFactoryImpl</code> instance manages
69  * PubSub outbound connectivity to a given JORAM server.
70  */

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

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

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

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

146   public ManagedConnection JavaDoc
147          createManagedConnection(Subject JavaDoc subject,
148                                  ConnectionRequestInfo JavaDoc cxRequest)
149     throws ResourceException JavaDoc {
150     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
151       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
152                                     this + " createManagedConnection(" + subject +
153                                     ", " + cxRequest + ")");
154
155     String JavaDoc userName;
156     String JavaDoc password;
157
158     String JavaDoc hostName = this.hostName;
159     int serverPort = this.serverPort;
160
161     // For XA recovery, connecting to the JORAM server with the default user
162
// identity.
163
if (cxRequest == null) {
164       userName = this.userName;
165       password = this.password;
166     }
167     else {
168       if (! (cxRequest instanceof ConnectionRequest)) {
169           if (out != null)
170               out.print("Provided ConnectionRequestInfo instance is not a JORAM object.");
171         throw new ResourceException JavaDoc("Provided ConnectionRequestInfo instance "
172                                     + "is not a JORAM object.");
173       }
174
175       userName = ((ConnectionRequest) cxRequest).getUserName();
176       password = ((ConnectionRequest) cxRequest).getPassword();
177     }
178
179     XAConnection JavaDoc cnx = null;
180
181     if (collocated) {
182         hostName = "localhost";
183         serverPort = -1;
184     }
185
186     try {
187         if (isHa) {
188             if (collocated) {
189                 if (cxRequest instanceof TopicConnectionRequest) {
190                     XATopicConnectionFactory JavaDoc factory = XATopicHALocalConnectionFactory.create();
191                     setParameters(factory);
192                     cnx = factory.createXATopicConnection(userName, password);
193                 } else {
194                     XAConnectionFactory JavaDoc factory = XAHALocalConnectionFactory.create();
195                     setParameters(factory);
196                     cnx = factory.createXAConnection(userName, password);
197                 }
198             } else {
199                 String JavaDoc urlHa = "hajoram://" + hostName + ":" + serverPort;
200                 if (cxRequest instanceof TopicConnectionRequest) {
201                     XATopicConnectionFactory JavaDoc factory = XATopicHATcpConnectionFactory.create(urlHa);
202                     setParameters(factory);
203                     cnx = factory.createXATopicConnection(userName, password);
204                 } else {
205                     XAConnectionFactory JavaDoc factory = XAHATcpConnectionFactory.create(urlHa);
206                     setParameters(factory);
207                     cnx = factory.createXAConnection(userName, password);
208                 }
209             }
210         } else {
211             if (collocated) {
212                 if (cxRequest instanceof TopicConnectionRequest) {
213                     XATopicConnectionFactory JavaDoc factory = XATopicLocalConnectionFactory.create();
214                     setParameters(factory);
215                     cnx = factory.createXATopicConnection(userName, password);
216                 } else {
217                     XAConnectionFactory JavaDoc factory = XALocalConnectionFactory.create();
218                     setParameters(factory);
219                     cnx = factory.createXAConnection(userName, password);
220                 }
221             } else {
222                 if (cxRequest instanceof TopicConnectionRequest) {
223                     XATopicConnectionFactory JavaDoc factory = XATopicTcpConnectionFactory.create(hostName, serverPort);
224                     setParameters(factory);
225                     cnx = factory.createXATopicConnection(userName, password);
226                 } else {
227                     XAConnectionFactory JavaDoc factory = XATcpConnectionFactory.create(hostName, serverPort);
228                     setParameters(factory);
229                     cnx = factory.createXAConnection(userName, password);
230                 }
231             }
232         }
233         if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
234             AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
235                                       this + " createManagedConnection cnx = " + cnx);
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 +
285                                     ", " + cxRequest + ")");
286
287     String JavaDoc userName;
288     String JavaDoc mode = "Unified";
289
290     // No user identification provided, using the default one.
291
if (cxRequest == null)
292       userName = this.userName;
293     else {
294       if (! (cxRequest instanceof ConnectionRequest)) {
295           if (out != null)
296               out.print("Provided ConnectionRequestInfo instance is not a JORAM object.");
297         throw new ResourceException JavaDoc("Provided ConnectionRequestInfo instance "
298                                     + "is not a JORAM object.");
299       }
300
301       userName = ((ConnectionRequest) cxRequest).getUserName();
302
303       if (cxRequest instanceof TopicConnectionRequest)
304         mode = "PubSub";
305     }
306
307     String JavaDoc hostName = this.hostName;
308     int serverPort = this.serverPort;
309
310     if (collocated) {
311         hostName = "localhost";
312         serverPort = -1;
313     }
314
315     ManagedConnectionImpl managedCx = null;
316     boolean matching = false;
317
318     Iterator JavaDoc it = connectionSet.iterator();
319     while (! matching && it.hasNext()) {
320       try {
321         managedCx = (ManagedConnectionImpl) it.next();
322         matching = managedCx.matches(hostName, serverPort, userName, mode);
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 ("PubSub:"
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 ManagedTopicConnectionFactoryImpl))
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