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 21 public class LogArchiverCache extends AbstractArchiverCache { 22 private static final Logger logger = Logger.getLogger(LogArchiverCache.class); 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 37 private final List listeningTo = new ArrayList (); 38 39 private final Resolver resolver; 40 41 49 public LogArchiverCache(Object root, Resolver resolver) { 50 this(root, LogArchiver.MAX_HISTORY, resolver); 51 } 52 53 62 public LogArchiverCache(Object root, int maxHistory, Resolver resolver) { 63 super(maxHistory); 64 this.resolver = resolver; 65 addChild(root); 66 } 67 68 73 void addChild(Object node) { 74 String archive = resolver.loggerFor(node); 75 if (archive != null) { 76 logger.debug("Adding archive [" + archive + "] for [" + node + "]"); 77 addArchive(new LogArchive(archive, getMaxHistory())); 78 } 79 80 if (node instanceof LogArchiver) { 81 return; 82 } 83 84 if (node instanceof Structural) { 85 ((Structural) node).addStructuralListener(structuralListener); 86 listeningTo.add(node); 87 } 88 } 89 90 94 void removeChild(Object node) { 95 String archive = resolver.loggerFor(node); 96 if (archive != null) { 97 logger.debug("Removing archive [" + archive + "] for [" + node + "]"); 98 removeArchive(archive); 99 } 100 101 if (node instanceof Structural) { 102 ((Structural) node).removeStructuralListener(structuralListener); 103 listeningTo.remove(node); 104 } 105 } 106 107 public void destroy() { 108 while (listeningTo.size() > 0) { 109 removeChild(listeningTo.get(0)); 110 } 111 } 112 113 114 121 public static interface Resolver { 122 public String loggerFor(Object component); 123 } 124 } 125 | Popular Tags |