KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > discovery > log > DiscoveryLogFactory


1 /*
2  * $Header: /home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/impl/SimpleLog.java,v 1.4 2002/06/15 20:54:48 craigmcc Exp $
3  * $Revision: 1.4 $
4  * $Date: 2002/06/15 20:54:48 $
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
63 package org.apache.commons.discovery.log;
64
65 import java.lang.reflect.Method JavaDoc;
66 import java.lang.reflect.Modifier JavaDoc;
67 import java.util.Enumeration JavaDoc;
68 import java.util.Hashtable JavaDoc;
69
70 import org.apache.commons.discovery.DiscoveryException;
71 import org.apache.commons.discovery.tools.ClassUtils;
72 import org.apache.commons.logging.Log;
73 import org.apache.commons.logging.LogFactory;
74
75
76 /**
77  * <p>Simple implementation of Log that sends all enabled log messages,
78  * for all defined loggers, to System.err.
79  * </p>
80  *
81  * <p>Hacked from commons-logging SimpleLog for use in discovery.
82  * This is intended to be enough of a Log implementation to bootstrap
83  * Discovery.
84  * </p>
85  *
86  * <p>One property: <code>org.apache.commons.discovery.log.level</code>.
87  * valid values: all, trace, debug, info, warn, error, fatal, off.
88  * </p>
89  *
90  * @author Richard A. Sitze
91  * @author <a HREF="mailto:sanders@apache.org">Scott Sanders</a>
92  * @author Rod Waldhoff
93  * @author Robert Burrell Donkin
94  *
95  * @version $Id: SimpleLog.java,v 1.4 2002/06/15 20:54:48 craigmcc Exp $
96  */

97 public class DiscoveryLogFactory {
98     private static LogFactory logFactory = null;
99     private static final Hashtable JavaDoc classRegistry = new Hashtable JavaDoc();
100     private static final Class JavaDoc[] setLogParamClasses = new Class JavaDoc[] { Log.class };
101
102     /**
103      * Above fields must be initialied before this one..
104      */

105     private static Log log = DiscoveryLogFactory._newLog(DiscoveryLogFactory.class);
106
107     /**
108      */

109     public static Log newLog(Class JavaDoc clazz) {
110         /**
111          * Required to implement 'public static void setLog(Log)'
112          */

113         try {
114             Method JavaDoc setLog = ClassUtils.findPublicStaticMethod(clazz,
115                                                               void.class,
116                                                               "setLog",
117                                                               setLogParamClasses);
118             
119             if (setLog == null) {
120                 String JavaDoc msg = "Internal Error: " + clazz.getName() + " required to implement 'public static void setLog(Log)'";
121                 log.fatal(msg);
122                 throw new DiscoveryException(msg);
123             }
124         } catch (SecurityException JavaDoc se) {
125             String JavaDoc msg = "Required Security Permissions not present";
126             log.fatal(msg, se);
127             throw new DiscoveryException(msg, se);
128         }
129
130         if (log.isDebugEnabled())
131             log.debug("Class meets requirements: " + clazz.getName());
132
133         return _newLog(clazz);
134     }
135
136     /**
137      * This method MUST not invoke any logging..
138      */

139     public static Log _newLog(Class JavaDoc clazz) {
140         classRegistry.put(clazz, clazz);
141
142         return (logFactory == null)
143                ? new SimpleLog(clazz.getName())
144                : logFactory.getInstance(clazz.getName());
145     }
146     
147     public static void setLog(Log _log) {
148         log = _log;
149     }
150
151     /**
152      * Set logFactory, works ONLY on first call.
153      */

154     public static void setFactory(LogFactory factory) {
155         if (logFactory == null) {
156             // for future generations.. if any
157
logFactory = factory;
158             
159             // now, go back and reset loggers for all current classes..
160
Enumeration JavaDoc elements = classRegistry.elements();
161             while (elements.hasMoreElements()) {
162                 Class JavaDoc clazz = (Class JavaDoc)elements.nextElement();
163
164                 if (log.isDebugEnabled())
165                     log.debug("Reset Log for: " + clazz.getName());
166                 
167                 Method JavaDoc setLog = null;
168                 
169                 // invoke 'setLog(Log)'.. we already know it's 'public static',
170
// have verified parameters, and return type..
171
try {
172                     setLog = clazz.getMethod("setLog", setLogParamClasses);
173                 } catch(Exception JavaDoc e) {
174                     String JavaDoc msg = "Internal Error: pre-check for " + clazz.getName() + " failed?!";
175                     log.fatal(msg, e);
176                     throw new DiscoveryException(msg, e);
177                 }
178     
179                 Object JavaDoc[] setLogParam = new Object JavaDoc[] { factory.getInstance(clazz.getName()) };
180                 
181                 try {
182                     setLog.invoke(null, setLogParam);
183                 } catch(Exception JavaDoc e) {
184                     String JavaDoc msg = "Internal Error: setLog failed for " + clazz.getName();
185                     log.fatal(msg, e);
186                     throw new DiscoveryException(msg, e);
187                 }
188             }
189         }
190     }
191 }
192
Popular Tags