KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > logging > LogSource


1 /*
2  * $Header: /cvsroot/proxool/proxool/src/java/org/logicalcobwebs/logging/LogSource.java,v 1.3 2003/03/11 00:02:06 billhorsman Exp $
3  * $Revision: 1.3 $
4  * $Date: 2003/03/11 00:02:06 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62 package org.logicalcobwebs.logging;
63
64 import org.logicalcobwebs.logging.impl.NoOpLog;
65
66 import java.lang.reflect.Constructor JavaDoc;
67 import java.util.Hashtable JavaDoc;
68
69 /**
70  * <p>Factory for creating {@link org.logicalcobwebs.logging.Log} instances. Applications should call
71  * the <code>makeNewLogInstance()</code> method to instantiate new instances
72  * of the configured {@link org.logicalcobwebs.logging.Log} implementation class.</p>
73  *
74  * <p>By default, calling <code>getInstance()</code> will use the following
75  * algorithm:</p>
76  * <ul>
77  * <li>If Log4J is available, return an instance of
78  * <code>org.logicalcobwebs.logging.impl.Log4JCategoryLog</code>.</li>
79  * <li>If JDK 1.4 or later is available, return an instance of
80  * <code>org.logicalcobwebs.logging.impl.Jdk14Logger</code>.</li>
81  * <li>Otherwise, return an instance of
82  * <code>org.logicalcobwebs.logging.impl.NoOpLog</code>.</li>
83  * </ul>
84  *
85  * <p>You can change the default behavior in one of two ways:</p>
86  * <ul>
87  * <li>On the startup command line, set the system property
88  * <code>org.apache.commons.logging.log</code> to the name of the
89  * <code>org.logicalcobwebs.logging.Log</code> implementation class
90  * you want to use.</li>
91  * <li>At runtime, call <code>LogSource.setLogImplementation()</code>.</li>
92  * </ul>
93  *
94  * @deprecated Use {@link org.logicalcobwebs.logging.LogFactory} instead - The default factory
95  * implementation performs exactly the same algorithm as this class did
96  *
97  * @author Rod Waldhoff
98  * @version $Id: LogSource.java,v 1.3 2003/03/11 00:02:06 billhorsman Exp $
99  */

