KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > jdk > JDKLogManager


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.logging.jdk;
23
24 import java.util.logging.LogManager JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.BufferedInputStream JavaDoc;
29 import java.io.FileNotFoundException JavaDoc;
30 import java.io.File JavaDoc;
31 import java.net.URL JavaDoc;
32 import org.jboss.logging.jdk.xml.DOMConfigurator;
33
34 /**
35  @author Scott.Stark@jboss.org
36  @version $Revision: 1958 $
37  */

38 public class JDKLogManager extends LogManager JavaDoc
39 {
40    private static final String JavaDoc DEFAULT_CONFIG_PROPS = "jdklogger.properties";
41    private static final String JavaDoc DEFAULT_CONFIG_XML = "jdklogger.xml";
42
43    public JDKLogManager()
44    {
45    }
46
47    /**
48     * Overriden to attempt to load the java.util.logging.config.file property value
49     * as a classpath resource before treating this as a file as is done by the
50     * standard jdk LogManager.
51     *
52     * In additional, if the resource ends in a .xml suffix, the
53     * org.jboss.logging.jdk.xml.DOMConfigurator is used to parse a logging
54     * configuration that is similar to the 1.2 log4j.xml format.
55     *
56     * @throws IOException
57     * @throws SecurityException
58     */

59    public void readConfiguration()
60       throws IOException JavaDoc, SecurityException JavaDoc
61    {
62       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
63       String JavaDoc config = SecurityActions.getProperty("java.util.logging.config.file");
64       URL JavaDoc configURL = null;
65       if( config == null )
66       {
67          // Search for a default configuration on the classpath
68
config = DEFAULT_CONFIG_XML;
69          configURL = loader.getResource(DEFAULT_CONFIG_XML);
70          if( configURL == null )
71          {
72             config = DEFAULT_CONFIG_PROPS;
73             configURL = loader.getResource(DEFAULT_CONFIG_PROPS);
74          }
75
76          // Search for a default configuration as a file
77
if( configURL == null )
78          {
79             config = DEFAULT_CONFIG_XML;
80             File JavaDoc test = new File JavaDoc(DEFAULT_CONFIG_XML);
81             if( test.exists() == true )
82                configURL = test.toURL();
83             else
84             {
85                config = DEFAULT_CONFIG_PROPS;
86                test = new File JavaDoc(DEFAULT_CONFIG_PROPS);
87                if( test.exists() == true )
88                   configURL = test.toURL();
89             }
90             // If there still is no file, throw an exception
91
if( configURL == null )
92             {
93                String JavaDoc msg = "No java.util.logging.config.file specified, and neither the default "
94                   + DEFAULT_CONFIG_XML + " or " + DEFAULT_CONFIG_PROPS + " was found";
95                throw new FileNotFoundException JavaDoc(msg);
96             }
97          }
98       }
99
100       // If there was a config specified, try to load it from the classpath
101
if( configURL == null )
102          configURL = loader.getResource(config);
103       InputStream JavaDoc is = null;
104       if( configURL == null )
105       {
106          // If the config was not on the classpath try it as a file
107
InputStream JavaDoc in = new FileInputStream JavaDoc(config);
108          is = new BufferedInputStream JavaDoc(in);
109       }
110       else
111       {
112          // Use the located config URL
113
is = configURL.openStream();
114       }
115
116       // Is this an xml file?
117
boolean isXML = config.endsWith(".xml");
118       try
119       {
120          if( isXML )
121          {
122             DOMConfigurator.configure(is);
123          }
124          else
125          {
126             // Parse the standard jdk properties file format
127
super.readConfiguration(is);
128          }
129       }
130       finally
131       {
132          if( is != null )
133             is.close();
134       }
135    }
136
137    /**
138     * Ignore the reset operation because the default behavior by the jdk LogManager
139     * is to close all handlers. This results in loss of logging information during
140     * the jdk shutdown. The jboss kernel can call doReset to cause a reset of the
141     * logging layer as the last step in shutdown.
142     *
143     * @see #doReset() to force a reset
144     */

145    public void reset()
146    {
147    }
148
149    /**
150     * Invokes the LogManager.reset() method.
151     */

152    public void doReset()
153    {
154       super.reset();
155    }
156 }
157
158
Popular Tags