KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > LogConfiguration


1 package org.jzonic.jlo;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Vector JavaDoc;
5 import org.jzonic.jlo.filter.LogFilter;
6 /**
7  * The LogConfiguration keeps everything together. It contains
8  * all logger, channels, filter, pipes and log-generator for one particular
9  * configuration. The LogManager keeps a list of several
10  * log configurations.
11  *
12  * @author Andreas Mecky
13  * @author Terry Dye
14  */

15 public class LogConfiguration {
16     
17     private String JavaDoc configurationName;
18     private HashMap JavaDoc logger;
19     private HashMap JavaDoc generator;
20     private HashMap JavaDoc channels;
21     private Vector JavaDoc pipes;
22     private HashMap JavaDoc filters;
23     /**
24      * @param configurationName
25      */

26     public LogConfiguration(String JavaDoc configurationName) {
27         logger = new HashMap JavaDoc();
28         generator = new HashMap JavaDoc();
29         channels = new HashMap JavaDoc();
30         pipes = new Vector JavaDoc();
31         filters = new HashMap JavaDoc();
32         this.configurationName = configurationName;
33     }
34     
35     /**
36      * This method returns a channel with the given name from this configuration.
37      *
38      * @param name the name of the channel
39      * @return the specific channel or NULL if the channel is not inside the configuration
40      */

41     public Channel getChannel(String JavaDoc name) {
42         Channel channel = (Channel)channels.get(name);
43         return channel;
44     }
45     
46     /**
47      * This method is used internally to search for a channel.
48      * It is exactly the same as findLoggerName except that
49      * it searches through the channels.
50      */

51     private String JavaDoc findChannelName(String JavaDoc name) {
52         boolean searching = true;
53         String JavaDoc myName = name;
54         while ( searching ) {
55             if ( !channels.containsKey(myName) ) {
56                 int pos = myName.lastIndexOf(".");
57                 if ( pos != -1 ) {
58                     myName = myName.substring(0,pos);
59                 }
60                 else {
61                     searching = false;
62                     myName = null;
63                 }
64             }
65             else {
66                 searching = false;
67             }
68         }
69         return myName;
70     }
71     
72     /**
73      * This method returns a logger from this configuration
74      * with the specific name. This method will try to search
75      * for a matching logger using the findLoggerName method.
76      *
77      * @param name the name of the logger
78      * @return the particular logger or a matching logger or NULL if not found
79      */

80     public Logger getLogger(String JavaDoc name) {
81         String JavaDoc myName = findLoggerName(name);
82         Logger myLogger = (Logger)logger.get(myName);
83         return myLogger;
84     }
85     
86     /**
87      * This method is used internally to find a logger by a given
88      * name. If the given names is org.jzonic.jlo.test then
89      * the method will look for this name first. If not found then
90      * it will cut off the last part of the name from the last "."
91      * and try it again.
92      */

93     private String JavaDoc findLoggerName(String JavaDoc name) {
94         boolean searching = true;
95         String JavaDoc myName = name;
96         while ( searching ) {
97             if ( !logger.containsKey(myName) ) {
98                 int pos = myName.lastIndexOf(".");
99                 if ( pos != -1 ) {
100                     myName = myName.substring(0,pos);
101                 }
102                 else {
103                     searching = false;
104                     myName = null;
105                 }
106             }
107             else {
108                 searching = false;
109             }
110         }
111         return myName;
112     }
113     
114     /**
115      * This method adds a logger to this configuration with
116      * the name of the logger. The name of the logger is used
117      * as key so it is not possible to add two loggers with the
118      * same name. The second one will overwrite the first one.
119      *
120      * @param l the logger that will be added
121      */

122     public void addLogger(Logger l) {
123         if ( l != null ) {
124             if ( l.getLoggerName() != null ) {
125                 logger.put(l.getLoggerName(), l);
126             }
127         }
128     }
129     
130     /**
131      * This method returns the number of loggers in this
132      * configuration.
133      *
134      * @return number of loggers
135      */

136     public int getLoggerCount() {
137         return logger.size();
138     }
139     
140     /**
141      * This method adds a LogGenerator to the configuration.
142      * The name of the LogGenerator is the key. It is not
143      * possible to add two LogGenerator with the same name.
144      * The second one will overwrite the first one.
145      *
146      * @param lg the LogGenerator that will be added
147      */

148     public void addLogGenerator(LogGenerator lg) {
149         if ( lg != null ) {
150             if ( lg.getName() != null ) {
151                 generator.put(lg.getName(),lg);
152             }
153         }
154     }
155     
156     /**
157      * This method returns a LogGenerator with the given name
158      * or NULL if the log generator is not in this configuration.
159      *
160      * @param name the name of the log generator
161      * @return the log generator
162      */

163     public LogGenerator getLogGenerator(String JavaDoc name) {
164         return (LogGenerator)generator.get(name);
165     }
166     
167     /**
168      * This method returns the number of LogGenerators
169      * in this configuration.
170      *
171      * @return number of log generators
172      */

173     public int getLogGeneratorCount() {
174         return generator.size();
175     }
176     
177     /**
178      * This method adds a channel to the configuration. This
179      * channel is stored with the given name as key. It is
180      * not possible to add two channels with the same name.
181      * The second one will overwrite the first one.
182      *
183      * @param channel the channel to add
184      */

185     public void addChannel(Channel channel) {
186         if ( channel != null ) {
187             if ( channel.getChannelName() != null ) {
188                 channels.put(channel.getChannelName(),channel);
189             }
190         }
191     }
192     
193     /**
194      * This method returns the number of channel in this
195      * configuration.
196      *
197      * @return number of channels
198      */

199     public int getChannelCount() {
200         return channels.size();
201     }
202     
203     /**
204      * This method returns the name of the configuration
205      *
206      * @return the name of the configuration
207      *
208      */

209     public String JavaDoc getName() {
210         return configurationName;
211     }
212    
213     /**
214      * @param pipe
215      */

216     public void addLogPipe(LogPipe pipe) {
217         if ( pipe != null ) {
218             pipes.add(pipe);
219         }
220     }
221     
222     /**
223      * This method returns all LogPipes
224      *
225      * @return a Vector containing all logpipes
226      */

227     public Vector JavaDoc getLogPipes() {
228         return pipes;
229     }
230     /**
231      * This method returns the number of LogPipes
232      * for this configuration.
233      *
234      * @return number of pipes
235      */

236     public int getLogPipesCount() {
237         return pipes.size();
238     }
239     
240     /**
241      * @param name
242      * @param filter
243      */

244     public void addLogFilter(String JavaDoc name,LogFilter filter) {
245         filters.put(name,filter);
246     }
247     
248     /**
249      * @param name
250      * @return
251      */

252     public LogFilter getLogFilter(String JavaDoc name) {
253         return (LogFilter)filters.get(name);
254     }
255     
256     /**
257      * @return
258      */

259     public int getFilterCount() {
260         return filters.size();
261     }
262 }
263
Popular Tags