KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > factory > PluginTargetFactory


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.logging.logkit.factory;
19
20 import java.io.File JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
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 /**
52  * A log target factory that establishes log targets based on a
53  * artifact reference.
54  *
55  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
56  * @version $Revision: 1.3 $ $Date: 2004/03/08 11:32:01 $
57  */

58 public class PluginTargetFactory implements LogTargetFactory
59 {
60     //--------------------------------------------------------------
61
// static
62
//--------------------------------------------------------------
63

64     private static final Resources REZ =
65       ResourceManager.getPackageResources( PluginTargetFactory.class );
66
67     //--------------------------------------------------------------
68
// immutable state
69
//--------------------------------------------------------------
70

71     private final LogTargetFactoryBuilder m_builder;
72
73     private final ClassLoader JavaDoc m_classloader;
74
75     private final InitialContext m_context;
76
77     private final Map JavaDoc m_factories = new HashMap JavaDoc();
78
79     //--------------------------------------------------------------
80
// constructor
81
//--------------------------------------------------------------
82

83     public PluginTargetFactory(
84       ClassLoader JavaDoc classloader, InitialContext context, LogTargetFactoryBuilder builder )
85     {
86         m_builder = builder;
87         m_classloader = classloader;
88         m_context = context;
89     }
90
91     //--------------------------------------------------------------
92
// LogTargetFactory
93
//--------------------------------------------------------------
94

95     /**
96      * Create a LogTarget based on a supplied configuration
97      * @param config the target coonfiguration
98      * @return the log target
99      */

100     public LogTarget createTarget( final Configuration config )
101         throws LogTargetException
102     {
103         final String JavaDoc spec = config.getAttribute( "artifact", null );
104         if( null == spec )
105         {
106             final String JavaDoc 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 JavaDoc spec )
116       throws LogTargetException
117     {
118         if( m_factories.containsKey( spec ) )
119         {
120             return (LogTargetFactory) m_factories.get( spec );
121         }
122
123         //
124
// otherwise we need to construct the factory, register it
125
// under the spec key and return it to the client
126
//
127

128         try
129         {
130             final String JavaDoc uri = "artifact:" + spec;
131             Artifact artifact = Artifact.createArtifact( uri );
132             Builder builder =
133               m_context.newBuilder( m_classloader, artifact );
134             Class JavaDoc clazz = builder.getFactoryClass();
135             LogTargetFactory factory =
136               m_builder.buildLogTargetFactory( clazz );
137             m_factories.put( spec, factory );
138             return factory;
139         }
140         catch( Throwable JavaDoc e )
141         {
142             final String JavaDoc error =
143               REZ.getString( "plugin.error.build", spec );
144             throw new LogTargetException( error, e );
145         }
146     }
147 }
148
149
Popular Tags