1 87 package org.codehaus.loom.components.kernel; 88 89 import java.io.File ; 90 import java.util.HashMap ; 91 import org.apache.avalon.framework.container.ContainerUtil; 92 import org.apache.excalibur.instrument.InstrumentManager; 93 import org.codehaus.loom.components.application.DefaultApplication; 94 import org.codehaus.loom.components.util.profile.PartitionProfile; 95 import org.codehaus.loom.interfaces.Application; 96 import org.codehaus.loom.interfaces.ApplicationContext; 97 import org.codehaus.loom.interfaces.ConfigurationInterceptor; 98 import org.codehaus.loom.interfaces.ConfigurationValidator; 99 import org.codehaus.loom.interfaces.Kernel; 100 import org.codehaus.loom.interfaces.LoomException; 101 import org.codehaus.loom.interfaces.SystemManager; 102 import org.codehaus.spice.loggerstore.LoggerStore; 103 import org.codehaus.spice.salt.i18n.ResourceManager; 104 import org.codehaus.spice.salt.i18n.Resources; 105 import org.codehaus.dna.AbstractLogEnabled; 106 import org.codehaus.dna.Active; 107 import org.codehaus.dna.Composable; 108 import org.codehaus.dna.Configurable; 109 import org.codehaus.dna.Configuration; 110 import org.codehaus.dna.ConfigurationException; 111 import org.codehaus.dna.Logger; 112 import org.codehaus.dna.MissingResourceException; 113 import org.codehaus.dna.ResourceLocator; 114 import org.codehaus.dna.impl.DefaultResourceLocator; 115 116 131 public class DefaultKernel 132 extends AbstractLogEnabled 133 implements Kernel, Active, Composable, Configurable 134 { 135 private static final Resources REZ = 136 ResourceManager.getPackageResources( DefaultKernel.class ); 137 138 private SystemManager m_systemManager; 140 141 private SystemManager m_applicationManager; 142 143 private ConfigurationInterceptor m_repository; 145 146 private ConfigurationValidator m_validator; 148 149 private InstrumentManager m_instrumentManager; 151 152 private final HashMap m_entries = new HashMap (); 153 154 private boolean m_addInvalidApplications; 156 157 private int m_lockCount; 159 160 private String [] m_lockedApplications; 162 163 169 public void compose( final ResourceLocator locator ) 170 throws MissingResourceException 171 { 172 m_systemManager = (SystemManager)locator. 173 lookup( SystemManager.class.getName() ); 174 m_repository = (ConfigurationInterceptor)locator. 175 lookup( ConfigurationInterceptor.class.getName() ); 176 m_validator = (ConfigurationValidator)locator. 177 lookup( ConfigurationValidator.class.getName() ); 178 m_instrumentManager = (InstrumentManager)locator. 179 lookup( InstrumentManager.class.getName() ); 180 } 181 182 public void configure( final Configuration configuration ) 183 throws ConfigurationException 184 { 185 m_addInvalidApplications = 186 configuration.getChild( "add-invalid-applications" ).getValueAsBoolean( 187 false ); 188 } 189 190 public void initialize() 191 throws Exception 192 { 193 m_applicationManager = 194 m_systemManager.getSubContext( null, "application" ); 195 } 196 197 public void dispose() 198 { 199 final String [] names = getApplicationNames(); 200 for( int i = 0; i < names.length; i++ ) 201 { 202 try 203 { 204 final SarEntry entry = (SarEntry)m_entries.get( names[ i ] ); 205 shutdown( entry ); 206 } 207 catch( final Exception e ) 208 { 209 final String message = REZ.format( 210 "kernel.error.entry.dispose", names[ i ] ); 211 getLogger().warn( message, e ); 212 } 213 } 214 } 215 216 220 public void lock() 221 { 222 synchronized( this ) 223 { 224 m_lockedApplications = getApplicationNames(); 225 m_lockCount++; 226 227 if( getLogger().isDebugEnabled() ) 228 { 229 getLogger().debug( "Kernel locked [count:" + m_lockCount + "]" ); 230 } 231 } 232 } 233 234 238 public void unlock() 239 { 240 synchronized( this ) 241 { 242 m_lockCount--; 243 244 if( m_lockCount < 0 ) 245 { 246 throw new IllegalStateException ( 247 REZ.getString( "kernel.error.negativelock" ) ); 248 } 249 250 if( getLogger().isDebugEnabled() ) 251 { 252 getLogger().debug( 253 "Kernel unlocked [count:" + m_lockCount + "]" ); 254 } 255 } 256 } 257 258 262 public String [] getApplicationNames() 263 { 264 if( isLocked() ) 265 { 266 return m_lockedApplications; 267 } 268 else 269 { 270 return (String [])m_entries.keySet().toArray( new String [ 0 ] ); 271 } 272 } 273 274 private boolean isLocked() 275 { 276 return m_lockCount > 0; 277 } 278 279 public Application getApplication( final String name ) 280 { 281 final SarEntry entry = (SarEntry)m_entries.get( name ); 282 if( null == entry ) 283 { 284 return null; 285 } 286 else 287 { 288 return entry.getApplication(); 289 } 290 } 291 292 299 private void startup( final SarEntry entry ) 300 throws Exception 301 { 302 synchronized( entry ) 304 { 305 final String name = entry.getProfile().getMetaData().getName(); 306 307 Application application = entry.getApplication(); 308 if( null == application ) 309 { 310 try 311 { 312 final Application newApp = new DefaultApplication(); 313 final Logger childLogger = 314 getLogger().getChildLogger( name ); 315 org.codehaus.dna.impl.ContainerUtil.enableLogging( 316 newApp, childLogger ); 317 318 final ApplicationContext context = 319 createApplicationContext( entry ); 320 newApp.setApplicationContext( context ); 321 322 org.codehaus.dna.impl.ContainerUtil.initialize( newApp ); 323 324 application = newApp; 325 } 326 catch( final Throwable t ) 327 { 328 entry.setApplication( null ); 331 332 final String message = 333 REZ.format( "kernel.error.entry.initialize", 334 entry.getProfile().getMetaData().getName() ); 335 throw new LoomException( message, t ); 336 } 337 338 try 339 { 340 ContainerUtil.start( application ); 341 } 342 catch( final Throwable t ) 343 { 344 final String message = 345 REZ.format( "kernel.error.entry.start", 346 entry.getProfile().getMetaData().getName() ); 347 348 if( m_addInvalidApplications ) 349 { 350 getLogger().warn( message, t ); 351 } 352 else 353 { 354 entry.setApplication( null ); 357 358 throw new LoomException( message, t ); 359 } 360 } 361 362 entry.setApplication( application ); 363 364 try 366 { 367 m_applicationManager.register( name, application ); 368 } 369 catch( final Throwable t ) 370 { 371 final String message = 372 REZ.format( "kernel.error.entry.manage", name ); 373 throw new LoomException( message, t ); 374 } 375 } 376 } 377 } 378 379 private void shutdown( final SarEntry entry ) 380 throws Exception 381 { 382 synchronized( entry ) 384 { 385 final Application application = entry.getApplication(); 386 if( null != application ) 387 { 388 entry.setApplication( null ); 389 ContainerUtil.stop( application ); 390 org.codehaus.dna.impl.ContainerUtil.dispose( application ); 391 } 392 else 393 { 394 final String message = 395 REZ.format( "kernel.error.entry.nostop", 396 entry.getProfile().getMetaData().getName() ); 397 getLogger().warn( message ); 398 } 399 } 400 } 401 402 public void addApplication( final PartitionProfile profile, 403 final File homeDirectory, 404 final File workDirectory, 405 final ClassLoader classLoader, 406 final LoggerStore store ) 407 throws Exception 408 { 409 410 final String name = profile.getMetaData().getName(); 411 final SarEntry entry = 412 new SarEntry( profile, homeDirectory, 413 workDirectory, classLoader, 414 store ); 415 m_entries.put( name, entry ); 416 417 try 418 { 419 startup( entry ); 420 } 421 catch( final Exception e ) 422 { 423 final String message = 424 REZ.format( "kernel.error.entry.start", name ); 425 getLogger().warn( message, e ); 426 throw e; 427 } 428 } 429 430 private ApplicationContext createApplicationContext( final SarEntry entry ) 431 throws Exception 432 { 433 final String name = entry.getProfile().getMetaData().getName(); 434 435 final DefaultApplicationContext context = 436 new DefaultApplicationContext( entry.getProfile(), 437 entry.getHomeDirectory(), 438 entry.getWorkDirectory(), 439 entry.getClassLoader(), 440 entry.getLoggerStore() ); 441 442 org.codehaus.dna.impl.ContainerUtil. 443 enableLogging( context, createContextLogger( name ) ); 444 org.codehaus.dna.impl.ContainerUtil. 445 compose( context, createResourceLocator() ); 446 org.codehaus.dna.impl.ContainerUtil.initialize( context ); 447 return context; 448 } 449 450 456 private Logger createContextLogger( final String name ) 457 { 458 final String loggerName = name + ".frame"; 459 final Logger childLogger = 460 getLogger().getChildLogger( loggerName ); 461 return childLogger; 462 } 463 464 private ResourceLocator createResourceLocator() 465 { 466 final DefaultResourceLocator serviceManager = new DefaultResourceLocator(); 467 serviceManager.put( SystemManager.class.getName(), m_systemManager ); 468 serviceManager.put( ConfigurationInterceptor.class.getName(), 469 m_repository ); 470 serviceManager.put( ConfigurationValidator.class.getName(), 471 m_validator ); 472 serviceManager.put( InstrumentManager.class.getName(), 473 m_instrumentManager ); 474 serviceManager.put( Kernel.class.getName(), this ); 475 serviceManager.makeReadOnly(); 476 return serviceManager; 477 } 478 479 484 public void removeApplication( final String name ) 485 throws Exception 486 { 487 final SarEntry entry = (SarEntry)m_entries.remove( name ); 488 if( null == entry ) 489 { 490 final String message = 491 REZ.format( "kernel.error.entry.initialize", name ); 492 throw new Exception ( message ); 493 } 494 else 495 { 496 try 498 { 499 m_applicationManager.unregister( name ); 500 } 501 catch( final Throwable t ) 502 { 503 final String message = 504 REZ.format( "kernel.error.entry.unmanage", name ); 505 throw new LoomException( message, t ); 506 } 507 508 shutdown( entry ); 509 } 510 } 511 } 512 | Popular Tags |