package org.jboss.cache;
import java.util.Iterator;
import java.util.Set;
import org.jboss.cache.TreeCache;
import org.jboss.cache.Fqn;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.TreeCacheListener;
import org.jgroups.View;
public class ConsoleListener implements TreeCacheListener
{
private TreeCache _cache;
private boolean _startCache;
public ConsoleListener(TreeCache cache)
throws Exception
{
this(cache, true, true);
}
public ConsoleListener(TreeCache cache,
boolean startCache, boolean stopCache)
throws Exception
{
_cache = cache;
_startCache = startCache;
if (stopCache)
{
new ListenerShutdownHook().register();
}
}
public void listen()
throws Exception
{
listen(true);
}
public void listen(boolean wait)
throws Exception
{
_cache.addTreeCacheListener(this);
if (_startCache)
{
_cache.startService();
}
if (wait)
{
synchronized(this)
{
wait();
}
}
}
public void cacheStarted(TreeCache cache)
{
printEvent("Cache started.");
}
public void cacheStopped(TreeCache cache)
{
printEvent("Cache stopped.");
}
public void nodeCreated(Fqn fqn)
{
printNode(fqn, "created");
}
public void nodeEvicted(Fqn fqn)
{
printEvent("Node evicted: " + fqn);
}
public void nodeLoaded(Fqn fqn)
{
printNode(fqn, "loaded");
}
public void nodeModified(Fqn fqn)
{
printNode(fqn, "modified");
}
public void nodeRemoved(Fqn fqn)
{
printEvent("Node removed: " + fqn);
}
public void nodeVisited(Fqn fqn)
{
printEvent("Node visited: " + fqn);
}
public void viewChange(View new_view)
{
printEvent("View change: " + new_view);
}
private void printEvent(String eventSuffix)
{
System.out.print("EVENT");
System.out.print(' ');
System.out.println(eventSuffix);
}
private void printNode(Fqn fqn, String eventDetail)
{
System.out.print("EVENT");
System.out.print(' ');
System.out.print("Node");
System.out.print(' ');
System.out.print(eventDetail);
System.out.print(':');
System.out.print(' ');
System.out.print(fqn);
System.out.println();
printNodeMap(fqn);
System.out.println();
}
private void printNodeMap(Fqn fqn)
{
try
{
Set keys = _cache.getKeys(fqn);
if (keys != null)
{
int maxKeyLength = 0;
Iterator iterator = keys.iterator();
while (iterator.hasNext())
{
String key = (String) iterator.next();
int keyLength = key.length();
if (keyLength > maxKeyLength)
{
maxKeyLength = keyLength;
}
}
iterator = keys.iterator();
while (iterator.hasNext())
{
String key = (String) iterator.next();
System.out.print('\t');
System.out.print('[');
pad(key, maxKeyLength);
System.out.print(',');
System.out.print(' ');
System.out.print(_cache.get(fqn, key));
System.out.println(']');
}
}
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
private void pad(String key, int maxKeyLength)
{
System.out.print(key);
int keyLength = key.length();
if (keyLength < maxKeyLength)
{
int padCount = maxKeyLength - keyLength;
for (int i = 0; i < padCount; i++)
{
System.out.print(' ');
}
}
}
private class ListenerShutdownHook extends Thread
{
public void register()
{
Runtime.getRuntime().addShutdownHook(this);
}
public void run()
{
_cache.stopService();
}
}
public static void main(String[] args)
{
final String DEFAULT_CONFIG_FILE_NAME = "jboss-cache.xml";
try
{
TreeCache cache = new TreeCache();
PropertyConfigurator configurator = new PropertyConfigurator();
String configFileName = DEFAULT_CONFIG_FILE_NAME;
if (args.length >= 1)
{
configFileName = args[0];
} else {
System.out.print("No xml config file argument is supplied. Will use jboss-cache.xml from classpath");
}
configurator.configure(cache, configFileName);
ConsoleListener listener = new ConsoleListener(cache);
listener.listen();
}
catch (Throwable throwable)
{
throwable.printStackTrace();
}
}
}