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

Quick Search    Search Deep

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


1   /*
2    * MessageLogger.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.Iterator;
13  import java.util.ArrayList;
14  import java.util.Collection;
15  
16  import java.lang.StringBuffer;
17  
18  /**
19   * <p>Provides a mechanism for recording published messages.</p>
20   *
21   * <p>As messages are published to the broker, the logging system can be used to record
22   * information about those messages. This information can be used for analysis and
23   * debugging.</p>
24   *
25   * @author  Trevor Milne
26   *
27   */
28  
29  public class MessageLogger
30  {
31    /** The recorded {@link Message Messages}. */
32    protected ArrayList messages;
33  
34    /** States whether messages should be logged. */
35    protected boolean active;
36  
37  
38    /* Constructors. */
39  
40  
41    /**
42     * Constructs a <code>MessageLogger</code>.
43     *
44     */
45  
46    public MessageLogger()
47    {
48      // create an empty message log
49      messages = new ArrayList();
50  
51      // do not log messages by default
52      active = false;
53    }
54  
55  
56    /* Accessors. */
57  
58  
59    /**
60     * Returns whether logging is enabled.
61     *
62     * @return                  True if logging if enabled, false otherwise.
63     *
64     */
65  
66    public boolean isActive()
67    {
68      return active;
69    }
70  
71    /**
72     * Returns the logged messages.
73     *
74     * @return                  A collection of {@link LoggedMessage LoggedMessages}.
75     *
76     */
77  
78    public Collection getMessages()
79    {
80      return messages;
81    }
82  
83  
84    /* Logging. */
85  
86  
87    /**
88     * Start the logging of messages. Any previously logged message are kept.
89     *
90     */
91  
92    public void start()
93    {
94      active = true;
95    }
96  
97    /**
98     * Stop message logging. The log is not cleared.
99     *
100    */
101 
102   public void stop()
103   {
104     active = false;
105   }
106 
107   /**
108    * Removes all messages currently in the log.
109    *
110    */
111 
112   public void clear()
113   {
114     // remove all logged messages
115     messages = new ArrayList();
116   }
117 
118   /**
119    * Writes a single message to the log.
120    *
121    * @param    message            The {@link Message} being published.
122    *
123    */
124 
125   protected void write(Message message)
126   {
127     // create logged message
128     LoggedMessage logged = new LoggedMessage(message);
129 
130     // add to log
131     messages.add(logged);
132   }
133 
134 
135   /* Inner Classes. */
136 
137 
138   /** Contains information on a single recorded {@link Message}. */
139 
140   public class LoggedMessage
141   {
142     /** The topic of the message. */
143     protected String topic;
144 
145     /** The published message. */
146     protected Message message;
147 
148     /** The receivers of the message. */
149     protected ArrayList receivers;
150 
151 
152     /* Constructors. */
153 
154     /**
155      * Creates a new <code>LoggedMessage</code> by copying information
156      * from the supplied {@link Message}.
157      *
158      * @param    message            The {@link Message} being published.
159      *
160      */
161 
162     protected LoggedMessage(Message publishMessage)
163     {
164       topic = publishMessage.getLocation().getPath();
165       message = new Message(publishMessage);
166       receivers = new ArrayList(publishMessage.getLocation().subscribers);
167     }
168 
169 
170     /* Accessors. */
171 
172 
173     /**
174      * Returns the topic the message was published to.
175      *
176      * @return                  The destination topic of the message.
177      *
178      */
179 
180     public String getTopic()
181     {
182       return topic;
183     }
184 
185     /**
186      * Returns the {@link Message} that was published.
187      *
188      * @return                  The published {@link Message}.
189      *
190      */
191 
192     public Message getMessage()
193     {
194       return message;
195     }
196 
197     /**
198      * Returns the receivers of the published {@link Message}.
199      *
200      * @return                  The receivers of the published {@link Message}.
201      *
202      */
203 
204     public Collection getReceivers()
205     {
206       return receivers;
207     }
208 
209 
210     /* Debugging. */
211 
212 
213     /**
214      * Returns a string describing this <code>LoggedMessage<code>.
215      *
216      * @return                  A description of this logged message.
217      *
218      */
219 
220     public String toString()
221     {
222       StringBuffer description = new StringBuffer();
223 
224       description.append("topic=" + topic + "; message=" + message.toString() + "; receivers=[");
225 
226       Iterator receiverIterator = receivers.iterator();
227 
228       while (receiverIterator.hasNext())
229       {
230         description.append(receiverIterator.next().toString());
231 
232         if (receiverIterator.hasNext())
233         {
234           description.append(", ");
235         }
236       }
237 
238       description.append("]");
239 
240       return description.toString();
241     }
242   }
243 }
244