KickJava   Java API By Example, From Geeks To Geeks.

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


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.Contextualizable;
17 import org.apache.avalon.framework.context.ContextException;
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.framework.logger.LogEnabled;
20
21 /**
22  * Default LogTargetFactoryManager implementation. It populates the LogTargetFactoryManager
23  * from a configuration file.
24  *
25  * @author <a HREF="mailto:giacomo@apache,org">Giacomo Pati</a>
26  * @version CVS $Revision: 1.5 $ $Date: 2001/12/11 09:53:29 $
27  * @since 4.0
28  */

29 public class DefaultLogTargetFactoryManager
30     extends AbstractLogEnabled
31     implements LogTargetFactoryManager, Contextualizable, Configurable
32 {
33     /** Map for name to logger mapping */
34     private final Map JavaDoc m_factories = new HashMap JavaDoc();
35
36     /** The context object */
37     private Context m_context;
38
39     /**
40      * Retrieves a LogTargetFactory from a name. Usually
41      * the factory name refers to a element name. If
42      * this LogTargetFactoryManager does not have the match a null
43      * will be returned.
44      *
45      * @param factoryName The name of a configured LogTargetFactory.
46      * @return the LogTargetFactory or null if none is found.
47      */

48     public final LogTargetFactory getLogTargetFactory( final String JavaDoc factoryName )
49     {
50         final LogTargetFactory factory = (LogTargetFactory) m_factories.get( factoryName );
51         return factory;
52     }
53
54     /**
55      * Reads a context object.
56      *
57      * @param context The context object.
58      * @throws ContextException if the context is malformed
59      */

60     public final void contextualize( final Context context )
61         throws ContextException
62     {
63         m_context = context;
64     }
65
66     /**
67      * Reads a configuration object and creates the category mapping.
68      *
69      * @param configuration The configuration object.
70      * @throws ConfigurationException if the configuration is malformed
71      */

72     public final void configure( final Configuration configuration )
73         throws ConfigurationException
74     {
75         final Configuration [] confs = configuration.getChildren( "factory" );
76         for( int i = 0; i < confs.length; i++ )
77         {
78             final String JavaDoc factoryClass = confs[i].getAttribute( "class" );
79             final String JavaDoc factoryType = confs[i].getAttribute( "type" );
80
81             //FIXME(GP): Is this the right way to load a class or
82
// should the ContextClassLoader be used?
83
final LogTargetFactory logTargetFactory;
84             try
85             {
86                 Class JavaDoc clazz = null;
87
88                 //First lets try the context ClassLoader
89
final ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
90                 if( null != classLoader )
91                 {
92                     try { clazz = classLoader.loadClass( factoryClass ); }
93                     catch( final ClassNotFoundException JavaDoc cnfe ) {}
94                 }
95
96                 //Okay now lets try classLoader this class was loaded from
97
if( null == clazz )
98                 {
99                     clazz = getClass().getClassLoader().loadClass( factoryClass );
100                 }
101
102                 logTargetFactory = (LogTargetFactory)clazz.newInstance();
103             }
104             catch( final ClassNotFoundException JavaDoc cnfe )
105             {
106                 throw new ConfigurationException( "cannot find LogTargetFactory class " + factoryClass, cnfe );
107             }
108             catch( final InstantiationException JavaDoc ie )
109             {
110                 throw new ConfigurationException( "cannot instantiate LogTargetFactory class " + factoryClass, ie );
111             }
112             catch( final IllegalAccessException JavaDoc iae )
113             {
114                 throw new ConfigurationException( "cannot access LogTargetFactory class " + factoryClass, iae );
115             }
116
117             if( logTargetFactory instanceof LogEnabled )
118             {
119                 ((LogEnabled) logTargetFactory).enableLogging( getLogger() );
120             }
121
122             if( logTargetFactory instanceof Configurable )
123             {
124                 ((Configurable) logTargetFactory).configure( confs[i] );
125             }
126
127             if( logTargetFactory instanceof Contextualizable )
128             {
129                 try
130                 {
131                     ((Contextualizable) logTargetFactory).contextualize( m_context );
132                 }
133                 catch( final ContextException ce )
134                 {
135                     throw new ConfigurationException( "cannot contextualize LogTargetFactory " + factoryClass, ce );
136                 }
137             }
138
139             if( logTargetFactory instanceof LogTargetFactoryManageable )
140             {
141                 ((LogTargetFactoryManageable) logTargetFactory).setLogTargetFactoryManager( this );
142             }
143
144             if( getLogger().isDebugEnabled() )
145             {
146                 getLogger().debug( "added new LogTargetFactory of type " + factoryType );
147             }
148             m_factories.put( factoryType, logTargetFactory );
149         }
150     }
151 }
152
Popular Tags