1 8 package org.apache.avalon.excalibur.logger; 9 10 import java.util.HashMap ; 11 import java.util.Map ; 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 29 public class DefaultLogTargetFactoryManager 30 extends AbstractLogEnabled 31 implements LogTargetFactoryManager, Contextualizable, Configurable 32 { 33 34 private final Map m_factories = new HashMap (); 35 36 37 private Context m_context; 38 39 48 public final LogTargetFactory getLogTargetFactory( final String factoryName ) 49 { 50 final LogTargetFactory factory = (LogTargetFactory) m_factories.get( factoryName ); 51 return factory; 52 } 53 54 60 public final void contextualize( final Context context ) 61 throws ContextException 62 { 63 m_context = context; 64 } 65 66 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 factoryClass = confs[i].getAttribute( "class" ); 79 final String factoryType = confs[i].getAttribute( "type" ); 80 81 final LogTargetFactory logTargetFactory; 84 try 85 { 86 Class clazz = null; 87 88 final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 90 if( null != classLoader ) 91 { 92 try { clazz = classLoader.loadClass( factoryClass ); } 93 catch( final ClassNotFoundException cnfe ) {} 94 } 95 96 if( null == clazz ) 98 { 99 clazz = getClass().getClassLoader().loadClass( factoryClass ); 100 } 101 102 logTargetFactory = (LogTargetFactory)clazz.newInstance(); 103 } 104 catch( final ClassNotFoundException cnfe ) 105 { 106 throw new ConfigurationException( "cannot find LogTargetFactory class " + factoryClass, cnfe ); 107 } 108 catch( final InstantiationException ie ) 109 { 110 throw new ConfigurationException( "cannot instantiate LogTargetFactory class " + factoryClass, ie ); 111 } 112 catch( final IllegalAccessException 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 |