001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JSession.java 4663 2004-04-28 15:31:21Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas_jms;
027:
028: import java.io.Serializable;
029:
030: import javax.jms.BytesMessage;
031: import javax.jms.Destination;
032: import javax.jms.JMSException;
033: import javax.jms.MapMessage;
034: import javax.jms.Message;
035: import javax.jms.MessageConsumer;
036: import javax.jms.MessageListener;
037: import javax.jms.MessageProducer;
038: import javax.jms.ObjectMessage;
039: import javax.jms.Queue;
040: import javax.jms.QueueBrowser;
041: import javax.jms.Session;
042: import javax.jms.StreamMessage;
043: import javax.jms.TemporaryQueue;
044: import javax.jms.TemporaryTopic;
045: import javax.jms.TextMessage;
046: import javax.jms.Topic;
047: import javax.jms.TopicSubscriber;
048: import javax.jms.XAConnection;
049: import javax.jms.XASession;
050: import javax.transaction.RollbackException;
051: import javax.transaction.Synchronization;
052: import javax.transaction.SystemException;
053: import javax.transaction.Transaction;
054: import javax.transaction.xa.XAResource;
055:
056: import org.objectweb.transaction.jta.TransactionManager;
057: import org.objectweb.util.monolog.api.BasicLevel;
058:
059: /**
060: * JSession
061: * @author Laurent Chauvirey, Frederic Maistre, Nicolas Tachker
062: * Contributor(s):
063: * Philippe Durieux
064: * Philippe Coq
065: */
066:
067: public class JSession implements Session, Synchronization {
068:
069: protected XAResource xares = null; // The underlaying XAResource
070: protected boolean txover = true;
071: protected Transaction currtx = null;
072: protected boolean closed = false;
073: protected JConnection jconn;
074: protected static TransactionManager tm = null;
075:
076: protected XAConnection xac;
077: protected Session sess = null;
078: protected XASession xasess = null;
079:
080: /**
081: * Prepares the construction of a JSession.
082: * @param jconn
083: */
084: protected JSession(JConnection jconn) {
085: this .jconn = jconn;
086: if (tm == null) {
087: tm = JmsManagerImpl.getTransactionManager();
088: }
089: }
090:
091: /**
092: * Constructor
093: */
094: public JSession(JConnection jconn, XAConnection xac) {
095: this (jconn);
096: this .xac = xac;
097: TraceJms.logger.log(BasicLevel.DEBUG, "");
098: }
099:
100: // -----------------------------------------------------------------------
101: // Internal Methods
102: // -----------------------------------------------------------------------
103:
104: /**
105: * Get the underlaying XAResource.
106: * @return - XAResource
107: */
108: protected XAResource getXAResource() {
109: return xares;
110: }
111:
112: /**
113: * Get the underlaying MOM Session.
114: * @return - session
115: */
116: protected Session getMOMSession() throws JMSException {
117: Transaction tx = null;
118: try {
119: tx = tm.getTransaction();
120: } catch (SystemException e) {
121: TraceJms.logger.log(BasicLevel.ERROR,
122: "cannot get Transaction");
123: }
124: if (tx == null) {
125: if (sess == null) {
126: sess = xac.createSession(false,
127: Session.AUTO_ACKNOWLEDGE);
128: jconn.sessionOpen(this );
129: }
130: return sess;
131: } else {
132: if (xasess == null) {
133: xasess = xac.createXASession();
134: if (currtx != null) {
135: TraceJms.logger.log(BasicLevel.ERROR,
136: "mixed transactions");
137: }
138: currtx = tx;
139: xares = xasess.getXAResource();
140: try {
141: tx.enlistResource(xares);
142: txover = false;
143: } catch (SystemException e) {
144: TraceJms.logger.log(BasicLevel.ERROR,
145: "cannot enlist session:" + e);
146: throw new JMSException(e.toString());
147: } catch (RollbackException e) {
148: TraceJms.logger.log(BasicLevel.ERROR,
149: "transaction rolled back");
150: throw new JMSException(e.toString());
151: }
152: }
153: return xasess.getSession();
154: }
155: }
156:
157: protected void MOMSessionClose() {
158: try {
159: if (xasess != null) {
160: xasess.close();
161: xasess = null;
162: }
163: if (sess != null) {
164: sess.close();
165: sess = null;
166: jconn.sessionClose(this );
167: }
168: } catch (JMSException e) {
169: TraceJms.logger.log(BasicLevel.ERROR, "exception:" + e);
170: }
171: }
172:
173: /**
174: *
175: */
176: protected void PhysicalClose() {
177: MOMSessionClose();
178: }
179:
180: // -----------------------------------------------------------------------
181: // Session Implementation
182: // -----------------------------------------------------------------------
183:
184: /**
185: *
186: */
187: public void close() throws JMSException {
188: TraceJms.logger.log(BasicLevel.DEBUG, "");
189: closed = true;
190: if (txover) {
191: PhysicalClose();
192: } else {
193: // delist XAResource now, that will lead to an XA-end call
194: if (currtx == null) {
195: TraceJms.logger.log(BasicLevel.ERROR,
196: "should be in a tx");
197: } else {
198: try {
199: currtx.delistResource(xares, XAResource.TMSUCCESS);
200: } catch (SystemException e) {
201: TraceJms.logger.log(BasicLevel.ERROR,
202: "cannot delist session:" + e);
203: throw new JMSException(e.toString());
204: }
205: }
206: }
207: }
208:
209: /**
210: *
211: */
212: public void commit() throws JMSException {
213: TraceJms.logger.log(BasicLevel.DEBUG, "");
214: throw new JMSException("JSession: commit Operation Not Allowed");
215: }
216:
217: /**
218: *
219: */
220: public QueueBrowser createBrowser(Queue queue) throws JMSException {
221: TraceJms.logger.log(BasicLevel.DEBUG, "");
222: return getMOMSession().createBrowser(queue);
223: }
224:
225: /**
226: *
227: */
228: public QueueBrowser createBrowser(Queue queue,
229: java.lang.String messageSelector) throws JMSException {
230: TraceJms.logger.log(BasicLevel.DEBUG, "");
231: return getMOMSession().createBrowser(queue, messageSelector);
232: }
233:
234: /**
235: *
236: */
237: public BytesMessage createBytesMessage() throws JMSException {
238: TraceJms.logger.log(BasicLevel.DEBUG, "");
239: return getMOMSession().createBytesMessage();
240: }
241:
242: /**
243: *
244: */
245: public MessageConsumer createConsumer(Destination destination)
246: throws JMSException {
247: TraceJms.logger.log(BasicLevel.DEBUG, "");
248: return getMOMSession().createConsumer(destination);
249: }
250:
251: /**
252: *
253: */
254: public MessageConsumer createConsumer(Destination destination,
255: String messageSelector) throws JMSException {
256: TraceJms.logger.log(BasicLevel.DEBUG, "");
257: return getMOMSession().createConsumer(destination,
258: messageSelector);
259: }
260:
261: /**
262: *
263: */
264: public MessageConsumer createConsumer(Destination destination,
265: String messageSelector, boolean NoLocal)
266: throws JMSException {
267: TraceJms.logger.log(BasicLevel.DEBUG, "");
268: return getMOMSession().createConsumer(destination,
269: messageSelector, NoLocal);
270: }
271:
272: /**
273: *
274: */
275: public TopicSubscriber createDurableSubscriber(Topic topic,
276: String name) throws JMSException {
277: TraceJms.logger.log(BasicLevel.DEBUG, "");
278: return getMOMSession().createDurableSubscriber(topic, name);
279: }
280:
281: /**
282: *
283: */
284: public TopicSubscriber createDurableSubscriber(Topic topic,
285: String name, String messageSelector, boolean noLocal)
286: throws JMSException {
287: TraceJms.logger.log(BasicLevel.DEBUG, "");
288: return getMOMSession().createDurableSubscriber(topic, name,
289: messageSelector, noLocal);
290: }
291:
292: /**
293: *
294: */
295: public MapMessage createMapMessage() throws JMSException {
296: TraceJms.logger.log(BasicLevel.DEBUG, "");
297: return getMOMSession().createMapMessage();
298: }
299:
300: /**
301: *
302: */
303: public Message createMessage() throws JMSException {
304: TraceJms.logger.log(BasicLevel.DEBUG, "");
305: return getMOMSession().createMessage();
306: }
307:
308: /**
309: *
310: */
311: public ObjectMessage createObjectMessage() throws JMSException {
312: TraceJms.logger.log(BasicLevel.DEBUG, "");
313: return getMOMSession().createObjectMessage();
314: }
315:
316: /**
317: *
318: */
319: public ObjectMessage createObjectMessage(Serializable object)
320: throws JMSException {
321: TraceJms.logger.log(BasicLevel.DEBUG, "");
322: return getMOMSession().createObjectMessage(object);
323: }
324:
325: /**
326: *
327: */
328: public MessageProducer createProducer(Destination destination)
329: throws JMSException {
330: TraceJms.logger.log(BasicLevel.DEBUG, "");
331: return getMOMSession().createProducer(destination);
332: }
333:
334: /**
335: *
336: */
337:
338: public Queue createQueue(String queueName) throws JMSException {
339: TraceJms.logger.log(BasicLevel.DEBUG, "");
340: return getMOMSession().createQueue(queueName);
341: }
342:
343: /**
344: *
345: */
346: public StreamMessage createStreamMessage() throws JMSException {
347: TraceJms.logger.log(BasicLevel.DEBUG, "");
348: return getMOMSession().createStreamMessage();
349: }
350:
351: /**
352: *
353: */
354: public TemporaryQueue createTemporaryQueue() throws JMSException {
355: TraceJms.logger.log(BasicLevel.DEBUG, "");
356: return getMOMSession().createTemporaryQueue();
357: }
358:
359: /**
360: *
361: */
362: public TemporaryTopic createTemporaryTopic() throws JMSException {
363: TraceJms.logger.log(BasicLevel.DEBUG, "");
364: return getMOMSession().createTemporaryTopic();
365: }
366:
367: /**
368: *
369: */
370: public TextMessage createTextMessage() throws JMSException {
371: TraceJms.logger.log(BasicLevel.DEBUG, "");
372: return getMOMSession().createTextMessage();
373: }
374:
375: /**
376: *
377: */
378: public TextMessage createTextMessage(String text)
379: throws JMSException {
380: TraceJms.logger.log(BasicLevel.DEBUG, "");
381: return getMOMSession().createTextMessage(text);
382: }
383:
384: /**
385: *
386: */
387: public Topic createTopic(String topicName) throws JMSException {
388: TraceJms.logger.log(BasicLevel.DEBUG, "");
389: return getMOMSession().createTopic(topicName);
390: }
391:
392: /**
393: *
394: */
395: public MessageListener getMessageListener() throws JMSException {
396: TraceJms.logger.log(BasicLevel.DEBUG, "");
397: return getMOMSession().getMessageListener();
398: }
399:
400: /**
401: *
402: */
403: public boolean getTransacted() throws JMSException {
404: TraceJms.logger.log(BasicLevel.DEBUG, "");
405: return getMOMSession().getTransacted();
406: }
407:
408: /**
409: *
410: */
411: public int getAcknowledgeMode() throws JMSException {
412: TraceJms.logger.log(BasicLevel.DEBUG, "");
413: return getMOMSession().getAcknowledgeMode();
414: }
415:
416: /**
417: *
418: */
419: public void recover() throws JMSException {
420: TraceJms.logger.log(BasicLevel.DEBUG, "");
421: throw new JMSException(
422: "JSession: recover Operation Not Allowed");
423: }
424:
425: /**
426: *
427: */
428: public void rollback() throws JMSException {
429: TraceJms.logger.log(BasicLevel.DEBUG, "");
430: throw new JMSException(
431: "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 e) {
442: TraceJms.logger.log(BasicLevel.ERROR, "exception: " + e);
443: }
444: }
445:
446: /**
447: *
448: */
449: public void setMessageListener(MessageListener listener)
450: throws JMSException {
451: TraceJms.logger.log(BasicLevel.DEBUG, "");
452: getMOMSession().setMessageListener(listener);
453: }
454:
455: /**
456: *
457: */
458: public void unsubscribe(String name) throws JMSException {
459: TraceJms.logger.log(BasicLevel.DEBUG, "");
460: getMOMSession().unsubscribe(name);
461: }
462:
463: // -----------------------------------------------------------------------
464: // Synchronization Implementation
465: // -----------------------------------------------------------------------
466:
467: /**
468: * called by the transaction manager prior to the start
469: * of the transaction completion process
470: */
471: public void beforeCompletion() {
472: if (TraceJms.isDebug()) {
473: TraceJms.logger.log(BasicLevel.DEBUG, "");
474: }
475: }
476:
477: /**
478: * called by the transaction manager after the transaction
479: * is committed or rolled back.
480: */
481:
482: public void afterCompletion(int status) {
483: if (TraceJms.isDebug()) {
484: TraceJms.logger.log(BasicLevel.DEBUG, "");
485: }
486: txover = true;
487: if (closed) {
488: PhysicalClose();
489: }
490: }
491:
492: }
|