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 */
017package org.apache.activemq;
018
019import java.io.Serializable;
020
021import javax.jms.BytesMessage;
022import javax.jms.Destination;
023import javax.jms.IllegalStateException;
024import javax.jms.InvalidDestinationException;
025import javax.jms.JMSException;
026import javax.jms.MapMessage;
027import javax.jms.Message;
028import javax.jms.MessageConsumer;
029import javax.jms.MessageListener;
030import javax.jms.MessageProducer;
031import javax.jms.ObjectMessage;
032import javax.jms.Queue;
033import javax.jms.QueueBrowser;
034import javax.jms.QueueReceiver;
035import javax.jms.QueueSender;
036import javax.jms.QueueSession;
037import javax.jms.StreamMessage;
038import javax.jms.TemporaryQueue;
039import javax.jms.TemporaryTopic;
040import javax.jms.TextMessage;
041import javax.jms.Topic;
042import javax.jms.TopicSubscriber;
043
044/**
045 * A QueueSession implementation that throws IllegalStateExceptions when Topic
046 * operations are attempted but which delegates to another QueueSession for all
047 * other operations. The ActiveMQSessions implement both Topic and Queue
048 * Sessions methods but the specification states that Queue session should throw
049 * Exceptions if topic operations are attempted on it.
050 */
051public class ActiveMQQueueSession implements QueueSession {
052
053    private final QueueSession next;
054
055    public ActiveMQQueueSession(QueueSession next) {
056        this.next = next;
057    }
058
059    @Override
060    public void close() throws JMSException {
061        next.close();
062    }
063
064    @Override
065    public void commit() throws JMSException {
066        next.commit();
067    }
068
069    @Override
070    public QueueBrowser createBrowser(Queue queue) throws JMSException {
071        return next.createBrowser(queue);
072    }
073
074    @Override
075    public QueueBrowser createBrowser(Queue queue, String messageSelector) throws JMSException {
076        return next.createBrowser(queue, messageSelector);
077    }
078
079    @Override
080    public BytesMessage createBytesMessage() throws JMSException {
081        return next.createBytesMessage();
082    }
083
084    @Override
085    public MessageConsumer createConsumer(Destination destination) throws JMSException {
086        if (destination instanceof Topic) {
087            throw new InvalidDestinationException("Topics are not supported by a QueueSession");
088        }
089        return next.createConsumer(destination);
090    }
091
092    @Override
093    public MessageConsumer createConsumer(Destination destination, String messageSelector) throws JMSException {
094        if (destination instanceof Topic) {
095            throw new InvalidDestinationException("Topics are not supported by a QueueSession");
096        }
097        return next.createConsumer(destination, messageSelector);
098    }
099
100    @Override
101    public MessageConsumer createConsumer(Destination destination, String messageSelector, boolean noLocal) throws JMSException {
102        if (destination instanceof Topic) {
103            throw new InvalidDestinationException("Topics are not supported by a QueueSession");
104        }
105        return next.createConsumer(destination, messageSelector, noLocal);
106    }
107
108    @Override
109    public TopicSubscriber createDurableSubscriber(Topic topic, String name) throws JMSException {
110        throw new IllegalStateException("Operation not supported by a QueueSession");
111    }
112
113    @Override
114    public TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal) throws JMSException {
115        throw new IllegalStateException("Operation not supported by a QueueSession");
116    }
117
118    @Override
119    public MapMessage createMapMessage() throws JMSException {
120        return next.createMapMessage();
121    }
122
123    @Override
124    public Message createMessage() throws JMSException {
125        return next.createMessage();
126    }
127
128    @Override
129    public ObjectMessage createObjectMessage() throws JMSException {
130        return next.createObjectMessage();
131    }
132
133    @Override
134    public ObjectMessage createObjectMessage(Serializable object) throws JMSException {
135        return next.createObjectMessage(object);
136    }
137
138    @Override
139    public MessageProducer createProducer(Destination destination) throws JMSException {
140        if (destination instanceof Topic) {
141            throw new InvalidDestinationException("Topics are not supported by a QueueSession");
142        }
143        return next.createProducer(destination);
144    }
145
146    @Override
147    public Queue createQueue(String queueName) throws JMSException {
148        return next.createQueue(queueName);
149    }
150
151    @Override
152    public QueueReceiver createReceiver(Queue queue) throws JMSException {
153        return next.createReceiver(queue);
154    }
155
156    @Override
157    public QueueReceiver createReceiver(Queue queue, String messageSelector) throws JMSException {
158        return next.createReceiver(queue, messageSelector);
159    }
160
161    @Override
162    public QueueSender createSender(Queue queue) throws JMSException {
163        return next.createSender(queue);
164    }
165
166    @Override
167    public StreamMessage createStreamMessage() throws JMSException {
168        return next.createStreamMessage();
169    }
170
171    @Override
172    public TemporaryQueue createTemporaryQueue() throws JMSException {
173        return next.createTemporaryQueue();
174    }
175
176    @Override
177    public TemporaryTopic createTemporaryTopic() throws JMSException {
178        throw new IllegalStateException("Operation not supported by a QueueSession");
179    }
180
181    @Override
182    public TextMessage createTextMessage() throws JMSException {
183        return next.createTextMessage();
184    }
185
186    @Override
187    public TextMessage createTextMessage(String text) throws JMSException {
188        return next.createTextMessage(text);
189    }
190
191    @Override
192    public Topic createTopic(String topicName) throws JMSException {
193        throw new IllegalStateException("Operation not supported by a QueueSession");
194    }
195
196    @Override
197    public boolean equals(Object arg0) {
198        if(this != arg0) {
199            return next.equals(arg0);
200        }
201
202        return true;
203    }
204
205    @Override
206    public int getAcknowledgeMode() throws JMSException {
207        return next.getAcknowledgeMode();
208    }
209
210    @Override
211    public MessageListener getMessageListener() throws JMSException {
212        return next.getMessageListener();
213    }
214
215    @Override
216    public boolean getTransacted() throws JMSException {
217        return next.getTransacted();
218    }
219
220    @Override
221    public int hashCode() {
222        return next.hashCode();
223    }
224
225    @Override
226    public void recover() throws JMSException {
227        next.recover();
228    }
229
230    @Override
231    public void rollback() throws JMSException {
232        next.rollback();
233    }
234
235    @Override
236    public void run() {
237        next.run();
238    }
239
240    @Override
241    public void setMessageListener(MessageListener listener) throws JMSException {
242        next.setMessageListener(listener);
243    }
244
245    @Override
246    public String toString() {
247        return next.toString();
248    }
249
250    @Override
251    public void unsubscribe(String name) throws JMSException {
252        throw new IllegalStateException("Operation not supported by a QueueSession");
253    }
254
255    public QueueSession getNext() {
256        return next;
257    }
258}