1 4 package org.oddjob.logging; 5 6 import java.util.HashMap ; 7 import java.util.Map ; 8 9 import org.apache.log4j.Logger; 10 11 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 23 private Map archives = new HashMap (); 24 25 28 private final SimpleCounter counter = new SimpleCounter(); 29 30 private final int maxHistory; 31 32 35 public AbstractArchiverCache() { 36 this(LogArchiver.MAX_HISTORY); 37 } 38 39 44 public AbstractArchiverCache(int maxHistory) { 45 this.maxHistory = maxHistory; 46 } 47 48 52 public int getMaxHistory() { 53 return maxHistory; 54 } 55 56 60 public long getLastMessageNumber(String archive) { 61 LogArchive logArchive = (LogArchive) archives.get(archive); 62 if (logArchive == null) { 63 throw new IllegalArgumentException ("Archive [" + archive + "] does not exist in this LogArchiver."); 64 } 65 return logArchive.getLastMessageNumber(); 66 } 67 68 77 public void addLogListener(LogListener l, String 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 95 public boolean removeLogListener(LogListener l, String 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 111 public boolean hasArchive(String archive) { 112 return archives.containsKey(archive); 113 } 114 115 120 protected void addArchive(final LogArchive logArchive) { 121 final String archive = logArchive.getArchive(); 122 counter.add(archive, new Runnable () { 123 public void run() { 124 archives.put(archive, logArchive); 125 logger.debug("Adding archive for [" + archive + "]"); 126 } 127 }); 128 } 129 130 135 protected void removeArchive(final String archive) { 136 counter.remove(archive, new Runnable () { 137 public void run() { 138 logger.debug("Deleting archive [" + archive + "]"); 139 archives.remove(archive); 140 } 141 }); 142 } 143 144 151 public void addEvent(String archive, LogLevel level, String message) { 152 LogArchive logArchive = (LogArchive) archives.get(archive); 153 if (logArchive == null) { 154 throw new IllegalArgumentException ("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 |