1 package org.jboss.cache; 2 3 import org.jboss.cache.factories.XmlConfigurationParser; 4 import org.jgroups.View; 5 6 import java.util.Iterator ; 7 import java.util.Map ; 8 import java.util.Set ; 9 10 23 public class ConsoleListener implements CacheListener 24 { 25 private CacheImpl _cache; 26 private boolean _startCache; 27 28 36 public ConsoleListener(CacheImpl cache) 37 throws Exception 38 { 39 this(cache, true, true); 40 } 41 42 51 public ConsoleListener(CacheImpl cache, 52 boolean startCache, boolean stopCache) 53 throws Exception 54 { 55 _cache = cache; 56 _startCache = startCache; 57 58 if (stopCache) 59 { 60 new ListenerShutdownHook().register(); 61 } 62 } 63 64 71 public void listen() 72 throws Exception 73 { 74 listen(true); 75 } 76 77 86 public void listen(boolean wait) 87 throws Exception 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 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 data) 125 { 126 if (pre) printNode(fqn, "loaded"); 127 } 128 129 public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, ModificationType modType, Map 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 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 213 private void printEvent(String eventSuffix) 214 { 215 System.out.print("EVENT"); 216 System.out.print(' '); 217 218 System.out.println(eventSuffix); 219 } 220 221 227 private void printNode(Fqn fqn, String 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 250 private void printNodeMap(Fqn fqn) 251 { 252 try 253 { 254 Set keys = _cache.getKeys(fqn); 255 256 if (keys != null) 257 { 258 int maxKeyLength = 0; 259 Iterator iterator = keys.iterator(); 260 261 while (iterator.hasNext()) 262 { 263 String key = (String ) 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 key = (String ) 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 e) 292 { 293 e.printStackTrace(System.out); 294 } 295 } 296 297 305 private void pad(String 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 325 private class ListenerShutdownHook extends Thread 326 { 327 330 public void register() 331 { 332 Runtime.getRuntime().addShutdownHook(this); 333 } 334 335 338 339 public void run() 340 { 341 _cache.stop(); 342 } 343 } 344 345 361 public static void main(String [] args) 362 { 363 final String DEFAULT_CONFIG_FILE_NAME = "jboss-cache.xml"; 364 365 try 366 { 367 CacheImpl cache = new CacheImpl(); 368 369 String 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 throwable) 386 { 387 throwable.printStackTrace(); 388 } 389 } 390 } 391 | Popular Tags |