KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > logging > LoggerAccess


1 /*
2  * Copyright 2003, 2004 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.ws.jaxme.logging;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.net.URL JavaDoc;
23
24
25 /** <p>This class implements access to the Loggers through static
26  * methods. The class typically configures itself from the
27  * environment. However, you may choose to configure the class
28  * explicitly by invoking {@link #setLoggerFactory(LoggerFactory)}.
29  *
30  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
31  */

32 public class LoggerAccess {
33    private static LoggerFactory theFactory;
34
35    /** <p>Sets the logger factory.</p>
36     */

37    public static synchronized void setLoggerFactory(LoggerFactory pFactory) {
38       theFactory = pFactory;
39    }
40
41    /** <p>Instantiates the given {@link LoggerFactory}.</p>
42     */

43    private static LoggerFactory newLoggerFactory(String JavaDoc pName) {
44       Class JavaDoc c = null;
45       Throwable JavaDoc t = null;
46       try {
47          c = Class.forName(pName);
48       } catch (Throwable JavaDoc th) {
49          t = th;
50       }
51       if (c == null) {
52          try {
53             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
54             if (cl == null) {
55                cl = LoggerAccess.class.getClassLoader();
56                if (cl == null) {
57                   cl = ClassLoader.getSystemClassLoader();
58                }
59             }
60             c = cl.loadClass(pName);
61          } catch (Throwable JavaDoc th) {
62             if (t == null) {
63                t = th;
64             }
65          }
66       }
67       if (c == null) {
68          if (t == null) {
69             t = new ClassNotFoundException JavaDoc(pName);
70          }
71          t.printStackTrace();
72          return null;
73       }
74
75       try {
76          return (LoggerFactory) c.newInstance();
77       } catch (Throwable JavaDoc th) {
78          t.printStackTrace();
79          return null;
80       }
81    }
82
83    /** <p>Creates a new instance of {@link LoggerFactory}. The
84     * implementation class is determined as follows:
85     * <ol>
86     * <li>If the system property <code>org.apache.ws.jaxme.logging.LoggerFactory</code>
87     * is set, uses the given class name.</li>
88     * <li>If the resource
89     * <code>META-INF/services/org.apache.ws.jaxme.logging.LoggerFactory</code>
90     * exists, uses the given class name.</li>
91     * <li>Otherwise returns a default instance logging to
92     * <code>System.err</code>.</li>
93     * </ol>
94     */

95    public static LoggerFactory newLoggerFactory() {
96       String JavaDoc p = LoggerFactory.class.getName();
97       String JavaDoc v = System.getProperty(p);
98       LoggerFactory result;
99       if (v != null) {
100          result = newLoggerFactory(v);
101          if (result != null) {
102             return result;
103          }
104       }
105
106       String JavaDoc res = "META-INF/services/" + p;
107       ClassLoader JavaDoc cl = LoggerAccess.class.getClassLoader();
108       if (cl == null) {
109          cl = ClassLoader.getSystemClassLoader();
110       }
111       URL JavaDoc url = cl.getResource(res);
112       if (url == null) {
113          cl = Thread.currentThread().getContextClassLoader();
114          if (cl != null) {
115             url = cl.getResource(res);
116          }
117       }
118       if (url != null) {
119          InputStream JavaDoc istream = null;
120          try {
121             istream = url.openStream();
122             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(istream));
123             v = reader.readLine();
124             if (v != null) {
125                result = newLoggerFactory(v);
126             }
127             istream.close();
128             istream = null;
129          } catch (Throwable JavaDoc t) {
130             t.printStackTrace();
131          } finally {
132             if (istream != null) { try { istream.close(); } catch (Throwable JavaDoc ignore) {} }
133          }
134       }
135
136       return new LoggerFactoryImpl() {
137          public Logger newLogger(String JavaDoc pName) {
138             return new LoggerImpl(pName);
139          }
140       };
141    }
142
143    /** <p>Returns the logger factory. If a logger factory is set (by
144     * previous calls to {@link #newLoggerFactory()} or
145     * {@link #setLoggerFactory(LoggerFactory)}), returns that factory. Otherwise
146     * invokes these methods and returns the result.</p>
147     */

148    public static synchronized LoggerFactory getLoggerFactory() {
149       if (theFactory == null) {
150          setLoggerFactory(newLoggerFactory());
151       }
152       return theFactory;
153    }
154
155    /** <p>Returns a new logger with the given name.</p>
156     */

157    public static synchronized Logger getLogger(String JavaDoc pName) {
158       LoggerFactory factory = getLoggerFactory();
159       return factory.getLogger(pName);
160    }
161
162    /** <p>Shortcut for <code>getLogger(pClass.getName())</code>.</p>
163     */

164    public static synchronized Logger getLogger(Class JavaDoc pClass) {
165       return getLogger(pClass.getName());
166    }
167 }
168
Popular Tags