KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
26 import javax.jms.IllegalStateException JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.objectweb.util.monolog.api.BasicLevel;
30
31 /**
32  * An <code>OutboundConnection</code> instance is a handler for a physical
33  * connection to an underlying JORAM server, allowing a component to
34  * transparently use this physical connection possibly within a transaction
35  * (local or global).
36  */

37 public class OutboundConnection implements javax.jms.Connection JavaDoc
38 {
39   /** The managed connection this "handle" belongs to. */
40   ManagedConnectionImpl managedCx;
41   /** The physical connection this "handle" handles. */
42   XAConnection xac;
43   /** <code>true</code> if this "handle" is valid. */
44   boolean valid = true;
45   /** Vector of the connection's sessions. */
46   Vector JavaDoc sessions;
47  
48   /**
49    * Constructs an <code>OutboundConnection</code> instance.
50    *
51    * @param managedCx The managed connection building the handle.
52    * @param xac The underlying physical connection to handle.
53    */

54   OutboundConnection(ManagedConnectionImpl managedCx,
55                      XAConnection xac) {
56
57     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
58       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
59                                     "OutboundConnection(" + managedCx +
60                                     ", " + xac + ")");
61
62     this.managedCx = managedCx;
63     this.xac = xac;
64     sessions = new Vector JavaDoc();
65   }
66
67   /**
68    * Forbidden call on an application or component's outbound connection,
69    * throws a <code>IllegalStateException</code> instance.
70    */

71   public void setClientID(String JavaDoc clientID) throws JMSException {
72     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
73       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
74                                     this + " setClientID(" + clientID + ")");
75     if (! valid)
76       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
77
78     throw new IllegalStateException JavaDoc("Forbidden call on a component's "
79                                     + "connection.");
80   }
81
82   /**
83    * Forbidden call on an application or component's outbound connection,
84    * throws a <code>IllegalStateException</code> instance.
85    */

86   public void setExceptionListener(ExceptionListener listener)
87     throws JMSException {
88     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
89       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
90                                     this + " setExceptionListener(" + listener + ")");
91
92     if (! valid)
93       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
94
95     throw new IllegalStateException JavaDoc("Forbidden call on a component's "
96                                     + "connection.");
97   }
98  
99   /**
100    * Returns the unique authorized JMS session per connection wrapped
101    * in an <code>OutboundSession</code> instance.
102    *
103    * @exception javax.jms.IllegalStateException If the handle is invalid.
104    * @exception javax.jms.JMSException Generic exception.
105    */

106   public Session createSession(boolean transacted, int acknowledgeMode)
107     throws JMSException {
108     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
109       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
110                                     this + " createSession(" + transacted +
111                                     ", " + acknowledgeMode + ")");
112
113     if (! valid)
114       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
115
116     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
117       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
118                                     this + " createSession sess = " + managedCx.session);
119
120     Session sess = managedCx.session;
121     if (sess == null)
122       sess = xac.createSession(false, acknowledgeMode);
123
124     return new OutboundSession(sess, this, transacted);
125   }
126
127   /**
128    * Forbidden call on an application or component's outbound connection,
129    * throws a <code>IllegalStateException</code> instance.
130    */

131   public String JavaDoc getClientID() throws JMSException
132   {
133     if (! valid)
134       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
135
136     throw new IllegalStateException JavaDoc("Forbidden call on a component's "
137                                     + "connection.");
138   }
139   
140   /**
141    * Delegates the call to the wrapped JMS connection.
142    *
143    * @exception javax.jms.IllegalStateException If the handle is invalid.
144    * @exception javax.jms.JMSException Generic exception.
145    */

146   public ConnectionMetaData getMetaData() throws JMSException
147   {
148     if (! valid)
149       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
150
151     return xac.getMetaData();
152   }
153
154   /**
155    * Forbidden call on an application or component's outbound connection,
156    * throws a <code>IllegalStateException</code> instance.
157    */

158   public ExceptionListener getExceptionListener() throws JMSException
159   {
160     if (! valid)
161       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
162
163     throw new IllegalStateException JavaDoc("Forbidden call on a component's "
164                                     + "connection.");
165   }
166   
167   /**
168    * Delegates the call to the wrapped JMS connection.
169    *
170    * @exception javax.jms.IllegalStateException If the handle is invalid.
171    * @exception javax.jms.JMSException Generic exception.
172    */

173   public void start() throws JMSException {
174     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
175       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " start()");
176
177     if (! valid)
178       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
179
180     xac.start();
181
182     for (int i = 0; i < sessions.size(); i++) {
183       OutboundSession session = (OutboundSession) sessions.get(i);
184       session.start();
185
186       if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
187         AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " start session = " + session);
188     }
189   }
190
191   /**
192    * Forbidden call on an application or component's outbound connection,
193    * throws a <code>IllegalStateException</code> instance.
194    */

195   public void stop() throws JMSException {
196
197     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
198       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " stop()");
199
200     if (! valid)
201       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
202
203     throw new IllegalStateException JavaDoc("Forbidden call on a component's "
204                                     + "connection.");
205   }
206
207   /**
208    * Forbidden call on an application or component's outbound connection,
209    * throws a <code>IllegalStateException</code> instance.
210    */

211   public ConnectionConsumer
212       createConnectionConsumer(Destination destination,
213                                String JavaDoc messageSelector,
214                                ServerSessionPool sessionPool,
215                                int maxMessages)
216     throws JMSException {
217     if (! valid)
218       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
219
220     throw new IllegalStateException JavaDoc("Forbidden call on a component's "
221                                     + "connection.");
222   }
223
224   /**
225    * Forbidden call on an application or component's outbound connection,
226    * throws a <code>IllegalStateException</code> instance.
227    */

228   public ConnectionConsumer
229          createDurableConnectionConsumer(Topic topic,
230                                          String JavaDoc subscriptionName,
231                                          String JavaDoc messageSelector,
232                                          ServerSessionPool sessionPool,
233                                          int maxMessages)
234          throws JMSException
235   {
236     if (! valid)
237       throw new javax.jms.IllegalStateException JavaDoc("Invalid connection handle.");
238
239     throw new IllegalStateException JavaDoc("Forbidden call on a component's "
240                                     + "connection.");
241   }
242
243   /**
244    * Requests to close the physical connection.
245    *
246    * @exception javax.jms.IllegalStateException If the handle is invalid.
247    * @exception javax.jms.JMSException Generic exception.
248    */

249   public synchronized void close() throws JMSException {
250     valid = false;
251
252     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
253       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " close()");
254
255     for (int i = 0; i < sessions.size(); i++) {
256       OutboundSession session = (OutboundSession) sessions.get(i);
257       if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
258         AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " close() session = " + session);
259
260       session.close();
261     }
262
263     managedCx.closeHandle(this);
264   }
265
266   /**
267    * returns <code>true</code> if the
268    * parameter is a <code>Connection</code> instance sharing the same
269    * proxy identifier and connection key.
270    */

271   public boolean cnxEquals(Object JavaDoc obj) {
272     return (obj instanceof Connection)
273            && xac.equals(obj);
274   }
275
276   /**
277    * close all session.
278    */

279   public void cleanup() {
280     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
281       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " cleanup()");
282     org.objectweb.joram.client.jms.Connection cnx =
283       (org.objectweb.joram.client.jms.Connection) xac;
284     cnx.cleanup();
285   }
286
287   public String JavaDoc toString()
288   {
289     return "OutboundConnection[" + xac.toString() + "]";
290   }
291 }
292
Popular Tags