KickJava   Java API By Example, From Geeks To Geeks.

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


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.Session JavaDoc;
28 import javax.jms.TopicSubscriber JavaDoc;
29
30 import org.objectweb.util.monolog.api.BasicLevel;
31
32 /**
33  * An <code>OutboundSession</code> instance wraps a JMS session (XA or not)
34  * for a component involved in outbound messaging.
35  */

36 public class OutboundSession implements javax.jms.Session JavaDoc
37 {
38   /** The <code>OutboundConnection</code> the session belongs to. */
39   protected OutboundConnection cnx;
40   /** The wrapped JMS session. */
41   Session JavaDoc sess;
42
43   /** <code>true</code> if this "handle" is valid. */
44   boolean valid = true;
45
46   /** <code>true</code> if the session is started. */
47   boolean started = false;
48
49   protected boolean transacted;
50
51   /**
52    * Constructs an <code>OutboundSession</code> instance.
53    */

54   OutboundSession(Session JavaDoc sess, OutboundConnection cnx) {
55     this.sess = sess;
56     this.cnx = cnx;
57     cnx.sessions.add(this);
58
59     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
60       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
61                                     "OutboundSession(" + sess +
62                                     ", " + cnx + ")" +
63                                     " cnx.sessions = " + cnx.sessions);
64   }
65  
66   /**
67    * Constructs an <code>OutboundSession</code> instance.
68    */

69   OutboundSession(Session JavaDoc sess,
70                   OutboundConnection cnx,
71                   boolean transacted) {
72     this.sess = sess;
73     this.cnx = cnx;
74     this.transacted = transacted;
75     cnx.sessions.add(this);
76
77     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
78       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
79                                     "OutboundSession(" + sess +
80                                     ", " + cnx + ")" +
81                                     " cnx.sessions = " + cnx.sessions);
82   }
83
84   /**
85    * Delegates the call to the wrapped JMS session.
86    */

87   public int getAcknowledgeMode() throws JMSException JavaDoc {
88     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
89       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
90                                     this + " getAcknowledgeMode() = " + sess.getAcknowledgeMode());
91     
92     checkValidity();
93     if (transacted)
94       return Session.SESSION_TRANSACTED;
95     return sess.getAcknowledgeMode();
96   }
97
98   /**
99    * Delegates the call to the wrapped JMS session.
100    */

101   public boolean getTransacted() throws JMSException JavaDoc {
102     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
103       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
104                                     this + " getTransacted() = " + sess.getTransacted());
105
106     checkValidity();
107     return sess.getTransacted();
108   }
109
110   /**
111    * Forbidden call on a component's outbound session, throws a
112    * <code>IllegalStateException</code> instance.
113    */

114   public void setMessageListener(javax.jms.MessageListener JavaDoc messageListener)
115               throws JMSException JavaDoc
116   {
117     checkValidity();
118     throw new IllegalStateException JavaDoc("Forbidden call on a component's session.");
119   }
120
121   /**
122    * Forbidden call on a component's outbound session, throws a
123    * <code>IllegalStateException</code> instance.
124    */

125   public javax.jms.MessageListener JavaDoc getMessageListener() throws JMSException JavaDoc
126   {
127     checkValidity();
128     throw new IllegalStateException JavaDoc("Forbidden call on a component's session.");
129   }
130
131   /**
132    * Delegates the call to the wrapped JMS session.
133    */

134   public javax.jms.Message JavaDoc createMessage() throws JMSException JavaDoc {
135     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
136       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
137                                     this + " createMessage()");
138
139     checkValidity();
140     return sess.createMessage();
141   }
142
143   /**
144    * Delegates the call to the wrapped JMS session.
145    */

146   public javax.jms.TextMessage JavaDoc createTextMessage() throws JMSException JavaDoc {
147     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
148       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
149                                     this + " createTextMessage()");
150
151     checkValidity();
152     return sess.createTextMessage();
153   }
154
155   /**
156    * Delegates the call to the wrapped JMS session.
157    */

158   public javax.jms.TextMessage JavaDoc createTextMessage(String JavaDoc text)
159     throws JMSException JavaDoc {
160     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
161       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
162                                     this + " createTextMessage(" + text + ")");
163
164     checkValidity();
165     return sess.createTextMessage(text);
166   }
167
168   /**
169    * Delegates the call to the wrapped JMS session.
170    */

