1. /*
  2. * Copyright 2001-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. package org.apache.commons.logging.impl;
  17. import java.util.Enumeration;
  18. import java.util.Hashtable;
  19. import java.util.Vector;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogConfigurationException;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.log4j.Logger;
  24. /**
  25. * <p>Concrete subclass of {@link LogFactory} specific to log4j.
  26. *
  27. * @deprecated Per discussion on COMMONS-DEV, the behind-the-scenes use
  28. * of this class as a proxy factory has been removed. For 1.0, you
  29. * can still request it directly if you wish, but it doesn't really
  30. * do anything useful, and will be removed in 1.1.
  31. *
  32. * @author Costin Manolache
  33. */
  34. public final class Log4jFactory extends LogFactory {
  35. public Log4jFactory() {
  36. super();
  37. }
  38. /**
  39. * The configuration attributes for this {@link LogFactory}.
  40. */
  41. private Hashtable attributes = new Hashtable();
  42. // Previously returned instances, to avoid creation of proxies
  43. private Hashtable instances = new Hashtable();
  44. // --------------------------------------------------------- Public Methods
  45. /**
  46. * Return the configuration attribute with the specified name (if any),
  47. * or <code>null</code> if there is no such attribute.
  48. *
  49. * @param name Name of the attribute to return
  50. */
  51. public Object getAttribute(String name) {
  52. return (attributes.get(name));
  53. }
  54. /**
  55. * Return an array containing the names of all currently defined
  56. * configuration attributes. If there are no such attributes, a zero
  57. * length array is returned.
  58. */
  59. public String[] getAttributeNames() {
  60. Vector names = new Vector();
  61. Enumeration keys = attributes.keys();
  62. while (keys.hasMoreElements()) {
  63. names.addElement((String) keys.nextElement());
  64. }
  65. String results[] = new String[names.size()];
  66. for (int i = 0; i < results.length; i++) {
  67. results[i] = (String) names.elementAt(i);
  68. }
  69. return (results);
  70. }
  71. /**
  72. * Convenience method to derive a name from the specified class and
  73. * call <code>getInstance(String)</code> with it.
  74. *
  75. * @param clazz Class for which a suitable Log name will be derived
  76. *
  77. * @exception LogConfigurationException if a suitable <code>Log</code>
  78. * instance cannot be returned
  79. */
  80. public Log getInstance(Class clazz)
  81. throws LogConfigurationException
  82. {
  83. Log instance = (Log) instances.get(clazz);
  84. if( instance != null )
  85. return instance;
  86. instance=new Log4JLogger( Logger.getLogger( clazz ));
  87. instances.put( clazz, instance );
  88. return instance;
  89. }
  90. public Log getInstance(String name)
  91. throws LogConfigurationException
  92. {
  93. Log instance = (Log) instances.get(name);
  94. if( instance != null )
  95. return instance;
  96. instance=new Log4JLogger( Logger.getLogger( name ));
  97. instances.put( name, instance );
  98. return instance;
  99. }
  100. /**
  101. * Release any internal references to previously created {@link Log}
  102. * instances returned by this factory. This is useful in environments
  103. * like servlet containers, which implement application reloading by
  104. * throwing away a ClassLoader. Dangling references to objects in that
  105. * class loader would prevent garbage collection.
  106. */
  107. public void release() {
  108. instances.clear();
  109. // what's the log4j mechanism to cleanup ???
  110. }
  111. /**
  112. * Remove any configuration attribute associated with the specified name.
  113. * If there is no such attribute, no action is taken.
  114. *
  115. * @param name Name of the attribute to remove
  116. */
  117. public void removeAttribute(String name) {
  118. attributes.remove(name);
  119. }
  120. /**
  121. * Set the configuration attribute with the specified name. Calling
  122. * this with a <code>null</code> value is equivalent to calling
  123. * <code>removeAttribute(name)</code>.
  124. *
  125. * @param name Name of the attribute to set
  126. * @param value Value of the attribute to set, or <code>null</code>
  127. * to remove any setting for this attribute
  128. */
  129. public void setAttribute(String name, Object value) {
  130. if (value == null) {
  131. attributes.remove(name);
  132. } else {
  133. attributes.put(name, value);
  134. }
  135. }
  136. }