Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/hermes/broker/MessageTopic.java


1   /*
2    * MessageTopic.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.hermes.broker;
11  
12  import java.util.HashSet;
13  import java.util.Iterator;
14  
15  import com.aendvari.common.osm.OsmNode;
16  
17  /**
18   * <p>Represents a message topic. Topics may only be used with the {@link MessageBroker}
19   * they are created by.</p>
20   *
21   * @author  Trevor Milne
22   *
23   */
24  
25  public class MessageTopic
26  {
27    /**
28     * The leaf node that the topic refers to in the hierarchy. This field is used
29     * by the {@link MessageBroker} for topic creation and management.
30     *
31     */
32  
33    protected OsmNode node;
34  
35    /** The topic path. */
36    protected String path;
37  
38    /** The subscribers to this topic. */
39    protected HashSet subscribers;
40  
41  
42    /* Constructors. */
43  
44  
45    /**
46     * Constructs a <code>MessageTopic</code>.
47     *
48     */
49  
50    public MessageTopic()
51    {
52      node = null;
53      path = null;
54      subscribers = new HashSet();
55    }
56  
57    /**
58     * Constructs a <code>MessageTopic</code>.
59     *
60     * @param    setNode            The node to associate with this topic.
61     *
62     */
63  
64    protected MessageTopic(OsmNode setNode)
65    {
66      node = setNode;
67      path = null;
68      subscribers = new HashSet();
69    }
70  
71  
72    /* Accessors. */
73  
74  
75    /**
76     * Returns the topic path.
77     *
78     * @return                  The path for this topic.
79     *
80     */
81  
82    public String getPath()
83    {
84      // lazy build/cache the path
85      if (path == null)
86      {
87        path = node.getNodePath();
88      }
89  
90      return path;
91    }
92  
93    /**
94     * Returns the node for this topic.
95     *
96     * @return                  The {@link OsmNode} associated with this topic.
97     *
98     */
99  
100   protected OsmNode getNode()
101   {
102     return node;
103   }
104 
105   /**
106    * Sets the node for this topic.
107    *
108    * @param    setNode            The node to associate with this topic.
109    *
110    */
111 
112   protected void setNode(OsmNode setNode)
113   {
114     node = setNode;
115   }
116 
117 
118   /* Subscriptions. */
119 
120 
121   /**
122    * Subscribes the listener to this topic.
123    *
124    * @param    listener          The {@link MessageListener} to add to the topic.
125    *
126    */
127 
128   protected void subscribe(MessageListener listener)
129   {
130     // add the new listener
131     subscribers.add(listener);
132   }
133 
134   /**
135    * Unsubscribes the listener from this topic.
136    *
137    * @param    listener          The {@link MessageListener} to remove from the topic.
138    *
139    */
140 
141   protected void unsubscribe(MessageListener listener)
142   {
143     // remove the listener
144     subscribers.remove(listener);
145 
146     // remove topic if no more listeners
147     if (subscribers.size() == 0)
148     {
149       // remove the node for this topic
150       node.getParentNode().removeChild(node);
151     }
152   }
153 
154 
155   /* Publishing. */
156 
157 
158   /**
159    * Publishes the message to this topic.
160    *
161    * @param    listener          The {@link Message} to publish.
162    *
163    */
164 
165   protected void publish(Message message)
166   {
167     // send the message to each subscriber
168     Iterator subIterator = subscribers.iterator();
169 
170     while (subIterator.hasNext())
171     {
172       // get the listener
173       MessageListener listener = (MessageListener)subIterator.next();
174 
175       // send the message
176       listener.onMessage(message);
177     }
178   }
179 }
180