1 17 package org.apache.avalon.excalibur.component; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.InputStream ; 22 23 import org.apache.avalon.excalibur.logger.LogKitLoggerManager; 24 import org.apache.avalon.excalibur.logger.LoggerManager; 25 import org.apache.avalon.framework.activity.Disposable; 26 import org.apache.avalon.framework.component.ComponentManager; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 29 import org.apache.avalon.framework.container.ContainerUtil; 30 import org.apache.avalon.framework.context.Context; 31 import org.apache.avalon.framework.context.DefaultContext; 32 import org.apache.avalon.framework.logger.ConsoleLogger; 33 import org.apache.avalon.framework.logger.LogKitLogger; 34 import org.apache.avalon.framework.logger.Logger; 35 import org.apache.avalon.framework.service.ServiceManager; 36 import org.apache.avalon.framework.service.WrapperServiceManager; 37 import org.apache.excalibur.instrument.InstrumentManager; 38 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager; 39 import org.apache.log.Hierarchy; 40 import org.apache.log.Priority; 41 42 76 public class ExcaliburComponentManagerCreator 77 implements Disposable 78 { 79 81 private Logger m_logger; 82 83 85 private final Logger m_primordialLogger; 86 87 88 private Context m_context; 89 90 91 private LoggerManager m_loggerManager; 92 93 94 private RoleManager m_roleManager; 95 96 97 private ComponentManager m_componentManager; 98 99 100 private ServiceManager m_serviceManager; 101 102 103 private InstrumentManager m_instrumentManager; 104 107 110 private static Context createDefaultContext() 111 { 112 DefaultContext context = new DefaultContext(); 113 context.makeReadOnly(); 114 return context; 115 } 116 117 126 private static Configuration readConfigurationFromStream( InputStream is ) 127 throws Exception 128 { 129 if( is == null ) 130 { 131 return null; 132 } 133 else 134 { 135 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 136 Configuration config = builder.build( is ); 137 return config; 138 } 139 } 140 141 150 private static Configuration readConfigurationFromFile( File file ) 151 throws Exception 152 { 153 if( file == null ) 154 { 155 return null; 156 } 157 else 158 { 159 InputStream is = new FileInputStream ( file ); 160 try 161 { 162 return readConfigurationFromStream( is ); 163 } 164 finally 165 { 166 is.close(); 167 } 168 } 169 } 170 171 174 192 public ExcaliburComponentManagerCreator( Context context, 193 Configuration loggerManagerConfig, 194 Configuration roleManagerConfig, 195 Configuration componentManagerConfig, 196 Configuration instrumentManagerConfig ) 197 throws Exception 198 { 199 if( context == null ) 200 { 201 m_context = createDefaultContext(); 202 } 203 else 204 { 205 m_context = context; 206 } 207 208 m_primordialLogger = new ConsoleLogger( ConsoleLogger.LEVEL_INFO ); 216 217 try 218 { 219 initializeLoggerManager( loggerManagerConfig ); 220 initializeRoleManager( roleManagerConfig ); 221 initializeInstrumentManager( instrumentManagerConfig ); 222 initializeComponentManager( componentManagerConfig ); 223 } 224 catch( Exception e ) 225 { 226 dispose(); 228 throw e; 229 } 230 } 231 232 254 public ExcaliburComponentManagerCreator( Context context, 255 InputStream loggerManagerConfigStream, 256 InputStream roleManagerConfigStream, 257 InputStream componentManagerConfigStream, 258 InputStream instrumentManagerConfigStream ) 259 throws Exception 260 { 261 this( context, 262 readConfigurationFromStream( loggerManagerConfigStream ), 263 readConfigurationFromStream( roleManagerConfigStream ), 264 readConfigurationFromStream( componentManagerConfigStream ), 265 readConfigurationFromStream( instrumentManagerConfigStream ) ); 266 } 267 268 289 public ExcaliburComponentManagerCreator( Context context, 290 File loggerManagerConfigFile, 291 File roleManagerConfigFile, 292 File componentManagerConfigFile, 293 File instrumentManagerConfigFile ) 294 throws Exception 295 { 296 this( context, 297 readConfigurationFromFile( loggerManagerConfigFile ), 298 readConfigurationFromFile( roleManagerConfigFile ), 299 readConfigurationFromFile( componentManagerConfigFile ), 300 readConfigurationFromFile( instrumentManagerConfigFile ) ); 301 } 302 303 306 310 public void dispose() 311 { 312 try 314 { 315 if( m_componentManager != null ) 316 { 317 ContainerUtil.shutdown( m_componentManager ); 318 } 319 320 if( m_instrumentManager != null ) 321 { 322 ContainerUtil.shutdown( m_instrumentManager ); 323 } 324 325 if( m_roleManager != null ) 326 { 327 ContainerUtil.shutdown( m_roleManager ); 328 } 329 330 if( m_loggerManager != null ) 331 { 332 ContainerUtil.shutdown( m_loggerManager ); 333 } 334 } 335 catch( Exception e ) 336 { 337 getLogger().error( "Unexpected error disposing managers.", e ); 338 } 339 } 340 341 344 349 public LoggerManager getLoggerManager() 350 { 351 return m_loggerManager; 352 } 353 354 360 public InstrumentManager getInstrumentManager() 361 { 362 return m_instrumentManager; 363 } 364 365 373 public ComponentManager getComponentManager() 374 { 375 return m_componentManager; 376 } 377 378 383 public ServiceManager getServiceManager() 384 { 385 return m_serviceManager; 386 } 387 388 391 private Logger getLogger() 392 { 393 if( m_logger != null ) 394 { 395 return m_logger; 396 } 397 return m_primordialLogger; 398 } 399 400 private void initializeLoggerManager( Configuration loggerManagerConfig ) 401 throws Exception 402 { 403 String logPrefix = null; 405 406 String lmDefaultLoggerName; 408 String lmLoggerName; 409 if( logPrefix == null ) 410 { 411 lmDefaultLoggerName = ""; 412 lmLoggerName = loggerManagerConfig.getAttribute( "logger", "system.logkit" ); 413 } 414 else 415 { 416 lmDefaultLoggerName = logPrefix; 417 lmLoggerName = logPrefix + org.apache.log.Logger.CATEGORY_SEPARATOR 418 + loggerManagerConfig.getAttribute( "logger", "system.logkit" ); 419 } 420 421 org.apache.log.Logger lmDefaultLogger = 423 Hierarchy.getDefaultHierarchy().getLoggerFor( lmDefaultLoggerName ); 424 lmDefaultLogger.setPriority( Priority.DEBUG ); 427 428 org.apache.log.Logger lmLogger = 430 Hierarchy.getDefaultHierarchy().getLoggerFor( lmLoggerName ); 431 lmLogger.setPriority( Priority.getPriorityForName( 432 loggerManagerConfig.getAttribute( "log-level", "DEBUG" ) ) ); 433 434 LogKitLoggerManager loggerManager = new LogKitLoggerManager( 436 logPrefix, Hierarchy.getDefaultHierarchy(), 437 new LogKitLogger( lmDefaultLogger ), new LogKitLogger( lmLogger ) ); 438 loggerManager.contextualize( m_context ); 439 loggerManager.configure( loggerManagerConfig ); 440 m_loggerManager = loggerManager; 441 442 if( m_logger == null ) 445 { 446 getLogger().debug( "Switching to default Logger provided by LoggerManager." ); 447 m_logger = m_loggerManager.getDefaultLogger(); 448 } 449 } 450 451 private void initializeRoleManager( Configuration roleManagerConfig ) 452 throws Exception 453 { 454 Logger rmLogger = m_loggerManager.getLoggerForCategory( 456 roleManagerConfig.getAttribute( "logger", "system.roles" ) ); 457 458 DefaultRoleManager roleManager = new DefaultRoleManager(); 460 roleManager.enableLogging( rmLogger ); 461 roleManager.configure( roleManagerConfig ); 462 m_roleManager = roleManager; 463 } 464 465 private void initializeInstrumentManager( Configuration instrumentManagerConfig ) 466 throws Exception 467 { 468 if( instrumentManagerConfig != null ) 469 { 470 Logger imLogger = m_loggerManager.getLoggerForCategory( 472 instrumentManagerConfig.getAttribute( "logger", "system.instrument" ) ); 473 474 DefaultInstrumentManager instrumentManager = new DefaultInstrumentManager(); 476 instrumentManager.enableLogging( imLogger ); 477 instrumentManager.configure( instrumentManagerConfig ); 478 instrumentManager.initialize(); 479 m_instrumentManager = instrumentManager; 480 } 481 } 482 483 private void initializeComponentManager( Configuration componentManagerConfig ) 484 throws Exception 485 { 486 Logger cmLogger = m_loggerManager.getLoggerForCategory( 488 componentManagerConfig.getAttribute( "logger", "system.components" ) ); 489 490 ExcaliburComponentManager componentManager = new ExcaliburComponentManager(); 492 componentManager.enableLogging( cmLogger ); 493 componentManager.setLoggerManager( m_loggerManager ); 494 componentManager.contextualize( m_context ); 495 if ( m_instrumentManager != null ) 496 { 497 componentManager.setInstrumentManager( m_instrumentManager ); 498 } 499 componentManager.setRoleManager( m_roleManager ); 500 componentManager.configure( componentManagerConfig ); 501 componentManager.initialize(); 502 m_componentManager = componentManager; 503 504 m_serviceManager = new WrapperServiceManager( m_componentManager ); 507 } 508 } 509 510 | Popular Tags |