View Javadoc

1   package org.codehaus.mojo.tomcat.log;
2   
3   /*
4    * Copyright 2006 Mark Hobson.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogConfigurationException;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * A JCL log factory implementation that delegates to a Maven log.
29   * 
30   * <p>
31   * The Maven log that logs produced by this class delegate to is configured by setting the log factory attribute
32   * <code>maven.log</code>.
33   * </p>
34   * 
35   * @author Mark Hobson <markhobson@gmail.com>
36   */
37  public class MavenLogFactory extends LogFactory
38  {
39      // ----------------------------------------------------------------------
40      // Fields
41      // ----------------------------------------------------------------------
42  
43      /**
44       * The map of log factory attributes keyed by their name.
45       * 
46       * @todo Ideally this wouldn't be static, although since JCL instantiates a new LogFactory per context classloader
47       *       we need a way of propagating the Maven log to all log factories.
48       */
49      private static final Map attributesByName = new HashMap();
50  
51      // ----------------------------------------------------------------------
52      // LogFactory Methods
53      // ----------------------------------------------------------------------
54  
55      /*
56       * @see org.apache.commons.logging.LogFactory#getAttribute(java.lang.String)
57       */
58      public Object getAttribute( String name )
59      {
60          return attributesByName.get( name );
61      }
62  
63      /*
64       * @see org.apache.commons.logging.LogFactory#getAttributeNames()
65       */
66      public String[] getAttributeNames()
67      {
68          Set names = attributesByName.keySet();
69  
70          return (String[]) names.toArray( new String[names.size()] );
71      }
72  
73      /*
74       * @see org.apache.commons.logging.LogFactory#getInstance(java.lang.Class)
75       */
76      public Log getInstance( Class clazz ) throws LogConfigurationException
77      {
78          return getInstance( clazz.getName() );
79      }
80  
81      /*
82       * @see org.apache.commons.logging.LogFactory#getInstance(java.lang.String)
83       */
84      public Log getInstance( String name ) throws LogConfigurationException
85      {
86          return new MavenLog( this );
87      }
88  
89      /*
90       * @see org.apache.commons.logging.LogFactory#release()
91       */
92      public void release()
93      {
94          // no-op
95      }
96  
97      /*
98       * @see org.apache.commons.logging.LogFactory#removeAttribute(java.lang.String)
99       */
100     public void removeAttribute( String name )
101     {
102         attributesByName.remove( name );
103     }
104 
105     /*
106      * @see org.apache.commons.logging.LogFactory#setAttribute(java.lang.String, java.lang.Object)
107      */
108     public void setAttribute( String name, Object value )
109     {
110         attributesByName.put( name, value );
111     }
112 }