1 87 package org.codehaus.loom.components.embeddor; 88 89 import java.io.File ; 90 import java.util.Date ; 91 import java.util.Observer ; 92 93 import org.codehaus.loom.interfaces.ContainerConstants; 94 import org.codehaus.loom.interfaces.Deployer; 95 import org.codehaus.loom.interfaces.Embeddor; 96 import org.codehaus.loom.interfaces.Kernel; 97 import org.codehaus.loom.interfaces.LoomException; 98 import org.codehaus.loom.interfaces.SystemManager; 99 import org.codehaus.spice.salt.i18n.ResourceManager; 100 import org.codehaus.spice.salt.i18n.Resources; 101 import org.codehaus.dna.AbstractLogEnabled; 102 import org.codehaus.dna.Active; 103 import org.codehaus.dna.Composable; 104 import org.codehaus.dna.Configurable; 105 import org.codehaus.dna.Configuration; 106 import org.codehaus.dna.ConfigurationException; 107 import org.codehaus.dna.Logger; 108 import org.codehaus.dna.MissingResourceException; 109 import org.codehaus.dna.ResourceLocator; 110 import org.codehaus.dna.impl.ContainerUtil; 111 import org.codehaus.dna.impl.DefaultResourceLocator; 112 113 123 public class DefaultEmbeddor 124 extends AbstractLogEnabled 125 implements Embeddor, Composable, Configurable, Active 126 { 127 private static final Resources REZ = 128 ResourceManager.getPackageResources( DefaultEmbeddor.class ); 129 130 private final EmbeddorObservable m_observable = new EmbeddorObservable(); 131 132 private File m_loomHome; 133 134 private EmbeddorEntry[] m_entries; 135 136 141 private boolean m_temporary; 142 143 150 private boolean m_shutdown; 151 152 153 private long m_startTime; 154 155 156 private ClassLoader m_commonClassLoader; 157 private ClassLoader m_containerClassLoader; 158 159 167 public void compose( final ResourceLocator locator ) 168 throws MissingResourceException 169 { 170 if( locator.contains( Observer .class.getName() ) ) 171 { 172 final Observer observer = (Observer )locator.lookup( 173 Observer .class.getName() ); 174 m_observable.addObserver( observer ); 175 } 176 else 177 { 178 final String message = REZ.getString( "embeddor.notice.no-restart" ); 179 getLogger().warn( message ); 180 } 181 m_commonClassLoader = (ClassLoader ) 182 locator.lookup( ClassLoader .class.getName() + "/common" ); 183 m_containerClassLoader = (ClassLoader ) 184 locator.lookup( ClassLoader .class.getName() + "/container" ); 185 m_loomHome = (File )locator.lookup( File .class.getName() + "/home" ); 186 final Boolean temporary = 187 (Boolean )locator.lookup( Boolean .class.getName() + "/temporary" ); 188 m_temporary = temporary.booleanValue(); 189 } 190 191 public void configure( final Configuration configuration ) 192 throws ConfigurationException 193 { 194 final Configuration[] children = configuration.getChildren( 195 "component" ); 196 m_entries = new EmbeddorEntry[ children.length ]; 197 for( int i = 0; i < children.length; i++ ) 198 { 199 final String role = children[ i ].getAttribute( "role", null ); 200 final String classname = children[ i ].getAttribute( "class" ); 201 final String logger = children[ i ].getAttribute( "logger" ); 202 m_entries[ i ] = 203 new EmbeddorEntry( role, classname, logger, children[ i ] ); 204 } 205 } 206 207 212 public void initialize() 213 throws Exception 214 { 215 m_startTime = System.currentTimeMillis(); 216 try 217 { 218 createComponents(); 219 setupComponents(); 220 registerComponents(); 221 } 222 catch( final Exception e ) 223 { 224 final String message = REZ.getString( 226 "embeddor.error.start.failed" ); 227 getLogger().error( message, e ); 228 throw e; 229 } 230 } 231 232 241 public void execute() 242 throws Exception 243 { 244 if( emptyKernel() && m_temporary ) 249 { 250 final String message = REZ.getString( 251 "embeddor.error.start.no-apps" ); 252 getLogger().error( message ); 253 } 254 else 255 { 256 while( true ) 258 { 259 if( m_shutdown || ( emptyKernel() && m_temporary ) ) 261 { 262 if( emptyKernel() && m_temporary ) 264 { 265 final String message = 266 REZ.getString( "embeddor.shutdown.all-apps-disposed" ); 267 getLogger().info( message ); 268 } 269 break; 270 } 271 gotoSleep(); 272 } 273 } 274 } 275 276 private boolean emptyKernel() 277 { 278 final Kernel kernel = (Kernel)getEmbeddorComponent( 279 Kernel.class.getName() ); 280 if( null != kernel ) 281 { 282 final String [] names = kernel.getApplicationNames(); 283 return ( 0 == names.length ); 284 } 285 else 286 { 287 return true; 290 } 291 } 292 293 private void gotoSleep() 294 { 295 try 296 { 297 synchronized( this ) 298 { 299 wait( 1000 ); 300 } 301 } 302 catch( final InterruptedException e ) 303 { 304 } 306 } 307 308 311 public synchronized void dispose() 312 { 313 shutdown(); 314 try 315 { 316 unregisterComponents(); 317 shutdownComponents(); 318 } 319 catch( final Exception e ) 320 { 321 final String message = REZ.getString( 323 "embeddor.error.shutdown.failed" ); 324 getLogger().error( message, e ); 325 } 326 for( int i = 0; i < m_entries.length; i++ ) 327 { 328 m_entries[ i ].setObject( null ); 329 } 330 System.gc(); } 332 333 336 public void shutdown() 337 { 338 m_shutdown = true; 339 synchronized( this ) 340 { 341 notifyAll(); 342 } 343 } 344 345 350 public void restart() 351 throws UnsupportedOperationException 352 { 353 try 354 { 355 m_observable.change(); 356 m_observable.notifyObservers( "restart" ); 357 } 358 catch( final Exception e ) 359 { 360 throw new UnsupportedOperationException (); 361 } 362 } 363 364 372 public String getName() 373 { 374 return ContainerConstants.SOFTWARE; 375 } 376 377 383 public String getHomeDirectory() 384 { 385 return m_loomHome.getPath(); 386 } 387 388 394 public Date getStartTime() 395 { 396 return new Date ( m_startTime ); 397 } 398 399 406 public long getUpTimeInMillis() 407 { 408 return System.currentTimeMillis() - m_startTime; 409 } 410 411 419 public String getVersion() 420 { 421 return ContainerConstants.VERSION; 422 } 423 424 431 public String getBuild() 432 { 433 return "(" + ContainerConstants.DATE + ")"; 434 } 435 436 443 private synchronized void createComponents() 444 throws Exception 445 { 446 try 447 { 448 for( int i = 0; i < m_entries.length; i++ ) 449 { 450 final String className = m_entries[ i ].getClassName(); 451 final Class clazz = Class.forName( className ); 452 final Object object = createObject( className, clazz ); 453 m_entries[ i ].setObject( object ); 454 } 455 } 456 catch( final Exception e ) 457 { 458 final String message = 459 REZ.getString( "embeddor.error.createComponents.failed" ); 460 getLogger().error( message, e ); 461 throw new LoomException( message, e ); 462 } 463 } 464 465 protected final synchronized void deployFile( final String name, 466 final File file ) 467 throws Exception 468 { 469 final Deployer deployer = (Deployer)getEmbeddorComponent( 470 Deployer.class.getName() ); 471 deployer.deploy( name, file.toURL() ); 472 } 473 474 private void setupComponents() 475 throws Exception 476 { 477 for( int i = 0; i < m_entries.length; i++ ) 478 { 479 final EmbeddorEntry entry = m_entries[ i ]; 480 setupComponent( entry.getObject(), 481 entry.getLoggerName(), 482 entry.getConfiguration() ); 483 } 484 } 485 486 492 private void setupComponent( final Object object, 493 final String loggerName, 494 final Configuration config ) 495 throws Exception 496 { 497 final Logger childLogger = getLogger().getChildLogger( loggerName ); 498 ContainerUtil.enableLogging( object, childLogger ); 499 ContainerUtil.compose( object, getResourceLocator() ); 500 ContainerUtil.configure( object, config ); 501 ContainerUtil.initialize( object ); 502 } 503 504 private void shutdownComponents() 505 throws Exception 506 { 507 for( int i = 0; i < m_entries.length; i++ ) 509 { 510 final Object object = m_entries[ i ].getObject(); 511 if( null == object ) 512 { 513 continue; 514 } 515 ContainerUtil.dispose( object ); 516 } 517 } 518 519 527 private Object createObject( final String classname, 528 final Class service ) 529 throws Exception 530 { 531 try 532 { 533 final Object object = Class.forName( classname ).newInstance(); 534 if( !service.isInstance( object ) ) 535 { 536 final String message = 537 REZ.format( "bad-type.error", 538 classname, 539 service.getName() ); 540 throw new Exception ( message ); 541 } 542 return object; 543 } 544 catch( final IllegalAccessException iae ) 545 { 546 final String message = REZ.format( "bad-ctor.error", 547 service.getName(), 548 classname ); 549 throw new LoomException( message, iae ); 550 } 551 catch( final InstantiationException ie ) 552 { 553 final String message = 554 REZ.format( "no-instantiate.error", 555 service.getName(), 556 classname ); 557 throw new LoomException( message, ie ); 558 } 559 catch( final ClassNotFoundException cnfe ) 560 { 561 final String message = REZ.format( "no-class.error", 562 service.getName(), 563 classname ); 564 throw new LoomException( message, cnfe ); 565 } 566 } 567 568 571 private void registerComponents() 572 throws Exception 573 { 574 final SystemManager systemManager = 575 (SystemManager)getResourceLocator().lookup( 576 SystemManager.class.getName() ); 577 578 final SystemManager mxExporter = 579 systemManager.getSubContext( null, "component" ); 580 581 mxExporter.register( "Embeddor", this ); 582 for( int i = 0; i < m_entries.length; i++ ) 583 { 584 final EmbeddorEntry entry = m_entries[ i ]; 585 mxExporter.register( entry.getLoggerName(), 586 entry.getObject() ); 587 } 588 } 589 590 593 private void unregisterComponents() 594 throws Exception 595 { 596 final SystemManager systemManager = 597 (SystemManager)getResourceLocator().lookup( 598 SystemManager.class.getName() ); 599 600 final SystemManager mxExporter = 601 systemManager.getSubContext( null, "component" ); 602 603 mxExporter.unregister( "Embeddor" ); 604 605 for( int i = 0; i < m_entries.length; i++ ) 606 { 607 final EmbeddorEntry entry = m_entries[ i ]; 608 mxExporter.unregister( entry.getLoggerName() ); 609 } 610 } 611 612 private ResourceLocator getResourceLocator() 613 throws Exception 614 { 615 final DefaultResourceLocator locator = new DefaultResourceLocator(); 616 locator.put( Embeddor.class.getName(), this ); 617 for( int i = 0; i < m_entries.length; i++ ) 618 { 619 final String role = m_entries[ i ].getRole(); 620 if( null == role ) 621 { 622 continue; 623 } 624 final Object component = getEmbeddorComponent( role ); 625 if( null != component ) 626 { 627 locator.put( role, component ); 628 } 629 } 630 631 locator.put( File .class.getName() + "/home", m_loomHome ); 632 locator.put( ClassLoader .class.getName() + "/common", 633 m_commonClassLoader ); 634 locator.put( ClassLoader .class.getName() + "/container", 635 m_containerClassLoader ); 636 637 return locator; 638 } 639 640 private Object getEmbeddorComponent( final String role ) 641 { 642 for( int i = 0; i < m_entries.length; i++ ) 643 { 644 final EmbeddorEntry entry = m_entries[ i ]; 645 final String candidate = entry.getRole(); 646 if( null == candidate ) 647 { 648 continue; 649 } 650 if( candidate.equals( role ) ) 651 { 652 return m_entries[ i ].getObject(); 653 } 654 } 655 return null; 658 } 659 } 660 661 662 | Popular Tags |