KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > logkit > LogKitConfHelper


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.logkit;
20
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.apache.avalon.excalibur.logger.DefaultLogTargetFactoryManager;
26 import org.apache.avalon.excalibur.logger.DefaultLogTargetManager;
27 import org.apache.avalon.excalibur.logger.LogTargetFactoryManager;
28 import org.apache.avalon.excalibur.logger.LogTargetManager;
29 import org.apache.avalon.excalibur.logger.util.LoggerUtil;
30 import org.apache.avalon.framework.activity.Disposable;
31 import org.apache.avalon.framework.configuration.Configurable;
32 import org.apache.avalon.framework.configuration.Configuration;
33 import org.apache.avalon.framework.configuration.ConfigurationException;
34 import org.apache.avalon.framework.container.ContainerUtil;
35 import org.apache.avalon.framework.context.Context;
36 import org.apache.avalon.framework.context.ContextException;
37 import org.apache.avalon.framework.context.Contextualizable;
38 import org.apache.avalon.framework.logger.AbstractLogEnabled;
39 import org.apache.log.Hierarchy;
40 import org.apache.log.LogTarget;
41 import org.apache.log.Priority;
42 import org.apache.log.util.Closeable;
43
44 /**
45  * Tie this object to a LoggerManagerTee, give it the Hierachy
46  * that LogKitAdapter operates upon and it will populate it
47  * from the Configuration object passed via configure().
48  * Note: this class assumes that this is a new Hierarchy,
49  * freshly created with new Hierarchy() not populated before.
50  *
51  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
52  * @version CVS $Revision: 1.6 $ $Date: 2004/03/10 13:54:51 $
53  * @since 4.0
54  */