171   public javax.jms.BytesMessage JavaDoc createBytesMessage() throws JMSException JavaDoc
172   {
173     checkValidity();
174     return sess.createBytesMessage();
175   }
176
177   /**
178    * Delegates the call to the wrapped JMS session.
179    */

180   public javax.jms.MapMessage JavaDoc createMapMessage() throws JMSException JavaDoc
181   {
182     checkValidity();
183     return sess.createMapMessage();
184   }
185
186   /**
187    * Delegates the call to the wrapped JMS session.
188    */

189   public javax.jms.ObjectMessage JavaDoc createObjectMessage() throws JMSException JavaDoc
190   {
191     checkValidity();
192     return sess.createObjectMessage();
193   }
194
195   /**
196    * Delegates the call to the wrapped JMS session.
197    */

198   public javax.jms.ObjectMessage JavaDoc createObjectMessage(java.io.Serializable JavaDoc obj)
199          throws JMSException JavaDoc
200   {
201     checkValidity();
202     return sess.createObjectMessage(obj);
203   }
204
205   /**
206    * Delegates the call to the wrapped JMS session.
207    */

208   public javax.jms.StreamMessage JavaDoc createStreamMessage()
209          throws JMSException JavaDoc
210   {
211     checkValidity();
212     return sess.createStreamMessage();
213   }
214
215   /**
216    * Delegates the call to the wrapped JMS session.
217    */

218   public javax.jms.QueueBrowser JavaDoc
219          createBrowser(javax.jms.Queue JavaDoc queue, String JavaDoc selector)
220          throws JMSException JavaDoc
221   {
222     checkValidity();
223     return sess.createBrowser(queue, selector);
224   }
225
226   /**
227    * Delegates the call to the wrapped JMS session.
228    */

229   public javax.jms.QueueBrowser JavaDoc createBrowser(javax.jms.Queue JavaDoc queue)
230          throws JMSException JavaDoc
231   {
232     checkValidity();
233     return sess.createBrowser(queue);
234   }
235
236   /**
237    * Delegates the call to the wrapped JMS session.
238    */

239   public javax.jms.MessageProducer JavaDoc createProducer(javax.jms.Destination JavaDoc dest)
240     throws JMSException JavaDoc {
241     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
242       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createProducer(" + dest + ")");
243     
244     checkValidity();
245     return new OutboundProducer(sess.createProducer(dest), this);
246   }
247
248   /**
249    * Delegates the call to the wrapped JMS session.
250    */

251   public javax.jms.MessageConsumer JavaDoc
252       createConsumer(javax.jms.Destination JavaDoc dest,
253                      String JavaDoc selector,
254                      boolean noLocal)
255     throws JMSException JavaDoc {
256     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
257       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConsumer(" + dest +
258                                     ", " + selector +
259                                     ", " + noLocal + ")");
260
261     checkValidity();
262     return new OutboundConsumer(sess.createConsumer(dest, selector, noLocal),
263                                 this);
264   }
265
266   /**
267    * Delegates the call to the wrapped JMS session.
268    */

269   public javax.jms.MessageConsumer JavaDoc
270          createConsumer(javax.jms.Destination JavaDoc dest, String JavaDoc selector)
271          throws JMSException JavaDoc {
272     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
273       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConsumer(" + dest +
274                                     ", " + selector + ")");
275
276     checkValidity();
277     return new OutboundConsumer(sess.createConsumer(dest, selector), this);
278   }
279
280   /**
281    * Delegates the call to the wrapped JMS session.
282    */

283   public javax.jms.MessageConsumer JavaDoc createConsumer(javax.jms.Destination JavaDoc dest)
284     throws JMSException JavaDoc {
285     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
286       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConsumer(" + dest + ")");
287
288     checkValidity();
289     return new OutboundConsumer(sess.createConsumer(dest), this);
290   }
291
292   /**
293    * Delegates the call to the wrapped JMS session.
294    */

295   public javax.jms.TopicSubscriber JavaDoc
296       createDurableSubscriber(javax.jms.Topic JavaDoc topic,
297                               String JavaDoc name,
298                               String JavaDoc selector,
299                               boolean noLocal)
300     throws JMSException JavaDoc {
301     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
302       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
303                                     this + " createDurableSubscriber(" + topic +
304                                     ", " + name +
305                                     ", " + selector +
306                                     ", " + noLocal + ")");
307
308     checkValidity();
309
310     TopicSubscriber JavaDoc sub =
311       sess.createDurableSubscriber(topic, name, selector, noLocal);
312
313     return new OutboundSubscriber(topic, noLocal, sub, this);
314   }
315
316   /**
317    * Delegates the call to the wrapped JMS session.
318    */

