View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package org.apache.jdo.util;
18  
19  import java.io.InputStream;
20  import java.io.IOException;
21  
22  import java.security.AccessController;
23  import java.security.PrivilegedAction;
24  
25  import java.util.logging.LogManager;
26  
27  import org.apache.commons.logging.impl.Jdk14Logger;
28  
29  /***
30   * JDO-specific subclass of the apache commons logging Log
31   * implementation that wraps the standard JDK 1.4 logging.
32   * This class configures the JDK LogManager using a properties file
33   * called logging.properties found via the CLASSPATH.
34   *
35   * @author Michael Bouschen
36   * @since 1.1
37   * @version 1.1
38   */
39  public class JDOJdk14Logger
40      extends Jdk14Logger
41  {
42      /*** Logging properties file name. */
43      public static final String PROPERIES_FILE = "logging.properties"; //NOI18N
44      
45      /*** Indicates whether JDK 1.4 logging has been configured by this class. */
46      private static boolean configured = false;
47      
48      /*** I18N support. */
49      private final static I18NHelper msg = 
50          I18NHelper.getInstance("org.apache.jdo.util.Bundle"); // NOI18N
51  
52      /*** 
53       * Constructor checking whether JDK 1.4 logging should be
54       * configuared after calling super constructor. 
55       */
56      public JDOJdk14Logger(String name) {
57          super(name);
58          if (!configured) {
59              configured = true;
60              configureJDK14Logger();
61          }
62      }
63  
64      /*** 
65       * Configures JDK 1.4 LogManager.
66       */
67      private void configureJDK14Logger() {
68          final LogManager logManager = LogManager.getLogManager();
69          final ClassLoader cl = getClass().getClassLoader();
70          AccessController.doPrivileged(new PrivilegedAction() {
71              public Object run () {
72                  try {
73                      InputStream config = cl.getResourceAsStream(PROPERIES_FILE);
74                      logManager.readConfiguration(config);
75                      return null;
76                  }
77                  catch (IOException ex) {
78                      throw new RuntimeException(
79                          msg.msg("EXC_LoggerSetupIOException", //NOI18N
80                                  PROPERIES_FILE) + ex); 
81                  }
82                  catch (SecurityException ex) {
83                      throw new RuntimeException(
84                          msg.msg("EXC_LoggerSetupSecurityException", // NOI18N
85                                  PROPERIES_FILE) + ex);
86                  }
87              }
88              });
89      }
90      
91  }