KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > DefaultLoggerManager


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.avalon.excalibur.logger;
20
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.configuration.Configurable;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.container.ContainerUtil;
26 import org.apache.avalon.framework.context.Context;
27 import org.apache.avalon.framework.context.ContextException;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.avalon.framework.logger.LogEnabled;
30 import org.apache.avalon.framework.logger.Logger;
31 import org.apache.avalon.framework.parameters.ParameterException;
32 import org.apache.avalon.framework.parameters.Parameterizable;
33 import org.apache.avalon.framework.parameters.Parameters;
34 import org.apache.avalon.framework.service.ServiceException;
35 import org.apache.avalon.framework.service.ServiceManager;
36 import org.apache.avalon.framework.service.Serviceable;
37 import org.apache.avalon.framework.thread.ThreadSafe;
38
39 /**
40  * This logger manager is a wrapper around all other "real" logger managers.
41  * The idea is to have one single configuration file where you can
42  * define, which logger manager (Log4J, LogKit etc.) you want to use, so
43  * you don't have to hard-code this.
44  *
45  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
46  * @version CVS $Revision: 1.9 $ $Date: 2004/03/10 13:54:49 $
47  */

48
49 public final class DefaultLoggerManager
50     implements LoggerManager,
51     ThreadSafe,
52     LogEnabled,
53     Contextualizable,
54     Configurable,
55     Serviceable,
56     Disposable
57 {
58     /** The used LoggerManager */
59     private LoggerManager m_loggermanager;
60
61     /** The context object */
62     private Context m_context;
63
64     /** The logger used to log output from the logger manager. */
65     private Logger m_logger;
66
67     /** The prefix */
68     private String JavaDoc m_prefix;
69
70     /** The service manager */
71     private ServiceManager m_manager;
72
73     /** Do we have to dispose the manager */
74     private boolean m_disposeManager = false;
75
76     /**
77      * Creates a new <code>DefaultLoggerManager</code>. .
78      */

79     public DefaultLoggerManager()
80     {
81     }
82
83     /**
84      * Creates a new <code>DefaultLoggerManager</code>. .
85      */

86     public DefaultLoggerManager( String JavaDoc prefix )
87     {
88         m_prefix = prefix;
89     }
90
91     /**
92      * Provide a logger.
93      *
94      * @param logger the logger
95      **/

96     public void enableLogging( final Logger logger )
97     {
98         m_logger = logger;
99     }
100
101     /**
102      * Return the Logger for the specified category.
103      */

104     public final Logger getLoggerForCategory( final String JavaDoc categoryName )
105     {
106         return m_loggermanager.getLoggerForCategory( categoryName );
107     }
108
109     /**
110      * Return the default Logger. This is basically the same
111      * as getting the Logger for the "" category.
112      */

113     public final Logger getDefaultLogger()
114     {
115         return m_loggermanager.getDefaultLogger();
116     }
117
118     /**
119      * Reads a context object that will be supplied to the logger manager.
120      *
121      * @param context The context object.
122      * @throws ContextException if the context is malformed
123      */

124     public final void contextualize( final Context context )
125         throws ContextException
126     {
127         m_context = context;
128     }
129
130     /**
131      * Reads a configuration object and creates the category mapping.
132      *
133      * @param configuration The configuration object.
134      * @throws ConfigurationException if the configuration is malformed
135      */

136     public final void configure( final Configuration configuration )
137         throws ConfigurationException
138     {
139         // first we test for the class name to use
140
final String JavaDoc className = configuration.getAttribute( "manager-class", null );
141
142         if( null != className )
143         {
144             // is a prefix available?
145
final String JavaDoc prefix = configuration.getAttribute( "prefix", m_prefix );
146
147             // create logger manager
148
try
149             {
150                 if( null == prefix )
151                 {
152                     m_loggermanager = (LoggerManager)Class.forName( className ).newInstance();
153                 }
154                 else
155                 {
156                     m_loggermanager = (LoggerManager)Class.forName( className )
157                         .getConstructor( new Class JavaDoc[]{String JavaDoc.class} )
158                         .newInstance( new Object JavaDoc[]{prefix} );
159                 }
160             }
161             catch( Exception JavaDoc e )
162             {
163                 throw new ConfigurationException( "Unable to create new logger manager for class " + className, e );
164             }
165
166             // now test for some lifecycle interfaces
167
ContainerUtil.enableLogging(m_loggermanager, m_logger );
168
169             try
170             {
171                 ContainerUtil.contextualize( m_loggermanager, m_context );
172             }
173             catch( ContextException ce )
174             {
175                 throw new ConfigurationException( "Unable to contextualize new logger manager.", ce );
176             }
177
178             try
179             {
180                 ContainerUtil.service( m_loggermanager, m_manager );
181             }
182             catch (ServiceException se )
183             {
184                 throw new ConfigurationException("Unable to service new logger manager.", se);
185             }
186             
187             if( m_loggermanager instanceof Configurable )
188             {
189                 ( (Configurable)m_loggermanager ).configure( configuration.getChildren()[ 0 ] );
190             }
191             else if( m_loggermanager instanceof Parameterizable )
192             {
193                 try
194                 {
195                     ( (Parameterizable)m_loggermanager ).parameterize( Parameters.fromConfiguration( configuration.getChildren()[ 0 ] ) );
196                 }
197                 catch( ParameterException pe )
198                 {
199                     throw new ConfigurationException( "Unable to parameterize new logger manager.", pe );
200                 }
201             }
202
203             try
204             {
205                 ContainerUtil.initialize( m_loggermanager );
206             }
207             catch (Exception JavaDoc e )
208             {
209                 throw new ConfigurationException("Unable to initialize new logger manager.");
210             }
211         }
212         else
213         {
214             // now test for role name
215
final String JavaDoc roleName = configuration.getAttribute( "manager-role", null );
216             if( null == roleName )
217             {
218                 throw new ConfigurationException( "The LoggerManager needs either a manager-role or a manager-class" );
219             }
220
221             try
222             {
223                 m_loggermanager = (LoggerManager)m_manager.lookup( roleName );
224                 m_disposeManager = true;
225             }
226             catch( ServiceException e )
227             {
228                 throw new ConfigurationException( "Unable to lookup logger manager with role " + roleName );
229             }
230         }
231     }
232
233     public void service( ServiceManager manager )
234         throws ServiceException
235     {
236         m_manager = manager;
237     }
238
239     public void dispose()
240     {
241         if( m_disposeManager && null != m_manager )
242         {
243             m_manager.release( m_loggermanager );
244         }
245         m_manager = null;
246         m_loggermanager = null;
247         m_disposeManager = false;
248     }
249
250 }
251
Popular Tags