KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - ScalAgent Distributed Technologies
4  * Copyright (C) 2004 - 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): Nicolas Tachker (Bull SA)
23  * ScalAgent Distributed Technologies
24  */

25 package org.objectweb.joram.client.connector;
26
27 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory;
28 import org.objectweb.joram.client.jms.tcp.QueueTcpConnectionFactory;
29 import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory;
30
31 import javax.jms.ConnectionFactory JavaDoc;
32 import javax.jms.IllegalStateException JavaDoc;
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.JMSSecurityException JavaDoc;
35 import javax.jms.QueueConnectionFactory JavaDoc;
36 import javax.jms.TopicConnectionFactory JavaDoc;
37 import javax.resource.ResourceException JavaDoc;
38 import javax.resource.spi.CommException JavaDoc;
39 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
40 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
41 import javax.resource.spi.SecurityException JavaDoc;
42
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 /**
46  * The <code>DefaultConnectionManager</code> class is the default connection
47  * manager provided with JORAM resource adapter, which intercepts connections
48  * requests coming from non managed client applications.
49  */

50 public class DefaultConnectionManager
51              implements javax.resource.spi.ConnectionManager JavaDoc,
52                         java.io.Serializable JavaDoc
53 {
54   /**
55    * Static reference to the local <code>DefaultConnectionManager</code>
56    * instance.
57    */

58   private static DefaultConnectionManager ref = null;
59
60
61   /**
62    * Creates a <code>DefaultConnectionManager</code> instance.
63    */

64   public DefaultConnectionManager()
65   {}
66
67
68   /**
69    * Returns a <code>javax.jms.Connection</code> connection instance for
70    * a non managed application.
71    *
72    * @exception CommException If connecting fails.
73    * @exception SecurityException If connecting is not authorized.
74    * @exception ResourceException Generic exception.
75    */

76   public Object JavaDoc allocateConnection(ManagedConnectionFactory JavaDoc mcf,
77                                    ConnectionRequestInfo JavaDoc cxRequest)
78     throws ResourceException JavaDoc {
79     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
80       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
81                                     this + " allocateConnection(" + mcf + "," + cxRequest + ")");
82     
83     String JavaDoc userName;
84     String JavaDoc password;
85     
86     if (cxRequest == null) {
87       userName = ((ManagedConnectionFactoryImpl) mcf).getUserName();
88       password = ((ManagedConnectionFactoryImpl) mcf).getPassword();
89     } else {
90       userName = ((ConnectionRequest) cxRequest).getUserName();
91       password = ((ConnectionRequest) cxRequest).getPassword();
92     }
93     
94     String JavaDoc hostName = ((ManagedConnectionFactoryImpl) mcf).getHostName();
95     int serverPort =
96       ((ManagedConnectionFactoryImpl) mcf).getServerPort().intValue();
97     
98     try {
99       if (cxRequest instanceof QueueConnectionRequest) {
100         QueueConnectionFactory JavaDoc factory =
101           QueueTcpConnectionFactory.create(hostName, serverPort);
102         setFactoryParameters((org.objectweb.joram.client.jms.ConnectionFactory) factory,
103                              (ManagedConnectionFactoryImpl) mcf);
104         return factory.createQueueConnection(userName, password);
105       } else if (cxRequest instanceof TopicConnectionRequest) {
106         TopicConnectionFactory JavaDoc factory =
107           TopicTcpConnectionFactory.create(hostName, serverPort);
108         setFactoryParameters((org.objectweb.joram.client.jms.ConnectionFactory) factory,
109                              (ManagedConnectionFactoryImpl) mcf);
110         return factory.createTopicConnection(userName, password);
111       } else {
112         ConnectionFactory factory =
113           TcpConnectionFactory.create(hostName, serverPort);
114         setFactoryParameters((org.objectweb.joram.client.jms.ConnectionFactory) factory,
115                              (ManagedConnectionFactoryImpl) mcf);
116         return factory.createConnection(userName, password);
117       }
118     } catch (IllegalStateException JavaDoc exc) {
119       throw new CommException JavaDoc("Could not access the JORAM server: " + exc);
120     } catch (JMSSecurityException JavaDoc exc) {
121       throw new SecurityException JavaDoc("Invalid user identification: " + exc);
122     } catch (JMSException JavaDoc exc) {
123       throw new ResourceException JavaDoc("Failed connecting process: " + exc);
124     }
125   }
126
127   private void setFactoryParameters(org.objectweb.joram.client.jms.ConnectionFactory factory ,
128                                     ManagedConnectionFactoryImpl mcf) {
129     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
130       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
131                                     this + " setFactoryParameters(" + factory + "," + mcf + ")");
132     factory.getParameters().connectingTimer = mcf.getConnectingTimer();
133     factory.getParameters().cnxPendingTimer = mcf.getCnxPendingTimer();
134     factory.getParameters().txPendingTimer = mcf.getTxPendingTimer();
135   }
136
137   /**
138    * Returns the reference to the <code>DefaultConnectionManager</code>
139    * instance, creates it if needed.
140    */

141   static DefaultConnectionManager getRef()
142   {
143     if (ref == null)
144       ref = new DefaultConnectionManager();
145
146     return ref;
147   }
148 }
149
Popular Tags