KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > logging > CommonLoggingMonologLogger


1 /*
2  *
3  * @ author Igor Smirnov
4  */

5
6
7 package org.enhydra.logging;
8
9 import java.io.FileInputStream JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Locale JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import javax.management.MBeanServerConnection JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19 import javax.management.remote.JMXConnector JavaDoc;
20 import javax.management.remote.JMXConnectorFactory JavaDoc;
21 import javax.management.remote.JMXServiceURL JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.objectweb.util.monolog.Monolog;
25 import org.objectweb.util.monolog.api.BasicLevel;
26 import org.objectweb.util.monolog.api.Logger;
27 import org.objectweb.util.monolog.api.LoggerFactory;
28 import org.objectweb.util.monolog.wrapper.common.Configurable;
29
30 import com.lutris.util.ConfigException;
31
32
33
34
35 /**
36  * <p>Implementation of {@link Log} that maps directly to a Monolog
37  * <strong>Logger</strong>. Initial configuration of the corresponding
38  * Logger instances should be done in the usual manner, as outlined in
39  * the Monolog documentation.</p>
40  *
41  *
42  * @author Igor Smirnov
43  */

44 public final class CommonLoggingMonologLogger implements Log {
45
46
47     // ------------------------------------------------------------- Attributes
48

49     /** The fully qualified name of the Monolog class. */
50     private static final String JavaDoc FQCN = CommonLoggingMonologLogger.class.getName();
51     
52     /** Log to this logger */
53     private org.objectweb.util.monolog.api.LoggerFactory lf;
54     private Logger logger = null;
55     private static ResourceBundle JavaDoc rb = null;
56     public static String JavaDoc PROPERTY_FILE;
57     static ObjectName JavaDoc objectName;
58
59
60     // ------------------------------------------------------------ Constructor
61

62     public CommonLoggingMonologLogger() throws ConfigException {
63       try {
64
65               configure();
66               this.logger = lf.getLogger("MonologFactory");
67
68             }catch (ConfigException ex) {
69               throw new ConfigException("Cannot configure logger. Logger not initialized.");
70             }
71       this.logger = lf.getLogger("CommonLoggingMonologAdapter");
72     }
73
74
75     /**
76      * Base constructor
77      */

78     public CommonLoggingMonologLogger(String JavaDoc name) throws ConfigException {
79       try {
80               //System.out.println("****3CommonLoggingMonologLogger");
81
configure();
82               this.logger = lf.getLogger("MonologFactory");
83             }catch (ConfigException ex) {
84               throw new ConfigException("Cannot configure logger. Logger not initialized.");
85             }
86       this.logger = lf.getLogger("CommonLoggingMonologAdapter");
87     }
88
89     /** For use with a Monolog factory
90      */

91     public CommonLoggingMonologLogger(Logger logger ) {
92         this.logger=logger;
93     }
94
95       
96
97
98     // ---------------------------------------------------------- Implmentation
99

100
101     /**
102      * Log a message to the CommonLoggingMonologLogger with <code>TRACE</code> priority.
103      * Currently logs to <code>DEBUG</code> level in CommonLoggingMonologLogger.
104      */

105     public void trace(Object JavaDoc message) {
106         logger.log(BasicLevel.LEVEL_DEBUG, message);
107     }
108
109
110     /**
111      * Log an error to the CommonLoggingMonologLogger Logger with <code>TRACE</code> priority.
112      * Currently logs to <code>DEBUG</code> level in CommonLoggingMonologLogger.
113      */

114     public void trace(Object JavaDoc message, Throwable JavaDoc t) {
115         logger.log(BasicLevel.LEVEL_DEBUG, message, t );
116     }
117
118
119     /**
120      * Log a message to the CommonLoggingMonologLogger Logger with <code>DEBUG</code> priority.
121      */

122     public void debug(Object JavaDoc message) {
123         logger.log(BasicLevel.LEVEL_DEBUG, message);
124     }
125
126     /**
127      * Log an error to the CommonLoggingMonologLogger Logger with <code>DEBUG</code> priority.
128      */

129     public void debug(Object JavaDoc message, Throwable JavaDoc t) {
130         logger.log(BasicLevel.LEVEL_DEBUG,message, t );
131     }
132
133
134     /**
135      * Log a message to the CommonLoggingMonologLogger Logger with <code>INFO</code> priority.
136      */

137     public void info(Object JavaDoc message) {
138         logger.log(BasicLevel.LEVEL_INFO, message);
139     }
140
141
142     /**
143      * Log an error to the CommonLoggingMonologLogger Logger with <code>INFO</code> priority.
144      */

145     public void info(Object JavaDoc message, Throwable JavaDoc t) {
146         logger.log(BasicLevel.LEVEL_INFO, message, t );
147     }
148
149
150     /**
151      * Log a message to the CommonLoggingMonologLogger Logger with <code>WARN</code> priority.
152      */

153     public void warn(Object JavaDoc message) {
154         logger.log(BasicLevel.LEVEL_WARN, message );
155     }
156
157
158     /**
159      * Log an error to the CommonLoggingMonologLogger Logger with <code>WARN</code> priority.
160      */

161     public void warn(Object JavaDoc message, Throwable JavaDoc t) {
162         logger.log(BasicLevel.LEVEL_WARN, message, t );
163     }
164
165
166     /**
167      * Log a message to the CommonLoggingMonologLogger Logger with <code>ERROR</code> priority.
168      */

169     public void error(Object JavaDoc message) {
170         logger.log(BasicLevel.LEVEL_ERROR, message );
171     }
172
173
174     /**
175      * Log an error to the CommonLoggingMonologLogger Logger with <code>ERROR</code> priority.
176      */

177     public void error(Object JavaDoc message, Throwable JavaDoc t) {
178         logger.log(BasicLevel.LEVEL_ERROR, message, t );
179     }
180
181
182     /**
183      * Log a message to the CommonLoggingMonologLogger Logger with <code>FATAL</code> priority.
184      */

185     public void fatal(Object JavaDoc message) {
186         logger.log(BasicLevel.LEVEL_FATAL, message );
187     }
188
189
190     /**
191      * Log an error to the CommonLoggingMonologLogger Logger with <code>FATAL</code> priority.
192      */

193     public void fatal(Object JavaDoc message, Throwable JavaDoc t) {
194         logger.log(BasicLevel.LEVEL_FATAL, message, t );
195     }
196
197
198     /**
199      * Return the native Logger instance we are using.
200      */

201     public Logger getLogger() {
202       
203         return (this.logger);
204     }
205
206
207     /**
208      * Check whether the CommonLoggingMonologLogger Logger used is enabled for <code>DEBUG</code> priority.
209      */

210     public boolean isDebugEnabled() {
211        return logger.isLoggable(BasicLevel.LEVEL_DEBUG);
212     }
213
214
215      /**
216      * Check whether the CommonLoggingMonologLogger Logger used is enabled for <code>ERROR</code> priority.
217      */

218     public boolean isErrorEnabled() {
219       return logger.isLoggable(BasicLevel.LEVEL_ERROR);
220     }
221
222
223     /**
224      * Check whether the CommonLoggingMonologLogger Logger used is enabled for <code>FATAL</code> priority.
225      */

226     public boolean isFatalEnabled() {
227       return logger.isLoggable(BasicLevel.LEVEL_FATAL);
228     }
229
230
231     /**
232      * Check whether the CommonLoggingMonologLogger Logger used is enabled for <code>INFO</code> priority.
233      */

234     public boolean isInfoEnabled() {
235       return logger.isLoggable(BasicLevel.LEVEL_INFO);
236     }
237
238
239     /**
240      * Check whether the CommonLoggingMonologLogger Logger used is enabled for <code>TRACE</code> priority.
241      * For CommonLoggingMonologLogger, this returns the value of <code>isDebugEnabled()</code>
242      */

243     public boolean isTraceEnabled() {
244        return logger.isLoggable(BasicLevel.LEVEL_DEBUG);
245     }
246
247     /**
248      * Check whether the CommonLoggingMonologLogger Logger used is enabled for <code>WARN</code> priority.
249      */

250     public boolean isWarnEnabled() {
251       return logger.isLoggable(BasicLevel.LEVEL_WARN);
252     }
253     
254     
255     /**
256      * Configure the logger. All current configuration is discarded.
257      * This is a simplistic initial implementation that just allows
258      * directing to a single log file or stderr on a level basis.
259      * A more complete interface will be provided in the future.
260      *
261      * @param logFile The log file to write to.
262      * @param fileLevels List of levels that will be written to the file.
263      * @param stderrLevels List of levels that will be written to stderr.
264      * The same level may appear in both lists.
265      * @exception java.io.IOException If an error occurs opening the log file.
266      */

267     public synchronized void configure(String JavaDoc monologConfFile) {
268       
269         if (monologConfFile==null || monologConfFile.length() == 0) {
270 // throw new ConfigException(
271
// "impossible to configure monolog without configuration file");
272
}
273         // Load the configuration in a Properties object
274
String JavaDoc b = null;
275       Properties JavaDoc p = new Properties JavaDoc();
276         try {
277
278            p.load(new FileInputStream JavaDoc(monologConfFile));
279         // Search the class name of the LoggerFactory which must be instanciated
280
b = p.getProperty("log.config.classname", null);
281            if (b == null) {
282     // throw new ConfigException("Malformed configuration log file:"
283
// + " log.config.classname not available");
284
}
285
286            // Instanciate the LoggerFactory
287
lf = (LoggerFactory) Class.forName(b).newInstance();
288            // Configure with the properties
289
p.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY);
290          p.put(Configurable.LOG_CONFIGURATION_FILE, monologConfFile);
291 // p.put(Configurable.LOG_CONFIGURATION_FILE_USE_CLASSPATH, "true");
292
((Configurable) lf).configure(p);
293
294 // PropertiesConfAccess.load(p, lf, (HandlerFactory) lf, (LevelFactory) lf);
295
}
296         catch (Exception JavaDoc e) {
297 // throw new ConfigException("Malformed configuration log file:"
298
// + " log.config.classname not available");
299
}
300
301   }
302
303     /**
304      * Default configuration of the logger. A JMX search for logger configuration file name.
305      * This file must be present in the classpath
306      */

