1 87 package org.codehaus.loom.components.deployer; 88 89 import java.io.File ; 90 import java.net.MalformedURLException ; 91 import java.net.URL ; 92 import java.util.HashMap ; 93 import java.util.Hashtable ; 94 import java.util.Map ; 95 import java.util.Set ; 96 97 import org.apache.avalon.phoenix.BlockContext; 98 99 import org.codehaus.loom.components.util.ConfigurationBuilder; 100 import org.codehaus.loom.components.util.profile.ComponentProfile; 101 import org.codehaus.loom.components.util.profile.PartitionProfile; 102 import org.codehaus.loom.components.util.profile.ProfileBuilder; 103 import org.codehaus.loom.components.util.verifier.SarVerifier; 104 import org.codehaus.loom.interfaces.ClassLoaderManager; 105 import org.codehaus.loom.interfaces.ClassLoaderSet; 106 import org.codehaus.loom.interfaces.ConfigurationInterceptor; 107 import org.codehaus.loom.interfaces.ConfigurationValidator; 108 import org.codehaus.loom.interfaces.ContainerConstants; 109 import org.codehaus.loom.interfaces.Deployer; 110 import org.codehaus.loom.interfaces.Installer; 111 import org.codehaus.loom.interfaces.Kernel; 112 import org.codehaus.loom.interfaces.LogManager; 113 import org.codehaus.loom.interfaces.LoomException; 114 import org.codehaus.spice.loggerstore.LoggerStore; 115 import org.codehaus.spice.salt.i18n.ResourceManager; 116 import org.codehaus.spice.salt.i18n.Resources; 117 import org.codehaus.dna.AbstractLogEnabled; 118 import org.codehaus.dna.Active; 119 import org.codehaus.dna.Composable; 120 import org.codehaus.dna.Configuration; 121 import org.codehaus.dna.ConfigurationException; 122 import org.codehaus.dna.MissingResourceException; 123 import org.codehaus.dna.ResourceLocator; 124 import org.codehaus.dna.impl.DefaultConfiguration; 125 import org.xml.sax.InputSource ; 126 127 134 public class DefaultDeployer 135 extends AbstractLogEnabled 136 implements Deployer, Composable, Active 137 { 138 private static final Resources REZ = 139 ResourceManager.getPackageResources( DefaultDeployer.class ); 140 141 private final SarVerifier m_verifier = new SarVerifier(); 142 private final ProfileBuilder m_builder = new PhoenixProfileBuilder(); 143 private final Map m_installations = new Hashtable (); 144 private LogManager m_logManager; 145 private Kernel m_kernel; 146 private Installer m_installer; 147 private ConfigurationInterceptor m_repository; 148 private ClassLoaderManager m_classLoaderManager; 149 private ConfigurationValidator m_validator; 150 151 159 public void compose( final ResourceLocator locator ) 160 throws MissingResourceException 161 { 162 m_kernel = (Kernel)locator.lookup( Kernel.class.getName() ); 163 m_repository = (ConfigurationInterceptor)locator. 164 lookup( ConfigurationInterceptor.class.getName() ); 165 m_classLoaderManager = (ClassLoaderManager)locator. 166 lookup( ClassLoaderManager.class.getName() ); 167 m_logManager = 168 (LogManager)locator.lookup( LogManager.class.getName() ); 169 m_validator = 170 (ConfigurationValidator)locator.lookup( ConfigurationValidator.class.getName() ); 171 m_installer = (Installer)locator.lookup( Installer.class.getName() ); 172 } 173 174 public void initialize() 175 throws Exception 176 { 177 setupLogger( m_verifier ); 178 setupLogger( m_builder ); 179 } 180 181 184 public void dispose() 185 { 186 final Set set = m_installations.keySet(); 187 final String [] applications = 188 (String [])set.toArray( new String [set.size()] ); 189 for( int i = 0; i < applications.length; i++ ) 190 { 191 final String name = applications[i]; 192 try 193 { 194 undeploy( name ); 195 } 196 catch( final LoomException de ) 197 { 198 final String message = 199 REZ.format( "deploy.undeploy-indispose.error", 200 name, 201 de.getMessage() ); 202 getLogger().error( message, de ); 203 } 204 } 205 } 206 207 212 public void redeploy( final String name ) 213 throws LoomException 214 { 215 final Map installation = 216 (Map )m_installations.get( name ); 217 if( null == installation ) 218 { 219 final String message = 220 REZ.format( "deploy.no-deployment.error", name ); 221 throw new LoomException( message ); 222 } 223 try 224 { 225 final File source = (File )installation. 226 get( ContainerConstants.INSTALL_SOURCE ); 227 redeploy( name, source.toURL() ); 228 } 229 catch( final LoomException e ) 230 { 231 throw e; 232 } 233 catch( final Exception e ) 234 { 235 throw new LoomException( e.getMessage(), e ); 236 } 237 } 238 239 247 public void redeploy( String name, URL location ) 248 throws LoomException 249 { 250 m_kernel.lock(); 251 try 252 { 253 undeploy( name ); 254 deploy( name, location ); 255 } 256 catch( final Exception e ) 257 { 258 throw new LoomException( e.getMessage(), e ); 259 } 260 finally 261 { 262 m_kernel.unlock(); 263 } 264 } 265 266 271 public void undeploy( final String name ) 272 throws LoomException 273 { 274 final Map installation = 275 (Map )m_installations.remove( name ); 276 if( null == installation ) 277 { 278 final String message = 279 REZ.format( "deploy.no-deployment.error", name ); 280 throw new LoomException( message ); 281 } 282 try 283 { 284 m_kernel.removeApplication( name ); 285 m_installer.uninstall( installation ); 286 } 287 catch( final Exception e ) 288 { 289 throw new LoomException( e.getMessage(), e ); 290 } 291 } 292 293 299 public void deploy( final String name, final String sarURL ) 300 throws LoomException 301 { 302 try 303 { 304 deploy( name, new URL ( sarURL ) ); 305 } 306 catch( MalformedURLException mue ) 307 { 308 throw new LoomException( mue.getMessage(), mue ); 309 } 310 } 311 312 318 public void deploy( final String name, final URL location ) 319 throws LoomException 320 { 321 if( m_installations.containsKey( name ) ) 322 { 323 final String message = 324 REZ.format( "deploy.already-deployed.error", 325 name ); 326 throw new LoomException( message ); 327 } 328 329 334 ResourceManager.clearResourceCache(); 335 336 Map installation = null; 337 boolean success = false; 338 try 339 { 340 installation = m_installer.install( name, location ); 342 343 final Configuration config = 344 getConfigurationFor( installation, 345 ContainerConstants.INSTALL_CONFIG, 346 null ); 347 final Configuration environment = 348 getConfigurationFor( installation, 349 ContainerConstants.INSTALL_ENVIRONMENT, 350 null ); 351 final Configuration assembly = 352 getConfigurationFor( installation, 353 ContainerConstants.INSTALL_ASSEMBLY, 354 ConfigurationBuilder.ASSEMBLY_SCHEMA ); 355 356 final File homeDirectory = 357 (File )installation.get( ContainerConstants.INSTALL_HOME ); 358 final File workDirectory = 359 (File )installation.get( ContainerConstants.INSTALL_WORK ); 360 361 final Map data = new HashMap (); 362 data.put( BlockContext.APP_NAME, name ); 363 data.put( BlockContext.APP_HOME_DIR, homeDirectory ); 364 365 final Configuration logs = environment.getChild( "logs", false ); 366 final LoggerStore store = 371 m_logManager.createHierarchy( logs, 372 homeDirectory, 373 workDirectory, 374 data ); 375 376 final ClassLoaderSet classLoaderSet = 377 m_classLoaderManager.createClassLoaderSet( environment, 378 data, 379 homeDirectory, 380 workDirectory ); 381 382 384 final ClassLoader classLoader = 385 classLoaderSet.getDefaultClassLoader(); 386 387 final Configuration newConfig = 388 processConfiguration( name, config ); 389 390 final Map parameters = new HashMap (); 391 parameters.put( ContainerConstants.ASSEMBLY_NAME, name ); 392 parameters.put( ContainerConstants.ASSEMBLY_DESCRIPTOR, assembly ); 393 parameters.put( ContainerConstants.CONFIG_DESCRIPTOR, newConfig ); 394 parameters.put( ContainerConstants.ASSEMBLY_CLASSLOADER, 395 classLoader ); 396 397 final PartitionProfile profile = 399 m_builder.buildProfile( parameters ); 400 401 m_verifier.verifySar( profile, classLoader ); 402 403 verifyConfiguration( profile, newConfig ); 405 406 validateConfiguration( profile, classLoader ); 407 408 m_kernel.addApplication( profile, 410 homeDirectory, 411 workDirectory, 412 classLoader, 413 store ); 414 415 m_installations.put( name, installation ); 416 417 final String message = 418 REZ.format( "deploy.notice.sar.add", 419 name ); 420 getLogger().debug( message ); 421 success = true; 422 } 423 catch( final LoomException de ) 424 { 425 throw de; 426 } 427 catch( final Exception e ) 428 { 429 throw new LoomException( e.getMessage(), e ); 431 } 432 finally 433 { 434 if( !success && null != installation ) 435 { 436 try 437 { 438 m_installer.uninstall( installation ); 439 } 440 catch( final LoomException ie ) 441 { 442 getLogger().error( ie.getMessage(), ie ); 443 } 444 } 445 } 446 } 447 448 458 private Configuration getConfigurationFor( final Map install, 459 final String key, 460 final String schema ) 461 throws LoomException 462 { 463 final String location = (String )install.get( key ); 464 try 465 { 466 return ConfigurationBuilder.build( new InputSource ( location ), 467 schema, 468 getLogger() ); 469 } 470 catch( final Exception e ) 471 { 472 final String message = REZ.format( "deploy.error.config.create", 473 location ); 474 getLogger().error( message, e ); 475 throw new LoomException( message, e ); 476 } 477 } 478 479 487 private Configuration processConfiguration( final String application, 488 final Configuration configuration ) 489 throws LoomException 490 { 491 final DefaultConfiguration newConfiguration = 492 new DefaultConfiguration( "config", 493 configuration.getPath(), 494 configuration.getLocation() ); 495 final Configuration[] configurations = configuration.getChildren(); 496 for( int i = 0; i < configurations.length; i++ ) 497 { 498 final Configuration config = configurations[i]; 499 try 500 { 501 final Configuration newConfig = 502 m_repository.processConfiguration( application, 503 config.getName(), 504 config ); 505 newConfiguration.addChild( newConfig ); 506 } 507 catch( final ConfigurationException ce ) 508 { 509 throw new LoomException( ce.getMessage(), ce ); 510 } 511 } 512 return newConfiguration; 513 } 514 515 523 private void verifyConfiguration( final PartitionProfile profile, 524 final Configuration config ) 525 throws LoomException 526 { 527 final Configuration[] configurations = config.getChildren(); 528 final PartitionProfile listenerPartition = 529 profile.getPartition( ContainerConstants.LISTENER_PARTITION ); 530 final PartitionProfile blockPartition = 531 profile.getPartition( ContainerConstants.BLOCK_PARTITION ); 532 for( int i = 0; i < configurations.length; i++ ) 533 { 534 final Configuration configuration = configurations[i]; 535 final String name = configuration.getName(); 536 ComponentProfile component = listenerPartition.getComponent( name ); 537 if( null == component ) 538 { 539 component = blockPartition.getComponent( name ); 540 } 541 if( null == component ) 542 { 543 final String message = 544 REZ.format( "deploy.error.extra.config", 545 name ); 546 throw new LoomException( message ); 547 } 548 } 549 } 550 551 559 private void validateConfiguration( final PartitionProfile profile, 560 final ClassLoader classLoader ) 561 throws LoomException 562 { 563 final PartitionProfile[] partitions = profile.getPartitions(); 564 for( int i = 0; i < partitions.length; i++ ) 565 { 566 validateConfiguration( partitions[i], classLoader ); 567 } 568 final ComponentProfile[] components = profile.getComponents(); 569 for( int i = 0; i < components.length; i++ ) 570 { 571 final ComponentProfile component = components[i]; 572 boolean isValid = false; 573 try 574 { 575 isValid = m_validator.isValid( component, classLoader ); 576 } 577 catch( final Exception e ) 578 { 579 getLogger().warn( e.getMessage(), e ); 580 } 581 if( !isValid ) 582 { 583 final String message = 584 "Unable to validate configuration of component " + 585 component.getTemplate().getName() + 586 " of type " + 587 component.getInfo().getType().getName(); 588 throw new LoomException( message ); 589 } 590 } 591 } 592 } 593 | Popular Tags |