001 package com.mockrunner.mock.jms;
002
003 import javax.jms.JMSException;
004 import javax.jms.Message;
005 import javax.jms.Topic;
006 import javax.jms.TopicSubscriber;
007
008 /**
009 * Mock implementation of JMS <code>TopicSubscriber</code>.
010 */
011 public class MockTopicSubscriber extends MockMessageConsumer implements TopicSubscriber
012 {
013 private MockSession session;
014 private MockTopic topic;
015 private boolean noLocal;
016 private String name;
017 private boolean isDurable;
018
019 public MockTopicSubscriber(MockConnection connection, MockSession session, MockTopic topic)
020 {
021 this(connection, session, topic, null, false);
022 }
023
024 public MockTopicSubscriber(MockConnection connection, MockSession session, MockTopic topic, String messageSelector, boolean noLocal)
025 {
026 super(connection, messageSelector);
027 this.session = session;
028 this.topic = topic;
029 this.noLocal = noLocal;
030 name = null;
031 isDurable = false;
032 }
033
034 /**
035 * Returns if this subscriber is durable.
036 * @return <code>true</code> if this subscriber is durable
037 */
038 public boolean isDurable()
039 {
040 return isDurable;
041 }
042
043 /**
044 * Set if this subscriber is durable. This is automatically
045 * done when creating the subscriber.
046 * @param isDurable is this a durable subscriber?
047 */
048 public void setDurable(boolean isDurable)
049 {
050 this.isDurable = isDurable;
051 }
052
053 /**
054 * Returns the name of this subscriber. Usually only durable
055 * subscribers have a name. If no name is specified, this
056 * method returns <code>null</code>.
057 * @return the name of this subscriber
058 */
059 public String getName()
060 {
061 return name;
062 }
063
064 /**
065 * Set the name of this subscriber.
066 * @param name the name of this subscriber
067 */
068 public void setName(String name)
069 {
070 this.name = name;
071 }
072
073 public Topic getTopic() throws JMSException
074 {
075 getConnection().throwJMSException();
076 return topic;
077 }
078
079 public boolean getNoLocal() throws JMSException
080 {
081 getConnection().throwJMSException();
082 return noLocal;
083 }
084
085 public Message receive() throws JMSException
086 {
087 getConnection().throwJMSException();
088 if(isClosed())
089 {
090 throw new JMSException("Subscriber is closed");
091 }
092 if(topic.isEmpty()) return null;
093 Message message;
094 if((!getConnection().getConfigurationManager().getUseMessageSelectors()) || (null == getMessageFilter()))
095 {
096 message = topic.getMessage();
097 }
098 else
099 {
100 message = topic.getMatchingMessage(getMessageFilter());
101 }
102 if(null == message) return null;
103 if(session.isAutoAcknowledge()) message.acknowledge();
104 return message;
105 }
106 }