KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.logger;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12 import org.apache.avalon.framework.configuration.Configurable;
13 import org.apache.avalon.framework.configuration.Configuration;
14 import org.apache.avalon.framework.configuration.ConfigurationException;
15 import org.apache.avalon.framework.context.Context;
16 import org.apache.avalon.framework.context.ContextException;
17 import org.apache.avalon.framework.context.Contextualizable;
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.log.LogTarget;
20
21 /**
22  * Default LogTargetManager implementation. It populates the LogTargetManager
23  * from a configuration file.
24  *
25  * @author <a HREF="mailto:giacomo@apache,org">Giacomo Pati</a>
26  * @version CVS $Revision: 1.4 $ $Date: 2001/12/11 09:53:29 $
27  * @since 4.0
28  */

29 public class DefaultLogTargetManager
30     extends AbstractLogEnabled
31     implements LogTargetManager, LogTargetFactoryManageable, Contextualizable, Configurable
32 {
33     /** Map for ID to LogTarget mapping */
34     final private Map JavaDoc m_targets = new HashMap JavaDoc();
35
36     /** The context object */
37     private Context m_context;
38
39     /** The LogTargetFactoryManager object */
40     private LogTargetFactoryManager m_factory_manager;
41
42     /**
43      * Retrieves a LogTarget for an ID. If this LogTargetManager
44      * does not have the match a null will be returned.
45      *
46      * @param ID The LogTarget ID
47      * @return the LogTarget or null if none is found.
48      */

49     public final LogTarget getLogTarget( final String JavaDoc id )
50     {
51         final LogTarget logTarget = (LogTarget) m_targets.get( id );
52         return logTarget;
53     }
54
55     /**
56      * Reads a context object.
57      *
58      * @param context The context object.
59      * @throws ContextException if the context is malformed
60      */

61     public final void contextualize( final Context context )
62         throws ContextException
63     {
64         m_context = context;
65     }
66
67     /**
68      * Gets the LogTargetFactoryManager.
69      */

70     public final void setLogTargetFactoryManager( final LogTargetFactoryManager logTargetFactoryManager )
71     {
72         m_factory_manager = logTargetFactoryManager;
73     }
74
75     /**
76      * Reads a configuration object and creates the log targets.
77      *
78      * @param configuration The configuration object.
79      * @throws ConfigurationException if the configuration is malformed
80      */

81     public final void configure( final Configuration configuration )
82         throws ConfigurationException
83     {
84         if( null == m_factory_manager)
85         {
86             throw new ConfigurationException( "LogTargetFactory not received" );
87         }
88
89         final Configuration [] confs = configuration.getChildren();
90         for( int i = 0; i < confs.length; i++ )
91         {
92             final String JavaDoc targetName = confs[i].getName();
93             final LogTargetFactory logTargetFactory = m_factory_manager.getLogTargetFactory( targetName );
94             final LogTarget logTarget = logTargetFactory.createTarget( confs[i] );
95             final String JavaDoc targetId = confs[i].getAttribute( "id" );
96             if( getLogger().isDebugEnabled() )
97             {
98                 getLogger().debug( "added new LogTarget of id " + targetId );
99             }
100             m_targets.put( targetId, logTarget );
101         }
102     }
103 }
104
Popular Tags