KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > logger > LoggerBase


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.catalina.logger;
30
31
32 import java.beans.PropertyChangeSupport JavaDoc;
33 import java.beans.PropertyChangeListener JavaDoc;
34 import java.io.CharArrayWriter JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.util.Set JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.management.MBeanServer JavaDoc;
40 import javax.management.MBeanRegistration JavaDoc;
41 import org.apache.catalina.Container;
42 import org.apache.catalina.Logger;
43 import org.apache.catalina.Lifecycle;
44 import org.apache.catalina.LifecycleEvent;
45 import org.apache.catalina.LifecycleException;
46 import org.apache.catalina.LifecycleListener;
47 import org.apache.catalina.core.StandardEngine;
48 import org.apache.catalina.core.StandardHost;
49 import org.apache.catalina.core.StandardContext;
50 import org.apache.catalina.util.LifecycleSupport;
51 import com.sun.org.apache.commons.logging.Log;
52 import com.sun.org.apache.commons.logging.LogFactory;
53 import com.sun.org.apache.commons.modeler.Registry;
54
55
56 /**
57  * Convenience base class for <b>Logger</b> implementations. The only
58  * method that must be implemented is <code>log(String msg)</code>, plus
59  * any property setting and lifecycle methods required for configuration.
60  *
61  * @author Craig R. McClanahan
62  * @version $Revision: 1.3 $ $Date: 2006/03/12 01:27:02 $
63  */

64
65 public class LoggerBase
66     implements Lifecycle, Logger, MBeanRegistration JavaDoc
67  {
68     private static Log log = LogFactory.getLog(LoggerBase.class);
69     
70     // ----------------------------------------------------- Instance Variables
71

72
73     /**
74      * The Container with which this Logger has been associated.
75      */

76     protected Container container = null;
77
78
79     /**
80      * The debugging detail level for this component.
81      */

82     protected int debug = 0;
83
84     
85     /**
86      * The descriptive information about this implementation.
87      */

88     protected static final String JavaDoc info =
89         "org.apache.catalina.logger.LoggerBase/1.0";
90
91
92     /**
93      * The lifecycle event support for this component.
94      */

95     protected LifecycleSupport lifecycle = new LifecycleSupport(this);
96
97
98     /**
99      * The property change support for this component.
100      */

101     protected PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
102
103
104     /**
105      * The verbosity level for above which log messages may be filtered.
106      */

107     protected int verbosity = ERROR;
108
109
110     // ------------------------------------------------------------- Properties
111

112
113     /**
114      * Return the Container with which this Logger has been associated.
115      */

116     public Container getContainer() {
117
118         return (container);
119
120     }
121
122
123     /**
124      * Set the Container with which this Logger has been associated.
125      *
126      * @param container The associated Container
127      */

128     public void setContainer(Container container) {
129
130         Container oldContainer = this.container;
131         this.container = container;
132         support.firePropertyChange("container", oldContainer, this.container);
133
134     }
135
136
137     /**
138      * Return the debugging detail level for this component.
139      */

140     public int getDebug() {
141
142         return (this.debug);
143
144     }
145
146
147     /**
148      * Set the debugging detail level for this component.
149      *
150      * @param debug The new debugging detail level
151      */

152     public void setDebug(int debug) {
153
154         this.debug = debug;
155
156     }
157
158
159     /**
160      * Return descriptive information about this Logger implementation and
161      * the corresponding version number, in the format
162      * <code>&lt;description&gt;/&lt;version&gt;</code>.
163      */

164     public String JavaDoc getInfo() {
165
166         return (info);
167
168     }
169
170
171     /**
172      * Return the verbosity level of this logger. Messages logged with a
173      * higher verbosity than this level will be silently ignored.
174      */

175     public int getVerbosity() {
176
177         return (this.verbosity);
178
179     }
180
181
182     /**
183      * Set the verbosity level of this logger. Messages logged with a
184      * higher verbosity than this level will be silently ignored.
185      *
186      * @param verbosity The new verbosity level
187      */

188     public void setVerbosity(int verbosity) {
189
190         this.verbosity = verbosity;
191
192     }
193
194
195     /**
196      * Set the verbosity level of this logger. Messages logged with a
197      * higher verbosity than this level will be silently ignored.
198      *
199      * @param verbosityLevel The new verbosity level, as a string
200      */

201     public void setVerbosityLevel(String JavaDoc verbosity) {
202
203         if ("FATAL".equalsIgnoreCase(verbosity))
204             this.verbosity = FATAL;
205         else if ("ERROR".equalsIgnoreCase(verbosity))
206             this.verbosity = ERROR;
207         else if ("WARNING".equalsIgnoreCase(verbosity))
208             this.verbosity = WARNING;
209         else if ("INFORMATION".equalsIgnoreCase(verbosity))
210             this.verbosity = INFORMATION;
211         else if ("DEBUG".equalsIgnoreCase(verbosity))
212             this.verbosity = DEBUG;
213
214     }
215
216
217     // --------------------------------------------------------- Public Methods
218

219
220     /**
221      * Add a property change listener to this component.
222      *
223      * @param listener The listener to add
224      */

225     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
226
227         support.addPropertyChangeListener(listener);
228
229     }
230
231
232     /**
233      * Writes the specified message to a servlet log file, usually an event
234      * log. The name and type of the servlet log is specific to the
235      * servlet container. This message will be logged unconditionally.
236      *
237      * @param message A <code>String</code> specifying the message to be
238      * written to the log file
239      */

