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: * --------------------------------------------------------------------------
023: * $Id: JTopicSession.java 4408 2004-03-19 14:31:54Z sauthieg $
024: * --------------------------------------------------------------------------
025: */
026:
027: package org.objectweb.jonas_jms;
028:
029: import javax.jms.InvalidDestinationException;
030: import javax.jms.JMSException;
031: import javax.jms.Session;
032: import javax.jms.TemporaryTopic;
033: import javax.jms.Topic;
034: import javax.jms.TopicPublisher;
035: import javax.jms.TopicSession;
036: import javax.jms.TopicSubscriber;
037: import javax.jms.XATopicConnection;
038: import javax.jms.XATopicSession;
039: import javax.transaction.RollbackException;
040: import javax.transaction.SystemException;
041: import javax.transaction.Transaction;
042:
043: import org.objectweb.util.monolog.api.BasicLevel;
044:
045: /**
046: * @author Laurent Chauvirey, Frederic Maistre, Nicolas Tachker
047: * Contributor(s):
048: * Philippe Durieux
049: * Philippe Coq
050: */
051:
052: public class JTopicSession extends JSession implements TopicSession {
053:
054: // Underlaying Objects
055: protected XATopicConnection xatc;
056: protected TopicSession ts = null;
057: protected XATopicSession xats = null;
058:
059: /**
060: * Constructor
061: */
062: public JTopicSession(JConnection jconn, XATopicConnection xatc) {
063: super (jconn);
064: this .xatc = xatc;
065: }
066:
067: // -----------------------------------------------------------------------
068: // Internal Methods
069: // -----------------------------------------------------------------------
070:
071: /**
072: * Get the underlaying MOM Session.
073: */
074: protected Session getMOMSession() throws JMSException {
075: return getMOMTopicSession();
076: }
077:
078: protected TopicSession getMOMTopicSession() throws JMSException {
079: Transaction tx = null;
080: try {
081: tx = tm.getTransaction();
082: } catch (SystemException e) {
083: TraceJms.logger.log(BasicLevel.ERROR,
084: "cannot get Transaction");
085: }
086: if (tx == null) {
087: if (ts == null) {
088: ts = xatc.createTopicSession(false,
089: Session.AUTO_ACKNOWLEDGE);
090: jconn.sessionOpen(this );
091: }
092: return ts;
093: } else {
094: if (xats == null) {
095: xats = xatc.createXATopicSession();
096: if (currtx != null) {
097: TraceJms.logger.log(BasicLevel.ERROR,
098: "mixed transactions");
099: }
100: currtx = tx;
101: xares = xats.getXAResource();
102: try {
103: tx.enlistResource(this .getXAResource());
104: txover = false;
105: } catch (SystemException e) {
106: TraceJms.logger.log(BasicLevel.ERROR,
107: "cannot enlist session:" + e);
108: throw new JMSException(e.toString());
109: } catch (RollbackException e) {
110: TraceJms.logger.log(BasicLevel.DEBUG,
111: "transaction rolled back");
112: throw new JMSException(e.toString());
113: }
114: }
115: return xats.getTopicSession();
116: }
117: }
118:
119: protected void MOMSessionClose() {
120: try {
121: if (xats != null) {
122: xats.close();
123: xats = null;
124: }
125: if (ts != null) {
126: ts.close();
127: ts = null;
128: jconn.sessionClose(this );
129: }
130: } catch (JMSException e) {
131: TraceJms.logger.log(BasicLevel.ERROR, "exception:" + e);
132: }
133: }
134:
135: // -----------------------------------------------------------------------
136: // TopicSession Implementation
137: // -----------------------------------------------------------------------
138:
139: /**
140: *
141: */
142: public Topic createTopic(String topicName) throws JMSException {
143: TraceJms.logger.log(BasicLevel.DEBUG, "");
144: return getMOMTopicSession().createTopic(topicName);
145: }
146:
147: /**
148: *
149: */
150: public TopicSubscriber createSubscriber(Topic topic)
151: throws JMSException {
152: TraceJms.logger.log(BasicLevel.DEBUG, "");
153: return getMOMTopicSession().createSubscriber(topic);
154: }
155:
156: /**
157: *
158: */
159: public TopicSubscriber createSubscriber(Topic topic,
160: String messageSelector, boolean noLocal)
161: throws JMSException {
162: TraceJms.logger.log(BasicLevel.DEBUG, "");
163: return getMOMTopicSession().createSubscriber(topic,
164: messageSelector, noLocal);
165: }
166:
167: /**
168: *
169: */
170: public TopicSubscriber createDurableSubscriber(Topic topic,
171: String name) throws JMSException {
172: TraceJms.logger.log(BasicLevel.DEBUG, "");
173: return getMOMTopicSession()
174: .createDurableSubscriber(topic, name);
175: }
176:
177: /**
178: *
179: */
180: public TopicSubscriber createDurableSubscriber(Topic topic,
181: String name, String messageSelector, boolean noLocal)
182: throws JMSException {
183: TraceJms.logger.log(BasicLevel.DEBUG, "");
184: return getMOMTopicSession().createDurableSubscriber(topic,
185: name, messageSelector, noLocal);
186: }
187:
188: /**
189: *
190: */
191: public TopicPublisher createPublisher(Topic topic)
192: throws JMSException {
193: TraceJms.logger.log(BasicLevel.DEBUG, "");
194: return getMOMTopicSession().createPublisher(topic);
195: }
196:
197: /**
198: *
199: */
200: public TemporaryTopic createTemporaryTopic() throws JMSException {
201: TraceJms.logger.log(BasicLevel.DEBUG, "");
202: return getMOMTopicSession().createTemporaryTopic();
203: }
204:
205: /**
206: *
207: */
208: public void unsubscribe(java.lang.String name) throws JMSException,
209: InvalidDestinationException {
210: TraceJms.logger.log(BasicLevel.DEBUG, "");
211: getMOMTopicSession().unsubscribe(name);
212: }
213: }
|