Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 230   Methods: 11
NCLOC: 131   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ConsoleListener.java 0% 0% 0% 0%
coverage
 1    package org.jboss.cache;
 2   
 3    import org.jboss.cache.notifications.annotation.CacheListener;
 4    import org.jboss.cache.notifications.annotation.CacheStarted;
 5    import org.jboss.cache.notifications.annotation.CacheStopped;
 6    import org.jboss.cache.notifications.annotation.NodeActivated;
 7    import org.jboss.cache.notifications.annotation.NodeCreated;
 8    import org.jboss.cache.notifications.annotation.NodeEvicted;
 9    import org.jboss.cache.notifications.annotation.NodeLoaded;
 10    import org.jboss.cache.notifications.annotation.NodeModified;
 11    import org.jboss.cache.notifications.annotation.NodeMoved;
 12    import org.jboss.cache.notifications.annotation.NodePassivated;
 13    import org.jboss.cache.notifications.annotation.NodeRemoved;
 14    import org.jboss.cache.notifications.annotation.NodeVisited;
 15    import org.jboss.cache.notifications.annotation.ViewChanged;
 16    import org.jboss.cache.notifications.event.Event;
 17    import org.jboss.cache.notifications.event.NodeEvent;
 18    import org.jboss.cache.notifications.event.ViewChangedEvent;
 19   
 20    /**
 21    * This class provides a non-graphical view of <em>JBossCache</em> replication
 22    * events for a replicated cache.
 23    * <p/>
 24    * It can be utilized as a standalone application or as a component in other
 25    * applications.
 26    * <p/>
 27    * <strong>WARNING</strong>: take care when using this class in conjunction with
 28    * transactionally replicated cache's as it can cause deadlock situations due to
 29    * the reading of values for nodes in the cache.
 30    *
 31    * @author Jimmy Wilson 12-2004
 32    */
 33    @CacheListener
 34    public class ConsoleListener
 35    {
 36    private CacheImpl _cache;
 37    private boolean _startCache;
 38   
 39    /**
 40    * Constructor.
 41    * <p/>
 42    * When using this constructor, this class with attempt to start and stop
 43    * the specified cache.
 44    *
 45    * @param cache the cache to monitor for replication events.
 46    */
 47  0 public ConsoleListener(CacheImpl cache)
 48    throws Exception
 49    {
 50  0 this(cache, true, true);
 51    }
 52   
 53    /**
 54    * Constructor.
 55    *
 56    * @param cache the cache to monitor for replication events.
 57    * @param startCache indicates whether or not the cache should be started by
 58    * this class.
 59    * @param stopCache indicates whether or not the cache should be stopped by
 60    * this class.
 61    */
 62  0 public ConsoleListener(CacheImpl cache,
 63    boolean startCache, boolean stopCache)
 64    throws Exception
 65    {
 66  0 _cache = cache;
 67  0 _startCache = startCache;
 68   
 69  0 if (stopCache)
 70    {
 71  0 new ListenerShutdownHook().register();
 72    }
 73    }
 74   
 75    /**
 76    * Instructs this class to listen for cache replication events.
 77    * <p/>
 78    * This method waits indefinately. Use the notify method of this class
 79    * (using traditional Java thread notification semantics) to cause this
 80    * method to return.
 81    */
 82  0 public void listen()
 83    throws Exception
 84    {
 85  0 listen(true);
 86    }
 87   
 88    /**
 89    * Instructs this class to listen for cache replication events.
 90    *
 91    * @param wait whether or not this method should wait indefinately.
 92    * <p/>
 93    * If this parameter is set to <code>true</code>, using the
 94    * notify method of this class (using traditional Java thread
 95    * notification semantics) will cause this method to return.
 96    */
 97  0 public void listen(boolean wait)
 98    throws Exception
 99    {
 100  0 _cache.getNotifier().addCacheListener(this);
 101   
 102  0 if (_startCache)
 103    {
 104  0 _cache.start();
 105    }
 106   
 107  0 synchronized (this)
 108    {
 109  0 while (wait)
 110    {
 111  0 wait();
 112    }
 113    }
 114    }
 115   
 116   
 117  0 @CacheStarted
 118    @CacheStopped
 119    public void printDetails(Event e)
 120    {
 121  0 printEvent("Cache started.");
 122    }
 123   
 124   
 125  0 @NodeCreated
 126    @NodeLoaded
 127    @NodeModified
 128    @NodeRemoved
 129    @NodeVisited
 130    @NodeMoved
 131    @NodeEvicted
 132    @NodeActivated
 133    @NodePassivated
 134    public void printDetailsWithFqn(NodeEvent e)
 135    {
 136  0 if (e.isPre())
 137    {
 138  0 printEvent("Event " + e.getType() + " on node [" + e.getFqn() + "] about to be invoked");
 139    }
 140    else
 141    {
 142  0 printEvent("Event " + e.getType() + " on node [" + e.getFqn() + "] invoked");
 143    }
 144    }
 145   
 146  0 @ViewChanged
 147    public void printNewView(ViewChangedEvent e)
 148    {
 149  0 printEvent("View change: " + e.getNewView());
 150    }
 151   
 152    /**
 153    * Prints an event message.
 154    *
 155    * @param eventSuffix the suffix of the event message.
 156    */
 157  0 private void printEvent(String eventSuffix)
 158    {
 159  0 System.out.print("EVENT");
 160  0 System.out.print(' ');
 161   
 162  0 System.out.println(eventSuffix);
 163    }
 164   
 165    /**
 166    * This class provides a shutdown hook for shutting down the cache.
 167    */
 168    private class ListenerShutdownHook extends Thread
 169    {
 170    /**
 171    * Registers this hook for invocation during shutdown.
 172    */
 173  0 public void register()
 174    {
 175  0 Runtime.getRuntime().addShutdownHook(this);
 176    }
 177   
 178    /*
 179    * Thread overrides.
 180    */
 181   
 182  0 public void run()
 183    {
 184  0 _cache.stop();
 185    }
 186    }
 187   
 188    /**
 189    * The main method.
 190    *
 191    * @param args command line arguments dictated by convention.
 192    * <p/>
 193    * The first command line argument is the name of the
 194    * <code>JBossCache</code> configuration file to be utilized
 195    * for configuration of the cache. Only the name of the
 196    * configuration file is necessary as it is read off of the
 197    * classpath.
 198    * <p/>
 199    * If a configuration file is not specified on the command line,
 200    * <code>jboss-cache.xml</code> will be the assumed file name.
 201    * <p/>
 202    * All command line arguments after the first are ignored.
 203    */
 204  0 public static void main(String[] args)
 205    {
 206  0 final String DEFAULT_CONFIG_FILE_NAME = "jboss-cache.xml";
 207   
 208  0 try
 209    {
 210  0 String configFileName = DEFAULT_CONFIG_FILE_NAME;
 211   
 212  0 if (args.length >= 1)
 213    {
 214  0 configFileName = args[0];
 215    }
 216    else
 217    {
 218  0 System.out.print("No xml config file argument is supplied. Will use jboss-cache.xml from classpath");
 219    }
 220   
 221  0 CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(configFileName);
 222  0 ConsoleListener listener = new ConsoleListener(cache);
 223  0 listener.listen();
 224    }
 225    catch (Throwable throwable)
 226    {
 227  0 throwable.printStackTrace();
 228    }
 229    }
 230    }