240     public void log(String JavaDoc msg) {
241         log.debug(msg);
242     }
243
244
245     /**
246      * Writes the specified exception, and message, to a servlet log file.
247      * The implementation of this method should call
248      * <code>log(msg, exception)</code> instead. This method is deprecated
249      * in the ServletContext interface, but not deprecated here to avoid
250      * many useless compiler warnings. This message will be logged
251      * unconditionally.
252      *
253      * @param exception An <code>Exception</code> to be reported
254      * @param msg The associated message string
255      */

256     public void log(Exception JavaDoc exception, String JavaDoc msg) {
257
258         log(msg, exception);
259
260     }
261
262
263     /**
264      * Writes an explanatory message and a stack trace for a given
265      * <code>Throwable</code> exception to the servlet log file. The name
266      * and type of the servlet log file is specific to the servlet container,
267      * usually an event log. This message will be logged unconditionally.
268      *
269      * @param msg A <code>String</code> that describes the error or
270      * exception
271      * @param throwable The <code>Throwable</code> error or exception
272      */

273     public void log(String JavaDoc msg, Throwable JavaDoc throwable) {
274
275         CharArrayWriter JavaDoc buf = new CharArrayWriter JavaDoc();
276         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(buf);
277         writer.println(msg);
278         throwable.printStackTrace(writer);
279         Throwable JavaDoc rootCause = null;
280         if (throwable instanceof LifecycleException)
281             rootCause = ((LifecycleException) throwable).getThrowable();
282         else if (throwable instanceof ServletException JavaDoc)
283             rootCause = ((ServletException JavaDoc) throwable).getRootCause();
284         if (rootCause != null) {
285             writer.println("----- Root Cause -----");
286             rootCause.printStackTrace(writer);
287         }
288         log(buf.toString());
289
290     }
291
292
293     /**
294      * Writes the specified message to the servlet log file, usually an event
295      * log, if the logger is set to a verbosity level equal to or higher than
296      * the specified value for this message.
297      *
298      * @param message A <code>String</code> specifying the message to be
299      * written to the log file
300      * @param verbosity Verbosity level of this message
301      */

302     public void log(String JavaDoc message, int verbosity) {
303
304         if (this.verbosity >= verbosity)
305             log(message);
306
307     }
308
309
310     /**
311      * Writes the specified message and exception to the servlet log file,
312      * usually an event log, if the logger is set to a verbosity level equal
313      * to or higher than the specified value for this message.
314      *
315      * @param message A <code>String</code> that describes the error or
316      * exception
317      * @param throwable The <code>Throwable</code> error or exception
318      * @param verbosity Verbosity level of this message
319      */

320     public void log(String JavaDoc message, Throwable JavaDoc throwable, int verbosity) {
321
322         if (this.verbosity >= verbosity)
323             log(message, throwable);
324
325     }
326
327
328     /**
329      * Remove a property change listener from this component.
330      *
331      * @param listener The listener to remove
332      */

