Source code: org/activemq/ra/jms/SessionProxy.java
1 /**
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activemq.ra.jms;
19
20 import org.activemq.ra.SessionAndProducer;
21 import org.activemq.ra.SessionAndProducerHelper;
22
23 import javax.jms.*;
24 import java.io.Serializable;
25
26 /**
27 * A {@link Session} implementation which can be used with the ActiveMQ JCA
28 * Resource Adapter to publish messages using the same JMS session that is used to dispatch
29 * messages.
30 *
31 * @version $Revision: 1.1.1.1 $
32 */
33 public class SessionProxy implements Session, QueueSession, TopicSession {
34
35 private SessionAndProducer sessionAndProducer;
36
37 public Session getSession() throws JMSException {
38 return getSessionAndProducer().getSession();
39 }
40
41 public QueueSession getQueueSession() throws JMSException {
42 Session session = getSession();
43 if (session instanceof QueueSession) {
44 return (QueueSession) session;
45 }
46 else {
47 throw new JMSException("The underlying JMS Session does not support QueueSession semantics: " + session);
48 }
49 }
50
51 public TopicSession getTopicSession() throws JMSException {
52 Session session = getSession();
53 if (session instanceof TopicSession) {
54 return (TopicSession) session;
55 }
56 else {
57 throw new JMSException("The underlying JMS Session does not support TopicSession semantics: " + session);
58 }
59 }
60
61 public SessionAndProducer getSessionAndProducer() throws JMSException {
62 if( sessionAndProducer==null ) {
63 sessionAndProducer = SessionAndProducerHelper.getActiveSessionAndProducer();
64 if (sessionAndProducer == null) {
65 throw new JMSException("No currently active Session. This JMS provider cannot be used outside a MessageListener.onMessage() invocation");
66 }
67 }
68 return sessionAndProducer;
69 }
70
71 public MessageProducer createProducer(Destination destination) throws JMSException {
72 return new MessageProducerProxy(getSessionAndProducer().getMessageProducer(), destination);
73 }
74
75 public void close() throws JMSException {
76 // we don't allow users to close this session
77 // as its used by the JCA container
78 }
79
80 public void commit() throws JMSException {
81 // the JCA container will handle transactions
82 }
83
84 public void rollback() throws JMSException {
85 // the JCA container will handle transactions
86 }
87
88 public void recover() throws JMSException {
89 // the JCA container will handle recovery
90 }
91
92 public void run() {
93 try {
94 getSession().run();
95 }
96 catch (JMSException e) {
97 throw new RuntimeException("Failed to run() on session due to: " + e, e);
98 }
99 }
100
101 // Straightforward delegation methods
102 //-------------------------------------------------------------------------
103
104 public QueueBrowser createBrowser(Queue queue) throws JMSException {
105 return getSession().createBrowser(queue);
106 }
107
108 public QueueBrowser createBrowser(Queue queue, String s) throws JMSException {
109 return getSession().createBrowser(queue, s);
110 }
111
112 public BytesMessage createBytesMessage() throws JMSException {
113 return getSession().createBytesMessage();
114 }
115
116 public MessageConsumer createConsumer(Destination destination) throws JMSException {
117 return getSession().createConsumer(destination);
118 }
119
120 public MessageConsumer createConsumer(Destination destination, String s) throws JMSException {
121 return getSession().createConsumer(destination, s);
122 }
123
124 public MessageConsumer createConsumer(Destination destination, String s, boolean b) throws JMSException {
125 return getSession().createConsumer(destination, s, b);
126 }
127
128 public TopicSubscriber createDurableSubscriber(Topic topic, String s) throws JMSException {
129 return getSession().createDurableSubscriber(topic, s);
130 }
131
132 public TopicSubscriber createDurableSubscriber(Topic topic, String s, String s1, boolean b) throws JMSException {
133 return getSession().createDurableSubscriber(topic, s, s1, b);
134 }
135
136 public MapMessage createMapMessage() throws JMSException {
137 return getSession().createMapMessage();
138 }
139
140 public Message createMessage() throws JMSException {
141 return getSession().createMessage();
142 }
143
144 public ObjectMessage createObjectMessage() throws JMSException {
145 return getSession().createObjectMessage();
146 }
147
148 public ObjectMessage createObjectMessage(Serializable serializable) throws JMSException {
149 return getSession().createObjectMessage(serializable);
150 }
151
152 public Queue createQueue(String s) throws JMSException {
153 return getSession().createQueue(s);
154 }
155
156 public StreamMessage createStreamMessage() throws JMSException {
157 return getSession().createStreamMessage();
158 }
159
160 public TemporaryQueue createTemporaryQueue() throws JMSException {
161 return getSession().createTemporaryQueue();
162 }
163
164 public TemporaryTopic createTemporaryTopic() throws JMSException {
165 return getSession().createTemporaryTopic();
166 }
167
168 public TextMessage createTextMessage() throws JMSException {
169 return getSession().createTextMessage();
170 }
171
172 public TextMessage createTextMessage(String s) throws JMSException {
173 return getSession().createTextMessage(s);
174 }
175
176 public Topic createTopic(String s) throws JMSException {
177 return getSession().createTopic(s);
178 }
179
180 public int getAcknowledgeMode() throws JMSException {
181 return getSession().getAcknowledgeMode();
182 }
183
184 public MessageListener getMessageListener() throws JMSException {
185 return getSession().getMessageListener();
186 }
187
188 public boolean getTransacted() throws JMSException {
189 return getSession().getTransacted();
190 }
191
192 public void setMessageListener(MessageListener messageListener) throws JMSException {
193 getSession().setMessageListener(messageListener);
194 }
195
196 public void unsubscribe(String s) throws JMSException {
197 getSession().unsubscribe(s);
198 }
199
200 public QueueReceiver createReceiver(Queue queue) throws JMSException {
201 return getQueueSession().createReceiver(queue);
202 }
203
204 public QueueReceiver createReceiver(Queue queue, String s) throws JMSException {
205 return getQueueSession().createReceiver(queue, s);
206 }
207
208 public QueueSender createSender(Queue queue) throws JMSException {
209 return new MessageProducerProxy(getSessionAndProducer().getMessageProducer(), queue);
210 }
211
212 public TopicSubscriber createSubscriber(Topic topic) throws JMSException {
213 return getTopicSession().createSubscriber(topic);
214 }
215
216 public TopicSubscriber createSubscriber(Topic topic, String s, boolean b) throws JMSException {
217 return getTopicSession().createSubscriber(topic, s, b);
218 }
219
220 public TopicPublisher createPublisher(Topic topic) throws JMSException {
221 return getTopicSession().createPublisher(topic);
222 }
223 }