KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > InboundSessionProxy


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.ra;
19
20
21 import javax.jms.*;
22
23 import java.io.Serializable JavaDoc;
24
25 /**
26  * A {@link Session} implementation which can be used with the ActiveMQ JCA
27  * Resource Adapter to publish messages using the same JMS session that is used to dispatch
28  * messages.
29  *
30  * @version $Revision$
31  */

32 public class InboundSessionProxy implements Session, QueueSession, TopicSession {
33
34     private InboundContext sessionAndProducer;
35
36     public Session getSession() throws JMSException {
37         return getSessionAndProducer().getSession();
38     }
39
40     public QueueSession getQueueSession() throws JMSException {
41         Session session = getSession();
42         if (session instanceof QueueSession) {
43             return (QueueSession) session;
44         }
45         else {
46             throw new JMSException("The underlying JMS Session does not support QueueSession semantics: " + session);
47         }
48     }
49
50     public TopicSession getTopicSession() throws JMSException {
51         Session session = getSession();
52         if (session instanceof TopicSession) {
53             return (TopicSession) session;
54         }
55         else {
56             throw new JMSException("The underlying JMS Session does not support TopicSession semantics: " + session);
57         }
58     }
59
60     public InboundContext getSessionAndProducer() throws JMSException {
61         if( sessionAndProducer==null ) {
62             sessionAndProducer = InboundContextSupport.getActiveSessionAndProducer();
63             if (sessionAndProducer == null) {
64                 throw new JMSException("No currently active Session. This JMS provider cannot be used outside a MessageListener.onMessage() invocation");
65             }
66         }
67         return sessionAndProducer;
68     }
69
70     public MessageProducer createProducer(Destination destination) throws JMSException {
71         return new InboundMessageProducerProxy(getSessionAndProducer().getMessageProducer(), destination);
72     }
73
74     public void close() throws JMSException {
75         // we don't allow users to close this session
76
// as its used by the JCA container
77
}
78
79     public void commit() throws JMSException {
80         // the JCA container will handle transactions
81
}
82
83     public void rollback() throws JMSException {
84         // the JCA container will handle transactions
85
}
86
87     public void recover() throws JMSException {
88         // the JCA container will handle recovery
89
}
90
91     public void run() {
92         try {
93             getSession().run();
94         }
95         catch (JMSException e) {
96             throw new RuntimeException JavaDoc("Failed to run() on session due to: " + e, e);
97         }
98     }
99
100     // Straightforward delegation methods
101
//-------------------------------------------------------------------------
102

103     public QueueBrowser createBrowser(Queue queue) throws JMSException {
104         return getSession().createBrowser(queue);
105     }
106
107     public QueueBrowser createBrowser(Queue queue, String JavaDoc s) throws JMSException {
108         return getSession().createBrowser(queue, s);
109     }
110
111     public BytesMessage createBytesMessage() throws JMSException {
112         return getSession().createBytesMessage();
113     }
114
115     public MessageConsumer createConsumer(Destination destination) throws JMSException {
116         return getSession().createConsumer(destination);
117     }
118
119     public MessageConsumer createConsumer(Destination destination, String JavaDoc s) throws JMSException {
120         return getSession().createConsumer(destination, s);
121     }
122
123     public MessageConsumer createConsumer(Destination destination, String JavaDoc s, boolean b) throws JMSException {
124         return getSession().createConsumer(destination, s, b);
125     }
126
127     public TopicSubscriber createDurableSubscriber(Topic topic, String JavaDoc s) throws JMSException {
128         return getSession().createDurableSubscriber(topic, s);
129     }
130
131     public TopicSubscriber createDurableSubscriber(Topic topic, String JavaDoc s, String JavaDoc s1, boolean b) throws JMSException {
132         return getSession().createDurableSubscriber(topic, s, s1, b);
133     }
134
135     public MapMessage createMapMessage() throws JMSException {
136         return getSession().createMapMessage();
137     }
138
139     public Message createMessage() throws JMSException {
140         return getSession().createMessage();
141     }
142
143     public ObjectMessage createObjectMessage() throws JMSException {
144         return getSession().createObjectMessage();
145     }
146
147     public ObjectMessage createObjectMessage(Serializable JavaDoc serializable) throws JMSException {
148         return getSession().createObjectMessage(serializable);
149     }
150
151     public Queue createQueue(String JavaDoc s) throws JMSException {
152         return getSession().createQueue(s);
153     }
154
155     public StreamMessage createStreamMessage() throws JMSException {
156         return getSession().createStreamMessage();
157     }
158
159     public TemporaryQueue createTemporaryQueue() throws JMSException {
160         return getSession().createTemporaryQueue();
161     }
162
163     public TemporaryTopic createTemporaryTopic() throws JMSException {
164         return getSession().createTemporaryTopic();
165     }
166
167     public TextMessage createTextMessage() throws JMSException {
168         return getSession().createTextMessage();
169     }
170
171     public TextMessage createTextMessage(String JavaDoc s) throws JMSException {
172         return getSession().createTextMessage(s);
173     }
174
175     public Topic createTopic(String JavaDoc s) throws JMSException {
176         return getSession().createTopic(s);
177     }
178
179     public int getAcknowledgeMode() throws JMSException {
180         return getSession().getAcknowledgeMode();
181     }
182
183     public MessageListener getMessageListener() throws JMSException {
184         return getSession().getMessageListener();
185     }
186
187     public boolean getTransacted() throws JMSException {
188         return getSession().getTransacted();
189     }
190
191     public void setMessageListener(MessageListener messageListener) throws JMSException {
192         getSession().setMessageListener(messageListener);
193     }
194
195     public void unsubscribe(String JavaDoc s) throws JMSException {
196         getSession().unsubscribe(s);
197     }
198
199     public QueueReceiver createReceiver(Queue queue) throws JMSException {
200         return getQueueSession().createReceiver(queue);
201     }
202
203     public QueueReceiver createReceiver(Queue queue, String JavaDoc s) throws JMSException {
204         return getQueueSession().createReceiver(queue, s);
205     }
206
207     public QueueSender createSender(Queue queue) throws JMSException {
208         return new InboundMessageProducerProxy(getSessionAndProducer().getMessageProducer(), queue);
209     }
210
211     public TopicSubscriber createSubscriber(Topic topic) throws JMSException {
212         return getTopicSession().createSubscriber(topic);
213     }
214
215     public TopicSubscriber createSubscriber(Topic topic, String JavaDoc s, boolean b) throws JMSException {
216         return getTopicSession().createSubscriber(topic, s, b);
217     }
218
219     public TopicPublisher createPublisher(Topic topic) throws JMSException {
220         return getTopicSession().createPublisher(topic);
221     }
222     
223     public String JavaDoc toString() {
224         try {
225             return "InboundSessionProxy { "+getSession()+" }";
226         } catch (JMSException e) {
227             return "InboundSessionProxy { null }";
228         }
229     }
230
231 }
232
Popular Tags