KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > logging > LogArchiverCache


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.logging;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
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 /**
15  * A Cache for Log Events. This cache tracks changes to the structure of
16  * the component tree and adds and removes LogArchives.
17  * <p>
18  *
19  * @author Rob Gordon.
20  */

21 public class LogArchiverCache extends AbstractArchiverCache {
22     private static final Logger logger = Logger.getLogger(LogArchiverCache.class);
23     
24     /** Structural listener */
25     private final StructuralListener structuralListener = new StructuralListener() {
26         public void childAdded(StructuralEvent event) {
27             Object JavaDoc node = event.getChild();
28             addChild(node);
29         }
30         public void childRemoved(StructuralEvent event) {
31             Object JavaDoc node = event.getChild();
32             removeChild(node);
33         }
34     };
35     
36     /** List of nodes we're listening to so we can tidy up. */
37     private final List JavaDoc listeningTo = new ArrayList JavaDoc();
38
39     private final Resolver resolver;
40     
41     /**
42      * Construct a LogArchiver archiving message from the given root object
43      * and all it's children.
44      *
45      * @param root The root object.
46      * @param resolver A reslover which resolves archive name, either locally
47      * or using a remote url.
48      */

49     public LogArchiverCache(Object JavaDoc root, Resolver resolver) {
50         this(root, LogArchiver.MAX_HISTORY, resolver);
51     }
52         
53     /**
54      * Construct a LogArchiver archiving message from the given root object
55      * and all it's children with the given amount of history.
56      *
57      * @param root The root object.
58      * @param maxHistory The number of lines to store for each logger
59      * @param resolver A reslover which resolves archive name, either locally
60      * or using a remote url.
61      */

62     public LogArchiverCache(Object JavaDoc root, int maxHistory, Resolver resolver) {
63         super(maxHistory);
64         this.resolver = resolver;
65         addChild(root);
66     }
67
68     /**
69      * Add a child node to this Log Archiver.
70      *
71      * @param node The child node.
72      */

73     void addChild(Object JavaDoc node) {
74         String JavaDoc 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     /**
91      * Remove a child node and possibly it archive.
92      * @param node
93      */

94     void removeChild(Object JavaDoc node) {
95         String JavaDoc 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     /**
115      * A LogArchiver.Resolver is able to resolve the logger name for a
116      * given component. This may involve inspecting the logger property or
117      * building a logger name from a components remote url and logger
118      * property.
119      *
120      */

121     public static interface Resolver {
122         public String JavaDoc loggerFor(Object JavaDoc component);
123     }
124 }
125
Popular Tags