1 17 18 package org.apache.avalon.logging.impl; 19 20 import java.io.File ; 21 import java.net.URL ; 22 import java.io.IOException ; 23 import java.util.Properties ; 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 48 public class DefaultLoggingCriteria extends Criteria implements LoggingCriteria 49 { 50 54 private static final String [] KEYS = 55 new String []{ 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 DEFAULTS = "/avalon.logging.properties"; 63 64 private static final Resources REZ = 65 ResourceManager.getPackageResources( DefaultLoggingCriteria.class ); 66 67 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 .class, 79 context.getInitialWorkingDirectory() ), 80 new Parameter( 81 LOGGING_DEBUG_KEY, 82 Boolean .class, 83 new Boolean ( false ) ), 84 new LoggerParameter( 85 LOGGING_BOOTSTRAP_KEY, 86 new ConsoleLogger( ConsoleLogger.LEVEL_WARN ) ), 87 new Parameter( 88 LOGGING_INTERVAL_KEY, 89 Long .class, 90 new Long ( -1 ) ) 91 }; 92 } 93 94 98 private final InitialContext m_context; 99 100 104 108 public DefaultLoggingCriteria( InitialContext context ) 109 { 110 super( buildParameters( context ) ); 111 m_context = context; 112 113 try 114 { 115 119 final String key = context.getApplicationKey(); 120 final File work = context.getInitialWorkingDirectory(); 121 DefaultsBuilder builder = new DefaultsBuilder( key, work ); 122 Properties defaults = 123 Defaults.getStaticProperties( LoggingCriteria.class ); 124 125 final String [] keys = super.getKeys(); 126 Properties properties = 127 builder.getConsolidatedProperties( defaults, keys ); 128 129 133 for( int i=0; i<keys.length; i++ ) 134 { 135 final String propertyKey = keys[i]; 136 final String value = properties.getProperty( propertyKey ); 137 if( null != value ) 138 { 139 put( propertyKey, value ); 140 } 141 } 142 } 143 catch ( IOException e ) 144 { 145 throw new LoggingRuntimeException( 146 "Failed to load implementation default resources.", e ); 147 } 148 } 149 150 154 158 public void setDebugEnabled( boolean mode ) 159 { 160 put( LOGGING_DEBUG_KEY, new Boolean ( mode ) ); 161 } 162 163 167 public void setBootstrapLogger( Logger logger ) 168 { 169 put( LOGGING_BOOTSTRAP_KEY, logger ); 170 } 171 172 176 public void setBaseDirectory( File dir ) 177 { 178 put( LOGGING_BASEDIR_KEY, dir ); 179 } 180 181 185 public void setLoggingConfiguration( URL url ) 186 { 187 put( LOGGING_CONFIGURATION_KEY, url ); 188 } 189 190 194 public Logger getBootstrapLogger() 195 { 196 return (Logger) get( LOGGING_BOOTSTRAP_KEY ); 197 } 198 199 203 public File getBaseDirectory() 204 { 205 return (File ) get( LOGGING_BASEDIR_KEY ); 206 } 207 208 214 public boolean isDebugEnabled() 215 { 216 Boolean value = (Boolean ) get( LOGGING_DEBUG_KEY ); 217 if( null != value ) 218 return value.booleanValue(); 219 return false; 220 } 221 222 226 public URL getLoggingConfiguration() 227 { 228 return (URL ) get( LOGGING_CONFIGURATION_KEY ); 229 } 230 231 233 public long getUpdateInterval() 234 { 235 Long value = (Long ) get( LOGGING_INTERVAL_KEY ); 236 if( null != value ) 237 return value.longValue(); 238 return -1; 239 } 240 241 private static File getCanonicalForm( File file ) 242 { 243 try 244 { 245 return file.getCanonicalFile(); 246 } 247 catch( Throwable e ) 248 { 249 final String error = 250 REZ.getString( 251 "criteria.artifact.cononical.error", 252 file.toString() ); 253 throw new LoggingRuntimeException( error, e ); 254 } 255 } 256 } 257 | Popular Tags |