KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > logging > BaseLogManager


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 package com.sun.enterprise.server.logging;
24
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.logging.LogManager JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import java.util.logging.Level JavaDoc;
36
37 import com.sun.logging.LogDomains;
38
39 import com.sun.enterprise.util.logging.IASLevel;
40
41
42 /**
43  * Class BaseLogManager serves as an abstract base class for the Application
44  * Client Container and Application Server loggers. The main purpose of
45  * this class is to override the addLogger method which provides a hook
46  * to attach custom handlers and formatters and to provide a default
47  * resource bundle if none is provided. Note that providing a default resource
48  * bundle somewhat alters the semantics of a Logger.getLogger(name) call
49  * followed by a Logger.getLogger(name, resourcebundle) call. With the
50  * BaseHandler installed, the later call will have no effect as the call to
51  * Logger.getLogger(name) implicitely defines a resource bundle.
52  */

53 public abstract class BaseLogManager extends LogManager JavaDoc {
54
55     // Due to implementation details of the LogManager, we must
56
// defer initializations until static initialization of the java.util.logging
57
// classes are complete and until the configuration is read.
58
// (In the FileHandler failure to do this results in improper
59
// file initialization and in the server handler, statically initialized
60
// loggers will always log to the stdout).
61
private boolean _configurationRead = false;
62     private List JavaDoc _unInitializedLoggers = new ArrayList JavaDoc();
63
64     private Boolean JavaDoc mogLoggerCreated = false;
65
66     // Used to log internal messages, this is essentially the first logger
67
// created.
68
protected static Logger JavaDoc _logger = null;
69
70     // WARNING: This is here to force static initialization of IASLevel.
71
// This is necessary so that Level.parse() can interpret the new
72
// statically initialized IASLevel constants without throwing an
73
// IllegalArgumentException
74
static {
75         IASLevel.ALERT.getName();
76     }
77
78     protected BaseLogManager() {
79         super();
80     }
81
82     // Used to fabricate a resource bundle name from the logger name. The
83
// default resource bundle name is used if one is not provided.
84
public String JavaDoc getLoggerResourceBundleName(String JavaDoc loggerName) {
85
86         String JavaDoc result = loggerName + "." + LogDomains.RESOURCE_BUNDLE;
87
88         return result.replaceFirst(LogDomains.DOMAIN_ROOT,
89                                    LogDomains.PACKAGE_ROOT);
90     }
91
92     protected abstract void initializeLogger(Logger JavaDoc logger);
93
94     protected void doInitializeLogger(Logger JavaDoc logger)
95     {
96         String JavaDoc loggerName = logger.getName( );
97         // We don't want to associate a Log Resource Bundle to org.apache or
98
// tomcat loggers.
99
// _REVISIT_: Clean this code not set any resource bundle in future,
100
// because resource bundles should be associated by the modules itself
101
if(! ( ( loggerName.startsWith( "org.apache" ) )
102           || ( loggerName.startsWith( "com.sun.faces" ) )
103           || ( loggerName.startsWith( "tomcat" ) ) ) )
104         {
105             // This is tricky. If the logger was not created with a resource
106
// bundle, we want to re-create it (to provide it with our default
107
// resource bundle name); however, due to implementation details in
108
// Logger.getLogger(name, resourceBundleName), we should get back
109
// the same instance
110
if (logger.getResourceBundleName() == null) {
111                 try {
112                     Logger JavaDoc newLogger =
113                         Logger.getLogger(logger.getName(),
114                             getLoggerResourceBundleName(logger.getName()));
115
116                     assert(logger == newLogger);
117                 } catch (Throwable JavaDoc ex) {
118                     // This exception is intentionally eaten. It indicates
119
// that the default resource bundle (specified by
120
// getLoggerResourceBundleName) does not exist.
121
// Logger is already created.
122
}
123             }
124         }
125        
126
127         // Finally call the real initialization
128
initializeLogger(logger);
129     }
130
131     // We subclass readConfiguration to keep track of whether the configuration
132
// has been read. There are multiple readConfiguration methods, but all result
133
// in this one being invoked. As soon as the configuration is read, we
134
// go back an reinitialize any loggers whose initialization was deferred
135
// prior to the configuration being read.
136
public void readConfiguration(InputStream JavaDoc ins)
137         throws IOException JavaDoc, SecurityException JavaDoc
138     {
139         super.readConfiguration(ins);
140         synchronized (_unInitializedLoggers) {
141             _configurationRead = true;
142             Iterator JavaDoc iter = _unInitializedLoggers.iterator();
143             while (iter.hasNext()) {
144                 Logger JavaDoc l = (Logger JavaDoc)iter.next();
145                 doInitializeLogger(l);
146             }
147             _unInitializedLoggers.clear();
148         }
149     }
150
151     // We override addLogger on the security manager so that we have a
152
// hook during Logger.getLogger() time where we can attach our own
153
// custom handlers and formatters, etc.
154
public boolean addLogger(Logger JavaDoc logger) {
155
156         String JavaDoc modLoggerName = ModuleToLoggerNameMapper.getMatchedModuleLoggerName(logger.getName());
157         if (modLoggerName !=null && !modLoggerName.equals(logger.getName())) {
158             Logger.getLogger(modLoggerName); //will results in recursive call to this method.
159
}
160
161         // The first call to initializeLogger may have a null _logger.
162
boolean result = super.addLogger(logger);
163
164         // result will be true if the logger does not exist already
165
if (result) {
166             try {
167
168                 // The first logger created becomes the logger that we will
169
// use internally to log.
170
if (_logger == null) {
171                     _logger = logger;
172                 }
173
174                 // Defer initialization until the configuration (e.g. logging.properties) is
175
// read. Once the configuration is read we go back and re-initialize any
176
// loggers that were created prior to the configuration being read.
177
synchronized (_unInitializedLoggers) {
178                     if (!_configurationRead) {
179                         _unInitializedLoggers.add(logger);
180                         return result;
181                     } else {
182                         doInitializeLogger(logger);
183                     }
184                 }
185             } catch (Throwable JavaDoc th) {
186                 th.printStackTrace();
187                 _logger.log(Level.SEVERE, "addLogger exception ", th);
188             }
189         }
190
191         return result;
192     }
193 }
194
195
Popular Tags