KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** This class manages the logging facility for JDO components. It is the
27  * class that keeps track of the log factory in use for getting loggers
28  * for use in JDO components.
29  * <P>
30  * This class has no JDK 1.4 dependencies.
31  * <P>
32  * The log factory is responsible for constructing the loggers and for
33  * ensuring that there is only one logger per component.
34  *
35  * @author Craig Russell
36  * @version 1.0
37  */

38
39 public class LogHelper {
40     
41     /** Flag to tell we are running in JDK 1.4 and can use
42      * java.util.logging.Logger implementation.
43      */

44     protected static boolean jdk14 = isJDK14();
45     
46     /** LoggerFactory registered for creating new loggers.
47      */

48     protected static LoggerFactory loggerFactory = null;
49     
50     /** Get a Logger. This call is delegated to the registered LoggerFactory.
51      * If there is no registered LoggerFactory, then initialize one based on
52      * whether we are running in JDK 1.4 (or higher).
53      * The bundle name and class loader are passed to allow the implementation
54      * to properly find and construct the internationalization bundle.
55      * This method is synchronized to avoid race conditions where two threads
56      * access a component using the same Logger at the same time.
57      * @param loggerName the relative name of this logger
58      * @param bundleName the fully qualified name of the resource bundle
59      * @param loader the class loader used to load the resource bundle, or null
60      * @return the logger
61      */

62     public synchronized static Logger getLogger(String JavaDoc loggerName, String JavaDoc bundleName, ClassLoader JavaDoc loader) {
63         // if an implementation has not registered a LoggerFactory, use a standard one.
64
if (loggerFactory == null) {
65             if (jdk14) {
66                 loggerFactory = new LoggerFactoryJDK14();
67             } else {
68                 loggerFactory = new LoggerFactoryJDK13();
69             }
70         }
71         return loggerFactory.getLogger(loggerName, bundleName, loader);
72     }
73         
74     /** Register a LoggerFactory for use in managed environments or
75      * for special situations. This
76      * factory will be delegated to for all getLogger requests.
77      * @param factory the LoggerFactory to use for all getLogger requests
78      */

79     public static void registerLoggerFactory (LoggerFactory factory) {
80         loggerFactory = factory;
81     }
82     
83     /** Check to see if the JDK 1.4 logging environment is available.
84      * @return true if JDK 1.4 logging is available
85      */

86     public static boolean isJDK14() {
87         try {
88             Class JavaDoc logger = Class.forName("java.util.logging.Logger"); //NOI18N
89
return true;
90         } catch (ClassNotFoundException JavaDoc ex) {
91             return false;
92         }
93     }
94         
95 }
96
Popular Tags