KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.oddjob.logging;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.apache.log4j.Logger;
10
11 /**
12  * An base implementation of a Cache for LogEvents.
13  * <p>
14  * @author Rob Gordon
15  */

16 abstract public class AbstractArchiverCache {
17     private static final Logger logger = Logger.getLogger(AbstractArchiverCache.class);
18     
19     protected static final LogEvent NO_LOG_AVAILABLE = new LogEvent("SYSTEM", 0, LogLevel.INFO, "No Log available");
20     /**
21      * Map of archive name to archive.
22      */

23     private Map JavaDoc /*<String, LogArchive> */ archives = new HashMap JavaDoc();
24     
25     /**
26      * Keep track of children and archives so we can delete an archive.
27      */

28     private final SimpleCounter counter = new SimpleCounter();
29         
30     private final int maxHistory;
31     
32     /**
33      * Default constructor.
34      */

35     public AbstractArchiverCache() {
36         this(LogArchiver.MAX_HISTORY);
37     }
38         
39     /**
40      * Construct a LogArchiver with the given amount of history.
41      *
42      * @param maxHistory The number of lines to store for each logger
43      */

44     public AbstractArchiverCache(int maxHistory) {
45         this.maxHistory = maxHistory;
46     }
47     
48     /*
49      * (non-Javadoc)
50      * @see org.oddjob.logging.FunctionalLogArchiver#getMaxHistory()
51      */

52     public int getMaxHistory() {
53         return maxHistory;
54     }
55     
56     /*
57      * (non-Javadoc)
58      * @see org.oddjob.logging.FunctionalLogArchiver#getLastMessageNumber(java.lang.String)
59      */

60     public long getLastMessageNumber(String JavaDoc archive) {
61         LogArchive logArchive = (LogArchive) archives.get(archive);
62         if (logArchive == null) {
63             throw new IllegalArgumentException JavaDoc("Archive [" + archive + "] does not exist in this LogArchiver.");
64         }
65         return logArchive.getLastMessageNumber();
66     }
67
68     /**
69      * Add a listener.
70      *
71      * @param l The listener
72      * @param archive The archive
73      * @param level The level
74      * @param last The last message number.
75      * @param history The max messages required.
76      */

77     public void addLogListener(LogListener l, String JavaDoc archive,
78             LogLevel level, long last, int history) {
79         LogArchive logArchive = (LogArchive) archives.get(archive);
80         if (logArchive == null) {
81             l.logEvent(NO_LOG_AVAILABLE);
82             return;
83         }
84         logger.debug("Adding LogListener [" + l + "] for ["
85                 + logArchive.getArchive() + "]");
86         logArchive.addListener(l, level, last, history);
87     }
88         
89     /**
90      * Remove a listener.
91      *
92      * @param l The listener.
93      * @param archive The archive.
94      */

95     public boolean removeLogListener(LogListener l, String JavaDoc archive) {
96         LogArchive logArchive = (LogArchive) archives.get(archive);
97         if (logArchive == null) {
98             return false;
99         }
100         logger.debug("Removing LogListener ["
101                 + l + "] from [" + logArchive.getArchive() + "]");
102         return logArchive.removeListener(l);
103     }
104     
105     /**
106      * Does this Acchiver contain the given archive.
107      *
108      * @param archive The archive.
109      * @return true if it does, false if it doesn't.
110      */

111     public boolean hasArchive(String JavaDoc archive) {
112         return archives.containsKey(archive);
113     }
114         
115     /**
116      * Add an achive this Log Archiver.
117      *
118      * @param archive The archive name.
119      */

120     protected void addArchive(final LogArchive logArchive) {
121         final String JavaDoc archive = logArchive.getArchive();
122         counter.add(archive, new Runnable JavaDoc() {
123             public void run() {
124                 archives.put(archive, logArchive);
125                 logger.debug("Adding archive for [" + archive + "]");
126             }
127         });
128     }
129
130     /**
131      * Remove an archive from this LogArchive.
132      *
133      * @param archive
134      */

135     protected void removeArchive(final String JavaDoc archive) {
136             counter.remove(archive, new Runnable JavaDoc() {
137                 public void run() {
138                     logger.debug("Deleting archive [" + archive + "]");
139                     archives.remove(archive);
140                 }
141             });
142     }
143     
144     /**
145      * Add an event to the cache.
146      *
147      * @param archive The archive.
148      * @param level The level.
149      * @param message The message.
150      */

151     public void addEvent(String JavaDoc archive, LogLevel level, String JavaDoc message) {
152         LogArchive logArchive = (LogArchive) archives.get(archive);
153         if (logArchive == null) {
154             throw new IllegalArgumentException JavaDoc("Archive [" + archive + "] does not exist in this LogArchiver.");
155         }
156         logArchive.addEvent(level, message);
157     }
158             
159     public abstract void destroy();
160 }
161
Popular Tags