KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > log > Factory


1 //========================================================================
2
//$Id: Factory.java,v 1.4 2005/12/05 12:51:01 gregwilkins Exp $
3
//Copyright 2004 Mort Bay Consulting Pty. Ltd.
4
//------------------------------------------------------------------------
5
//Licensed under the Apache License, Version 2.0 (the "License");
6
//you may not use this file except in compliance with the License.
7
//You may obtain a copy of the License at
8
//http://www.apache.org/licenses/LICENSE-2.0
9
//Unless required by applicable law or agreed to in writing, software
10
//distributed under the License is distributed on an "AS IS" BASIS,
11
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//See the License for the specific language governing permissions and
13
//limitations under the License.
14
//========================================================================
15

16 package org.mortbay.log;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogConfigurationException;
23
24
25 /**
26  * Commons Logging Factory for org.mortbay.log
27  * Returns a static default log, unless an alternate Log implementation has been set as
28  * an attribute keyed by the classname or other name of the log. If the name of the attibute ends
29  * with '.*' it is assumed to be a name prefix match.
30  * Attributes with string values are treated as references to other attributes.
31  *
32  * This class needs to be configured in the META-INF/services directory (see build.xml for example)
33  * for automatic discovery. Or it can be configured with the system property:
34  * -Dorg.apache.commons.logging.LogFactory=org.mortbay.log.Factory
35  *
36  */

37 public class Factory extends org.apache.commons.logging.LogFactory
38 {
39      static LogImpl log = new LogImpl();
40      static HashMap JavaDoc attributes = new HashMap JavaDoc();
41      static ArrayList JavaDoc prefixes = new ArrayList JavaDoc();
42     
43     /**
44      *
45      */

46     public Factory()
47     {
48         super();
49     }
50
51     /* (non-Javadoc)
52      * @see org.apache.commons.logging.LogFactory#getAttribute(java.lang.String)
53      */

54     public Object JavaDoc getAttribute(String JavaDoc n)
55     {
56         return attributes.get(n);
57     }
58
59     /* (non-Javadoc)
60      * @see org.apache.commons.logging.LogFactory#getAttributeNames()
61      */

62     public String JavaDoc[] getAttributeNames()
63     {
64         return (String JavaDoc[]) attributes.keySet().toArray(new String JavaDoc[attributes.size()]);
65     }
66
67     /* (non-Javadoc)
68      * @see org.apache.commons.logging.LogFactory#getInstance(java.lang.Class)
69      */

70     public Log getInstance(Class JavaDoc c) throws LogConfigurationException
71     {
72         if (c!=null)
73             return getInstance(c.getName());
74         return getInstance((String JavaDoc)null);
75     }
76
77     /* (non-Javadoc)
78      * @see org.apache.commons.logging.LogFactory#getInstance(java.lang.String)
79      */

80     public Log getInstance(String JavaDoc name) throws LogConfigurationException
81     {
82         String JavaDoc match="";
83         for (int i=prefixes.size();name!=null && i-->0;)
84         {
85             String JavaDoc prefix=(String JavaDoc)prefixes.get(i);
86             if (name.startsWith(prefix) && prefix.length()>match.length())
87                 match=prefix;
88         }
89         if (match.length()>0)
90             name=match+".*";
91         
92         // Get value
93
Object JavaDoc o = attributes.get(name);
94         
95         // Dereference string attributes
96
while(o!=null && o instanceof String JavaDoc)
97             o=attributes.get(o);
98         
99         // return actual log.
100
if (o instanceof Log)
101             return (Log)o;
102         return log;
103     }
104
105     /* (non-Javadoc)
106      * @see org.apache.commons.logging.LogFactory#release()
107      */

108     public void release()
109     {
110     }
111
112     /* (non-Javadoc)
113      * @see org.apache.commons.logging.LogFactory#removeAttribute(java.lang.String)
114      */

115     public void removeAttribute(String JavaDoc n)
116     {
117         attributes.remove(n);
118         if (n.endsWith(".*"))
119             prefixes.remove(n.substring(0,n.length()-2));
120     }
121
122     /* (non-Javadoc)
123      * @see org.apache.commons.logging.LogFactory#setAttribute(java.lang.String, java.lang.Object)
124      */

125     public void setAttribute(String JavaDoc n, Object JavaDoc v)
126     {
127         attributes.put(n,v);
128         if (n.endsWith(".*") && v instanceof Log)
129             prefixes.add(n.substring(0,n.length()-2));
130     }
131
132 }
Popular Tags