307   public synchronized void configure()
308         throws ConfigException {
309           String JavaDoc b = null;
310           String JavaDoc propkey;
311           String JavaDoc propvalue;
312           Properties JavaDoc props = new Properties JavaDoc();
313              
314      try{
315          //findMBeanServer();
316
findObjectName();
317                System.out.println ("***PROSAO findObjectName");
318          if (objectName != null)
319             PROPERTY_FILE = objectName.getKeyProperty("fname");
320          // Search the classpath for the logger configuration file
321
rb = ResourceBundle.getBundle(PROPERTY_FILE,new Locale JavaDoc("en","US"), ClassLoader.getSystemClassLoader());
322       
323      Enumeration JavaDoc enumeration = rb.getKeys();
324      
325      while (enumeration.hasMoreElements())
326      {
327
328          propkey = (String JavaDoc) enumeration.nextElement();
329          propvalue = rb.getString(propkey);
330          props.setProperty(propkey, propvalue);
331      }
332          
333       } catch (Exception JavaDoc e){
334         throw new ConfigException("Logger configuration file could not be found: logger could not be initialized!");
335     }
336
337         try {
338
339         // Search the class name of the LoggerFactory which must be instanciated
340
b = props.getProperty("log.config.classname", null);
341            if (b == null) {
342                throw new ConfigException("Malformed configuration log file:"
343                    + " log.config.classname not available");
344            }
345
346            // Instanciate the LoggerFactory
347

348          props.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY);
349          lf = Monolog.getMonologFactory(props);
350          this.logger = lf.getLogger("MonologFileLogger");
351
352           }
353         catch (Exception JavaDoc e) {
354            throw new ConfigException("Malformed configuration log file:"
355                    + " log.config.classname not available");
356        }
357     
358 }
359
360
361   /**
362    * finds the ObjectName which correspondes to the MBean of the jonas logger
363    */

