KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > ConsoleListener


1 package org.jboss.cache;
2
3 import org.jboss.cache.factories.XmlConfigurationParser;
4 import org.jgroups.View;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.Set JavaDoc;
9
10 /**
11  * This class provides a non-graphical view of <em>JBossCache</em> replication
12  * events for a replicated cache.
13  * <p/>
14  * It can be utilized as a standalone application or as a component in other
15  * applications.
16  * <p/>
17  * <strong>WARNING</strong>: take care when using this class in conjunction with
18  * transactionally replicated cache's as it can cause deadlock situations due to
19  * the reading of values for nodes in the cache.
20  *
21  * @author Jimmy Wilson 12-2004
22  */

23 public class ConsoleListener implements CacheListener
24 {
25    private CacheImpl _cache;
26    private boolean _startCache;
27
28    /**
29     * Constructor.
30     * <p/>
31     * When using this constructor, this class with attempt to start and stop
32     * the specified cache.
33     *
34     * @param cache the cache to monitor for replication events.
35     */

36    public ConsoleListener(CacheImpl cache)
37            throws Exception JavaDoc
38    {
39       this(cache, true, true);
40    }
41
42    /**
43     * Constructor.
44     *
45     * @param cache the cache to monitor for replication events.
46     * @param startCache indicates whether or not the cache should be started by
47     * this class.
48     * @param stopCache indicates whether or not the cache should be stopped by
49     * this class.
50     */

51    public ConsoleListener(CacheImpl cache,
52                           boolean startCache, boolean stopCache)
53            throws Exception JavaDoc
54    {
55       _cache = cache;
56       _startCache = startCache;
57
58       if (stopCache)
59       {
60          new ListenerShutdownHook().register();
61       }
62    }
63
64    /**
65     * Instructs this class to listen for cache replication events.
66     * <p/>
67     * This method waits indefinately. Use the notify method of this class
68     * (using traditional Java thread notification semantics) to cause this
69     * method to return.
70     */

71    public void listen()
72            throws Exception JavaDoc
73    {
74       listen(true);
75    }
76
77    /**
78     * Instructs this class to listen for cache replication events.
79     *
80     * @param wait whether or not this method should wait indefinately.
81     * <p/>
82     * If this parameter is set to <code>true</code>, using the
83     * notify method of this class (using traditional Java thread
84     * notification semantics) will cause this method to return.
85     */

86    public void listen(boolean wait)
87            throws Exception JavaDoc
88    {
89       _cache.getNotifier().addCacheListener(this);
90
91       if (_startCache)
92       {
93          _cache.start();
94       }
95
96       if (wait)
97       {
98          synchronized (this)
99          {
100             wait();
101          }
102       }
103    }
104
105    /*
106    * TreeCacheListener implementation.
107    */

108
109    public void cacheStarted(CacheSPI cache)
110    {
111       printEvent("Cache started.");
112    }
113
114    public void cacheStopped(CacheSPI cache)
115    {
116       printEvent("Cache stopped.");
117    }
118
119    public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
120    {
121       if (pre) printNode(fqn, "created");
122    }
123
124    public void nodeLoaded(Fqn fqn, boolean pre, Map JavaDoc data)
125    {
126       if (pre) printNode(fqn, "loaded");
127    }
128
129    public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, ModificationType modType, Map JavaDoc data)
130    {
131       if (pre)
132          printEvent("DataNode about to be modified: " + fqn);
133       else
134          printEvent("DataNode modified: " + fqn);
135    }
136
137    public void nodeRemoved(Fqn fqn, boolean pre, boolean isLocal, Map JavaDoc data)
138    {
139       if (pre)
140       {
141          printEvent("DataNode about to be removed: " + fqn);
142       }
143       else
144       {
145          printEvent("DataNode removed: " + fqn);
146       }
147
148    }
149
150    public void nodeMoved(Fqn from, Fqn to, boolean pre, boolean isLocal)
151    {
152       if (pre)
153       {
154          printEvent("About to move " + from + " to " + to);
155       }
156       else
157       {
158          printEvent("Moved " + from + " to " + to);
159       }
160    }
161
162    public void nodeVisited(Fqn fqn, boolean pre)
163    {
164       if (pre) printEvent("DataNode visited: " + fqn);
165    }
166
167    public void viewChange(View new_view)
168    {
169       printEvent("View change: " + new_view);
170    }
171
172    public void nodeEvicted(Fqn fqn, boolean pre, boolean isLocal)
173    {
174       if (pre)
175       {
176          printEvent("DataNode about to be evicted: " + fqn);
177       }
178       else
179       {
180          printEvent("DataNode evicted: " + fqn);
181       }
182    }
183
184    public void nodeActivated(Fqn fqn, boolean pre)
185    {
186       if (pre)
187       {
188          printEvent("DataNode about to be activated: " + fqn);
189       }
190       else
191       {
192          printEvent("DataNode activated: " + fqn);
193       }
194    }
195
196    public void nodePassivated(Fqn fqn, boolean pre)
197    {
198       if (pre)
199       {
200          printEvent("DataNode about to be evicted: " + fqn);
201       }
202       else
203       {
204          printEvent("DataNode evicted: " + fqn);
205       }
206    }
207
208    /**
209     * Prints an event message.
210     *
211     * @param eventSuffix the suffix of the event message.
212     */

