1 4 package org.oddjob.logging; 5 6 import java.util.ArrayList ; 7 import java.util.List ; 8 9 import org.apache.log4j.Logger; 10 import org.oddjob.Structural; 11 import org.oddjob.structural.StructuralEvent; 12 import org.oddjob.structural.StructuralListener; 13 14 18 public class ConsoleArchiverCache extends AbstractArchiverCache { 19 private static final Logger logger = Logger.getLogger(ConsoleArchiverCache.class); 20 21 22 private final List listeningTo = new ArrayList (); 23 24 25 private final StructuralListener structuralListener = new StructuralListener() { 26 public void childAdded(StructuralEvent event) { 27 Object node = event.getChild(); 28 addChild(node); 29 } 30 public void childRemoved(StructuralEvent event) { 31 Object node = event.getChild(); 32 removeChild(node); 33 } 34 }; 35 36 private final Resolver resolver; 37 38 public ConsoleArchiverCache(Object root, Resolver resolver) { 39 this(root, LogArchiver.MAX_HISTORY, resolver); 40 } 41 42 public ConsoleArchiverCache(Object root, int maxHistory, Resolver resolver) { 43 super(maxHistory); 44 this.resolver = resolver; 45 addChild(root); 46 } 47 48 53 void addChild(Object node) { 54 LogArchive consoleArchive = resolver.archiveFor(node); 55 if (consoleArchive == null) { 56 throw new IllegalStateException ("Console must not be null"); 57 } 58 addArchive(consoleArchive); 59 60 if (node instanceof ConsoleArchiver) { 61 return; 62 } 63 64 if (node instanceof Structural) { 65 ((Structural) node).addStructuralListener(structuralListener); 66 listeningTo.add(node); 67 } 68 } 69 70 void removeChild(Object node) { 71 String archive = resolver.idFor(node); 72 removeArchive(archive); 73 74 if (node instanceof Structural) { 75 ((Structural) node).removeStructuralListener(structuralListener); 76 listeningTo.remove(node); 77 } 78 } 79 80 public void destroy() { 81 while (listeningTo.size() > 0) { 82 removeChild(listeningTo.get(0)); 83 } 84 } 85 86 90 public static interface Resolver { 91 public LogArchive archiveFor(Object component); 92 93 public String idFor(Object component); 94 } 95 96 } 97 | Popular Tags |