319   public javax.jms.TopicSubscriber JavaDoc
320       createDurableSubscriber(javax.jms.Topic JavaDoc topic, String JavaDoc name)
321     throws JMSException JavaDoc {
322     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
323       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
324                                     this + " createDurableSubscriber(" + topic +
325                                     ", " + name + ")");
326
327     checkValidity();
328
329     TopicSubscriber JavaDoc sub = sess.createDurableSubscriber(topic, name);
330     return new OutboundSubscriber(topic, false, sub, this);
331   }
332
333   /**
334    * Delegates the call to the wrapped JMS session.
335    */

336   public javax.jms.Queue JavaDoc createQueue(String JavaDoc queueName) throws JMSException JavaDoc {
337     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
338       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
339                                     this + " createQueue(" + queueName + ")");
340
341     checkValidity();
342     return sess.createQueue(queueName);
343   }
344
345   /**
346    * Delegates the call to the wrapped JMS session.
347    */

348   public javax.jms.Topic JavaDoc createTopic(String JavaDoc topicName) throws JMSException JavaDoc {
349     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
350       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
351                                     this + " createTopic(" + topicName + ")");
352
353     checkValidity();
354     return sess.createTopic(topicName);
355   }
356
357   /**
358    * Delegates the call to the wrapped JMS session.
359    */

360   public javax.jms.TemporaryQueue JavaDoc createTemporaryQueue() throws JMSException JavaDoc {
361     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
362       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
363                                     this + " createTemporaryQueue()");
364
365     checkValidity();
366     return sess.createTemporaryQueue();
367   }
368
369   /**
370    * Delegates the call to the wrapped JMS session.
371    */

372   public javax.jms.TemporaryTopic JavaDoc createTemporaryTopic() throws JMSException JavaDoc {
373     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
374       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
375                                     this + " createTemporaryTopic()");
376
377     checkValidity();
378     return sess.createTemporaryTopic();
379   }
380
381   /** Method never used by a component, does nothing. */
382   public void run()
383   {}
384
385   /**
386    * Forbidden call on a component's outbound session, throws a
387    * <code>IllegalStateException</code> instance.
388    */

389   public void commit() throws JMSException JavaDoc
390   {
391     checkValidity();
392     throw new IllegalStateException JavaDoc("Forbidden call on a component's session.");
393   }
394
395   /**
396    * Forbidden call on a component's outbound session, throws a
397    * <code>IllegalStateException</code> instance.
398    */

399   public void rollback() throws JMSException JavaDoc
400   {
401     checkValidity();
402     throw new IllegalStateException JavaDoc("Forbidden call on a component's session.");
403   }
404
405   /**
406    * Delegates the call to the wrapped JMS session.
407    */

408   public void recover() throws JMSException JavaDoc {
409     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
410       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
411                                     this + " recover()");
412
413     checkValidity();
414     sess.recover();
415   }
416
417
418   /**
419    * Delegates the call to the wrapped JMS session.
420    */

421   public void unsubscribe(String JavaDoc name) throws JMSException JavaDoc {
422     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
423       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
424                                     this + " unsubscribe(" + name + ")");
425
426     checkValidity();
427     sess.unsubscribe(name);
428   }
429
430   /**
431    * set started = true
432    */

433   void start() {
434    if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
435       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
436                                     this + " start() started = true");
437
438     started = true;
439   }
440
441   /**
442    * Actually does nothing, closing of the session occurs while closing
443    * the component's connection.
444    */

445   public void close() throws JMSException JavaDoc {
446     if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG))
447       AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG,
448                                     this + " close()");
449
450     valid = false;
451     cnx.sessions.remove(this);
452     started = false;
453   }
454
455   /**
456    * return started value.
457    */

458   public boolean isStarted() {
459     return started;
460   }
461
462   /** Checks the validity of the session. */
463   void checkValidity() throws IllegalStateException JavaDoc
464   {
465     boolean validity;
466
467     if (! valid)
468       validity = false;
469     else
470       validity = cnx.valid;
471
472    if (! validity)
473      throw new IllegalStateException JavaDoc("Invalid state: session is closed.");
474   }
475 }
476
Popular Tags