364   private void findObjectName () throws Exception JavaDoc {
365     JMXConnector JavaDoc connector = null;
366     try {
367      
368       String JavaDoc serviceName = "jonas";
369       //System.out.println ("MONOLOG FILE Name of the service is 1 " + serviceName);
370

371       // The name of the started jonas server (service name)
372
serviceName = System.getProperty("jonas.name");
373       if ( serviceName == null) {
374         serviceName = "jonas";
375       }
376       
377       System.out.println ("findObjectName servicename: " + serviceName);
378
379       // The address of the connector server
380
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:rmi://localhost/jndi/jrmpconnector_" + serviceName);
381
382       // Connect a JSR 160 JMXConnector to the server side
383
connector = JMXConnectorFactory.connect(url);
384
385       // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
386
// connector server is bound to
387
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
388
389         objectName = new ObjectName JavaDoc(serviceName + ":type=service,name=log,*");
390         
391       Set JavaDoc mBeans = connection.queryNames(objectName, null);
392       int l = mBeans.size();
393       if ((l) > 1) {
394         throw new Exception JavaDoc("MBean set size not equal 1: " + l);
395       }else if (l == 0){
396         objectName = null;
397         if (connector!=null){
398             try {
399                 connector.close();
400             } catch (Exception JavaDoc exept){}
401         }
402         return;
403         }
404       Iterator JavaDoc i = mBeans.iterator();
405       objectName = (ObjectName JavaDoc)i.next();
406     } catch (Exception JavaDoc e) {
407       throw new Exception JavaDoc(e);
408     }finally {
409         if (connector!=null){
410             try {
411                 connector.close();
412             } catch (Exception JavaDoc exept){}
413         }
414     }
415     return;
416   }
417
418
419   }
420
Popular Tags