KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_jms > JSession


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
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  * --------------------------------------------------------------------------
22  * $Id: JSession.java,v 1.10 2004/04/28 15:31:20 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_jms;
27
28 import java.io.Serializable JavaDoc;
29
30 import javax.jms.BytesMessage JavaDoc;
31 import javax.jms.Destination JavaDoc;
32 import javax.jms.JMSException JavaDoc;
33 import javax.jms.MapMessage JavaDoc;
34 import javax.jms.Message JavaDoc;
35 import javax.jms.MessageConsumer JavaDoc;
36 import javax.jms.MessageListener JavaDoc;
37 import javax.jms.MessageProducer JavaDoc;
38 import javax.jms.ObjectMessage JavaDoc;
39 import javax.jms.Queue JavaDoc;
40 import javax.jms.QueueBrowser JavaDoc;
41 import javax.jms.Session JavaDoc;
42 import javax.jms.StreamMessage JavaDoc;
43 import javax.jms.TemporaryQueue JavaDoc;
44 import javax.jms.TemporaryTopic JavaDoc;
45 import javax.jms.TextMessage JavaDoc;
46 import javax.jms.Topic JavaDoc;
47 import javax.jms.TopicSubscriber JavaDoc;
48 import javax.jms.XAConnection JavaDoc;
49 import javax.jms.XASession JavaDoc;
50 import javax.transaction.RollbackException JavaDoc;
51 import javax.transaction.Synchronization JavaDoc;
52 import javax.transaction.SystemException JavaDoc;
53 import javax.transaction.Transaction JavaDoc;
54 import javax.transaction.xa.XAResource JavaDoc;
55
56 import org.objectweb.transaction.jta.TransactionManager;
57 import org.objectweb.util.monolog.api.BasicLevel;
58
59
60 /**
61  * JSession
62  * @author Laurent Chauvirey, Frederic Maistre, Nicolas Tachker
63  * Contributor(s):
64  * Philippe Durieux
65  * Philippe Coq
66  */

