KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > log4j > Log4JLoggingFactory


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

18
19 package org.apache.avalon.logging.log4j;
20
21 import java.io.File JavaDoc;
22
23 import java.net.URL JavaDoc;
24
25 import java.util.Map JavaDoc;
26
27 import org.apache.avalon.logging.impl.DefaultLoggingCriteria;
28 import org.apache.avalon.logging.provider.LoggingCriteria;
29 import org.apache.avalon.logging.provider.LoggingFactory;
30 import org.apache.avalon.logging.provider.LoggingManager;
31 import org.apache.avalon.logging.provider.LoggingException;
32
33 import org.apache.avalon.repository.provider.InitialContext;
34
35 import org.apache.avalon.util.i18n.ResourceManager;
36 import org.apache.avalon.util.i18n.Resources;
37
38 import org.apache.log4j.PropertyConfigurator;
39 import org.apache.log4j.xml.DOMConfigurator;
40
41 /**
42  * A Log4J factory.
43  *
44  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
45  * @version $Revision: 1.10 $
46  */

47 public class Log4JLoggingFactory
48     implements LoggingFactory
49 {
50     private static final Resources REZ =
51       ResourceManager.getPackageResources( Log4JLoggingFactory.class );
52     
53     private final ClassLoader JavaDoc m_Classloader;
54     private final InitialContext m_Context;
55     private File JavaDoc m_BaseDirectory;
56    
57    /**
58     * Creation of a new default factory.
59     * @param context the repository inital context
60     * @param classloader the factory classloader
61     */

62     public Log4JLoggingFactory( InitialContext context, ClassLoader JavaDoc classloader )
63     {
64         m_Context = context;
65         m_Classloader = classloader;
66     }
67
68     //--------------------------------------------------------------------------
69
// LoggingFactory
70
//--------------------------------------------------------------------------
71

72    /**
73     * Return of map containing the default parameters.
74     *
75     * @return the default parameters
76     */

77     public LoggingCriteria createDefaultLoggingCriteria()
78     {
79         return new DefaultLoggingCriteria( m_Context );
80     }
81
82    /**
83     * Create a new LoggingManager using the supplied logging criteria.
84     *
85     * @param criteria the logging system factory criteria
86     * @exception LoggingException is a logging system creation error occurs
87     */

88     public LoggingManager createLoggingManager( LoggingCriteria criteria )
89       throws LoggingException
90     {
91         try
92         {
93             return (LoggingManager) create( criteria );
94         }
95         catch( Throwable JavaDoc e )
96         {
97             final String JavaDoc error =
98               "Cannot build logging manager.";
99             throw new LoggingException( error, e );
100         }
101     }
102
103     //--------------------------------------------------------------------------
104
// Factory
105
//--------------------------------------------------------------------------
106

107    /**
108     * Return a new instance of default criteria for the factory.
109     * @return a new criteria instance
110     */

111     public Map JavaDoc createDefaultCriteria()
112     {
113         return createDefaultLoggingCriteria();
114     }
115
116    /**
117     * Create a new instance of an application.
118     * @return the application instance
119     */

120     public Object JavaDoc create() throws Exception JavaDoc
121     {
122         return new LoggingManagerImpl();
123     }
124
125    /**
126     * Create a new instance of an application.
127     * @param criteriaMap the creation criteria
128     * @return the application instance
129     */

130     public Object JavaDoc create( Map JavaDoc criteriaMap )
131         throws Exception JavaDoc
132     {
133         if( null == criteriaMap )
134         {
135             throw new NullPointerException JavaDoc( "criteriaMap" );
136         }
137         LoggingCriteria criteria = getLoggingCriteria( criteriaMap );
138         m_BaseDirectory = criteria.getBaseDirectory();
139         
140         String JavaDoc cwd = System.getProperty( "user.dir" );
141         try
142         {
143             System.setProperty( "user.dir", m_BaseDirectory.getAbsolutePath() );
144             URL JavaDoc conf = criteria.getLoggingConfiguration();
145             long updateInterval = criteria.getUpdateInterval();
146             configure( conf, updateInterval );
147             return new LoggingManagerImpl();
148         } finally
149         {
150             System.setProperty( "user.dir", cwd );
151         }
152     }
153
154     private void configure( URL JavaDoc url, long interval )
155     {
156         String JavaDoc src = url.toExternalForm();
157         if( src.startsWith( "file:" ) )
158         {
159             src = src.substring( 5 );
160             while( src.startsWith( "/" ) )
161                 src = src.substring( 1 );
162             configureFile( src, interval );
163         }
164         configureURL( url );
165     }
166     
167     private void configureFile( String JavaDoc src, long interval )
168     {
169         if( interval > 0 )
170         {
171             if( src.endsWith( ".xml" ) )
172             {
173                 DOMConfigurator.configureAndWatch( src, interval );
174             }
175             else
176             {
177                 PropertyConfigurator.configureAndWatch( src, interval );
178             }
179         }
180         else
181         {
182             if( src.endsWith( ".xml" ) )
183             {
184                 DOMConfigurator.configureAndWatch( src );
185             }
186             else
187             {
188                 PropertyConfigurator.configureAndWatch( src );
189             }
190         }
191     }
192     
193     private void configureURL( URL JavaDoc url )
194     {
195         String JavaDoc src = url.toExternalForm();
196         
197         if( src.endsWith( ".xml" ) )
198         {
199             DOMConfigurator.configure( url );
200         }
201         else
202         {
203             PropertyConfigurator.configure( url );
204         }
205     }
206
207     private LoggingCriteria getLoggingCriteria( Map JavaDoc criteriaMap )
208     {
209         if( criteriaMap instanceof LoggingCriteria )
210         {
211             return (LoggingCriteria) criteriaMap;
212         }
213         else
214         {
215             final String JavaDoc error =
216               REZ.getString(
217                 "factory.bad-criteria",
218                 criteriaMap.getClass().getName() );
219             throw new IllegalArgumentException JavaDoc( error );
220         }
221     }
222 }
223
Popular Tags