KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > impl > DefaultLoggingCriteria


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.impl;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.apache.avalon.framework.logger.Logger;
26
27 import org.apache.avalon.logging.provider.LoggingCriteria;
28 import org.apache.avalon.logging.provider.LoggingRuntimeException;
29
30 import org.apache.avalon.repository.provider.InitialContext;
31
32 import org.apache.avalon.util.criteria.Criteria;
33 import org.apache.avalon.util.criteria.Parameter;
34
35 import org.apache.avalon.util.defaults.Defaults;
36 import org.apache.avalon.util.defaults.DefaultsBuilder;
37
38 import org.apache.avalon.util.i18n.ResourceManager;
39 import org.apache.avalon.util.i18n.Resources;
40
41 /**
42  * DefaultLoggingCriteria is a class holding the values supplied by a user
43  * for application to a LoggingManager factory.
44  *
45  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
46  * @version $Revision: 1.3 $
47  */

48 public class DefaultLoggingCriteria extends Criteria implements LoggingCriteria
49 {
50     //--------------------------------------------------------------
51
// static
52
//--------------------------------------------------------------
53

54     private static final String JavaDoc[] KEYS =
55       new String JavaDoc[]{
56         LOGGING_CONFIGURATION_KEY,
57         LOGGING_BASEDIR_KEY,
58         LOGGING_DEBUG_KEY,
59         LOGGING_BOOTSTRAP_KEY,
60         LOGGING_INTERVAL_KEY };
61
62     private static final String JavaDoc DEFAULTS = "/avalon.logging.properties";
63
64     private static final Resources REZ =
65       ResourceManager.getPackageResources( DefaultLoggingCriteria.class );
66
67    /**
68     * The factory parameters template.
69     * @return the set of parameters constraining the criteria
70     */

71     private static Parameter[] buildParameters( InitialContext context )
72     {
73         return new Parameter[] {
74             new ConfigurationParameter(
75               LOGGING_CONFIGURATION_KEY ),
76             new Parameter(
77               LOGGING_BASEDIR_KEY,
78               File JavaDoc.class,
79               context.getInitialWorkingDirectory() ),
80             new Parameter(
81               LOGGING_DEBUG_KEY,
82               Boolean JavaDoc.class,
83               new Boolean JavaDoc( false ) ),
84             new LoggerParameter(
85               LOGGING_BOOTSTRAP_KEY,
86               new ConsoleLogger( ConsoleLogger.LEVEL_WARN ) ),
87             new Parameter(
88               LOGGING_INTERVAL_KEY,
89               Long JavaDoc.class,
90               new Long JavaDoc( -1 ) )
91         };
92     }
93
94     //--------------------------------------------------------------
95
// immutable state
96
//--------------------------------------------------------------
97

98     private final InitialContext m_context;
99
100     //--------------------------------------------------------------
101
// constructor
102
//--------------------------------------------------------------
103

104    /**
105     * Creation of a new default logging criteria.
106     * @param context the initial repository context
107     */

108     public DefaultLoggingCriteria( InitialContext context )
109     {
110         super( buildParameters( context ) );
111         m_context = context;
112
113         try
114         {
115             //
116
// get the properties declared relative to the application
117
//
118

119             final String JavaDoc key = context.getApplicationKey();
120             final File JavaDoc work = context.getInitialWorkingDirectory();
121             DefaultsBuilder builder = new DefaultsBuilder( key, work );
122             Properties JavaDoc defaults =
123               Defaults.getStaticProperties( LoggingCriteria.class );
124
125             final String JavaDoc[] keys = super.getKeys();
126             Properties JavaDoc properties =
127               builder.getConsolidatedProperties( defaults, keys );
128
129             //
130
// apply any non-null properties to the criteria
131
//
132

133             for( int i=0; i<keys.length; i++ )
134             {
135                 final String JavaDoc propertyKey = keys[i];
136                 final String JavaDoc value = properties.getProperty( propertyKey );
137                 if( null != value )
138                 {
139                     put( propertyKey, value );
140                 }
141             }
142         }
143         catch ( IOException JavaDoc e )
144         {
145             throw new LoggingRuntimeException(
146              "Failed to load implementation default resources.", e );
147         }
148     }
149
150     //--------------------------------------------------------------
151
// LoggingCriteria
152
//--------------------------------------------------------------
153

154    /**
155     * Set the debug enabled policy
156     * @param mode TRUE to enabled debug mode else FALSE
157     */

158     public void setDebugEnabled( boolean mode )
159     {
160         put( LOGGING_DEBUG_KEY, new Boolean JavaDoc( mode ) );
161     }
162
163    /**
164     * Set the bootstrap logging channel
165     * @param logger the boootstrap logging channel
166     */

167     public void setBootstrapLogger( Logger logger )
168     {
169         put( LOGGING_BOOTSTRAP_KEY, logger );
170     }
171
172    /**
173     * Set the base directory.
174     * @param dir the base directory
175     */

176     public void setBaseDirectory( File JavaDoc dir )
177     {
178         put( LOGGING_BASEDIR_KEY, dir );
179     }
180
181    /**
182     * Set the configuration URL.
183     * @param url the configuration URL
184     */

185     public void setLoggingConfiguration( URL JavaDoc url )
186     {
187         put( LOGGING_CONFIGURATION_KEY, url );
188     }
189
190    /**
191     * Get the bootstrap logging channel
192     * @return the boootstrap logging channel
193     */

194     public Logger getBootstrapLogger()
195     {
196         return (Logger) get( LOGGING_BOOTSTRAP_KEY );
197     }
198
199    /**
200     * Returns the base directory for logging resources.
201     * @return the base directory
202     */

203     public File JavaDoc getBaseDirectory()
204     {
205         return (File JavaDoc) get( LOGGING_BASEDIR_KEY );
206     }
207
208    /**
209     * Returns debug policy. If TRUE all logging channels will be
210     * set to debug level.
211     *
212     * @return the debug policy
213     */

214     public boolean isDebugEnabled()
215     {
216         Boolean JavaDoc value = (Boolean JavaDoc) get( LOGGING_DEBUG_KEY );
217         if( null != value )
218             return value.booleanValue();
219         return false;
220     }
221
222    /**
223     * Returns an external logging system configuration file
224     * @return the configuration file (possibly null)
225     */

226     public URL JavaDoc getLoggingConfiguration()
227     {
228         return (URL JavaDoc) get( LOGGING_CONFIGURATION_KEY );
229     }
230
231     /** Returns the logging configuration update interval.
232      */

233     public long getUpdateInterval()
234     {
235         Long JavaDoc value = (Long JavaDoc) get( LOGGING_INTERVAL_KEY );
236         if( null != value )
237             return value.longValue();
238         return -1;
239     }
240     
241     private static File JavaDoc getCanonicalForm( File JavaDoc file )
242     {
243         try
244         {
245             return file.getCanonicalFile();
246         }
247         catch( Throwable JavaDoc e )
248         {
249             final String JavaDoc error =
250               REZ.getString(
251                 "criteria.artifact.cononical.error",
252                 file.toString() );
253             throw new LoggingRuntimeException( error, e );
254         }
255     }
256 }
257
Popular Tags