213    private void printEvent(String JavaDoc eventSuffix)
214    {
215       System.out.print("EVENT");
216       System.out.print(' ');
217
218       System.out.println(eventSuffix);
219    }
220
221    /**
222     * Prints the contents of the specified node.
223     *
224     * @param fqn the fully qualified name of the node to print.
225     * @param eventDetail a description of why the node is being printed.
226     */

227    private void printNode(Fqn fqn, String JavaDoc eventDetail)
228    {
229       System.out.print("EVENT");
230       System.out.print(' ');
231       System.out.print("DataNode");
232       System.out.print(' ');
233       System.out.print(eventDetail);
234       System.out.print(':');
235       System.out.print(' ');
236       System.out.print(fqn);
237       System.out.println();
238
239       printNodeMap(fqn);
240
241       System.out.println();
242    }
243
244    /**
245     * Prints the contents of the specified node's <code>Map</code>.
246     *
247     * @param fqn the fully qualified name of the node containing the
248     * <code>Map</code> to be printed.
249     */

250    private void printNodeMap(Fqn fqn)
251    {
252       try
253       {
254          Set JavaDoc keys = _cache.getKeys(fqn);
255
256          if (keys != null)
257          {
258             int maxKeyLength = 0;
259             Iterator JavaDoc iterator = keys.iterator();
260
261             while (iterator.hasNext())
262             {
263                String JavaDoc key = (String JavaDoc) iterator.next();
264
265                int keyLength = key.length();
266
267                if (keyLength > maxKeyLength)
268                {
269                   maxKeyLength = keyLength;
270                }
271             }
272
273             iterator = keys.iterator();
274
275             while (iterator.hasNext())
276             {
277                String JavaDoc key = (String JavaDoc) iterator.next();
278
279                System.out.print('\t');
280                System.out.print('[');
281
282                pad(key, maxKeyLength);
283
284                System.out.print(',');
285                System.out.print(' ');
286                System.out.print(_cache.get(fqn, key));
287                System.out.println(']');
288             }
289          }
290       }
291       catch (Exception JavaDoc e)
292       {
293          e.printStackTrace(System.out);
294       }
295    }
296
297    /**
298     * Pads standard output for printing of the specified key.
299     *
300     * @param key the to be printed in a padded fashion.
301     * @param maxKeyLength the maximum length of the keys printed using this
302     * method. All keys whose length is less than this
303     * value will be printed in padded fashion.
304     */

305    private void pad(String JavaDoc key, int maxKeyLength)
306    {
307       System.out.print(key);
308
309       int keyLength = key.length();
310
311       if (keyLength < maxKeyLength)
312       {
313          int padCount = maxKeyLength - keyLength;
314
315          for (int i = 0; i < padCount; i++)
316          {
317             System.out.print(' ');
318          }
319       }
320    }
321
322    /**
323     * This class provides a shutdown hook for shutting down the cache.
324     */

325    private class ListenerShutdownHook extends Thread JavaDoc
326    {
327       /**
328        * Registers this hook for invocation during shutdown.
329        */

330       public void register()
331       {
332          Runtime.getRuntime().addShutdownHook(this);
333       }
334
335       /*
336       * Thread overrides.
337       */

338
339       public void run()
340       {
341          _cache.stop();
342       }
343    }
344
345    /**
346     * The main method.
347     *
348     * @param args command line arguments dictated by convention.
349     * <p/>
350     * The first command line argument is the name of the
351     * <code>JBossCache</code> configuration file to be utilized
352     * for configuration of the cache. Only the name of the
353     * configuration file is necessary as it is read off of the
354     * classpath.
355     * <p/>
356     * If a configuration file is not specified on the command line,
357     * <code>jboss-cache.xml</code> will be the assumed file name.
358     * <p/>
359     * All command line arguments after the first are ignored.
360     */

361    public static void main(String JavaDoc[] args)
362    {
363       final String JavaDoc DEFAULT_CONFIG_FILE_NAME = "jboss-cache.xml";
364
365       try
366       {
367          CacheImpl cache = new CacheImpl();
368
369          String JavaDoc configFileName = DEFAULT_CONFIG_FILE_NAME;
370
371          if (args.length >= 1)
372          {
373             configFileName = args[0];
374          }
375          else
376          {
377             System.out.print("No xml config file argument is supplied. Will use jboss-cache.xml from classpath");
378          }
379
380          cache.setConfiguration(new XmlConfigurationParser().parseFile(configFileName));
381
382          ConsoleListener listener = new ConsoleListener(cache);
383          listener.listen();
384       }
385       catch (Throwable JavaDoc throwable)
386       {
387          throwable.printStackTrace();
388       }
389    }
390 }
391
Popular Tags