1 17 18 package org.apache.avalon.logging.logkit.factory; 19 20 import java.io.File ; 21 import java.util.Map ; 22 import java.util.HashMap ; 23 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.logger.Logger; 27 28 import org.apache.avalon.logging.logkit.LogTargetException; 29 import org.apache.avalon.logging.logkit.LogTargetFactory; 30 import org.apache.avalon.logging.logkit.LogTargetManager; 31 import org.apache.avalon.logging.logkit.LogTargetFactoryBuilder; 32 33 import org.apache.avalon.repository.Artifact; 34 import org.apache.avalon.repository.provider.InitialContext; 35 import org.apache.avalon.repository.provider.Builder; 36 import org.apache.avalon.repository.provider.Factory; 37 38 import org.apache.avalon.util.i18n.ResourceManager; 39 import org.apache.avalon.util.i18n.Resources; 40 41 import org.apache.excalibur.configuration.ConfigurationUtil; 42 43 import org.apache.log.LogTarget; 44 import org.apache.log.LogEvent; 45 import org.apache.log.format.ExtendedPatternFormatter; 46 import org.apache.log.format.Formatter; 47 import org.apache.log.format.PatternFormatter; 48 import org.apache.log.format.RawFormatter; 49 import org.apache.log.output.net.DatagramOutputTarget; 50 51 58 public class PluginTargetFactory implements LogTargetFactory 59 { 60 64 private static final Resources REZ = 65 ResourceManager.getPackageResources( PluginTargetFactory.class ); 66 67 71 private final LogTargetFactoryBuilder m_builder; 72 73 private final ClassLoader m_classloader; 74 75 private final InitialContext m_context; 76 77 private final Map m_factories = new HashMap (); 78 79 83 public PluginTargetFactory( 84 ClassLoader classloader, InitialContext context, LogTargetFactoryBuilder builder ) 85 { 86 m_builder = builder; 87 m_classloader = classloader; 88 m_context = context; 89 } 90 91 95 100 public LogTarget createTarget( final Configuration config ) 101 throws LogTargetException 102 { 103 final String spec = config.getAttribute( "artifact", null ); 104 if( null == spec ) 105 { 106 final String error = 107 REZ.getString( "plugin.error.missing-artifact" ); 108 throw new LogTargetException( error ); 109 } 110 111 LogTargetFactory factory = getLogTargetFactory( spec ); 112 return factory.createTarget( config ); 113 } 114 115 private LogTargetFactory getLogTargetFactory( String spec ) 116 throws LogTargetException 117 { 118 if( m_factories.containsKey( spec ) ) 119 { 120 return (LogTargetFactory) m_factories.get( spec ); 121 } 122 123 128 try 129 { 130 final String uri = "artifact:" + spec; 131 Artifact artifact = Artifact.createArtifact( uri ); 132 Builder builder = 133 m_context.newBuilder( m_classloader, artifact ); 134 Class clazz = builder.getFactoryClass(); 135 LogTargetFactory factory = 136 m_builder.buildLogTargetFactory( clazz ); 137 m_factories.put( spec, factory ); 138 return factory; 139 } 140 catch( Throwable e ) 141 { 142 final String error = 143 REZ.getString( "plugin.error.build", spec ); 144 throw new LogTargetException( error, e ); 145 } 146 } 147 } 148 149 | Popular Tags |