1 8 package org.apache.avalon.phoenix.components.application; 9 10 import java.util.Arrays ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import org.apache.avalon.excalibur.i18n.ResourceManager; 14 import org.apache.avalon.excalibur.i18n.Resources; 15 import org.apache.avalon.framework.activity.Disposable; 16 import org.apache.avalon.framework.activity.Initializable; 17 import org.apache.avalon.framework.activity.Startable; 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 import org.apache.avalon.framework.logger.Logger; 20 import org.apache.avalon.phoenix.ApplicationListener; 21 import org.apache.avalon.phoenix.BlockListener; 22 import org.apache.avalon.phoenix.interfaces.Application; 23 import org.apache.avalon.phoenix.interfaces.ApplicationContext; 24 import org.apache.avalon.phoenix.interfaces.ApplicationException; 25 import org.apache.avalon.phoenix.interfaces.ApplicationMBean; 26 import org.apache.avalon.phoenix.metadata.BlockListenerMetaData; 27 import org.apache.avalon.phoenix.metadata.BlockMetaData; 28 import org.apache.avalon.phoenix.metadata.SarMetaData; 29 import org.apache.excalibur.containerkit.lifecycle.LifecycleException; 30 import org.apache.excalibur.containerkit.lifecycle.LifecycleHelper; 31 import org.apache.excalibur.threadcontext.ThreadContext; 32 33 41 public final class DefaultApplication 42 extends AbstractLogEnabled 43 implements Application, ApplicationMBean, Initializable, Startable, Disposable 44 { 45 private static final Resources REZ = 46 ResourceManager.getPackageResources( DefaultApplication.class ); 47 48 private static final String PHASE_STARTUP = "startup"; 49 50 private static final String PHASE_SHUTDOWN = "shutdown"; 51 52 private boolean m_running = false; 53 54 private ApplicationContext m_context; 55 56 private HashMap m_entries = new HashMap (); 57 58 61 private BlockResourceProvider m_blockAccessor; 62 63 66 private ListenerResourceProvider m_listenerAccessor; 67 68 71 private ListenerSupport m_listenerSupport = new ListenerSupport(); 72 73 76 private final LifecycleHelper m_lifecycleHelper = new LifecycleHelper(); 77 78 81 private final ExportHelper m_exportHelper = new ExportHelper(); 82 83 public void enableLogging( Logger logger ) 87 { 88 super.enableLogging( logger ); 89 setupLogger( m_lifecycleHelper ); 90 setupLogger( m_exportHelper ); 91 } 92 93 public void initialize() 94 throws Exception 95 { 96 try 97 { 98 loadBlockListeners(); 100 } 101 catch( final Throwable t ) 102 { 103 getLogger().info( "exception while loading listeners:" + t.getMessage() + "\n" ); 104 t.printStackTrace(); 105 throw new ApplicationException( t.getMessage(), t ); 106 } 107 } 108 109 119 public void start() 120 throws IllegalStateException , ApplicationException 121 { 122 if( isRunning() ) 123 { 124 throw new IllegalStateException (); 125 } 126 else 127 { 128 try 129 { 130 final BlockMetaData[] blocks = m_context.getMetaData().getBlocks(); 131 for( int i = 0; i < blocks.length; i++ ) 132 { 133 final String blockName = blocks[ i ].getName(); 134 final BlockEntry blockEntry = new BlockEntry( blocks[ i ] ); 135 m_entries.put( blockName, blockEntry ); 136 } 137 138 runPhase( PHASE_STARTUP ); 140 } 141 catch( final Throwable t ) 142 { 143 getLogger().info( "exception while starting:" + t.getMessage() + "\n" ); 144 t.printStackTrace(); 145 throw new ApplicationException( t.getMessage(), t ); 146 } 147 148 m_running = true; 149 } 150 } 151 152 164 public void restart() 165 throws IllegalStateException , ApplicationException 166 { 167 stop(); 168 start(); 169 } 170 171 181 public void stop() 182 throws IllegalStateException , ApplicationException 183 { 184 if( !isRunning() ) 185 { 186 throw new IllegalStateException (); 187 } 188 else 189 { 190 try 191 { 192 runPhase( PHASE_SHUTDOWN ); 193 } 194 catch( final Throwable t ) 195 { 196 getLogger().info( "exception while stopping:" + t.getMessage() + "\n" ); 197 t.printStackTrace(); 198 throw new ApplicationException( t.getMessage(), t ); 199 } 200 201 m_running = false; 202 } 203 } 204 205 public void dispose() 206 { 207 m_entries.clear(); 208 } 209 210 public void setApplicationContext( final ApplicationContext context ) 214 { 215 m_context = context; 216 m_blockAccessor = new BlockResourceProvider( context, this ); 217 setupLogger( m_blockAccessor, "lifecycle" ); 218 m_listenerAccessor = new ListenerResourceProvider( context ); 219 setupLogger( m_listenerAccessor, "lifecycle" ); 220 } 221 222 public String [] getBlockNames() 223 { 224 return (String [])m_entries.keySet().toArray( new String [ 0 ] ); 225 } 226 227 233 public Object getBlock( final String name ) 234 { 235 final BlockEntry entry = (BlockEntry)m_entries.get( name ); 236 if( null == entry ) 237 { 238 return null; 239 } 240 else 241 { 242 return entry.getProxy(); 243 } 244 } 245 246 251 public String getName() 252 { 253 return getMetaData().getName(); 254 } 255 256 261 public String getDisplayName() 262 { 263 return getMetaData().getName(); 264 } 265 266 271 public String getDescription() 272 { 273 return "The " + getDisplayName() + " application."; 274 } 275 276 281 public String getHomeDirectory() 282 { 283 return getMetaData().getHomeDirectory().getPath(); 284 } 285 286 292 public boolean isRunning() 293 { 294 return m_running; 295 } 296 297 protected final SarMetaData getMetaData() 298 { 299 return m_context.getMetaData(); 300 } 301 302 306 private void loadBlockListeners() 307 throws Exception 308 { 309 ThreadContext.setThreadContext( m_context.getThreadContext() ); 311 312 try 313 { 314 doLoadBlockListeners(); 315 } 316 finally 317 { 318 ThreadContext.setThreadContext( null ); 319 } 320 } 321 322 327 private void doLoadBlockListeners() 328 throws Exception 329 { 330 final BlockListenerMetaData[] listeners = m_context.getMetaData().getListeners(); 331 for( int i = 0; i < listeners.length; i++ ) 332 { 333 try 334 { 335 startupListener( listeners[ i ] ); 336 } 337 catch( final Exception e ) 338 { 339 final String name = listeners[ i ].getName(); 340 final String message = 341 REZ.getString( "bad-listener", "startup", name, e.getMessage() ); 342 getLogger().error( message, e ); 343 throw e; 344 } 345 } 346 } 347 348 357 private final void runPhase( final String name ) 358 throws Exception 359 { 360 ThreadContext.setThreadContext( m_context.getThreadContext() ); 362 363 try 364 { 365 doRunPhase( name ); 366 } 367 finally 368 { 369 ThreadContext.setThreadContext( null ); 370 } 371 } 372 373 381 private final void doRunPhase( final String name ) 382 throws Exception 383 { 384 final BlockMetaData[] blocks = m_context.getMetaData().getBlocks(); 385 final String [] order = DependencyGraph.walkGraph( PHASE_STARTUP == name, blocks ); 386 387 if( getLogger().isInfoEnabled() ) 391 { 392 final Integer count = new Integer ( blocks.length ); 393 final List pathList = Arrays.asList( order ); 394 final String message = 395 REZ.getString( "blocks-processing", count, name, pathList ); 396 getLogger().info( message ); 397 } 398 399 if( PHASE_STARTUP == name ) 401 { 402 m_listenerSupport.fireApplicationStartingEvent( getMetaData() ); 404 } 405 else 406 { 407 m_listenerSupport.applicationStopping(); 409 } 410 411 413 for( int i = 0; i < order.length; i++ ) 414 { 415 final String block = order[ i ]; 416 417 if( getLogger().isDebugEnabled() ) 419 { 420 final String message = REZ.getString( "process-block", block, name ); 421 getLogger().debug( message ); 422 } 423 424 try 425 { 426 final BlockEntry entry = (BlockEntry)m_entries.get( block ); 427 if( PHASE_STARTUP == name ) 428 { 429 startup( entry ); 430 } 431 else 432 { 433 shutdown( entry ); 434 } 435 } 436 catch( final Exception e ) 437 { 438 final String message = 439 REZ.getString( "app.error.run-phase", name, block, e.getMessage() ); 440 getLogger().error( message, e ); 441 m_listenerSupport.applicationFailure( e ); 442 throw e; 443 } 444 445 if( getLogger().isDebugEnabled() ) 447 { 448 final String message = REZ.getString( "processed-block", block, name ); 449 getLogger().debug( message ); 450 } 451 } 452 453 if( PHASE_STARTUP == name ) 455 { 456 m_listenerSupport.applicationStarted(); 458 } 459 else 460 { 461 m_listenerSupport.applicationStopped(); 463 } 464 } 465 466 478 public void startup( final BlockEntry entry ) 479 throws Exception 480 { 481 final Object block = 482 m_lifecycleHelper.startup( entry.getName(), 483 entry, 484 m_blockAccessor ); 485 486 m_exportHelper.exportBlock( m_context, 487 entry.getMetaData(), 488 block ); 489 490 entry.setObject( block ); 491 492 m_listenerSupport.fireBlockAddedEvent( entry ); 493 } 494 495 504 public void shutdown( final BlockEntry entry ) 505 throws LifecycleException 506 { 507 m_listenerSupport.fireBlockRemovedEvent( entry ); 508 509 final Object object = entry.getObject(); 510 try 511 { 512 m_exportHelper.unexportBlock( m_context, 514 entry.getMetaData(), 515 object ); 516 entry.invalidate(); 517 518 m_lifecycleHelper.shutdown( entry.getName(), 519 object ); 520 } 521 finally 522 { 523 entry.setObject( null ); 524 } 525 } 526 527 536 public void startupListener( final BlockListenerMetaData metaData ) 537 throws Exception 538 { 539 final String name = metaData.getName(); 540 final Object listener = 541 m_lifecycleHelper.startup( name, 542 metaData, 543 m_listenerAccessor ); 544 545 if( listener instanceof ApplicationListener ) 547 { 548 m_listenerSupport.addApplicationListener( (ApplicationListener)listener ); 549 } 550 else 551 { 552 m_listenerSupport.addBlockListener( (BlockListener)listener ); 555 556 final String message = 557 REZ.getString( "helper.isa-blocklistener.error", 558 name, 559 metaData.getClassname() ); 560 getLogger().error( message ); 561 System.err.println( message ); 562 } 563 } 564 } 565 | Popular Tags |