1 87 package org.codehaus.loom.components.application; 88 89 import java.io.File ; 90 import java.util.Arrays ; 91 import java.util.HashMap ; 92 import java.util.List ; 93 94 import org.apache.avalon.phoenix.ApplicationListener; 95 import org.apache.avalon.phoenix.BlockListener; 96 import org.apache.avalon.phoenix.metadata.SarMetaData; 97 98 import org.codehaus.loom.components.util.ComponentMetaDataConverter; 99 import org.codehaus.loom.components.util.lifecycle.LifecycleHelper; 100 import org.codehaus.loom.components.util.profile.ComponentProfile; 101 import org.codehaus.loom.components.util.profile.PartitionProfile; 102 import org.codehaus.loom.interfaces.Application; 103 import org.codehaus.loom.interfaces.ApplicationContext; 104 import org.codehaus.loom.interfaces.ContainerConstants; 105 import org.codehaus.loom.interfaces.LoomException; 106 import org.codehaus.spice.salt.i18n.ResourceManager; 107 import org.codehaus.spice.salt.i18n.Resources; 108 import org.codehaus.dna.AbstractLogEnabled; 109 import org.codehaus.dna.Active; 110 import org.codehaus.dna.Logger; 111 112 121 public final class DefaultApplication 122 extends AbstractLogEnabled 123 implements Application, Active 124 { 125 private static final Resources REZ = 126 ResourceManager.getPackageResources( DefaultApplication.class ); 127 128 private static final String PHASE_STARTUP = "startup"; 129 130 private static final String PHASE_SHUTDOWN = "shutdown"; 131 132 private boolean m_running = false; 133 134 private ApplicationContext m_context; 135 136 private final HashMap m_entries = new HashMap (); 137 138 141 private BlockResourceProvider m_blockAccessor; 142 143 146 private final ListenerSupport m_listenerSupport = new ListenerSupport(); 147 148 151 private final LifecycleHelper m_lifecycleHelper = new LifecycleHelper(); 152 153 public void enableLogging( final Logger logger ) 157 { 158 super.enableLogging( logger ); 159 setupLogger( m_lifecycleHelper ); 160 } 161 162 public void initialize() 163 throws Exception 164 { 165 try 166 { 167 loadBlockListeners(); 169 } 170 catch( final Throwable t ) 171 { 172 getLogger().info( "Exception loading listeners:" + t.getMessage() + "\n", t ); 173 throw new LoomException( t.getMessage(), t ); 174 } 175 try 176 { 177 final PartitionProfile partition = 178 m_context.getPartitionProfile().getPartition( ContainerConstants.BLOCK_PARTITION ); 179 final ComponentProfile[] blocks = partition.getComponents(); 180 for( int i = 0; i < blocks.length; i++ ) 181 { 182 final String blockName = blocks[i].getTemplate().getName(); 183 final BlockEntry blockEntry = new BlockEntry( blocks[i] ); 184 m_entries.put( blockName, blockEntry ); 185 } 186 187 runPhase( PHASE_STARTUP ); 189 } 190 catch( final Throwable t ) 191 { 192 getLogger().info( "exception while starting:" + t.getMessage() + "\n" ); 193 t.printStackTrace(); 194 throw new LoomException( t.getMessage(), t ); 195 } 196 197 m_running = true; 198 } 199 200 public void dispose() 201 { 202 try 203 { 204 runPhase( PHASE_SHUTDOWN ); 205 } 206 catch( final Throwable t ) 207 { 208 getLogger().info( "Exception stopping:" + t.getMessage() + "\n", 209 t ); 210 } 211 212 m_running = false; 213 m_entries.clear(); 214 } 215 216 public void setApplicationContext( final ApplicationContext context ) 220 { 221 m_context = context; 222 m_blockAccessor = new BlockResourceProvider( context, this ); 223 setupLogger( m_blockAccessor, "lifecycle" ); 224 } 225 226 229 public String [] getBlockNames() 230 { 231 return (String [])m_entries.keySet().toArray( new String [0] ); 232 } 233 234 public Object getBlock( final String name ) 235 { 236 final BlockEntry entry = (BlockEntry)m_entries.get( name ); 237 if( null == entry ) 238 { 239 return null; 240 } 241 else 242 { 243 return entry.getProxy(); 244 } 245 } 246 247 250 public String getName() 251 { 252 return m_context.getPartitionProfile().getMetaData().getName(); 253 } 254 255 258 public String getDisplayName() 259 { 260 return m_context.getPartitionProfile().getMetaData().getName(); 261 } 262 263 266 public String getDescription() 267 { 268 return "The " + getDisplayName() + " application."; 269 } 270 271 274 public String getHomeDirectory() 275 { 276 return m_context.getHomeDirectory().getPath(); 277 } 278 279 282 public boolean isRunning() 283 { 284 return m_running; 285 } 286 287 291 private void loadBlockListeners() 292 throws Exception 293 { 294 final ClassLoader loader = Thread.currentThread() 296 .getContextClassLoader(); 297 Thread.currentThread().setContextClassLoader( m_context.getClassLoader() ); 298 299 try 300 { 301 doLoadBlockListeners(); 302 } 303 finally 304 { 305 Thread.currentThread().setContextClassLoader( loader ); 306 } 307 } 308 309 313 private void doLoadBlockListeners() 314 throws Exception 315 { 316 final ComponentProfile[] listeners = 317 getComponentsInPartition( ContainerConstants.LISTENER_PARTITION ); 318 for( int i = 0; i < listeners.length; i++ ) 319 { 320 try 321 { 322 startupListener( listeners[i] ); 323 } 324 catch( final Exception e ) 325 { 326 final String name = listeners[i].getTemplate().getName(); 327 final String message = 328 REZ.format( "bad-listener", 329 "startup", 330 name, 331 e.getMessage() ); 332 getLogger().error( message, e ); 333 throw e; 334 } 335 } 336 } 337 338 private ComponentProfile[] getComponentsInPartition( final String key ) 339 { 340 final PartitionProfile partition = 341 m_context.getPartitionProfile().getPartition( key ); 342 return partition.getComponents(); 343 } 344 345 353 private final void runPhase( final String name ) 354 throws Exception 355 { 356 final ClassLoader loader = Thread.currentThread() 358 .getContextClassLoader(); 359 Thread.currentThread().setContextClassLoader( m_context.getClassLoader() ); 360 361 try 362 { 363 doRunPhase( name ); 364 } 365 finally 366 { 367 Thread.currentThread().setContextClassLoader( loader ); 368 } 369 } 370 371 379 private final void doRunPhase( final String name ) 380 throws Exception 381 { 382 final ComponentProfile[] blocks = 383 getComponentsInPartition( ContainerConstants.BLOCK_PARTITION ); 384 final String [] order = DependencyGraph.walkGraph( PHASE_STARTUP == name, blocks ); 385 386 if( getLogger().isInfoEnabled() ) 390 { 391 final Integer count = new Integer ( blocks.length ); 392 final List pathList = Arrays.asList( order ); 393 final String message = 394 REZ.format( "blocks-processing", count, name, pathList ); 395 getLogger().info( message ); 396 } 397 398 if( PHASE_STARTUP == name ) 400 { 401 final PartitionProfile partition = m_context.getPartitionProfile(); 403 final File homeDirectory = m_context.getHomeDirectory(); 404 final SarMetaData sarMetaData = 405 ComponentMetaDataConverter.toSarMetaData( partition, 406 homeDirectory ); 407 m_listenerSupport.fireApplicationStartingEvent( sarMetaData ); 408 } 409 else 410 { 411 m_listenerSupport.applicationStopping(); 413 } 414 415 417 for( int i = 0; i < order.length; i++ ) 418 { 419 final String block = order[i]; 420 421 if( getLogger().isDebugEnabled() ) 423 { 424 final String message = REZ.format( "process-block", 425 block, 426 name ); 427 getLogger().debug( message ); 428 } 429 430 try 431 { 432 final BlockEntry entry = (BlockEntry)m_entries.get( block ); 433 if( PHASE_STARTUP == name ) 434 { 435 startup( entry ); 436 } 437 else 438 { 439 shutdown( entry ); 440 } 441 } 442 catch( final Exception e ) 443 { 444 final String message = 445 REZ.format( "app.error.run-phase", 446 name, 447 block, 448 e.getMessage() ); 449 getLogger().error( message, e ); 450 m_listenerSupport.applicationFailure( e ); 451 throw e; 452 } 453 454 if( getLogger().isDebugEnabled() ) 456 { 457 final String message = REZ.format( "processed-block", 458 block, 459 name ); 460 getLogger().debug( message ); 461 } 462 } 463 464 if( PHASE_STARTUP == name ) 466 { 467 m_listenerSupport.applicationStarted(); 469 } 470 else 471 { 472 m_listenerSupport.applicationStopped(); 474 } 475 } 476 477 487 private void startup( final BlockEntry entry ) 488 throws Exception 489 { 490 final Object block = 491 m_lifecycleHelper.startup( entry.getName(), 492 entry, 493 m_blockAccessor ); 494 495 m_context.exportObject( entry.getName(), block ); 496 entry.setObject( block ); 497 498 m_listenerSupport.fireBlockAddedEvent( entry ); 499 } 500 501 508 private void shutdown( final BlockEntry entry ) 509 throws Exception 510 { 511 m_listenerSupport.fireBlockRemovedEvent( entry ); 512 513 final Object object = entry.getObject(); 514 try 515 { 516 try 518 { 519 m_context.unexportObject( entry.getName() ); 520 } 521 finally 522 { 523 entry.invalidate(); 524 m_lifecycleHelper.shutdown( entry.getName(), 525 object ); 526 } 527 } 528 finally 529 { 530 entry.setObject( null ); 531 } 532 } 533 534 542 private void startupListener( final ComponentProfile profile ) 543 throws Exception 544 { 545 final String name = profile.getTemplate().getName(); 546 final Object listener = 547 m_lifecycleHelper.startup( name, 548 new BlockEntry( profile ), 549 m_blockAccessor ); 550 551 if( listener instanceof ApplicationListener ) 553 { 554 m_listenerSupport.addApplicationListener( (ApplicationListener)listener ); 555 } 556 else 557 { 558 m_listenerSupport.addBlockListener( (BlockListener)listener ); 561 562 final String message = 563 REZ.format( "helper.isa-blocklistener.error", 564 name, 565 profile.getTemplate().getImplementationKey() ); 566 getLogger().error( message ); 567 System.err.println( message ); 568 } 569 } 570 } 571 | Popular Tags |