KickJava   Java API By Example, From Geeks To Geeks.

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


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

23 package org.objectweb.joram.client.connector;
24
25 import javax.jms.IllegalStateException JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.JMSSecurityException JavaDoc;
28 import javax.naming.Reference JavaDoc;
29 import javax.resource.spi.ConnectionManager JavaDoc;
30 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
31
32 import org.objectweb.util.monolog.api.BasicLevel;
33
34 /**
35  * An <code>OutboundConnectionFactory</code> instance is used for
36  * getting a connection to an underlying JORAM server.
37  */

38 public class OutboundConnectionFactory implements javax.jms.ConnectionFactory JavaDoc,
39                                                   java.io.Serializable JavaDoc,
40                                                   javax.resource.Referenceable JavaDoc
41 {
42   /** Central manager for outbound connectivity. */
43   protected ManagedConnectionFactoryImpl mcf;
44   /** Manager for connection pooling. */
45   protected ConnectionManager JavaDoc cxManager;
46
47   /** Naming reference of this instance. */
48   protected Reference JavaDoc reference;
49
50
51   /**
52    * Constructs an <code>OutboundConnectionFactory</code> instance.
53    *
54    * @param mcf Central manager for outbound connectivity.
55    * @param cxManager Manager for connection pooling.
56    */

57   OutboundConnectionFactory(ManagedConnectionFactoryImpl mcf,
58                             ConnectionManager JavaDoc cxManager) {
59
60     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
61       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
62                                     "OutboundConnectionFactory(" + mcf +
63                                     ", " + cxManager + ")");
64
65     this.mcf = mcf;
66     if (cxManager != null) {
67       this.cxManager = cxManager;
68     } else {
69       this.cxManager = DefaultConnectionManager.getRef();
70     }
71   }
72
73
74   /**
75    * Requests a connection for the default user, eventually returns an
76    * <code>OutboundConnection</code> instance.
77    *
78    * @exception JMSSecurityException If connecting is not allowed.
79    * @exception IllegalStateException If the underlying JORAM server
80    * is not reachable.
81    * @exception JMSException Generic exception.
82    */

83   public javax.jms.Connection JavaDoc createConnection()
84     throws JMSException JavaDoc {
85     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
86       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConnection()");
87
88     return createConnection(mcf.userName, mcf.password);
89   }
90
91   /**
92    * Requests a connection for a given user, eventually returns an
93    * <code>OutboundConnection</code> instance.
94    *
95    * @exception JMSSecurityException If connecting is not allowed.
96    * @exception IllegalStateException If the underlying JORAM server
97    * is not reachable.
98    * @exception JMSException Generic exception.
99    */

100   public javax.jms.Connection JavaDoc
101       createConnection(String JavaDoc userName, String JavaDoc password)
102     throws JMSException JavaDoc {
103
104     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
105       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
106                                     this + " createConnection(" + userName +
107                                     ", " + password + ")");
108
109     try {
110       ConnectionRequest cxRequest =
111         new ConnectionRequest(userName, password);
112
113       if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
114         AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
115                                       this + " createConnection cxManager = " + cxManager);
116
117       Object JavaDoc o = cxManager.allocateConnection(mcf, cxRequest);
118
119       if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
120         AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
121                                       this + " createConnection connection = " + o);
122
123       return (javax.jms.Connection JavaDoc) o;
124     } catch (javax.resource.spi.SecurityException JavaDoc exc) {
125       throw new JMSSecurityException JavaDoc("Invalid user identification: " + exc);
126     } catch (javax.resource.spi.CommException JavaDoc exc) {
127       throw new IllegalStateException JavaDoc("Could not connect to the JORAM server: "
128                                       + exc);
129     } catch (javax.resource.ResourceException JavaDoc exc) {
130       throw new JMSException JavaDoc("Could not create connection: " + exc);
131     }
132   }
133
134   /** Sets the naming reference of this factory. */
135   public void setReference(Reference JavaDoc ref)
136   {
137     this.reference = ref;
138   }
139
140   /** Returns the naming reference of this factory. */
141   public Reference JavaDoc getReference()
142   {
143     return reference;
144   }
145 }
146
Popular Tags