001: package org.jzonic.jlo;
002:
003: import java.util.HashMap;
004: import java.util.Vector;
005: import org.jzonic.jlo.filter.LogFilter;
006:
007: /**
008: * The LogConfiguration keeps everything together. It contains
009: * all logger, channels, filter, pipes and log-generator for one particular
010: * configuration. The LogManager keeps a list of several
011: * log configurations.
012: *
013: * @author Andreas Mecky
014: * @author Terry Dye
015: */
016: public class LogConfiguration {
017:
018: private String configurationName;
019: private HashMap logger;
020: private HashMap generator;
021: private HashMap channels;
022: private Vector pipes;
023: private HashMap filters;
024:
025: /**
026: * @param configurationName
027: */
028: public LogConfiguration(String configurationName) {
029: logger = new HashMap();
030: generator = new HashMap();
031: channels = new HashMap();
032: pipes = new Vector();
033: filters = new HashMap();
034: this .configurationName = configurationName;
035: }
036:
037: /**
038: * This method returns a channel with the given name from this configuration.
039: *
040: * @param name the name of the channel
041: * @return the specific channel or NULL if the channel is not inside the configuration
042: */
043: public Channel getChannel(String name) {
044: Channel channel = (Channel) channels.get(name);
045: return channel;
046: }
047:
048: /**
049: * This method is used internally to search for a channel.
050: * It is exactly the same as findLoggerName except that
051: * it searches through the channels.
052: */
053: private String findChannelName(String name) {
054: boolean searching = true;
055: String myName = name;
056: while (searching) {
057: if (!channels.containsKey(myName)) {
058: int pos = myName.lastIndexOf(".");
059: if (pos != -1) {
060: myName = myName.substring(0, pos);
061: } else {
062: searching = false;
063: myName = null;
064: }
065: } else {
066: searching = false;
067: }
068: }
069: return myName;
070: }
071:
072: /**
073: * This method returns a logger from this configuration
074: * with the specific name. This method will try to search
075: * for a matching logger using the findLoggerName method.
076: *
077: * @param name the name of the logger
078: * @return the particular logger or a matching logger or NULL if not found
079: */
080: public Logger getLogger(String name) {
081: String myName = findLoggerName(name);
082: Logger myLogger = (Logger) logger.get(myName);
083: return myLogger;
084: }
085:
086: /**
087: * This method is used internally to find a logger by a given
088: * name. If the given names is org.jzonic.jlo.test then
089: * the method will look for this name first. If not found then
090: * it will cut off the last part of the name from the last "."
091: * and try it again.
092: */
093: private String findLoggerName(String name) {
094: boolean searching = true;
095: String myName = name;
096: while (searching) {
097: if (!logger.containsKey(myName)) {
098: int pos = myName.lastIndexOf(".");
099: if (pos != -1) {
100: myName = myName.substring(0, pos);
101: } else {
102: searching = false;
103: myName = null;
104: }
105: } else {
106: searching = false;
107: }
108: }
109: return myName;
110: }
111:
112: /**
113: * This method adds a logger to this configuration with
114: * the name of the logger. The name of the logger is used
115: * as key so it is not possible to add two loggers with the
116: * same name. The second one will overwrite the first one.
117: *
118: * @param l the logger that will be added
119: */
120: public void addLogger(Logger l) {
121: if (l != null) {
122: if (l.getLoggerName() != null) {
123: logger.put(l.getLoggerName(), l);
124: }
125: }
126: }
127:
128: /**
129: * This method returns the number of loggers in this
130: * configuration.
131: *
132: * @return number of loggers
133: */
134: public int getLoggerCount() {
135: return logger.size();
136: }
137:
138: /**
139: * This method adds a LogGenerator to the configuration.
140: * The name of the LogGenerator is the key. It is not
141: * possible to add two LogGenerator with the same name.
142: * The second one will overwrite the first one.
143: *
144: * @param lg the LogGenerator that will be added
145: */
146: public void addLogGenerator(LogGenerator lg) {
147: if (lg != null) {
148: if (lg.getName() != null) {
149: generator.put(lg.getName(), lg);
150: }
151: }
152: }
153:
154: /**
155: * This method returns a LogGenerator with the given name
156: * or NULL if the log generator is not in this configuration.
157: *
158: * @param name the name of the log generator
159: * @return the log generator
160: */
161: public LogGenerator getLogGenerator(String name) {
162: return (LogGenerator) generator.get(name);
163: }
164:
165: /**
166: * This method returns the number of LogGenerators
167: * in this configuration.
168: *
169: * @return number of log generators
170: */
171: public int getLogGeneratorCount() {
172: return generator.size();
173: }
174:
175: /**
176: * This method adds a channel to the configuration. This
177: * channel is stored with the given name as key. It is
178: * not possible to add two channels with the same name.
179: * The second one will overwrite the first one.
180: *
181: * @param channel the channel to add
182: */
183: public void addChannel(Channel channel) {
184: if (channel != null) {
185: if (channel.getChannelName() != null) {
186: channels.put(channel.getChannelName(), channel);
187: }
188: }
189: }
190:
191: /**
192: * This method returns the number of channel in this
193: * configuration.
194: *
195: * @return number of channels
196: */
197: public int getChannelCount() {
198: return channels.size();
199: }
200:
201: /**
202: * This method returns the name of the configuration
203: *
204: * @return the name of the configuration
205: *
206: */
207: public String getName() {
208: return configurationName;
209: }
210:
211: /**
212: * @param pipe
213: */
214: public void addLogPipe(LogPipe pipe) {
215: if (pipe != null) {
216: pipes.add(pipe);
217: }
218: }
219:
220: /**
221: * This method returns all LogPipes
222: *
223: * @return a Vector containing all logpipes
224: */
225: public Vector getLogPipes() {
226: return pipes;
227: }
228:
229: /**
230: * This method returns the number of LogPipes
231: * for this configuration.
232: *
233: * @return number of pipes
234: */
235: public int getLogPipesCount() {
236: return pipes.size();
237: }
238:
239: /**
240: * @param name
241: * @param filter
242: */
243: public void addLogFilter(String name, LogFilter filter) {
244: filters.put(name, filter);
245: }
246:
247: /**
248: * @param name
249: * @return
250: */
251: public LogFilter getLogFilter(String name) {
252: return (LogFilter) filters.get(name);
253: }
254:
255: /**
256: * @return
257: */
258: public int getFilterCount() {
259: return filters.size();
260: }
261: }
|