100 public class LogSource {
101
102     // ------------------------------------------------------- Class Attributes
103

104     private static Hashtable JavaDoc logs = new Hashtable JavaDoc ();
105
106     /** Is log4j available (in the current classpath) */
107     private static boolean log4jIsAvailable = false;
108
109     /** Is JD 1.4 logging available */
110     private static boolean jdk14IsAvailable = false;
111
112     /** Constructor for current log class */
113     private static Constructor JavaDoc logImplctor = null;
114
115
116     // ----------------------------------------------------- Class Initializers
117

118     static {
119
120         // Is Log4J Available?
121
try {
122             if (null != Class.forName ("org.apache.log4j.Category")) {
123                 log4jIsAvailable = true;
124             } else {
125                 log4jIsAvailable = false;
126             }
127         } catch (Throwable JavaDoc t) {
128             log4jIsAvailable = false;
129         }
130
131         // Is JDK 1.4 Logging Available?
132
try {
133             if ((null != Class.forName ("java.util.logging.Logger"))
134                 && (null != Class.forName ("org.logicalcobwebs.logging.impl.Jdk14Logger"))) {
135                 jdk14IsAvailable = true;
136             } else {
137                 jdk14IsAvailable = false;
138             }
139         } catch (Throwable JavaDoc t) {
140             jdk14IsAvailable = false;
141         }
142
143         // Set the default Log implementation
144
String JavaDoc name = null;
145         try {
146             name = System.getProperty ("org.apache.commons.logging.log");
147             if (name == null) {
148                 name = System.getProperty ("org.logicalcobwebs.logging.Log");
149             }
150         } catch (Throwable JavaDoc t) {
151             // oh well
152
}
153         if (name != null) {
154             try {
155                 setLogImplementation (name);
156             } catch (Throwable JavaDoc t) {
157                 try {
158                     setLogImplementation
159                             ("org.logicalcobwebs.logging.impl.NoOpLog");
160                 } catch (Throwable JavaDoc u) {
161                     ;
162                 }
163             }
164         } else {
165             try {
166                 if (log4jIsAvailable) {
167                     setLogImplementation
168                             ("org.logicalcobwebs.logging.impl.Log4JCategoryLog");
169                 } else if (jdk14IsAvailable) {
170                     setLogImplementation
171                             ("org.logicalcobwebs.logging.impl.Jdk14Logger");
172                 } else {
173                     setLogImplementation
174                             ("org.logicalcobwebs.logging.impl.NoOpLog");
175                 }
176             } catch (Throwable JavaDoc t) {
177                 try {
178                     setLogImplementation
179                             ("org.logicalcobwebs.logging.impl.NoOpLog");
180                 } catch (Throwable JavaDoc u) {
181                     ;
182                 }
183             }
184         }
185
186     }
187
188
189     // ------------------------------------------------------------ Constructor
190

191
192     /** Don't allow others to create instances */
193     private LogSource () {
194     }
195
196
197     // ---------------------------------------------------------- Class Methods
198

199
200     /**
201      * Set the log implementation/log implementation factory
202      * by the name of the class. The given class
203      * must implement {@link org.logicalcobwebs.logging.Log}, and provide a constructor that
204      * takes a single {@link java.lang.String} argument (containing the name
205      * of the log).
206      */

207     public static void setLogImplementation (String JavaDoc classname) throws
208             LinkageError JavaDoc, ExceptionInInitializerError JavaDoc,
209             SecurityException JavaDoc {
210         try {
211             Class JavaDoc logclass = Class.forName (classname);
212             Class JavaDoc[] argtypes = new Class JavaDoc[1];
213             argtypes[0] = "".getClass ();
214             logImplctor = logclass.getConstructor (argtypes);
215         } catch (Throwable JavaDoc t) {
216             logImplctor = null;
217         }
218     }
219
220     /**
221      * Set the log implementation/log implementation factory
222      * by class. The given class must implement {@link org.logicalcobwebs.logging.Log},
223      * and provide a constructor that takes a single {@link java.lang.String}
224      * argument (containing the name of the log).
225      */

226     public static void setLogImplementation (Class JavaDoc logclass) throws
227             LinkageError JavaDoc, ExceptionInInitializerError JavaDoc,
228             NoSuchMethodException JavaDoc, SecurityException JavaDoc {
229         Class JavaDoc[] argtypes = new Class JavaDoc[1];
230         argtypes[0] = "".getClass ();
231         logImplctor = logclass.getConstructor (argtypes);
232     }
233
234     /** Get a <code>Log</code> instance by class name */
235     public static Log getInstance (String JavaDoc name) {
236         Log log = (Log) (logs.get (name));
237         if (null == log) {
238             log = makeNewLogInstance (name);
239             logs.put (name, log);
240         }
241         return log;
242     }
243
244     /** Get a <code>Log</code> instance by class */
245     public static Log getInstance (Class JavaDoc clazz) {
246         return getInstance (clazz.getName ());
247     }
248
249     /**
250      * Create a new {@link org.logicalcobwebs.logging.Log} implementation, based
251      * on the given <i>name</i>.
252      * <p>
253      * The specific {@link org.logicalcobwebs.logging.Log} implementation returned
254      * is determined by the value of the
255      * <tt>org.apache.commons.logging.log</tt> property.
256      * The value of <tt>org.apache.commons.logging.log</tt> may be set to
257      * the fully specified name of a class that implements
258      * the {@link org.logicalcobwebs.logging.Log} interface. This class must also
259      * have a public constructor that takes a single
260      * {@link java.lang.String} argument (containing the <i>name</i>
261      * of the {@link org.logicalcobwebs.logging.Log} to be constructed.
262      * <p>
263      * When <tt>org.apache.commons.logging.log</tt> is not set,
264      * or when no corresponding class can be found,
265      * this method will return a Log4JCategoryLog
266      * if the log4j Category class is
267      * available in the {@link org.logicalcobwebs.logging.LogSource}'s classpath, or a
268      * Jdk14Logger if we are on a JDK 1.4 or later system, or
269      * NoOpLog if neither of the above conditions is true.
270      *
271      * @param name the log name (or category)
272      */

273     public static Log makeNewLogInstance (String JavaDoc name) {
274
275         Log log = null;
276         try {
277             Object JavaDoc[] args = new Object JavaDoc[1];
278             args[0] = name;
279             log = (Log) (logImplctor.newInstance (args));
280         } catch (Throwable JavaDoc t) {
281             log = null;
282         }
283         if (null == log) {
284             log = new NoOpLog (name);
285         }
286         return log;
287
288     }
289
290     /**
291      * Returns a {@link java.lang.String} array containing the names of
292      * all logs known to me.
293      */

294     public static String JavaDoc[] getLogNames () {
295         return (String JavaDoc[]) (logs.keySet ().toArray (new String JavaDoc[logs.size ()]));
296     }
297
298 }
299
300
Popular Tags