KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > memory > MemoryTopic


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.memory;
30
31 import com.caucho.jms.AbstractDestination;
32 import com.caucho.jms.session.SessionImpl;
33 import com.caucho.log.Log;
34 import com.caucho.util.L10N;
35
36 import javax.jms.JMSException JavaDoc;
37 import javax.jms.Message JavaDoc;
38 import javax.jms.MessageConsumer JavaDoc;
39 import javax.jms.Topic JavaDoc;
40 import javax.jms.TopicSubscriber JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * A basic topic.
47  */

48 public class MemoryTopic extends AbstractDestination implements Topic JavaDoc {
49   static final Logger JavaDoc log = Log.open(MemoryTopic.class);
50   static final L10N L = new L10N(MemoryTopic.class);
51
52   ArrayList JavaDoc<MemoryQueue> _subscribers = new ArrayList JavaDoc<MemoryQueue>();
53
54   private HashMap JavaDoc<String JavaDoc,MemoryQueue> _durableSubscribers =
55     new HashMap JavaDoc<String JavaDoc,MemoryQueue>();
56
57   private String JavaDoc _topicName;
58
59   public MemoryTopic()
60   {
61   }
62                                             
63   /**
64    * Returns the topic's name.
65    */

66   public String JavaDoc getTopicName()
67   {
68     return _topicName;
69   }
70
71   /**
72    * Sets the topic's name.
73    */

74   public void setTopicName(String JavaDoc name)
75   {
76     _topicName = name;
77   }
78
79   public void send(Message JavaDoc message)
80     throws JMSException JavaDoc
81   {
82     for (int i = 0; i < _subscribers.size(); i++) {
83       MemoryQueue queue = _subscribers.get(i);
84
85       queue.send(message);
86     }
87   }
88   
89   /**
90    * Creates a consumer.
91    *
92    * @param session the owning session
93    * @param selector the consumer's selector
94    * @param noLocal true if pub/sub should not send local requests
95    */

96   public MessageConsumer JavaDoc createConsumer(SessionImpl session,
97                     String JavaDoc selector,
98                     boolean noLocal)
99     throws JMSException JavaDoc
100   {
101     return new MemoryTopicConsumer(session, selector, this);
102   }
103
104   /**
105    * Creates a durable subscriber.
106    */

107   public TopicSubscriber JavaDoc createDurableSubscriber(SessionImpl session,
108                          String JavaDoc selector,
109                          boolean noLocal,
110                          String JavaDoc name)
111     throws JMSException JavaDoc
112   {
113     return new MemoryTopicConsumer(session, selector, this, name);
114   }
115
116   /**
117    * finds/creates a durable subscriber.
118    */

119   public MemoryQueue createDurableSubscriber(String JavaDoc name)
120     throws JMSException JavaDoc
121   {
122     MemoryQueue queue = _durableSubscribers.get(name);
123
124     if (queue == null) {
125       queue = createSubscriberQueue();
126       _durableSubscribers.put(name, queue);
127     }
128
129     return queue;
130   }
131
132   MemoryQueue createSubscriberQueue()
133     throws JMSException JavaDoc
134   {
135     MemoryQueue queue = new MemoryQueue();
136     
137     _subscribers.add(queue);
138
139     return queue;
140   }
141
142   public void removeSubscriber(MemoryQueue queue)
143   {
144     _subscribers.remove(queue);
145   }
146
147   /**
148    * Returns a printable view of the topic.
149    */

150   public String JavaDoc toString()
151   {
152     return "Topic[" + _topicName + "]";
153   }
154 }
155
156
Popular Tags