KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > lib > Log


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2004 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
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 (at your option) 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 USA
20  *
21  * Initial developers: M. Alia, S. Chassande-Barrioz, A. Lefebvre
22  */

23 package org.objectweb.medor.lib;
24
25 import org.objectweb.util.monolog.api.LoggerFactory;
26 import org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl;
27 import org.objectweb.util.monolog.api.HandlerFactory;
28 import org.objectweb.util.monolog.api.LevelFactory;
29 import org.objectweb.util.monolog.api.Logger;
30 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
31 import org.objectweb.util.monolog.Monolog;
32
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.FileNotFoundException JavaDoc;
37 import java.util.Properties JavaDoc;
38
39 /**
40  * This class references the logger factory used in MEDOR. A default logger
41  * factory is instanciated. It is based on a printwriter implementation which by
42  * default logs anything.
43  *
44  * @author S.Chassande-Barrioz
45  */

46 public class Log
47         implements LoggerFactory {
48
49     public static final boolean DEBUG = false;
50
51     public static final String JavaDoc MEDOR_PREFIX = "org.objectweb.medor";
52
53     public static LoggerFactory loggerFactory = new LoggerImpl();
54
55     /**
56      * Default name of the logger factory
57      */

58     public final static String JavaDoc DEFAULT_LOGGER_FACTORY =
59             "org.objectweb.util.monolog.wrapper.printwriter.LoggerImpl";
60
61     public static LoggerFactory getLoggerFactory(String JavaDoc lfClassName)
62             throws Exception JavaDoc {
63         if (lfClassName == null)
64             return (LoggerFactory)
65                     Class.forName(DEFAULT_LOGGER_FACTORY).newInstance();
66         else
67             return (LoggerFactory) Class.forName(lfClassName).newInstance();
68     }
69
70     /**
71      * Gets a new <code>LoggerFactory</code>
72      *
73      * @param propFileName properties containing <code>logger.factory</code>
74      * and <code>log.conf.file</code>
75      * @return the build LoggerFactory
76      */

77     public static LoggerFactory loadLoggerFactory(String JavaDoc propFileName)
78             throws Exception JavaDoc {
79         if (propFileName == null)
80             return (LoggerFactory)
81                     Class.forName(DEFAULT_LOGGER_FACTORY).newInstance();
82         // lookup the properties file in the file system
83
File JavaDoc f = new File JavaDoc(propFileName);
84         if (f.exists()) {
85             Properties JavaDoc p = new Properties JavaDoc();
86             p.load(new FileInputStream JavaDoc(propFileName));
87             return loadLoggerFactory(p);
88         }
89         //lookup the properties file in the classpath
90
InputStream JavaDoc is = ClassLoader.getSystemResourceAsStream(propFileName);
91         if (is != null) {
92             Properties JavaDoc p = new Properties JavaDoc();
93             p.load(is);
94             return loadLoggerFactory(p);
95         }
96         throw new FileNotFoundException JavaDoc(propFileName + " was not found");
97     }
98
99     /**
100      * Gets a new <code>LoggerFactory</code>
101      *
102      * @param prop properties containing <code>logger.factory</code> and <code>log.conf.file</code>
103      * @return the build LoggerFactory
104      */

105     public static LoggerFactory loadLoggerFactory(Properties JavaDoc prop) throws Exception JavaDoc {
106         if (prop == null)
107             return (LoggerFactory)
108                     Class.forName(DEFAULT_LOGGER_FACTORY).newInstance();
109
110         // Creates the factory if needed
111
String JavaDoc lfn = prop.getProperty("log.config.classname", DEFAULT_LOGGER_FACTORY);
112         LoggerFactory factory = (LoggerFactory) Class.forName(lfn).newInstance();
113
114         if (factory instanceof HandlerFactory && factory instanceof LevelFactory) {
115             // Configures the factory
116
PropertiesConfAccess.load(prop,
117                     factory,
118                     (HandlerFactory) factory,
119                     (LevelFactory) factory);
120         }
121         return factory;
122     }
123
124     public static LoggerFactory getLoggerFactory() {
125         if (lastLF == null || lastLF == Monolog.getDefaultMonologFactory()) {
126             lastLF = Monolog.initialize();
127         }
128         return lastLF;
129     }
130
131     private static LoggerFactory lastLF = null;
132
133     private LoggerFactory delegate = null;
134     private String JavaDoc pfn = null;
135
136     // IMPLEMENTATION OF THE LogAttributes INTERFACE //
137
//-----------------------------------------------//
138

139     public String JavaDoc getPropertiesFileName() {
140         return pfn;
141     }
142
143     public void setPropertiesFileName(String JavaDoc propfn) {
144         if (pfn != null && pfn.equals(propfn))
145             return;
146         pfn = propfn;
147         try {
148             delegate = Log.loadLoggerFactory(pfn);
149         } catch (Exception JavaDoc e) {
150             e.printStackTrace();
151             try {
152                 delegate = Log.loadLoggerFactory((String JavaDoc) null);
153             } catch (Exception JavaDoc e1) {
154                 e1.printStackTrace();
155             }
156         }
157         lastLF = this;
158     }
159
160     // IMPLEMENTATION OF THE LoggerFactory INTERFACE //
161
//-----------------------------------------------//
162

163     public Logger getLogger(String JavaDoc s) {
164         return delegate.getLogger(s);
165     }
166
167     public Logger getLogger(String JavaDoc s, String JavaDoc s1) {
168         return delegate.getLogger(s1);
169     }
170
171     public String JavaDoc getResourceBundleName() {
172         return delegate.getResourceBundleName();
173     }
174
175     public void setResourceBundleName(String JavaDoc s) {
176         delegate.setResourceBundleName(s);
177     }
178
179     public Logger[] getLoggers() {
180         return delegate.getLoggers();
181     }
182 }
183
Popular Tags