KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > config > BasicFactory


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.util.monolog.wrapper.config;
20
21 import org.objectweb.util.monolog.api.Handler;
22 import org.objectweb.util.monolog.api.Level;
23 import org.objectweb.util.monolog.api.Logger;
24 import org.objectweb.util.monolog.api.MonologFactory;
25 import org.objectweb.util.monolog.api.MonologFactoryListener;
26 import org.objectweb.util.monolog.api.TopicalLogger;
27 import org.objectweb.util.monolog.wrapper.common.LevelImpl;
28
29 import java.io.Serializable JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 /**
35  * This class is a basic implementation of the monolog factories
36  * (HandlerFactory, LoggerFactory and LevelFactory). It does not linked to an
37  * any underlying log system.
38  *
39  * @author Sebastien Chassande-Barrioz
40  */

41 public class BasicFactory
42     implements MonologFactory, Serializable JavaDoc {
43
44     /**
45      * This field references the level instances by their names.<br/>
46      * key = a level name<br/>
47      * value = the unique Level instance linked to the name.
48      */

49     protected HashMap JavaDoc nameToLevel = null;
50
51     /**
52      * This field reference the level names by their integer value.<br/>
53      * key = a java.lang.Integer which the value is the level<br/>
54      * value = a String or an ArrayList of String. The strings represent the
55      * name which match to the integer value. Indeed both name can be associated
56      * to the same integer value.
57      */

58     protected HashMap JavaDoc intToNames = null;
59
60     /**
61      * This field references the handler instance by their names.<br/>
62      * key = a String object which is an handler name.
63      * value = the unique handler instance which has the key for name.
64      */

65     protected HashMap JavaDoc handlers = null;
66
67     /**
68      * This field references the handler instance by their names.<br/>
69      * key = a String object which is a logger topic.
70      * value = the unique handler instance which has the key for name.
71      */

72     protected HashMap JavaDoc loggers = null;
73
74     /**
75      * The resource bundle linked to the LoggerFactory.
76      */

77     protected String JavaDoc resourceBundleName = null;
78
79     /**
80      * It intializes the data struture, defines the default level and the root
81      * logger.
82      */

83     public BasicFactory() {
84         intToNames = new HashMap JavaDoc();
85         nameToLevel = new HashMap JavaDoc();
86         handlers = new HashMap JavaDoc();
87         loggers = new HashMap JavaDoc();
88         defineDefaultLevels();
89         defineRootLogger();
90     }
91
92     /**
93      * It initializes the default monolog level:
94      * <ul>
95      * <li>DEBUG: 10 000</li>
96      * <li>INFO: 20 000</li>
97      * <li>WARN: 30 000</li>
98      * <li>ERROR: 40 000</li>
99      * <li>FATAL: 50 000</li>
100      * </ul>
101      */

102     protected void defineDefaultLevels() {
103         defineLevel("INHERIT", -1);
104         defineLevel("DEBUG", 10000);
105         defineLevel("INFO", 20000);
106         defineLevel("WARN", 30000);
107         defineLevel("ERROR", 40000);
108         defineLevel("FATAL", 50000);
109     }
110
111     /**
112      * It defines the level of the root logger to WARN.
113      */

114     protected void defineRootLogger() {
115         getLogger("").setLevel(getLevel("WARN"));
116     }
117
118     public void addMonologFactoryListener(MonologFactoryListener mfl) {
119     }
120
121     public void removeMonologFactoryListener(MonologFactoryListener mfl) {
122     }
123
124     // IMPLEMENTATION OF THE LevelFactory INTERFACE //
125
//-----------------------------------------------//
126

127     public Level defineLevel(String JavaDoc name, int value) {
128         return defineLevel(new LevelImpl(name, value));
129     }
130
131     public Level defineLevel(String JavaDoc name, String JavaDoc value) {
132         return defineLevel(new LevelImpl(name, value, this));
133     }
134
135     public Level getLevel(String JavaDoc name) {
136         return (Level) nameToLevel.get(name);
137     }
138
139     public Level getLevel(int value) {
140         Object JavaDoc temp = intToNames.get(new Integer JavaDoc(value));
141         if (temp == null) {
142             return null;
143         }
144         else if (temp instanceof String JavaDoc) {
145             return getLevel((String JavaDoc) temp);
146         }
147         else if (temp instanceof ArrayList JavaDoc) {
148             return getLevel((String JavaDoc) ((ArrayList JavaDoc) temp).get(0));
149         }
150         return null;
151     }
152
153     public Level[] getLevels() {
154         return (Level[]) nameToLevel.values().toArray(new Level[0]);
155     }
156
157     public void removeLevel(String JavaDoc name) {
158         Level removed = (Level) nameToLevel.remove(name);
159         if (removed != null) {
160             Integer JavaDoc i = new Integer JavaDoc(removed.getIntValue());
161             Object JavaDoc temp = intToNames.get(i);
162             if (temp instanceof String JavaDoc) {
163                 intToNames.remove(i);
164             }
165             else if (temp instanceof ArrayList JavaDoc) {
166                 ((ArrayList JavaDoc) temp).remove(name);
167             }
168         }
169     }
170
171     // OTHER METHODS //
172
//---------------//
173

174     /**
175      * Insert a level into the data structure.<br/>
176      * If the level name is already used with other integer value, the null
177      * value is returned.<br/>
178      * If the level name is already used with the same integer value, the level
179      * found in the data structure is returned.<br/>
180      *
181      * @param l the Level instance which must be inserted.
182      * @return the Level instance or a null value.
183      */

184     private Level defineLevel(Level l) {
185         String JavaDoc name = l.getName();
186         int value = l.getIntValue();
187         Level res = (Level) nameToLevel.get(name);
188         if (res != null) {
189             // The name is already defined.
190
return (res.getIntValue() == value ? res : null);
191         }
192         else {
193             res = l;
194             nameToLevel.put(name, res);
195             Integer JavaDoc i = new Integer JavaDoc(value);
196             Object JavaDoc temp = intToNames.get(i);
197             if (temp != null) {
198                 if (temp instanceof String JavaDoc) {
199                     if (!((Level) temp).getName().equalsIgnoreCase(name)) {
200                         // The int value has already another name
201
// Add the new name to the other
202
ArrayList JavaDoc al = new ArrayList JavaDoc(5);
203                         al.add(temp);
204                         al.add(name);
205                         intToNames.put(i, al);
206                     }
207                 }
208                 else if (temp instanceof ArrayList JavaDoc) {
209                     // The int value has already several another name
210
ArrayList JavaDoc al = (ArrayList JavaDoc) temp;
211                     if (!al.contains(name)) {
212                         // Add the new name to the others
213
al.add(name);
214                     }
215                 }
216             }
217             else {
218                 // The int value does not have any name
219
intToNames.put(i, name);
220             }
221         }
222         return res;
223     }
224
225     public void configure(Properties JavaDoc prop) throws Exception JavaDoc {
226     }
227     
228     // IMPLEMENTATION OF THE LoggerFactory INTERFACE //
229
//-----------------------------------------------//
230

231     public Logger getLogger(String JavaDoc _key) {
232         String JavaDoc key = _key;
233         if ("".equals(_key)) {
234             key = "root";
235         }
236         synchronized (loggers) {
237             Logger res = (Logger) loggers.get(key);
238             if (res == null) {
239                 res = new BasicLogger(key, this);
240                 loggers.put(key, res);
241             }
242             return res;
243         }
244     }
245     public void removeLogger(Logger logger) {
246         synchronized (loggers) {
247             loggers.remove(logger.getName());
248         }
249     }
250     
251     public Logger getLogger(String JavaDoc key, String JavaDoc resourceBundleName) {
252         return getLogger(key);
253     }
254
255     public String JavaDoc getResourceBundleName() {
256         return resourceBundleName;
257     }
258
259     public void setResourceBundleName(String JavaDoc rbn) {
260         resourceBundleName = rbn;
261     }
262
263     public Logger[] getLoggers() {
264         return (TopicalLogger[]) loggers.values().toArray(
265                 new TopicalLogger[loggers.size()]);
266     }
267     public String JavaDoc getTopicPrefix() {
268         return null;
269     }
270     
271
272     // IMPLEMENTATION OF THE HandlerFactory INTERFACE //
273
//------------------------------------------------//
274

275     public Handler createHandler(String JavaDoc hn, String JavaDoc handlertype) {
276         Handler res = (Handler) handlers.get(hn);
277         if (res != null) {
278             return null;
279         }
280         res = new BasicHandler(hn, handlertype);
281         handlers.put(hn, res);
282         return res;
283     }
284
285     public Handler[] getHandlers() {
286         return (Handler[]) handlers.values().toArray(new Handler[0]);
287     }
288
289     public Handler getHandler(String JavaDoc hn) {
290         return (Handler) handlers.get(hn);
291     }
292
293     public Handler removeHandler(String JavaDoc hn) {
294         return (Handler) handlers.remove(hn);
295     }
296 }
297
Popular Tags