KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > provider > lib > LoggerProviderImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2003 USTL - LIFL - GOAL
5 Contact: openccm-team@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Offroy.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.monolog.provider.lib;
28
29 import java.io.FileInputStream JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 import org.objectweb.util.monolog.api.BasicLevel;
34 import org.objectweb.util.monolog.api.HandlerFactory;
35 import org.objectweb.util.monolog.api.Level;
36 import org.objectweb.util.monolog.api.LevelFactory;
37 import org.objectweb.util.monolog.api.Logger;
38 import org.objectweb.util.monolog.api.LoggerFactory;
39 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
40 import org.objectweb.util.monolog.provider.api.LoggerProvider;
41 import org.objectweb.util.monolog.provider.lib.topics.BundleTopics;
42
43
44 /**
45  * This class provides utility method for using Monolog
46  * This class is actually a wrapper on the unique loggerFactory of the current JVM
47  *
48  * @author Jerome OFFROY.
49  */

50 public class LoggerProviderImpl implements LoggerProvider {
51     /**
52      * default config file name for monolog configuration
53      */

54     private String JavaDoc configFile = "monolog.CCM.properties";
55
56     /**
57      * use or not classpath to find file named configFile
58      */

59     private boolean usecp = false;
60
61     /**
62      * The Singleton Local LoggerFactory
63      */

64     private LoggerFactory lf = null;
65
66     /**
67      * Properties used for Logging system
68      */

69     private Properties JavaDoc props = null;
70
71     /**
72      * constructor
73      */

74     public LoggerProviderImpl() {
75     }
76
77     /**
78      * Configure Logger for OpenCCM modules
79      * This is called only from the ComponentServer.
80      */

81     public void configure(String JavaDoc file, boolean useclasspath) {
82         configFile = file;
83         usecp = useclasspath;
84         LoggerFactory lf = getLoggerFactory();
85     }
86
87     /**
88      * It returns the unique LoggerFactory used in OpenCCM.
89      * initialize it if not already done.
90      */

91     public synchronized LoggerFactory getLoggerFactory() {
92         if (lf == null) {
93             try {
94                 // load properties from config file
95
props = new Properties JavaDoc();
96                 if (usecp) {
97                     props.load(ClassLoader.getSystemResourceAsStream(configFile));
98                 } else {
99                     props.load(new FileInputStream JavaDoc(configFile));
100                 }
101                 // Instanciate the LoggerFactory
102
String JavaDoc b = props.getProperty("log.config.classname", null);
103                 if (b == null) {
104                     System.err.println("Malformed configuration log file: log.config.classname not available");
105                     return null;
106                 }
107                 lf = (LoggerFactory) Class.forName(b).newInstance();
108                 // Configure the LoggerFactory with the properties
109
PropertiesConfAccess.load(props, lf, (HandlerFactory) lf, (LevelFactory) lf);
110             } catch (Exception JavaDoc e) {
111                 System.err.println("Cannot get LoggerFactory:" + e);
112                 e.printStackTrace();
113             }
114         }
115         return lf;
116     }
117
118     /**
119      * Shortcut that returns the LevelFactory
120      */

121     public LevelFactory getLevelFactory() {
122         return (LevelFactory) getLoggerFactory();
123     }
124
125     /**
126      * Shortcut to get the Logger by its topic name.
127      * @param topic is the topic of the returned logger
128      * @return always a logger instance (never null value).
129      */

130     public Logger getLogger(String JavaDoc topic) {
131         return getLoggerFactory().getLogger(topic);
132     }
133
134     /**
135      * @see org.objectweb.util.monolog.provider.api.LoggerProvider#getLogger(String, Object)
136      */

137     public Logger getLogger(String JavaDoc topic, Class JavaDoc caller) {
138         String JavaDoc preTopic = TopicProviderSingleton.getTopicProvider().getTopic(caller);
139         getLogger(BundleTopics.getPreTopic("PRETOPIC_OPENCCM_DEFAULT")).log(BasicLevel.DEBUG,
140             "The Topic of " + caller + " is : " + preTopic);
141         return getLoggerFactory().getLogger(preTopic);
142     }
143
144     /**
145      * @see org.objectweb.util.monolog.provider.api.LoggerProvider#createLogger(String, Object, String[])
146      */

147     public Logger createLogger(String JavaDoc topic, Class JavaDoc caller, String JavaDoc[] description) {
148         TopicProviderSingleton.createTopic(topic, caller, description);
149         return getLogger(topic, caller);
150     }
151
152
153     /**
154      * get Topics defined in Monolog.
155      * Assumes that all Loggers are TopicalLoggers.
156      */

157     public String JavaDoc[] getTopics() {
158         Logger[] logs = getLoggerFactory().getLoggers();
159         // put names in alphabetical order
160
TreeSet JavaDoc tset = new TreeSet JavaDoc();
161         for (int i = 0; i < logs.length; i++) {
162             tset.add(logs[i].getName());
163         }
164         return (String JavaDoc[]) tset.toArray(new String JavaDoc[0]);
165     }
166
167     /**
168      * get Topic Level
169      */

170     public String JavaDoc getTopicLevel(String JavaDoc topic) {
171         String JavaDoc ret = null;
172         Logger log = getLogger(topic);
173         Level lev = log.getCurrentLevel();
174         return lev.getName();
175     }
176
177     /**
178      * set Topic Level
179      */

180     public void setTopicLevel(String JavaDoc topic, String JavaDoc l) {
181         Logger log = getLogger(topic);
182         Level lev = getLevelFactory().getLevel(l);
183         // must check null (bug monolog)
184
if (lev != null) {
185             log.setLevel(lev);
186         } else {
187            
188         }
189     }
190
191     /**
192      * returns list of properties for logging system
193      */

194     public Properties JavaDoc getProperties() {
195         if (props == null) {
196             getLoggerFactory();
197         }
198         return props;
199     }
200
201 }
202
Popular Tags