1 17 18 package org.apache.avalon.logging.logkit; 19 20 import java.io.File ; 21 22 import java.lang.reflect.Constructor ; 23 24 import org.apache.avalon.framework.logger.Logger; 25 26 import org.apache.avalon.logging.provider.LoggingException; 27 28 import org.apache.avalon.repository.provider.InitialContext; 29 30 import org.apache.avalon.util.i18n.ResourceManager; 31 import org.apache.avalon.util.i18n.Resources; 32 33 37 public class DefaultLogTargetFactoryBuilder implements LogTargetFactoryBuilder 38 { 39 43 private static final Resources REZ = 44 ResourceManager.getPackageResources( DefaultLogTargetFactoryBuilder.class ); 45 46 private static final FormatterFactory FORMATTER = 47 new DefaultFormatterFactory(); 48 49 53 private final ClassLoader m_classloader; 54 private final InitialContext m_context; 55 private final Logger m_logger; 56 private final File m_basedir; 57 private final LogTargetFactoryManager m_factories; 58 private final LogTargetManager m_targets; 59 60 64 69 public DefaultLogTargetFactoryBuilder( 70 InitialContext context, ClassLoader classloader, Logger logger, File basedir, 71 LogTargetFactoryManager factories, LogTargetManager targets ) 72 { 73 m_context = context; 74 m_classloader = classloader; 75 m_logger = logger; 76 m_basedir = basedir; 77 m_factories = factories; 78 m_targets = targets; 79 } 80 81 85 97 public LogTargetFactory buildLogTargetFactory( Class clazz ) 98 throws LoggingException 99 { 100 Constructor [] constructors = clazz.getConstructors(); 101 if( constructors.length < 1 ) 102 { 103 final String error = 104 REZ.getString( 105 "factory.error.no-constructor", 106 clazz.getName() ); 107 throw new LoggingException( error ); 108 } 109 110 114 Constructor constructor = constructors[0]; 115 Class [] classes = constructor.getParameterTypes(); 116 Object [] args = new Object [ classes.length ]; 117 for( int i=0; i<classes.length; i++ ) 118 { 119 Class c = classes[i]; 120 if( File .class.isAssignableFrom( c ) ) 121 { 122 args[i] = m_basedir; 123 } 124 else if( Logger.class.isAssignableFrom( c ) ) 125 { 126 args[i] = m_logger; 127 } 128 else if( LogTargetFactoryManager.class.isAssignableFrom( c ) ) 129 { 130 args[i] = m_factories; 131 } 132 else if( LogTargetManager.class.isAssignableFrom( c ) ) 133 { 134 args[i] = m_targets; 135 } 136 else if( FormatterFactory.class.isAssignableFrom( c ) ) 137 { 138 args[i] = FORMATTER; 139 } 140 else if( ClassLoader .class.isAssignableFrom( c ) ) 141 { 142 args[i] = m_classloader; 143 } 144 else if( InitialContext.class.isAssignableFrom( c ) ) 145 { 146 args[i] = m_context; 147 } 148 else if( LogTargetFactoryBuilder.class.isAssignableFrom( c ) ) 149 { 150 args[i] = this; 151 } 152 else 153 { 154 final String error = 155 REZ.getString( 156 "factory.error.unrecognized-parameter", 157 c.getName(), 158 clazz.getName() ); 159 throw new LoggingException( error ); 160 } 161 } 162 163 167 return instantiateLogTargetFactory( constructor, args ); 168 } 169 170 179 private LogTargetFactory instantiateLogTargetFactory( 180 Constructor constructor, Object [] args ) 181 throws LoggingException 182 { 183 Class clazz = constructor.getDeclaringClass(); 184 try 185 { 186 return (LogTargetFactory) constructor.newInstance( args ); 187 } 188 catch( Throwable e ) 189 { 190 final String error = 191 REZ.getString( 192 "target.error.instantiation", 193 clazz.getName() ); 194 throw new LoggingException( error, e ); 195 } 196 } 197 } 198 | Popular Tags |