KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > logging > LoggerFactoryJDK14


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.jdo.spi.persistence.utility.logging;
25
26 import java.io.IOException JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.LogManager JavaDoc;
31 import java.util.logging.Formatter JavaDoc;
32 import java.util.logging.FileHandler JavaDoc;
33 import java.util.logging.SimpleFormatter JavaDoc;
34 import java.security.AccessController JavaDoc;
35 import java.security.PrivilegedAction JavaDoc;
36 import com.sun.jdo.spi.persistence.utility.I18NHelper;
37
38 /**
39  *
40  * @author Craig Russell
41  * @version 1.0
42  */

43
44 public class LoggerFactoryJDK14 extends AbstractLoggerFactory {
45
46     /** I18N message handler for this class */
47     private static final ResourceBundle JavaDoc _messages =
48       I18NHelper.loadBundle(LoggerFactoryJDK14.class);
49
50     /** Get the message bundle for the AbstractLogger itself.
51      */

52     protected static ResourceBundle JavaDoc getMessages () { return _messages; }
53
54     /** Creates new LoggerFactory */
55     public LoggerFactoryJDK14() {
56     }
57
58     protected LoggerJDK14 findLogger(String JavaDoc absoluteLoggerName) {
59         return (LoggerJDK14)
60         LogManager.getLogManager().getLogger(absoluteLoggerName);
61     }
62
63     /** Create a new Logger. create a logger for the named component.
64      * The bundle name and class loader are passed to allow the implementation
65      * to properly find and construct the internationalization bundle.
66      * This operation is executed as a privileged action to allow
67      * permission access for the following operations:
68      *
69      * LogManager.getLogManager().addLogger - this might do checkAccess.
70      * new FileHandler
71      * FileHandler.setLevel
72      * FileHandler.setFormatter
73      * Logger.addHandler
74      *
75      * @param absoluteLoggerName the absolute name of this logger
76      * @param bundleName the fully qualified name of the resource bundle
77      * @param loader the class loader used to load the resource bundle, or null
78      * @return the logger
79      */

80     protected Logger createLogger (final String JavaDoc absoluteLoggerName,
81         final String JavaDoc bundleName, final ClassLoader JavaDoc loader) {
82         return (Logger) AccessController.doPrivileged (
83             new PrivilegedAction JavaDoc () {
84                 public Object JavaDoc run () {
85                     LoggerJDK14 logger = null;
86                     ClassLoader JavaDoc pushed = Thread.currentThread().getContextClassLoader();
87                     if(loader!=null) {
88                         setContextClassLoader (loader);
89                     }
90                     try {
91                         logger = createLogger(absoluteLoggerName, bundleName);
92                         LogManager.getLogManager().addLogger(logger);
93                         configureFileHandler(logger);
94
95                         return logger;
96                     } catch (Exception JavaDoc ex) {
97                           MessageFormat JavaDoc messageFormat = new MessageFormat JavaDoc(
98                               getMessages().getString("errorlogger.create.exception"));
99
100                           getErrorLogger().log(Logger.SEVERE, messageFormat.format(
101                               new String JavaDoc[]{absoluteLoggerName}), ex);
102                     } finally {
103                         setContextClassLoader (pushed);
104                     }
105
106                     return logger;
107                 }
108             }
109         );
110     }
111
112     /**
113      * This method throws SecurityException if a security manager exists and if
114      * the caller does not have <tt>LoggingPermission("control"))</tt> or the
115      * calling code is not placed in the doPrivileged() block.
116      */

117     protected void setContextClassLoader (final ClassLoader JavaDoc loader) {
118         if (loader != null) {
119             Thread.currentThread().setContextClassLoader (loader);
120         }
121     } //setContextClassLoader
122

123
124     protected LoggerJDK14 createLogger (String JavaDoc absoluteLoggerName, String JavaDoc
125         bundleName) {
126             LoggerJDK14 result = new LoggerJDK14(absoluteLoggerName, bundleName);
127             return result;
128     }
129     
130     /**
131      * This method throws SecurityException if a security manager exists and if
132      * the caller does not have <tt>LoggingPermission("control"))</tt> or the
133      * calling code is not placed in the doPrivileged() block.
134      */

135     protected void configureFileHandler(LoggerJDK14 logger) {
136         String JavaDoc name = logger.getName();
137         String JavaDoc baseName = name + ".FileHandler"; //NOI18N
138
LogManager JavaDoc logManager = LogManager.getLogManager();
139         
140         String JavaDoc pattern = logManager.getProperty(baseName + ".pattern"); //NOI18N
141
if(pattern != null) {
142             //If pattern != null, create and attach a FileHandler to logger.
143
//Look various properties . If not found, fall back to
144
//defaults
145

146             int defaultLimit = 0;
147             String JavaDoc limit = logManager.getProperty(baseName + ".limit"); //NOI18N
148
if(limit != null) {
149                 try {
150                     defaultLimit = Integer.parseInt(limit);
151                     if(defaultLimit < 0)
152                         defaultLimit = 0;
153                 }
154                 catch (NumberFormatException JavaDoc e) {
155                 }
156             }
157
158             int defaultCount = 1;
159             String JavaDoc count = logManager.getProperty(baseName + ".count"); //NOI18N
160
if(count != null) {
161                 try {
162                     defaultCount = Integer.parseInt(count);
163                     if(defaultCount < 1)
164                         defaultCount = 1;
165                 }
166                 catch (NumberFormatException JavaDoc e) {
167                 }
168             }
169             
170             boolean defaultAppend = false;
171             String JavaDoc append = logManager.getProperty(baseName + ".append"); //NOI18N
172
if(append != null) {
173                 defaultAppend = Boolean.valueOf(append).booleanValue();
174             }
175             
176             FileHandler JavaDoc fileHandler = null;
177             try {
178                 fileHandler = new FileHandler JavaDoc(pattern, defaultLimit,
179                             defaultCount, defaultAppend);
180             }
181             catch(Exception JavaDoc e) {
182                 MessageFormat JavaDoc messageFormat = new MessageFormat JavaDoc( getMessages().getString(
183                     "errorlogger.filehandler.initialize.exception")); //NOI18N
184

185                 getErrorLogger().log(Logger.WARNING,
186                     messageFormat.format(new String JavaDoc[]{name}), e);
187             }
188
189             if(fileHandler != null) {
190                 //Initialize various attributes for the new fileHandler
191
//--Level
192
String JavaDoc level = logManager.getProperty(baseName + ".level"); //NOI18N
193
if (level != null) {
194                     try {
195                         fileHandler.setLevel(Level.parse(level) );
196                     }
197                     catch(IllegalArgumentException JavaDoc e) {
198                     }
199                 }
200
201                 //--Formatter
202
Formatter JavaDoc defaultFormatter = null;
203                 //Initialize various attributes for the new fileHandler
204
String JavaDoc formatter = logManager.getProperty(baseName + ".formatter"); //NOI18N
205
if(formatter != null) {
206                     try {
207                         Class JavaDoc clz = ClassLoader.getSystemClassLoader().loadClass(formatter);
208                             defaultFormatter = (Formatter JavaDoc) clz.newInstance();
209                     } catch (Exception JavaDoc e) {
210                       // We got one of a variety of exceptions in creating the
211
// class or creating an instance.
212
// Drop through.
213
MessageFormat JavaDoc messageFormat = new MessageFormat JavaDoc(
214                          getMessages().getString("errorlogger.formatter.initialize.exception"));
215
216                       getErrorLogger().log(Logger.WARNING, messageFormat.format(new String JavaDoc[]{name}), e);
217                     }
218                 
219                 }
220                 
221                 if (defaultFormatter == null) {
222                     defaultFormatter = new SimpleFormatter JavaDoc();
223                 }
224                 
225                 try {
226                    fileHandler.setFormatter(defaultFormatter);
227                 }
228                 catch(IllegalArgumentException JavaDoc e) {
229                 }
230
231                 logger.addHandler(fileHandler);
232
233             } //if(fileHandler != null)
234

235         } //if(pattern != null)
236

237     }
238 }
239
Popular Tags