KickJava   Java API By Example, From Geeks To Geeks.

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


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 ConsoleArchiver archivers.
16  * <p>
17  */

18 public class ConsoleArchiverCache extends AbstractArchiverCache {
19     private static final Logger logger = Logger.getLogger(ConsoleArchiverCache.class);
20     
21     /** List of nodes we're listening to so we can tidy up. */
22     private final List JavaDoc listeningTo = new ArrayList JavaDoc();
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     private final Resolver resolver;
37     
38     public ConsoleArchiverCache(Object JavaDoc root, Resolver resolver) {
39         this(root, LogArchiver.MAX_HISTORY, resolver);
40     }
41     
42     public ConsoleArchiverCache(Object JavaDoc root, int maxHistory, Resolver resolver) {
43         super(maxHistory);
44         this.resolver = resolver;
45         addChild(root);
46     }
47         
48     /**
49      * Add a child node to this Log Archiver.
50      *
51      * @param node The child node.
52      */

53     void addChild(Object JavaDoc node) {
54         LogArchive consoleArchive = resolver.archiveFor(node);
55         if (consoleArchive == null) {
56             throw new IllegalStateException JavaDoc("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 JavaDoc node) {
71         String JavaDoc 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     /**
87      * Resolves the LogArchive and Id for a ConsoleArchiver.
88      *
89      */

90     public static interface Resolver {
91         public LogArchive archiveFor(Object JavaDoc component);
92         
93         public String JavaDoc idFor(Object JavaDoc component);
94     }
95
96 }
97
Popular Tags