001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.client;
022
023 import javax.jms.JMSException;
024 import javax.jms.Message;
025 import javax.jms.MessageConsumer;
026 import javax.jms.MessageListener;
027 import javax.jms.Queue;
028 import javax.jms.QueueReceiver;
029
030 /**
031 * Class that wraps a MessageConsumer for backwards JMS compatibility
032 * Returned by methods in AMQSession etc
033 */
034 public class QueueReceiverAdaptor implements QueueReceiver {
035
036 protected MessageConsumer _consumer;
037 protected Queue _queue;
038
039 protected QueueReceiverAdaptor(Queue queue, MessageConsumer consumer)
040 {
041 _consumer = consumer;
042 _queue = queue;
043 }
044
045 public String getMessageSelector() throws JMSException
046 {
047 checkPreConditions();
048 return _consumer.getMessageSelector();
049 }
050
051 public MessageListener getMessageListener() throws JMSException
052 {
053 checkPreConditions();
054 return _consumer.getMessageListener();
055 }
056
057 public void setMessageListener(MessageListener messageListener) throws JMSException
058 {
059 checkPreConditions();
060 _consumer.setMessageListener(messageListener);
061 }
062
063 public Message receive() throws JMSException
064 {
065 checkPreConditions();
066 return _consumer.receive();
067 }
068
069 public Message receive(long l) throws JMSException
070 {
071 checkPreConditions();
072 return _consumer.receive(l);
073 }
074
075 public Message receiveNoWait() throws JMSException
076 {
077 checkPreConditions();
078 return _consumer.receiveNoWait();
079 }
080
081 public void close() throws JMSException
082 {
083 _consumer.close();
084 }
085
086 /**
087 * Return the queue associated with this receiver
088 * @return
089 * @throws JMSException
090 */
091 public Queue getQueue() throws JMSException
092 {
093 checkPreConditions();
094 return _queue;
095 }
096
097 private void checkPreConditions() throws javax.jms.IllegalStateException {
098 BasicMessageConsumer msgConsumer = (BasicMessageConsumer)_consumer;
099
100 if (msgConsumer.isClosed() ){
101 throw new javax.jms.IllegalStateException("Consumer is closed");
102 }
103
104 if(_queue == null){
105 throw new UnsupportedOperationException("Queue is null");
106 }
107
108 AMQSession session = msgConsumer.getSession();
109
110 if(session == null || session.isClosed()){
111 throw new javax.jms.IllegalStateException("Invalid Session");
112 }
113 }
114
115 }
|