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 the subscription if the subscription 055 * is durable. Otherwise, this method returns <code>null</code>. 056 * @return the name of this subscriber 057 */ 058 public String getName() 059 { 060 return name; 061 } 062 063 /** 064 * Set the name of the subscription. 065 * @param name the name of this subscriber 066 */ 067 public void setName(String name) 068 { 069 this.name = name; 070 } 071 072 public Topic getTopic() throws JMSException 073 { 074 getConnection().throwJMSException(); 075 return topic; 076 } 077 078 public boolean getNoLocal() throws JMSException 079 { 080 getConnection().throwJMSException(); 081 return noLocal; 082 } 083 084 public Message receive() throws JMSException 085 { 086 getConnection().throwJMSException(); 087 if(isClosed()) 088 { 089 throw new JMSException("Subscriber is closed"); 090 } 091 if(topic.isEmpty()) return null; 092 Message message; 093 if((!getConnection().getConfigurationManager().getUseMessageSelectors()) || (null == getMessageFilter())) 094 { 095 message = topic.getMessage(); 096 } 097 else 098 { 099 message = topic.getMatchingMessage(getMessageFilter()); 100 } 101 if(null == message) return null; 102 if(session.isAutoAcknowledge()) message.acknowledge(); 103 return message; 104 } 105 }