001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.activemq.ra;
018
019 import java.io.Serializable;
020
021 import javax.jms.BytesMessage;
022 import javax.jms.Destination;
023 import javax.jms.IllegalStateException;
024 import javax.jms.JMSException;
025 import javax.jms.MapMessage;
026 import javax.jms.Message;
027 import javax.jms.MessageConsumer;
028 import javax.jms.MessageListener;
029 import javax.jms.MessageProducer;
030 import javax.jms.ObjectMessage;
031 import javax.jms.Queue;
032 import javax.jms.QueueBrowser;
033 import javax.jms.QueueReceiver;
034 import javax.jms.QueueSender;
035 import javax.jms.QueueSession;
036 import javax.jms.Session;
037 import javax.jms.StreamMessage;
038 import javax.jms.TemporaryQueue;
039 import javax.jms.TemporaryTopic;
040 import javax.jms.TextMessage;
041 import javax.jms.Topic;
042 import javax.jms.TopicPublisher;
043 import javax.jms.TopicSession;
044 import javax.jms.TopicSubscriber;
045
046 import org.apache.activemq.ActiveMQSession;
047
048 /**
049 * Acts as a pass through proxy for a JMS Session object. It intercepts events
050 * that are of interest of the ActiveMQManagedConnection. There is one proxy for each session.
051 *
052 * @version $Revision$
053 */
054 public class ManagedSessionProxy implements Session, QueueSession, TopicSession {
055
056 private final ActiveMQSession session;
057 private boolean closed;
058 private ManagedConnectionProxy connectionProxy;
059
060 public ManagedSessionProxy(ActiveMQSession session, ManagedConnectionProxy connectionProxy) {
061 this.session = session;
062 this.connectionProxy = connectionProxy;
063 }
064
065 public void setUseSharedTxContext(boolean enable) throws JMSException {
066 if (session.getTransactionContext() != null) {
067 ((ManagedTransactionContext)session.getTransactionContext()).setUseSharedTxContext(enable);
068 }
069 }
070
071 /**
072 * @throws JMSException
073 */
074 public void close() throws JMSException {
075 if (closed) {
076 return;
077 }
078 cleanup();
079 connectionProxy.sessionClosed(this);
080 }
081
082 /**
083 * Called by the ManagedConnectionProxy to invalidate this proxy.
084 *
085 * @throws JMSException if session proxy has a problem
086 */
087 public void cleanup() throws JMSException {
088 closed = true;
089 session.close();
090 }
091
092 /**
093 *
094 * @return underlying session, unless this proxy is closed
095 * @throws javax.jms.JMSException if session is closed
096 */
097 private Session getSession() throws JMSException {
098 if (closed) {
099 throw new IllegalStateException("The Session is closed");
100 }
101 return session;
102 }
103
104 /**
105 * @throws JMSException
106 */
107 public void commit() throws JMSException {
108 getSession().commit();
109 }
110
111 /**
112 * @param queue
113 * @return
114 * @throws JMSException
115 */
116 public QueueBrowser createBrowser(Queue queue) throws JMSException {
117 return getSession().createBrowser(queue);
118 }
119
120 /**
121 * @param queue
122 * @param messageSelector
123 * @return
124 * @throws JMSException
125 */
126 public QueueBrowser createBrowser(Queue queue, String messageSelector) throws JMSException {
127 return getSession().createBrowser(queue, messageSelector);
128 }
129
130 /**
131 * @return
132 * @throws JMSException
133 */
134 public BytesMessage createBytesMessage() throws JMSException {
135 return getSession().createBytesMessage();
136 }
137
138 /**
139 * @param destination
140 * @return
141 * @throws JMSException
142 */
143 public MessageConsumer createConsumer(Destination destination) throws JMSException {
144 return getSession().createConsumer(destination);
145 }
146
147 /**
148 * @param destination
149 * @param messageSelector
150 * @return
151 * @throws JMSException
152 */
153 public MessageConsumer createConsumer(Destination destination, String messageSelector) throws JMSException {
154 return getSession().createConsumer(destination, messageSelector);
155 }
156
157 /**
158 * @param destination
159 * @param messageSelector
160 * @param noLocal
161 * @return
162 * @throws JMSException
163 */
164 public MessageConsumer createConsumer(Destination destination, String messageSelector, boolean noLocal) throws JMSException {
165 return getSession().createConsumer(destination, messageSelector, noLocal);
166 }
167
168 /**
169 * @param topic
170 * @param name
171 * @return
172 * @throws JMSException
173 */
174 public TopicSubscriber createDurableSubscriber(Topic topic, String name) throws JMSException {
175 return getSession().createDurableSubscriber(topic, name);
176 }
177
178 /**
179 * @param topic
180 * @param name
181 * @param messageSelector
182 * @param noLocal
183 * @return
184 * @throws JMSException
185 */
186 public TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal) throws JMSException {
187 return getSession().createDurableSubscriber(topic, name, messageSelector, noLocal);
188 }
189
190 /**
191 * @return
192 * @throws JMSException
193 */
194 public MapMessage createMapMessage() throws JMSException {
195 return getSession().createMapMessage();
196 }
197
198 /**
199 * @return
200 * @throws JMSException
201 */
202 public Message createMessage() throws JMSException {
203 return getSession().createMessage();
204 }
205
206 /**
207 * @return
208 * @throws JMSException
209 */
210 public ObjectMessage createObjectMessage() throws JMSException {
211 return getSession().createObjectMessage();
212 }
213
214 /**
215 * @param object
216 * @return
217 * @throws JMSException
218 */
219 public ObjectMessage createObjectMessage(Serializable object) throws JMSException {
220 return getSession().createObjectMessage(object);
221 }
222
223 /**
224 * @param destination
225 * @return
226 * @throws JMSException
227 */
228 public MessageProducer createProducer(Destination destination) throws JMSException {
229 return getSession().createProducer(destination);
230 }
231
232 /**
233 * @param queueName
234 * @return
235 * @throws JMSException
236 */
237 public Queue createQueue(String queueName) throws JMSException {
238 return getSession().createQueue(queueName);
239 }
240
241 /**
242 * @return
243 * @throws JMSException
244 */
245 public StreamMessage createStreamMessage() throws JMSException {
246 return getSession().createStreamMessage();
247 }
248
249 /**
250 * @return
251 * @throws JMSException
252 */
253 public TemporaryQueue createTemporaryQueue() throws JMSException {
254 return getSession().createTemporaryQueue();
255 }
256
257 /**
258 * @return
259 * @throws JMSException
260 */
261 public TemporaryTopic createTemporaryTopic() throws JMSException {
262 return getSession().createTemporaryTopic();
263 }
264
265 /**
266 * @return
267 * @throws JMSException
268 */
269 public TextMessage createTextMessage() throws JMSException {
270 return getSession().createTextMessage();
271 }
272
273 /**
274 * @param text
275 * @return
276 * @throws JMSException
277 */
278 public TextMessage createTextMessage(String text) throws JMSException {
279 return getSession().createTextMessage(text);
280 }
281
282 /**
283 * @param topicName
284 * @return
285 * @throws JMSException
286 */
287 public Topic createTopic(String topicName) throws JMSException {
288 return getSession().createTopic(topicName);
289 }
290
291 /**
292 * @return
293 * @throws JMSException
294 */
295 public int getAcknowledgeMode() throws JMSException {
296 return getSession().getAcknowledgeMode();
297 }
298
299 /**
300 * @return
301 * @throws JMSException
302 */
303 public MessageListener getMessageListener() throws JMSException {
304 return getSession().getMessageListener();
305 }
306
307 /**
308 * @return
309 * @throws JMSException
310 */
311 public boolean getTransacted() throws JMSException {
312 return getSession().getTransacted();
313 }
314
315 /**
316 * @throws JMSException
317 */
318 public void recover() throws JMSException {
319 getSession().recover();
320 }
321
322 /**
323 * @throws JMSException
324 */
325 public void rollback() throws JMSException {
326 getSession().rollback();
327 }
328
329 /**
330 * @param listener
331 * @throws JMSException
332 */
333 public void setMessageListener(MessageListener listener) throws JMSException {
334 getSession().setMessageListener(listener);
335 }
336
337 /**
338 * @param name
339 * @throws JMSException
340 */
341 public void unsubscribe(String name) throws JMSException {
342 getSession().unsubscribe(name);
343 }
344
345 /**
346 * @param queue
347 * @return
348 * @throws JMSException
349 */
350 public QueueReceiver createReceiver(Queue queue) throws JMSException {
351 return ((QueueSession)getSession()).createReceiver(queue);
352 }
353
354 /**
355 * @param queue
356 * @param messageSelector
357 * @return
358 * @throws JMSException
359 */
360 public QueueReceiver createReceiver(Queue queue, String messageSelector) throws JMSException {
361 return ((QueueSession)getSession()).createReceiver(queue, messageSelector);
362 }
363
364 /**
365 * @param queue
366 * @return
367 * @throws JMSException
368 */
369 public QueueSender createSender(Queue queue) throws JMSException {
370 return ((QueueSession)getSession()).createSender(queue);
371 }
372
373 /**
374 * @param topic
375 * @return
376 * @throws JMSException
377 */
378 public TopicPublisher createPublisher(Topic topic) throws JMSException {
379 return ((TopicSession)getSession()).createPublisher(topic);
380 }
381
382 /**
383 * @param topic
384 * @return
385 * @throws JMSException
386 */
387 public TopicSubscriber createSubscriber(Topic topic) throws JMSException {
388 return ((TopicSession)getSession()).createSubscriber(topic);
389 }
390
391 /**
392 * @param topic
393 * @param messageSelector
394 * @param noLocal
395 * @return
396 * @throws JMSException
397 */
398 public TopicSubscriber createSubscriber(Topic topic, String messageSelector, boolean noLocal) throws JMSException {
399 return ((TopicSession)getSession()).createSubscriber(topic, messageSelector, noLocal);
400 }
401
402 /**
403 * @see javax.jms.Session#run()
404 */
405 public void run() {
406 throw new RuntimeException("Operation not supported.");
407 }
408
409 public String toString() {
410 return "ManagedSessionProxy { " + session + " }";
411 }
412
413 }