KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > DefaultLogTargetFactoryBuilder


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;
19
20 import java.io.File JavaDoc;
21
22 import java.lang.reflect.Constructor JavaDoc;
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 /**
34  * The DefaultLoggingFactory provides support for the establishment of a
35  * new logging system using LogKit as the implementation.
36  */

37 public class DefaultLogTargetFactoryBuilder implements LogTargetFactoryBuilder
38 {
39     //--------------------------------------------------------------------------
40
// static
41
//--------------------------------------------------------------------------
42

43     private static final Resources REZ =
44       ResourceManager.getPackageResources( DefaultLogTargetFactoryBuilder.class );
45
46     private static final FormatterFactory FORMATTER =
47       new DefaultFormatterFactory();
48
49     //--------------------------------------------------------------------------
50
// immutable state
51
//--------------------------------------------------------------------------
52

53     private final ClassLoader JavaDoc m_classloader;
54     private final InitialContext m_context;
55     private final Logger m_logger;
56     private final File JavaDoc m_basedir;
57     private final LogTargetFactoryManager m_factories;
58     private final LogTargetManager m_targets;
59     
60     //--------------------------------------------------------------------------
61
// constructor
62
//--------------------------------------------------------------------------
63

64    /**
65     * Creation of a new default factory.
66     * @param context the repository inital context
67     * @param classloader the factory classloader
68     */

69     public DefaultLogTargetFactoryBuilder(
70       InitialContext context, ClassLoader JavaDoc classloader, Logger logger, File JavaDoc 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     //--------------------------------------------------------------------------
82
// implementation
83
//--------------------------------------------------------------------------
84

85    /**
86     * Build a log target factory using a supplied class. The implementation
87     * checks the first available constructor arguments and builds a set of
88     * arguments based on the arguments supplied to this builder instance.
89     *
90     * @param clazz the log target factory class
91     * @return a instance of the class
92     * @exception LoggingException if the class does not expose a public
93     * constructor, or the constructor requires arguments that the
94     * builder cannot resolve, or if a unexpected instantiation error
95     * ooccurs
96     */

97     public LogTargetFactory buildLogTargetFactory( Class JavaDoc clazz )
98       throws LoggingException
99     {
100         Constructor JavaDoc[] constructors = clazz.getConstructors();
101         if( constructors.length < 1 )
102         {
103             final String JavaDoc error =
104               REZ.getString(
105                 "factory.error.no-constructor",
106                 clazz.getName() );
107             throw new LoggingException( error );
108         }
109
110         //
111
// log target factories only have one constructor
112
//
113

114         Constructor JavaDoc constructor = constructors[0];
115         Class JavaDoc[] classes = constructor.getParameterTypes();
116         Object JavaDoc[] args = new Object JavaDoc[ classes.length ];
117         for( int i=0; i<classes.length; i++ )
118         {
119             Class JavaDoc c = classes[i];
120             if( File JavaDoc.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 JavaDoc.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 JavaDoc error =
155                   REZ.getString(
156                     "factory.error.unrecognized-parameter",
157                     c.getName(),
158                     clazz.getName() );
159                 throw new LoggingException( error );
160             }
161         }
162
163         //
164
// instantiate the factory
165
//
166

167         return instantiateLogTargetFactory( constructor, args );
168     }
169
170    /**
171     * Instantiation of a factory instance using a supplied constructor
172     * and arguments.
173     *
174     * @param constructor the factory constructor
175     * @param args the constructor arguments
176     * @return the factory instance
177     * @exception LoggingException if an instantiation error occurs
178     */

179     private LogTargetFactory instantiateLogTargetFactory(
180       Constructor JavaDoc constructor, Object JavaDoc[] args )
181       throws LoggingException
182     {
183         Class JavaDoc clazz = constructor.getDeclaringClass();
184         try
185         {
186             return (LogTargetFactory) constructor.newInstance( args );
187         }
188         catch( Throwable JavaDoc e )
189         {
190             final String JavaDoc error =
191               REZ.getString(
192                 "target.error.instantiation",
193                 clazz.getName() );
194             throw new LoggingException( error, e );
195         }
196     }
197 }
198
Popular Tags