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. package org.apache.commons.discovery.log;
  62. import java.lang.reflect.Method;
  63. import java.util.Enumeration;
  64. import java.util.Hashtable;
  65. import org.apache.commons.discovery.DiscoveryException;
  66. import org.apache.commons.discovery.tools.ClassUtils;
  67. import org.apache.commons.logging.Log;
  68. import org.apache.commons.logging.LogFactory;
  69. /**
  70. * <p>Simple implementation of Log that sends all enabled log messages,
  71. * for all defined loggers, to System.err.
  72. * </p>
  73. *
  74. * <p>Hacked from commons-logging SimpleLog for use in discovery.
  75. * This is intended to be enough of a Log implementation to bootstrap
  76. * Discovery.
  77. * </p>
  78. *
  79. * <p>One property: <code>org.apache.commons.discovery.log.level</code>.
  80. * valid values: all, trace, debug, info, warn, error, fatal, off.
  81. * </p>
  82. *
  83. * @author Richard A. Sitze
  84. * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
  85. * @author Rod Waldhoff
  86. * @author Robert Burrell Donkin
  87. *
  88. * @version $Id: SimpleLog.java,v 1.4 2002/06/15 20:54:48 craigmcc Exp $
  89. */
  90. public class DiscoveryLogFactory {
  91. private static LogFactory logFactory = null;
  92. private static final Hashtable classRegistry = new Hashtable();
  93. private static final Class[] setLogParamClasses = new Class[] { Log.class };
  94. /**
  95. * Above fields must be initialied before this one..
  96. */
  97. private static Log log = DiscoveryLogFactory._newLog(DiscoveryLogFactory.class);
  98. /**
  99. */
  100. public static Log newLog(Class clazz) {
  101. /**
  102. * Required to implement 'public static void setLog(Log)'
  103. */
  104. try {
  105. Method setLog = ClassUtils.findPublicStaticMethod(clazz,
  106. void.class,
  107. "setLog",
  108. setLogParamClasses);
  109. if (setLog == null) {
  110. String msg = "Internal Error: " + clazz.getName() + " required to implement 'public static void setLog(Log)'";
  111. log.fatal(msg);
  112. throw new DiscoveryException(msg);
  113. }
  114. } catch (SecurityException se) {
  115. String msg = "Required Security Permissions not present";
  116. log.fatal(msg, se);
  117. throw new DiscoveryException(msg, se);
  118. }
  119. if (log.isDebugEnabled())
  120. log.debug("Class meets requirements: " + clazz.getName());
  121. return _newLog(clazz);
  122. }
  123. /**
  124. * This method MUST not invoke any logging..
  125. */
  126. public static Log _newLog(Class clazz) {
  127. classRegistry.put(clazz, clazz);
  128. return (logFactory == null)
  129. ? new SimpleLog(clazz.getName())
  130. : logFactory.getInstance(clazz.getName());
  131. }
  132. public static void setLog(Log _log) {
  133. log = _log;
  134. }
  135. /**
  136. * Set logFactory, works ONLY on first call.
  137. */
  138. public static void setFactory(LogFactory factory) {
  139. if (logFactory == null) {
  140. // for future generations.. if any
  141. logFactory = factory;
  142. // now, go back and reset loggers for all current classes..
  143. Enumeration elements = classRegistry.elements();
  144. while (elements.hasMoreElements()) {
  145. Class clazz = (Class)elements.nextElement();
  146. if (log.isDebugEnabled())
  147. log.debug("Reset Log for: " + clazz.getName());
  148. Method setLog = null;
  149. // invoke 'setLog(Log)'.. we already know it's 'public static',
  150. // have verified parameters, and return type..
  151. try {
  152. setLog = clazz.getMethod("setLog", setLogParamClasses);
  153. } catch(Exception e) {
  154. String msg = "Internal Error: pre-check for " + clazz.getName() + " failed?!";
  155. log.fatal(msg, e);
  156. throw new DiscoveryException(msg, e);
  157. }
  158. Object[] setLogParam = new Object[] { factory.getInstance(clazz.getName()) };
  159. try {
  160. setLog.invoke(null, setLogParam);
  161. } catch(Exception e) {
  162. String msg = "Internal Error: setLog failed for " + clazz.getName();
  163. log.fatal(msg, e);
  164. throw new DiscoveryException(msg, e);
  165. }
  166. }
  167. }
  168. }
  169. }