View Javadoc

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