55 public class LogKitConfHelper extends AbstractLogEnabled implements
56         Contextualizable,
57         Configurable,
58         Disposable
59 {
60     /* The hierarchy to operate upon */
61     private final Hierarchy m_hierarchy;
62
63     /* Creates an instance of LogKitLoggerHelper. */
64     public LogKitConfHelper( final Hierarchy hierarchy )
65     {
66         if ( hierarchy == null ) throw new NullPointerException JavaDoc( "hierarchy" );
67         m_hierarchy = hierarchy;
68     }
69
70     /** Set of log targets */
71     final private Set JavaDoc m_targets = new HashSet JavaDoc();
72
73     /** The context object */
74     private Context m_context;
75
76     /**
77      * Reads a context object that will be supplied to the log target factory manager.
78      *
79      * @param context The context object.
80      * @throws ContextException if the context is malformed
81      */

82     public final void contextualize( final Context context )
83             throws ContextException
84     {
85         m_context = context;
86     }
87
88     /**
89      * Populates the underlying <code>Hierarchy</code>.
90      *
91      * @param configuration The configuration object.
92      * @throws ConfigurationException if the configuration is malformed
93      */

94     public final void configure( final Configuration configuration )
95         throws ConfigurationException
96     {
97         final Configuration factories = configuration.getChild( "factories" );
98         final LogTargetFactoryManager targetFactoryManager = setupTargetFactoryManager( factories );
99
100         final Configuration targets = configuration.getChild( "targets" );
101         final LogTargetManager targetManager = setupTargetManager( targets, targetFactoryManager );
102
103         final Configuration categories = configuration.getChild( "categories" );
104         setupLoggers( targetManager,
105                       null,
106                       categories,
107                       true,
108                       categories.getAttributeAsBoolean( "additive", false ) );
109     }
110
111     /**
112      * Setup a LogTargetFactoryManager
113      *
114      * @param configuration The configuration object.
115      * @throws ConfigurationException if the configuration is malformed
116      */

117     private final LogTargetFactoryManager setupTargetFactoryManager(
118             final Configuration configuration )
119         throws ConfigurationException
120     {
121         final DefaultLogTargetFactoryManager targetFactoryManager = new DefaultLogTargetFactoryManager();
122
123         ContainerUtil.enableLogging( targetFactoryManager, getLogger() );
124
125         try
126         {
127             ContainerUtil.contextualize( targetFactoryManager, m_context );
128         }
129         catch( final ContextException ce )
130         {
131             throw new ConfigurationException( "cannot contextualize default factory manager", ce );
132         }
133
134         ContainerUtil.configure( targetFactoryManager, configuration );
135
136         return targetFactoryManager;
137     }
138
139     /**
140      * Setup a LogTargetManager
141      *
142      * @param configuration The configuration object.
143      * @throws ConfigurationException if the configuration is malformed
144      */

145     private final LogTargetManager setupTargetManager( final Configuration configuration,
146             final LogTargetFactoryManager targetFactoryManager )
147         throws ConfigurationException
148     {
149         final DefaultLogTargetManager targetManager = new DefaultLogTargetManager();
150
151         ContainerUtil.enableLogging( targetManager, getLogger() );
152
153         targetManager.setLogTargetFactoryManager( targetFactoryManager );
154
155         ContainerUtil.configure( targetManager, configuration );
156
157         return targetManager;
158     }
159
160     /**
161      * Setup Loggers
162      *
163      * @param parentElement The array object of configurations for categories.
164      * @param root shows if we're processing the root of the configuration
165      * @throws ConfigurationException if the configuration is malformed
166      */

167     private final void setupLoggers( final LogTargetManager targetManager,
168                                      final String JavaDoc parentCategory,
169                                      final Configuration parentElement,
170                                      boolean root,
171                                      final boolean defaultAdditive )
172         throws ConfigurationException
173     {
174         boolean rootLoggerConfigured = false;
175
176         final Configuration[] categories = parentElement.getChildren( "category" );
177
178         if( null != categories )
179         {
180             for( int i = 0; i < categories.length; i++ )
181             {
182                 final Configuration category = categories[ i ];
183                 final String JavaDoc name = category.getAttribute( "name" );
184                 final String JavaDoc loglevel = category.getAttribute( "log-level" ).toUpperCase();
185                 final boolean additive = category.
186                     getAttributeAsBoolean( "additive", defaultAdditive );
187
188                 final Configuration[] targets = category.getChildren( "log-target" );
189                 final LogTarget[] logTargets = new LogTarget[ targets.length ];
190                 for( int j = 0; j < targets.length; j++ )
191                 {
192                     final String JavaDoc id = targets[ j ].getAttribute( "id-ref" );
193                     logTargets[ j ] = targetManager.getLogTarget( id );
194                     if( !m_targets.contains( logTargets[ j ] ) )
195                     {
196                         m_targets.add( logTargets[ j ] );
197                     }
198                 }
199
200                 final String JavaDoc fullCategory;
201                 final org.apache.log.Logger logger;
202
203                 if ( "".equals( name ) )
204                 {
205                     if ( !root )
206                     {
207                         final String JavaDoc message = "'category' element with empty name not " +
208                                 "at the root level: " + category.getLocation();
209                         throw new ConfigurationException( message );
210                     }
211
212                     if ( logTargets.length == 0 )
213                     {
214                         final String JavaDoc message = "At least one log-target should be " +
215                                 "specified for the root category " + category.getLocation();
216                         throw new ConfigurationException( message );
217                     }
218
219                     fullCategory = null;
220                     logger = m_hierarchy.getRootLogger();
221                     rootLoggerConfigured = true;
222                 }
223                 else
224                 {
225                     fullCategory = LoggerUtil.getFullCategoryName( parentCategory, name );
226                     logger = m_hierarchy.getLoggerFor( fullCategory );
227                 }
228
229                 if( getLogger().isDebugEnabled() )
230                 {
231                     /**
232                      * We have to identify ourselves now via 'LogKitConfHelper:'
233                      * because we are likely to be logging to a shared bootstrap
234                      * logger, not to a dedicated category Logger.
235                      */

236                     final String JavaDoc message = "LogKitConfHelper: adding logger for category '" +
237                             ( fullCategory != null ? fullCategory : "" ) + "'";
238                     getLogger().debug( message );
239                 }
240
241                 logger.setPriority( Priority.getPriorityForName( loglevel ) );
242                 logger.setLogTargets( logTargets );
243                 logger.setAdditivity( additive );
244
245                 setupLoggers( targetManager, fullCategory, category, false, defaultAdditive );
246             }
247         }
248
249         if ( root && !rootLoggerConfigured )
250         {
251             final String JavaDoc message =
252                     "No configuration for root category (<category name=''/>) found in "+
253                             parentElement.getLocation();
254             throw new ConfigurationException( message );
255         }
256     }
257
258     /**
259      * Closes all our LogTargets.
260      */

261     public void dispose()
262     {
263         final Iterator JavaDoc iterator = m_targets.iterator();
264         while( iterator.hasNext() )
265         {
266             final LogTarget target = (LogTarget)iterator.next();
267             if( target instanceof Closeable )
268             {
269                 ( (Closeable)target ).close();
270             }
271         }
272     }
273 }
274
Popular Tags