mcconnell 2002/12/10 20:58:55 Added: merlin/src/java/org/apache/avalon/merlin/block BlockLoader.java Log: Abstract of the functionality common to the kernel and blocks supporting the loading of subsidiary blocks. Revision Changes Path 1.1 avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/block/BlockLoader.java Index: BlockLoader.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.avalon.merlin.block; import java.io.File; import java.io.IOException; import java.io.FileInputStream; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.jar.JarFile; import java.util.jar.Attributes; import java.util.jar.Manifest; import java.util.zip.ZipEntry; import java.net.JarURLConnection; import org.apache.avalon.framework.CascadingException; import org.apache.avalon.framework.logger.Logger; import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.activity.Startable; import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; import org.apache.avalon.framework.configuration.DefaultConfiguration; import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.DefaultContext; import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.context.ContextException; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.service.DefaultServiceManager; import org.apache.avalon.meta.model.LoggingDirective; import org.apache.avalon.merlin.block.Block; import org.apache.avalon.merlin.block.DefaultBlock; import org.apache.avalon.merlin.container.builder.XMLContainerCreator; import org.apache.avalon.assembly.logging.LoggingManager; import org.apache.avalon.assembly.logging.LoggingDescriptor; import org.apache.avalon.assembly.logging.DefaultLoggingManager; import org.apache.avalon.assembly.engine.EngineClassLoader; import org.apache.avalon.assembly.engine.Engine; import org.apache.avalon.assembly.engine.model.LibraryDescriptor; import org.apache.avalon.assembly.engine.model.ClasspathDescriptor; import org.apache.avalon.assembly.util.ExceptionHelper; /** * Default kernel implementation. The implementation provides support for * the following sequence of activties: * * <ul> * <li>loading internal jar-file dependencies * <li>establishment of a logging system * <li>creation of a root container * <li>startup of the container * <li>management of interupt conditions or request for kernel disposal * <li>error reporting * </ul> * * @see DefaultKernel * * @author <a href="mailto:avalon-dev@jakarta.apache.org">Avalon Development Team</a> * @version $Revision: 1.1 $ $Date: 2002/12/11 04:58:55 $ */ public class BlockLoader extends AbstractLogEnabled { //============================================================== // static //============================================================== protected static final String BLOCK_XML_ENTRY = "BLOCK-INF/block.xml"; protected static final XMLContainerCreator CREATOR = new XMLContainerCreator(); //============================================================== // BlockLoader //============================================================== protected Block[] install( Engine engine, File home, URL[] urls ) throws Exception { ArrayList blocks = new ArrayList(); for( int i=0; i<urls.length; i++ ) { URL url = urls[i]; try { blocks.add( installBlock( engine, home, url ) ); } catch( Throwable e ) { final String error = "Error during block deployment for url: " + url; } } Block[] result = (Block[]) blocks.toArray( new Block[0] ); int n = result.length; if( n > 0 ) { if( n > 1 ) { getLogger().debug( "installed " + urls.length + " blocks." ); } else { getLogger().debug( "installed one subsidiary block." ); } } return result; } private Block installBlock( Engine engine, File home, URL url ) throws Exception { JarFile jar = getJarFile( url ); Manifest manifest = jar.getManifest(); if( !isBlock( manifest ) ) { final String warning = "Manifest does not declare a block on resource: " + url; throw new IllegalArgumentException( warning ); } Configuration blockConfig = getBlockConfiguration( jar ); String name = DefaultBlock.getName( manifest ); if( name == null ) { final String error = "Missing name in block: " + url; throw new IllegalArgumentException( error ); } Logger logger = getLogger().getChildLogger( name ) ; logger.debug( "[" + name + "]"); DefaultBlock block = new DefaultBlock(); block.enableLogging( logger ); DefaultContext context = new DefaultContext(); context.put( "urn:assembly:classloader", engine ); context.put( "urn:merlin:block.manifest", manifest ); context.put( "urn:merlin:block.url", url ); context.put( "urn:avalon:home", home ); context.makeReadOnly(); block.contextualize( context ); block.initialize(); return block; } protected boolean isBlock( Manifest manifest ) { if( manifest == null ) { return false; } return ( manifest.getAttributes( Block.AVALON_BLOCK_KEY ) != null ); } protected JarFile getJarFile( URL url ) { try { JarURLConnection connection = (JarURLConnection) url.openConnection(); return connection.getJarFile(); } catch( IOException ioe ) { final String error = "Unexpected IO Exception while jar file from url: " + url; throw new RuntimeException( error ); } } protected Configuration getBlockConfiguration( JarFile jar ) throws Exception { if( jar == null ) { throw new NullPointerException( "jar" ); } ZipEntry entry = jar.getEntry( BLOCK_XML_ENTRY ); if( entry == null ) { if( getLogger().isDebugEnabled() ) { final String msg = "No block configuration - applying defaults."; getLogger().debug( msg ); } return new DefaultConfiguration( "default", null ); } try { DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); InputStream is = jar.getInputStream( entry ); if( is == null ) { throw new RuntimeException( "Could not load the configuration resource \"" + jar.getName() + "\"" ); } return builder.build( is ); } catch( Throwable e ) { final String error = "Unable to create configuration from jar file: " + jar.getName(); throw new BlockException( error, e ); } } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>