001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
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.jms;
019    
020    import org.activemq.ra.SessionAndProducer;
021    import org.activemq.ra.SessionAndProducerHelper;
022    
023    import javax.jms.*;
024    import java.io.Serializable;
025    
026    /**
027     * A {@link Session} implementation which can be used with the ActiveMQ JCA
028     * Resource Adapter to publish messages using the same JMS session that is used to dispatch
029     * messages.
030     *
031     * @version $Revision: 1.1.1.1 $
032     */
033    public class SessionProxy implements Session, QueueSession, TopicSession {
034    
035        private SessionAndProducer sessionAndProducer;
036    
037        public Session getSession() throws JMSException {
038            return getSessionAndProducer().getSession();
039        }
040    
041        public QueueSession getQueueSession() throws JMSException {
042            Session session = getSession();
043            if (session instanceof QueueSession) {
044                return (QueueSession) session;
045            }
046            else {
047                throw new JMSException("The underlying JMS Session does not support QueueSession semantics: " + session);
048            }
049        }
050    
051        public TopicSession getTopicSession() throws JMSException {
052            Session session = getSession();
053            if (session instanceof TopicSession) {
054                return (TopicSession) session;
055            }
056            else {
057                throw new JMSException("The underlying JMS Session does not support TopicSession semantics: " + session);
058            }
059        }
060    
061        public SessionAndProducer getSessionAndProducer() throws JMSException {
062            if( sessionAndProducer==null ) {
063                sessionAndProducer = SessionAndProducerHelper.getActiveSessionAndProducer();
064                if (sessionAndProducer == null) {
065                    throw new JMSException("No currently active Session. This JMS provider cannot be used outside a MessageListener.onMessage() invocation");
066                }
067            }
068            return sessionAndProducer;
069        }
070    
071        public MessageProducer createProducer(Destination destination) throws JMSException {
072            return new MessageProducerProxy(getSessionAndProducer().getMessageProducer(), destination);
073        }
074    
075        public void close() throws JMSException {
076            // we don't allow users to close this session
077            // as its used by the JCA container
078        }
079    
080        public void commit() throws JMSException {
081            // the JCA container will handle transactions
082        }
083    
084        public void rollback() throws JMSException {
085            // the JCA container will handle transactions
086        }
087    
088        public void recover() throws JMSException {
089            // the JCA container will handle recovery
090        }
091    
092        public void run() {
093            try {
094                getSession().run();
095            }
096            catch (JMSException e) {
097                throw new RuntimeException("Failed to run() on session due to: " + e, e);
098            }
099        }
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    }