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
012: TopicSubscriber {
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,
020: MockSession session, MockTopic topic) {
021: this (connection, session, topic, null, false);
022: }
023:
024: public MockTopicSubscriber(MockConnection connection,
025: MockSession session, MockTopic topic,
026: String messageSelector, boolean noLocal) {
027: super (connection, messageSelector);
028: this .session = session;
029: this .topic = topic;
030: this .noLocal = noLocal;
031: name = null;
032: isDurable = false;
033: }
034:
035: /**
036: * Returns if this subscriber is durable.
037: * @return <code>true</code> if this subscriber is durable
038: */
039: public boolean isDurable() {
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: this .isDurable = isDurable;
050: }
051:
052: /**
053: * Returns the name of the subscription if the subscription
054: * is durable. Otherwise, this method returns <code>null</code>.
055: * @return the name of this subscriber
056: */
057: public String getName() {
058: return name;
059: }
060:
061: /**
062: * Set the name of the subscription.
063: * @param name the name of this subscriber
064: */
065: public void setName(String name) {
066: this .name = name;
067: }
068:
069: public Topic getTopic() throws JMSException {
070: getConnection().throwJMSException();
071: return topic;
072: }
073:
074: public boolean getNoLocal() throws JMSException {
075: getConnection().throwJMSException();
076: return noLocal;
077: }
078:
079: public Message receive() throws JMSException {
080: getConnection().throwJMSException();
081: if (isClosed()) {
082: throw new JMSException("Subscriber is closed");
083: }
084: if (topic.isEmpty())
085: return null;
086: Message message;
087: if ((!getConnection().getConfigurationManager()
088: .getUseMessageSelectors())
089: || (null == getMessageFilter())) {
090: message = topic.getMessage();
091: } else {
092: message = topic.getMatchingMessage(getMessageFilter());
093: }
094: if (null == message)
095: return null;
096: if (session.isAutoAcknowledge())
097: message.acknowledge();
098: return message;
099: }
100: }
|