333     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
334
335         support.removePropertyChangeListener(listener);
336
337     }
338                                                                                
339     protected String JavaDoc domain;
340     protected String JavaDoc host;
341     protected String JavaDoc path;
342     protected ObjectName JavaDoc oname;
343     protected ObjectName JavaDoc controller;
344     protected MBeanServer JavaDoc mserver;
345
346     public ObjectName JavaDoc getController() {
347         return controller;
348     }
349
350     public void setController(ObjectName JavaDoc controller) {
351         this.controller = controller;
352     }
353
354     public ObjectName JavaDoc getObjectName() {
355         return oname;
356     }
357           
358     public String JavaDoc getDomain() {
359         return domain;
360     }
361     
362     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server,
363                                   ObjectName JavaDoc name) throws Exception JavaDoc {
364         oname=name;
365         mserver=server;
366         // FIXME null pointer exception
367
if (name == null) {
368             return null;
369         }
370         domain=name.getDomain();
371         host=name.getKeyProperty("host");
372         path=name.getKeyProperty("path");
373         log("preRegister with "+name);
374         if( container== null ) {
375             // Register with the parent
376
try {
377                 ObjectName JavaDoc cname=null;
378                 if( host == null ) {
379                     // global
380
cname=new ObjectName JavaDoc(domain +":type=Engine");
381                 } else if( path==null ) {
382                     cname=new ObjectName JavaDoc(domain +
383                             ":type=Host,host=" + host);
384                 } else {
385                     cname=new ObjectName JavaDoc(domain +":j2eeType=WebModule,name=//" +
386                             host + "/" + path);
387                 }
388                 log.debug("Register with " + cname);
389                 mserver.invoke(cname, "setLogger", new Object JavaDoc[] {this},
390                         new String JavaDoc[] {"org.apache.catalina.Logger"});
391             } catch (Exception JavaDoc e) {
392                 e.printStackTrace(); //To change body of catch statement use Options | File Templates.
393
}
394         }
395                 
396         return name;
397     }
398
399     public void postRegister(Boolean JavaDoc registrationDone) {
400     }
401
402     public void preDeregister() throws Exception JavaDoc {
403     }
404
405     public void postDeregister() {
406     }
407
408     public void init() {
409         
410     }
411     
412     public void destroy() {
413         
414     }
415     
416     public ObjectName JavaDoc createObjectName() {
417         log("createObjectName with "+container);
418         // register
419
try {
420             StandardEngine engine=null;
421             String JavaDoc suffix="";
422             if( container instanceof StandardEngine ) {
423                 engine=(StandardEngine)container;
424             } else if( container instanceof StandardHost ) {
425                 engine=(StandardEngine)container.getParent();
426                 suffix=",host=" + container.getName();
427             } else if( container instanceof StandardContext ) {
428                 String JavaDoc path = ((StandardContext)container).getPath();
429                 // add "/" to avoid MalformedObjectName Exception
430
if (path.equals("")) {
431                     path = "/";
432                 }
433                 engine=(StandardEngine)container.getParent().getParent();
434                 suffix= ",path=" + path + ",host=" +
435                         container.getParent().getName();
436             } else {
437                 log.error("Unknown container " + container );
438             }
439             if( engine != null ) {
440                 oname=new ObjectName JavaDoc(engine.getDomain()+ ":type=Logger" + suffix);
441             } else {
442                 log.error("Null engine !! " + container);
443             }
444         } catch (Throwable JavaDoc e) {
445             e.printStackTrace(); //To change body of catch statement use Options | File Templates.
446
}
447         return oname;
448     }
449
450
451    // ------------------------------------------------------ Lifecycle Methods
452

453     /**
454      * Add a lifecycle event listener to this component.
455      *
456      * @param listener The listener to add
457      */

458     public void addLifecycleListener(LifecycleListener listener) {
459
460         lifecycle.addLifecycleListener(listener);
461
462     }
463
464
465     /**
466      * Get the lifecycle listeners associated with this lifecycle. If this
467      * Lifecycle has no listeners registered, a zero-length array is returned.
468      */

469     public LifecycleListener[] findLifecycleListeners() {
470
471         return lifecycle.findLifecycleListeners();
472
473     }
474
475
476     /**
477      * Remove a lifecycle event listener from this component.
478      *
479      * @param listener The listener to add
480      */

481     public void removeLifecycleListener(LifecycleListener listener) {
482
483         lifecycle.removeLifecycleListener(listener);
484
485     }
486
487
488     /**
489      * Prepare for the beginning of active use of the public methods of this
490      * component. This method should be called after <code>configure()</code>,
491      * and before any of the public methods of the component are utilized.
492      *
493      * @exception LifecycleException if this component detects a fatal error
494      * that prevents this component from being used
495      */

496     public void start() throws LifecycleException {
497                                                                 
498         // register this logger
499
if ( getObjectName()==null ) {
500             ObjectName JavaDoc oname = createObjectName();
501             try {
502                 Registry.getRegistry().registerComponent(this, oname, null);
503                 log.debug( "registering logger " + oname );
504             } catch( Exception JavaDoc ex ) {
505                 log.error( "Can't register logger " + oname, ex);
506             }
507         }
508
509     }
510                               
511                               
512     /**
513      * Gracefully terminate the active use of the public methods of this
514      * component. This method should be the last one called on a given
515      * instance of this component.
516      *
517      * @exception LifecycleException if this component detects a fatal error
518      * that needs to be reported
519      */

520     public void stop() throws LifecycleException {
521
522         // unregister this logger
523
if ( getObjectName()!=null ) {
524             ObjectName JavaDoc oname = createObjectName();
525             try {
526                 Registry.getRegistry().unregisterComponent(oname);
527                 log.info( "unregistering logger " + oname );
528             } catch( Exception JavaDoc ex ) {
529                 log.error( "Can't unregister logger " + oname, ex);
530             }
531         }
532     }
533   
534 }
535
Popular Tags