67
68 public class JSession implements Session JavaDoc, Synchronization JavaDoc {
69
70     protected XAResource JavaDoc xares = null; // The underlaying XAResource
71
protected boolean txover = true;
72     protected Transaction JavaDoc currtx = null;
73     protected boolean closed = false;
74     protected JConnection jconn;
75     protected static TransactionManager tm = null;
76
77     protected XAConnection JavaDoc xac;
78     protected Session JavaDoc sess = null;
79     protected XASession JavaDoc xasess = null;
80
81     /**
82      * Prepares the construction of a JSession.
83      * @param jconn
84      */

85     protected JSession(JConnection jconn) {
86         this.jconn = jconn;
87         if (tm == null) {
88             tm = JmsManagerImpl.getTransactionManager();
89         }
90     }
91
92     /**
93      * Constructor
94      */

95     public JSession(JConnection jconn, XAConnection JavaDoc xac) {
96         this(jconn);
97         this.xac = xac;
98         TraceJms.logger.log(BasicLevel.DEBUG,"");
99     }
100
101     // -----------------------------------------------------------------------
102
// Internal Methods
103
// -----------------------------------------------------------------------
104

105     /**
106      * Get the underlaying XAResource.
107      * @return - XAResource
108      */

109     protected XAResource JavaDoc getXAResource() {
110         return xares;
111     }
112
113     /**
114      * Get the underlaying MOM Session.
115      * @return - session
116      */

117     protected Session JavaDoc getMOMSession() throws JMSException JavaDoc {
118         Transaction JavaDoc tx = null;
119         try {
120             tx = tm.getTransaction();
121         } catch (SystemException JavaDoc e) {
122             TraceJms.logger.log(BasicLevel.ERROR,"cannot get Transaction");
123         }
124         if (tx == null) {
125             if (sess == null) {
126                 sess = xac.createSession(false, Session.AUTO_ACKNOWLEDGE);
127                 jconn.sessionOpen(this);
128             }
129             return sess;
130         } else {
131             if (xasess == null) {
132                 xasess = xac.createXASession();
133                 if (currtx != null) {
134                     TraceJms.logger.log(BasicLevel.ERROR,"mixed transactions");
135                 }
136                 currtx = tx;
137                 xares = xasess.getXAResource();
138                 try {
139                     tx.enlistResource(xares);
140                     txover = false;
141                 } catch (SystemException JavaDoc e) {
142                     TraceJms.logger.log(BasicLevel.ERROR,"cannot enlist session:"+e);
143                     throw new JMSException JavaDoc(e.toString());
144                 } catch (RollbackException JavaDoc e) {
145                     TraceJms.logger.log(BasicLevel.ERROR,"transaction rolled back");
146                     throw new JMSException JavaDoc(e.toString());
147                 }
148             }
149             return xasess.getSession();
150         }
151     }
152
153     protected void MOMSessionClose() {
154         try {
155             if (xasess != null) {
156                 xasess.close();
157                 xasess = null;
158             }
159             if (sess != null) {
160                 sess.close();
161                 sess = null;
162                 jconn.sessionClose(this);
163             }
164         } catch (JMSException JavaDoc e) {
165             TraceJms.logger.log(BasicLevel.ERROR,"exception:"+e);
166         }
167     }
168
169     /**
170      *
171      */

172     protected void PhysicalClose() {
173         MOMSessionClose();
174     }
175
176     // -----------------------------------------------------------------------
177
// Session Implementation
178
// -----------------------------------------------------------------------
179

180     /**
181      *
182      */

183     public void close() throws JMSException JavaDoc {
184         TraceJms.logger.log(BasicLevel.DEBUG, "");
185         closed = true;
186         if (txover) {
187             PhysicalClose();
188         } else {
189             // delist XAResource now, that will lead to an XA-end call
190
if (currtx == null) {
191                 TraceJms.logger.log(BasicLevel.ERROR, "should be in a tx");
192             } else {
193                 try {
194                     currtx.delistResource(xares, XAResource.TMSUCCESS);
195                 } catch (SystemException JavaDoc e) {
196                     TraceJms.logger.log(BasicLevel.ERROR, "cannot delist session:"+e);
197                     throw new JMSException JavaDoc(e.toString());
198                 }
199             }
200         }
201     }
202
203     /**
204      *
205      */

206     public void commit() throws JMSException JavaDoc {
207         TraceJms.logger.log(BasicLevel.DEBUG, "");
208         throw new JMSException JavaDoc("JSession: commit Operation Not Allowed");
209     }
210
211     /**
212      *
213      */

214     public QueueBrowser JavaDoc createBrowser(Queue JavaDoc queue)
215         throws JMSException JavaDoc {
216         TraceJms.logger.log(BasicLevel.DEBUG, "");
217         return getMOMSession().createBrowser(queue);
218     }
219
220     /**
221      *
222      */

223     public QueueBrowser JavaDoc createBrowser(Queue JavaDoc queue, java.lang.String JavaDoc messageSelector)
224         throws JMSException JavaDoc {
225         TraceJms.logger.log(BasicLevel.DEBUG, "");
226         return getMOMSession().createBrowser(queue, messageSelector);
227     }
228
229     /**
230      *
231      */

232     public BytesMessage JavaDoc createBytesMessage() throws JMSException JavaDoc {
233         TraceJms.logger.log(BasicLevel.DEBUG, "");
234         return getMOMSession().createBytesMessage();
235     }
236
237
238     /**
239      *
240      */

241     public MessageConsumer JavaDoc createConsumer(Destination JavaDoc destination)
242         throws JMSException JavaDoc {
243         TraceJms.logger.log(BasicLevel.DEBUG, "");
244         return getMOMSession().createConsumer(destination);
245     }
246
247
248     /**
249      *
250      */

251     public MessageConsumer JavaDoc createConsumer(Destination JavaDoc destination,
252                                           String JavaDoc messageSelector)
253         throws JMSException JavaDoc {
254         TraceJms.logger.log(BasicLevel.DEBUG, "");
255         return getMOMSession().createConsumer(destination, messageSelector);
256     }
257
258     /**
259      *
260      */

261     public MessageConsumer JavaDoc createConsumer(Destination JavaDoc destination,
262                                           String JavaDoc messageSelector,
263                                           boolean NoLocal)
264         throws JMSException JavaDoc {
265         TraceJms.logger.log(BasicLevel.DEBUG, "");
266         return getMOMSession().createConsumer(destination, messageSelector,NoLocal );
267     }
268
269     /**
270      *
271      */

272     public TopicSubscriber JavaDoc createDurableSubscriber(Topic JavaDoc topic,
273                                                    String JavaDoc name)
274         throws JMSException JavaDoc {
275         TraceJms.logger.log(BasicLevel.DEBUG, "");
276         return getMOMSession().createDurableSubscriber(topic, name);
277     }
278
279     /**
280      *
281      */

282     public TopicSubscriber JavaDoc createDurableSubscriber(Topic JavaDoc topic,
283                                                    String JavaDoc name,
284                                                    String JavaDoc messageSelector,
285                                                    boolean noLocal)
286         throws JMSException JavaDoc {
287         TraceJms.logger.log(BasicLevel.DEBUG, "");
288         return getMOMSession().createDurableSubscriber(topic, name, messageSelector, noLocal);
289     }
290
291     /**
292      *
293      */

294     public MapMessage JavaDoc createMapMessage() throws JMSException JavaDoc {
295         TraceJms.logger.log(BasicLevel.DEBUG, "");
296         return getMOMSession().createMapMessage();
297     }
298
299     /**
300      *
301      */

302     public Message JavaDoc createMessage() throws JMSException JavaDoc {
303         TraceJms.logger.log(BasicLevel.DEBUG, "");
304         return getMOMSession().createMessage();
305     }
306
307     /**
308      *
309      */

310     public ObjectMessage JavaDoc createObjectMessage() throws JMSException JavaDoc {
311         TraceJms.logger.log(BasicLevel.DEBUG, "");
312         return getMOMSession().createObjectMessage();
313     }
314
315     /**
316      *
317      */

318     public ObjectMessage JavaDoc createObjectMessage(Serializable JavaDoc object) throws JMSException JavaDoc {
319         TraceJms.logger.log(BasicLevel.DEBUG, "");
320         return getMOMSession().createObjectMessage(object);
321     }
322
323     /**
324      *
325      */

326     public MessageProducer JavaDoc createProducer(Destination JavaDoc destination)
327         throws JMSException JavaDoc {
328         TraceJms.logger.log(BasicLevel.DEBUG, "");
329         return getMOMSession().createProducer(destination);
330     }
331
332     /**
333      *
334      */

335
336     public Queue JavaDoc createQueue(String JavaDoc queueName)
337         throws JMSException JavaDoc {
338         TraceJms.logger.log(BasicLevel.DEBUG, "");
339         return getMOMSession().createQueue(queueName);
340     }
341
342     /**
343      *
344      */

345     public StreamMessage JavaDoc createStreamMessage() throws JMSException JavaDoc {
346         TraceJms.logger.log(BasicLevel.DEBUG, "");
347         return getMOMSession().createStreamMessage();
348     }
349
350     /**
351      *
352      */

353     public TemporaryQueue JavaDoc createTemporaryQueue()
354         throws JMSException JavaDoc {
355         TraceJms.logger.log(BasicLevel.DEBUG, "");
356         return getMOMSession().createTemporaryQueue();
357     }
358
359     /**
360      *
361      */

362     public TemporaryTopic JavaDoc createTemporaryTopic()
363         throws JMSException JavaDoc {
364         TraceJms.logger.log(BasicLevel.DEBUG, "");
365         return getMOMSession().createTemporaryTopic();
366     }
367
368     /**
369      *
370      */

371     public TextMessage JavaDoc createTextMessage() throws JMSException JavaDoc {
372         TraceJms.logger.log(BasicLevel.DEBUG, "");
373         return getMOMSession().createTextMessage();
374     }
375
376     /**
377      *
378      */

379     public TextMessage JavaDoc createTextMessage(String JavaDoc text) throws JMSException JavaDoc {
380         TraceJms.logger.log(BasicLevel.DEBUG, "");
381         return getMOMSession().createTextMessage(text);
382     }
383
384     /**
385      *
386      */

387     public Topic JavaDoc createTopic(String JavaDoc topicName)
388         throws JMSException JavaDoc{
389         TraceJms.logger.log(BasicLevel.DEBUG, "");
390         return getMOMSession().createTopic(topicName);
391     }
392
393     /**
394      *
395      */

396     public MessageListener JavaDoc getMessageListener() throws JMSException JavaDoc {
397         TraceJms.logger.log(BasicLevel.DEBUG, "");
398         return getMOMSession().getMessageListener();
399     }
400     
401     /**
402      *
403      */

404     public boolean getTransacted() throws JMSException JavaDoc {
405         TraceJms.logger.log(BasicLevel.DEBUG, "");
406         return getMOMSession().getTransacted();
407     }
408
409     /**
410      *
411      */

412     public int getAcknowledgeMode() throws JMSException JavaDoc {
413         TraceJms.logger.log(BasicLevel.DEBUG, "");
414         return getMOMSession().getAcknowledgeMode();
415     }
416
417
418     /**
419      *
420      */

421     public void recover() throws JMSException JavaDoc {
422         TraceJms.logger.log(BasicLevel.DEBUG, "");
423         throw new JMSException JavaDoc("JSession: recover Operation Not Allowed");
424     }
425
426     /**
427      *
428      */

429     public void rollback() throws JMSException JavaDoc {
430         TraceJms.logger.log(BasicLevel.DEBUG, "");
431         throw new JMSException JavaDoc("JSession: rollback Operation Not Allowed");
432     }
433
434     /**
435      *
436      */

437     public void run() {
438         TraceJms.logger.log(BasicLevel.DEBUG, "");
439         try {
440             getMOMSession().run();
441         } catch (JMSException JavaDoc e) {
442             TraceJms.logger.log(BasicLevel.ERROR, "exception: "+e);
443         }
444     }
445
446     /**
447      *
448      */

449     public void setMessageListener(MessageListener JavaDoc listener) throws JMSException JavaDoc {
450         TraceJms.logger.log(BasicLevel.DEBUG, "");
451         getMOMSession().setMessageListener(listener);
452     }
453
454
455     /**
456      *
457      */

458     public void unsubscribe(String JavaDoc name)
459         throws JMSException JavaDoc {
460         TraceJms.logger.log(BasicLevel.DEBUG, "");
461         getMOMSession().unsubscribe(name);
462     }
463
464
465     // -----------------------------------------------------------------------
466
// Synchronization Implementation
467
// -----------------------------------------------------------------------
468

469     /**
470      * called by the transaction manager prior to the start
471      * of the transaction completion process
472      */

473     public void beforeCompletion() {
474         if(TraceJms.isDebug()){
475             TraceJms.logger.log(BasicLevel.DEBUG, "");
476         }
477     }
478
479     /**
480      * called by the transaction manager after the transaction
481      * is committed or rolled back.
482      */

483
484     public void afterCompletion(int status) {
485         if(TraceJms.isDebug()){
486             TraceJms.logger.log(BasicLevel.DEBUG, "");
487         }
488         txover = true;
489         if (closed) {
490             PhysicalClose();
491         }
492     }
493
494
495 }
496
497
Popular Tags