1 17 18 package org.apache.avalon.logging.logkit; 19 20 import java.io.File ; 21 import java.io.InputStream ; 22 import java.net.URL ; 23 import java.util.Map ; 24 import java.util.HashMap ; 25 import java.util.ArrayList ; 26 27 import org.apache.avalon.framework.logger.Logger; 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 31 32 import org.apache.avalon.logging.impl.ConsoleLogger; 33 import org.apache.avalon.logging.impl.DefaultLoggingCriteria; 34 35 import org.apache.avalon.logging.data.CategoriesDirective; 36 import org.apache.avalon.logging.data.CategoryDirective; 37 38 import org.apache.avalon.logging.provider.LoggingCriteria; 39 import org.apache.avalon.logging.provider.LoggingFactory; 40 import org.apache.avalon.logging.provider.LoggingException; 41 import org.apache.avalon.logging.provider.LoggingManager; 42 43 import org.apache.avalon.logging.logkit.factory.FileTargetFactory; 44 import org.apache.avalon.logging.logkit.factory.StreamTargetFactory; 45 import org.apache.avalon.logging.logkit.factory.MulticastTargetFactory; 46 import org.apache.avalon.logging.logkit.factory.PluginTargetFactory; 47 48 import org.apache.avalon.repository.provider.InitialContext; 49 50 import org.apache.avalon.util.i18n.ResourceManager; 51 import org.apache.avalon.util.i18n.Resources; 52 53 import org.apache.excalibur.configuration.ConfigurationUtil; 54 55 import org.apache.log.LogTarget; 56 57 61 public class DefaultLoggingFactory implements LoggingFactory 62 { 63 67 private static final Resources REZ = 68 ResourceManager.getPackageResources( DefaultLoggingFactory.class ); 69 70 private static final FormatterFactory FORMATTER = 71 new DefaultFormatterFactory(); 72 73 77 private final ClassLoader m_classloader; 78 private final InitialContext m_context; 79 80 84 private Logger m_logger; 85 private File m_basedir; 86 private LogTargetFactoryManager m_factories; 87 private LogTargetManager m_targets; 88 private LogTargetFactoryBuilder m_builder; 89 90 94 99 public DefaultLoggingFactory( InitialContext context, ClassLoader classloader ) 100 { 101 m_context = context; 102 m_classloader = classloader; 103 } 104 105 109 114 public LoggingCriteria createDefaultLoggingCriteria() 115 { 116 return new DefaultLoggingCriteria( m_context ); 117 } 118 119 125 public LoggingManager createLoggingManager( LoggingCriteria criteria ) 126 throws LoggingException 127 { 128 try 129 { 130 return (LoggingManager) create( criteria ); 131 } 132 catch( Throwable e ) 133 { 134 final String error = 135 "Cannot build logging manager."; 136 throw new LoggingException( error, e ); 137 } 138 } 139 140 144 149 public Map createDefaultCriteria() 150 { 151 return createDefaultLoggingCriteria(); 152 } 153 154 160 public Object create() throws Exception 161 { 162 return create( createDefaultCriteria() ); 163 } 164 165 172 public Object create( final Map map ) throws Exception 173 { 174 if( null == map ) 175 { 176 throw new NullPointerException ( "map" ); 177 } 178 179 final LoggingCriteria criteria = getLoggingCriteria( map ); 180 181 186 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 187 188 URL configURL = criteria.getLoggingConfiguration(); 189 Configuration config; 190 if( configURL != null ) 191 { 192 config = builder.build( configURL.toExternalForm() ); 193 } 194 else 195 { 196 InputStream defaultConf = 197 getClass().getClassLoader(). 198 getResourceAsStream( 199 "org/apache/avalon/logging/logkit/default-logging.xml" 200 ); 201 config = builder.build( defaultConf ); 202 } 203 204 m_logger = setUpBootstrapLogger( criteria, config ); 205 m_basedir = criteria.getBaseDirectory(); 206 207 Map factoriesMap = new HashMap (); 208 m_factories = new DefaultLogTargetFactoryManager( factoriesMap ); 209 210 Map targetsMap = new HashMap (); 211 m_targets = new DefaultLogTargetManager( targetsMap ); 212 213 m_builder = 214 new DefaultLogTargetFactoryBuilder( 215 m_context, m_classloader, m_logger, m_basedir, 216 m_factories, m_targets ); 217 218 222 final Configuration targetsConfig = config.getChild( "targets" ); 223 setupTargets( factoriesMap, targetsMap, targetsConfig ); 224 225 229 CategoriesDirective categories = 230 getCategoriesDirective( config.getChild( "categories" ), true ); 231 232 236 String internal = 237 config.getChild( "logger" ).getAttribute( "name", "logger" ); 238 239 243 boolean debug = criteria.isDebugEnabled(); 244 245 249 LoggingManager manager = 250 new DefaultLoggingManager( m_logger, m_targets, categories, internal, debug ); 251 252 256 return manager; 257 } 258 259 private LoggingCriteria getLoggingCriteria( Map map ) 260 { 261 if( map instanceof LoggingCriteria ) 262 { 263 return (LoggingCriteria) map; 264 } 265 else 266 { 267 final String error = 268 REZ.getString( 269 "factory.bad-criteria", 270 map.getClass().getName() ); 271 throw new IllegalArgumentException ( error ); 272 } 273 } 274 275 private Logger setUpBootstrapLogger( LoggingCriteria criteria, Configuration config ) 276 { 277 if( config.getAttribute( "debug", "false" ).equals( "true" ) ) 278 { 279 return new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ); 280 } 281 else 282 { 283 return criteria.getBootstrapLogger(); 284 } 285 } 286 287 294 private void setupTargets( 295 final Map factories, final Map targets, final Configuration config ) 296 throws LoggingException 297 { 298 Configuration[] children = config.getChildren(); 299 for( int i = 0; i < children.length; i++ ) 300 { 301 Configuration child = children[i]; 302 final String id = getTargetId( child ); 303 try 304 { 305 final LogTarget target = 306 createLogTarget( factories, id, child ); 307 targets.put( id, target ); 308 final String message = 309 REZ.getString( "target.notice.add", id ); 310 m_logger.debug( message ); 311 } 312 catch( Throwable e ) 313 { 314 final String error = 315 REZ.getString( "target.notice.fail", id ); 316 m_logger.error( error, e ); 317 throw new LoggingException( error, e ); 318 } 319 } 320 } 321 322 329 private LogTarget createLogTarget( 330 Map factories, final String id, final Configuration config ) 331 throws LoggingException 332 { 333 final String key = getTargetFactoryKey( config ); 334 final LogTargetFactory factory = 335 getLogTargetFactory( factories, key ); 336 return factory.createTarget( config ); 337 } 338 339 private LogTargetFactory getLogTargetFactory( Map factories, String key ) 340 throws LoggingException 341 { 342 final LogTargetFactory factory = 343 m_factories.getLogTargetFactory( key ); 344 if( factory != null ) 345 { 346 return factory; 347 } 348 else 349 { 350 Class clazz = getLogTargetFactoryClass( key ); 351 LogTargetFactory newFactory = 352 m_builder.buildLogTargetFactory( clazz ); 353 factories.put( key, newFactory ); 354 return newFactory; 355 } 356 } 357 358 private Class getLogTargetFactoryClass( final String key ) 359 throws LoggingException 360 { 361 if( key.equals( "file" ) ) 362 { 363 return FileTargetFactory.class; 364 } 365 else if( key.equals( "stream" ) ) 366 { 367 return StreamTargetFactory.class; 368 } 369 else if( key.equals( "multicast" ) ) 370 { 371 return MulticastTargetFactory.class; 372 } 373 else if( key.equals( "target" ) ) 374 { 375 return PluginTargetFactory.class; 376 } 377 else 378 { 379 final String message = 380 REZ.getString( "factory.error.unknown", key ); 381 throw new LoggingException( message ); 382 } 383 } 384 385 390 private String getTargetFactoryKey( Configuration config ) 391 throws LoggingException 392 { 393 return config.getName(); 394 } 395 396 402 private String getTargetId( Configuration config ) 403 throws LoggingException 404 { 405 try 406 { 407 return config.getAttribute( "id" ); 408 } 409 catch( ConfigurationException e ) 410 { 411 final String listing = ConfigurationUtil.list( config ); 412 final String error = 413 REZ.getString( 414 "target.error.missing-id", 415 listing ); 416 throw new LoggingException( error ); 417 } 418 } 419 420 private CategoriesDirective getCategoriesDirective( Configuration config ) 421 throws LoggingException 422 { 423 return getCategoriesDirective( config, false ); 424 } 425 426 private CategoriesDirective getCategoriesDirective( 427 Configuration config, boolean root ) 428 throws LoggingException 429 { 430 final String name = getCategoryName( config, root ); 431 final String priority = config.getAttribute( "priority", null ); 432 final String target = config.getAttribute( "target", null ); 433 CategoryDirective[] categories = 434 getCategoryDirectives( config ); 435 return new CategoriesDirective( name, priority, target, categories ); 436 } 437 438 private String getCategoryName( Configuration config, boolean root ) 439 throws LoggingException 440 { 441 if( root ) return ""; 442 final String name = config.getAttribute( "name", null ); 443 if( null != name ) return name; 444 445 final String error = 446 REZ.getString( "target.error.missing-category-name" ); 447 throw new LoggingException( error ); 448 } 449 450 private CategoryDirective[] getCategoryDirectives( Configuration config ) 451 throws LoggingException 452 { 453 ArrayList list = new ArrayList (); 454 Configuration[] children = config.getChildren(); 455 for( int i=0; i<children.length; i++ ) 456 { 457 Configuration child = children[i]; 458 if( child.getName().equals( "category" ) ) 459 { 460 CategoryDirective directive = 461 getCategoryDirective( child ); 462 list.add( directive ); 463 } 464 else if( child.getName().equals( "categories" ) ) 465 { 466 CategoriesDirective directive = 467 getCategoriesDirective( child ); 468 list.add( directive ); 469 } 470 } 471 return (CategoryDirective[]) list.toArray( new CategoryDirective[0] ); 472 } 473 474 private CategoryDirective getCategoryDirective( Configuration config ) 475 throws LoggingException 476 { 477 final String name = getCategoryName( config, false ); 478 final String priority = config.getAttribute( "priority", null ); 479 final String target = config.getAttribute( "target", null ); 480 return new CategoryDirective( name, priority, target ); 481 } 482 } 483 | Popular Tags |