KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > logger > LoggerBase


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

23
24 package com.sun.enterprise.web.logger;
25
26 import java.beans.PropertyChangeSupport JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.io.CharArrayWriter JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import org.apache.catalina.Container;
32 import org.apache.catalina.LifecycleException;
33 import org.apache.catalina.Logger;
34
35 /**
36  * Convenience base class for <b>Logger</b> implementations. The only
37  * method that must be implemented is
38  * <code>write(String msg, int verbosity)</code>, plus any property
39  * setting and lifecycle methods required for configuration.
40  *
41  */

42
43 abstract class LoggerBase implements Logger {
44
45     // ----------------------------------------------------- Instance Variables
46

47     /**
48      * The Container with which this Logger has been associated.
49      */

50     protected Container container = null;
51
52     /**
53      * The descriptive information about this implementation.
54      */

55     protected static final String JavaDoc info =
56         "com.sun.enterprise.web.logger.LoggerBase/1.0";
57
58     /**
59      * The property change support for this component.
60      */

61     protected PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
62
63     // ------------------------------------------------------------- Properties
64

65     /**
66      * Return the Container with which this Logger has been associated.
67      */

68     public Container getContainer() {
69         return (container);
70     }
71
72     /**
73      * Set the Container with which this Logger has been associated.
74      *
75      * @param container The associated Container
76      */

77     public void setContainer(Container container) {
78
79         Container oldContainer = this.container;
80         this.container = container;
81         support.firePropertyChange("container", oldContainer, this.container);
82     }
83
84     /**
85      * Return descriptive information about this Logger implementation and
86      * the corresponding version number, in the format
87      * <code>&lt;description&gt;/&lt;version&gt;</code>.
88      */

89     public String JavaDoc getInfo() {
90         return (info);
91     }
92
93     /**
94      * Logger interface method (ignored)
95      */

96     public int getVerbosity() {
97         //Ignored
98
return -1;
99     }
100
101     /**
102      * Logger interface method (ignored)
103      *
104      * @param verbosity The new verbosity level
105      */

106     public void setVerbosity(int verbosity) {
107         //Ignored
108
}
109
110     // --------------------------------------------------------- Public Methods
111

112     /**
113      * Add a property change listener to this component.
114      *
115      * @param listener The listener to add
116      */

117     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
118         support.addPropertyChangeListener(listener);
119     }
120
121     /**
122      * Writes the specified message to a servlet log file, usually an event
123      * log. The name and type of the servlet log is specific to the
124      * servlet container.
125      *
126      * @param message A <code>String</code> specifying the message to be
127      * written to the log file
128      */

129     public void log(String JavaDoc msg) {
130         write(msg, DEBUG);
131     }
132
133     /**
134      * Writes the specified exception, and message, to a servlet log file.
135      * The implementation of this method should call
136      * <code>log(msg, exception)</code> instead. This method is deprecated
137      * in the ServletContext interface, but not deprecated here to avoid
138      * many useless compiler warnings. This message will be logged
139      * unconditionally.
140      *
141      * @param exception An <code>Exception</code> to be reported
142      * @param msg The associated message string
143      */

144     public void log(Exception JavaDoc exception, String JavaDoc msg) {
145         log(msg, exception);
146     }
147
148     /**
149      * Writes an explanatory message and a stack trace for a given
150      * <code>Throwable</code> exception to the servlet log file. The name
151      * and type of the servlet log file is specific to the servlet container,
152      * usually an event log. This message will be logged unconditionally.
153      *
154      * @param msg A <code>String</code> that describes the error or
155      * exception
156      * @param throwable The <code>Throwable</code> error or exception
157      */

158     public void log(String JavaDoc msg, Throwable JavaDoc throwable) {
159         write(msg, throwable, ERROR);
160     }
161
162     /**
163      * Writes the specified message to the servlet log file, usually an event
164      * log, if the logger is set to a verbosity level equal to or higher than
165      * the specified value for this message.
166      *
167      * @param message A <code>String</code> specifying the message to be
168      * written to the log file
169      * @param verbosity Verbosity level of this message
170      */

171     public void log(String JavaDoc message, int verbosity) {
172         write(message, verbosity);
173     }
174
175     /**
176      * Writes the specified message and exception to the servlet log file,
177      * usually an event log, if the logger is set to a verbosity level equal
178      * to or higher than the specified value for this message.
179      *
180      * @param message A <code>String</code> that describes the error or
181      * exception
182      * @param throwable The <code>Throwable</code> error or exception
183      * @param verbosity Verbosity level of this message
184      */

185     public void log(String JavaDoc message, Throwable JavaDoc throwable, int verbosity) {
186         write(message, throwable, verbosity);
187     }
188
189     /**
190      * Remove a property change listener from this component.
191      *
192      * @param listener The listener to remove
193      */

194     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
195         support.removePropertyChangeListener(listener);
196     }
197
198     protected void write(String JavaDoc msg, Throwable JavaDoc throwable, int verbosity) {
199         CharArrayWriter JavaDoc buf = new CharArrayWriter JavaDoc();
200         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(buf);
201         writer.println(msg);
202         throwable.printStackTrace(writer);
203         Throwable JavaDoc rootCause = null;
204         if (throwable instanceof LifecycleException)
205             rootCause = ((LifecycleException) throwable).getThrowable();
206         else if (throwable instanceof ServletException JavaDoc)
207             rootCause = ((ServletException JavaDoc) throwable).getRootCause();
208         if (rootCause != null) {
209             writer.println("----- Root Cause -----");
210             rootCause.printStackTrace(writer);
211         }
212         write(buf.toString(), verbosity);
213     }
214
215     /**
216      * Logs the given message at the given verbosity level.
217      */

218     protected abstract void write(String JavaDoc msg, int verbosity);
219 }
220
Popular Tags