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