KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > LogManager


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2003 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.kernel;
65
66 import org.apache.log4j.Logger;
67 import org.apache.log4j.xml.DOMConfigurator;
68
69 import java.io.File JavaDoc;
70 import java.io.IOException JavaDoc;
71 import java.net.URL JavaDoc;
72
73 /**
74  * New LogManager implementation. Loads and configures log4j and also provides
75  * classloader reloading capabilities if you wish to have log4j low on the classloader
76  * hierarchy and want your webapps to reload ok.
77  *
78  * @author Michael Rimov
79  */

80 public final class LogManager {
81     public static long lastChecked = System.currentTimeMillis();
82
83     private static boolean initialized = false;
84
85
86     /**
87      * Default Constructor. Do not use
88      */

89     protected LogManager() {
90
91     }
92
93     /**
94      * Construct the log manager given the location of the file and log directory
95      *
96      * @param logConfigFile may be null if you initialize log4j through some other
97      * way such as static class resource, or system property.
98      * @param logDirectory may be null if you don't expand any expresso-related
99      * macros in the logging file.
100      */

101     public LogManager(String JavaDoc logConfigFile, String JavaDoc logDirectory) {
102         initialize(logConfigFile, logDirectory);
103     }
104
105     /**
106      * Constructs and initializes the logging system given the URL of a config file
107      * [Classloader friendly] and destination of the log directory
108      *
109      * @param logConfigFile cannot be null
110      * @param logDirectory may be null if you don't expand any expresso-related
111      * macros in the logging file.
112      */

113     public LogManager(URL JavaDoc logConfigFile, String JavaDoc logDirectory) {
114         initialize(logConfigFile, logDirectory);
115     }
116
117     /**
118      * Destroy the lastUsed Map
119      */

120     public static void destroy() {
121         initialized = false;
122         org.apache.log4j.LogManager.shutdown();
123     }
124
125
126     /**
127      * Sets up the whole logging system. expressoLogging.xml must exist in the
128      * system. After that, it parses all .xml files that have doctype of
129      * Configuration. Called through instantiate() function.
130      *
131      * @param loggingConfiguration The location of the logging configuration. May
132      * be overridden by the <code>log4j.configuration</code> System property.
133      * @param loggingDirectory The location of where Expresso should log it's
134      * data.
135      * @throws IllegalArgumentException if any of the arguments fail.
136      */

137     synchronized private void initialize(String JavaDoc loggingConfiguration, String JavaDoc loggingDirectory) {
138         synchronized (LogManager.class) {
139             if (initialized) {
140                 return;
141             }
142
143             if (loggingConfiguration == null) {
144                 loggingConfiguration = System.getProperty("log4j.configuration", null);
145             }
146
147             java.net.URL JavaDoc configFile;
148             if (loggingConfiguration == null) {
149                 configFile = this.getClass().getResource("log4j.xml");
150             } else {
151                 initLoggingDirectory(loggingDirectory);
152                 java.io.File JavaDoc f = new java.io.File JavaDoc(loggingConfiguration);
153                 if (f == null) {
154                     throw new IllegalArgumentException JavaDoc(
155                             "Unable to locate logging configuration file " + loggingConfiguration);
156                 }
157                 DOMConfigurator.configureAndWatch(loggingConfiguration, 5000);
158                 Logger log = Logger.getLogger(LogManager.class);
159                 if (log.isInfoEnabled()) {
160                     log.info("Logging Configuration Complete. Config File: " +
161                             loggingConfiguration);
162                 }
163                 return;
164             }
165
166             if (configFile == null) {
167                 throw new IllegalArgumentException JavaDoc("Unable to locate a log4j " +
168                         "configuration file, either through log.xml or loggingConfiguration parameter");
169             }
170
171             initialize(configFile, loggingDirectory);
172         }
173     } /* initialize() */
174
175     /**
176      * Initialize the system parameters for the logging directory (optional)
177      *
178      * @param loggingDirectory The location of where to save the logging directory.
179      * May be overridden by <code>expresso.logDir</code> system property.
180      */

181     private void initLoggingDirectory(String JavaDoc loggingDirectory) {
182         if (loggingDirectory == null) {
183             return;
184         }
185
186         if (loggingDirectory != null && loggingDirectory.length() > 0 &&
187                 System.getProperty("expresso.logDir", null) == null) {
188             System.setProperty("expresso.logDir", loggingDirectory);
189         }
190         File JavaDoc logdir = new File JavaDoc(loggingDirectory);
191         if (!logdir.exists()) {
192             logdir.mkdirs();
193         }
194         try {
195             System.out.println("Logging directory is: " + logdir.getCanonicalPath());
196         } catch (IOException JavaDoc e) {
197             e.printStackTrace();
198         }
199
200     }
201
202     protected void initialize(URL JavaDoc loggingConfiguration, String JavaDoc loggingDirectory) {
203         initLoggingDirectory(loggingDirectory);
204
205         DOMConfigurator.configure(loggingConfiguration);
206         Logger log = Logger.getLogger(LogManager.class);
207         if (log.isInfoEnabled()) {
208             log.info("Logging Configuration Complete. Using config URL: " +
209                     loggingConfiguration.toString());
210         }
211
212
213     }
214
215 }
Popular Tags