mcconnell 2002/12/10 20:59:46 Modified: merlin kernel.xml merlin/src/java/org/apache/avalon/merlin/block DefaultBlock.java merlin/src/java/org/apache/avalon/merlin/container/builder XMLContainerCreator.java merlin/src/java/org/apache/avalon/merlin/kernel DefaultKernel.java merlin/src/test/org/apache/avalon/merlin/kernel KernelTestCase.java Log: Abstract of the functionality common to the kernel and blocks supporting the loading of subsidiary blocks. Revision Changes Path 1.5 +2 -2 avalon-sandbox/merlin/kernel.xml Index: kernel.xml =================================================================== RCS file: /home/cvs/avalon-sandbox/merlin/kernel.xml,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- kernel.xml 11 Dec 2002 02:53:40 -0000 1.4 +++ kernel.xml 11 Dec 2002 04:59:46 -0000 1.5 @@ -1,14 +1,14 @@ <kernel> - <logging priority="INFO" target="default"> + <logging target="default"> <category name="/sys/logger" priority="WARN"/> <target name="kernel"> <file location="kernel.log" /> </target> </logging> - <categories priority="WARN"> + <categories priority="DEBUG"> <category priority="DEBUG" name="/sys" /> </categories> 1.5 +22 -2 avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/block/DefaultBlock.java Index: DefaultBlock.java =================================================================== RCS file: /home/cvs/avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/block/DefaultBlock.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DefaultBlock.java 11 Dec 2002 01:26:28 -0000 1.4 +++ DefaultBlock.java 11 Dec 2002 04:59:46 -0000 1.5 @@ -2,6 +2,7 @@ package org.apache.avalon.merlin.block; +import java.io.File; import java.io.IOException; import java.util.jar.JarFile; import java.net.URL; @@ -16,8 +17,12 @@ import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.context.ContextException; +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.assembly.engine.model.ClasspathDescriptor; -public class DefaultBlock extends AbstractLogEnabled implements Block, Contextualizable, Initializable +public class DefaultBlock extends BlockLoader implements Block, Contextualizable, Initializable { //============================================================== // static @@ -47,6 +52,15 @@ private Manifest m_manifest; + private URL m_url; + + private File m_home; + + /** + * The packaged configuration. + */ + private Configuration m_config; + //============================================================== // Contextualizable //============================================================== @@ -59,8 +73,10 @@ */ public void contextualize( Context context ) throws ContextException { + m_url = (URL) context.get( "urn:merlin:block.url" ); m_engine = (EngineClassLoader) context.get( "urn:assembly:classloader" ); m_manifest = (Manifest) context.get( "urn:merlin:block.manifest" ); + m_home = (File) context.get( "urn:avalon:home" ); } //============================================================== @@ -70,6 +86,11 @@ public void initialize() throws Exception { getLogger().debug( "initialization" ); + m_config = getBlockConfiguration( getJarFile( m_url ) ); + ClasspathDescriptor path = + CREATOR.createClasspathDescriptor( m_config.getChild( "blocks" ) ); + URL[] urls = ClasspathDescriptor.expand( m_home, path ); + Block[] blocks = install( m_engine, m_home, urls ); } //============================================================== @@ -80,5 +101,4 @@ { return getName( m_manifest ); } - } 1.3 +3 -3 avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/container/builder/XMLContainerCreator.java Index: XMLContainerCreator.java =================================================================== RCS file: /home/cvs/avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/container/builder/XMLContainerCreator.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- XMLContainerCreator.java 7 Dec 2002 09:47:26 -0000 1.2 +++ XMLContainerCreator.java 11 Dec 2002 04:59:46 -0000 1.3 @@ -104,7 +104,7 @@ private Class m_clazz; - public XMLContainerCreator() throws Exception + public XMLContainerCreator() { this( ContainerDescriptor.class ); } @@ -114,7 +114,7 @@ * new container profile creation. * @param base the container descriptor base class */ - public XMLContainerCreator( Class base ) throws Exception + public XMLContainerCreator( Class base ) { super( base ); m_clazz = base; 1.6 +26 -144 avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/kernel/DefaultKernel.java Index: DefaultKernel.java =================================================================== RCS file: /home/cvs/avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/kernel/DefaultKernel.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- DefaultKernel.java 11 Dec 2002 01:27:05 -0000 1.5 +++ DefaultKernel.java 11 Dec 2002 04:59:46 -0000 1.6 @@ -91,6 +91,7 @@ import org.apache.avalon.assembly.util.ExceptionHelper; import org.apache.avalon.merlin.block.Block; import org.apache.avalon.merlin.block.DefaultBlock; +import org.apache.avalon.merlin.block.BlockLoader; /** * Default kernel implementation. The implementation provides support for @@ -111,19 +112,17 @@ * @version $Revision$ $Date$ */ -public class DefaultKernel implements Kernel, Contextualizable, Configurable, Initializable, Startable, Disposable +public class DefaultKernel extends BlockLoader implements Kernel, Contextualizable, Configurable, Initializable, Startable, Disposable { //============================================================== // static //============================================================== - private static final String BLOCK_XML_ENTRY = "BLOCK-INF/block.xml"; - /** * The logging manager that we use to construct logging catagories * and logging channels. */ - private static LoggingManager c_logging; + private static LoggingManager LOGGING; //============================================================== // state @@ -139,11 +138,6 @@ */ private File m_home; - /** - * The container definition builder. - */ - private XMLContainerCreator m_creator; - /** * Logging channel for the container. */ @@ -221,8 +215,6 @@ throw new IllegalStateException("configuration"); } - m_creator = new XMLContainerCreator(); - // // Setup the logging system // @@ -230,15 +222,17 @@ try { final String root = "/"; - c_logging = bootstrapLoggingManager( root ); - Configuration categoriesConfig = m_config.getChild( "categories" ); - LoggingDirective categories = - m_creator.createLoggingDirective( root, categoriesConfig ); - c_logging.addCategories( root, categories ); - m_logger = c_logging.getLoggerForCategory( root ); - + if( LOGGING == null ) + { + LOGGING = bootstrapLoggingManager( root ); + Configuration categoriesConfig = m_config.getChild( "categories" ); + LoggingDirective categories = + CREATOR.createLoggingDirective( root, categoriesConfig ); + LOGGING.addCategories( root, categories ); + } final String sys = "/sys"; - m_local = c_logging.getLoggerForCategory( sys ); + m_local = LOGGING.getLoggerForCategory( sys ); + enableLogging( m_local ); } catch( Throwable e ) { @@ -264,7 +258,7 @@ try { Configuration engineConfig = m_config.getChild( "engine" ); - m_engine = bootstrapEngine( c_logging, engineConfig ); + m_engine = bootstrapEngine( LOGGING, engineConfig ); } catch( Throwable e ) { @@ -288,55 +282,11 @@ getLogger().debug( "block assembly" ); } - ClasspathDescriptor blocks = - m_creator.createClasspathDescriptor( m_config.getChild( "blocks" ) ); - URL[] urls = ClasspathDescriptor.expand( m_home, blocks ); + ClasspathDescriptor path = + CREATOR.createClasspathDescriptor( m_config.getChild( "blocks" ) ); + URL[] urls = ClasspathDescriptor.expand( m_home, path ); + Block[] blocks = install( m_engine, m_home, urls ); - for( int i=0; i<urls.length; i++ ) - { - URL url = urls[i]; - try - { - installBlock( url ); - } - catch( Throwable e ) - { - final String error = - "Error during block deployment for url: " + url; - } - } - } - - private Block installBlock( 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 ); - } - - getLogger().debug( "[" + name + "]"); - DefaultBlock block = new DefaultBlock(); - block.enableLogging( getLogger().getChildLogger( name ) ); - DefaultContext context = new DefaultContext(); - context.put( "urn:assembly:classloader", m_engine ); - context.put( "urn:merlin:block.manifest", manifest ); - context.makeReadOnly(); - block.contextualize( context ); - block.initialize(); - return block; } //============================================================== @@ -407,18 +357,17 @@ { throw new IllegalStateException( "context" ); } - if( m_creator == null ) + if( CREATOR == null ) { - throw new IllegalStateException( "initialization" ); + throw new NullPointerException( "creator" ); } try { DefaultLoggingManager logging = new DefaultLoggingManager(); - m_creator = new XMLContainerCreator(); LoggingDescriptor descriptor = - m_creator.createLoggingDescriptor( + CREATOR.createLoggingDescriptor( m_config.getChild( "logging" ), root ); DefaultContext context = new DefaultContext(); @@ -449,9 +398,9 @@ { throw new IllegalStateException( "logging" ); } - if( m_creator == null ) + if( CREATOR == null ) { - throw new IllegalStateException( "initialization" ); + throw new NullPointerException( "creator" ); } if( getLogger().isDebugEnabled() ) @@ -460,11 +409,11 @@ } LibraryDescriptor extensions = - m_creator.createLibraryDescriptor( + CREATOR.createLibraryDescriptor( config.getChild( "library" ) ); ClasspathDescriptor classpath = - m_creator.createClasspathDescriptor( config.getChild( "classpath" ) ); + CREATOR.createClasspathDescriptor( config.getChild( "classpath" ) ); try { @@ -492,36 +441,6 @@ } } - private Logger getLogger() - { - return m_local; - } - - private boolean isBlock( Manifest manifest ) - { - if( manifest == null ) - { - return false; - } - return ( manifest.getAttributes( Block.AVALON_BLOCK_KEY ) != null ); - } - - private 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 ); - } - } - - /** * Return a string representation of the kernel. * @return the string representation @@ -531,41 +450,4 @@ return "DefaultKernel" + ":" + System.identityHashCode( this ); } - - private 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 KernelException( error, e ); - } - } - } 1.2 +2 -48 avalon-sandbox/merlin/src/test/org/apache/avalon/merlin/kernel/KernelTestCase.java Index: KernelTestCase.java =================================================================== RCS file: /home/cvs/avalon-sandbox/merlin/src/test/org/apache/avalon/merlin/kernel/KernelTestCase.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- KernelTestCase.java 9 Dec 2002 12:16:26 -0000 1.1 +++ KernelTestCase.java 11 Dec 2002 04:59:46 -0000 1.2 @@ -58,49 +58,6 @@ String[] args = new String[]{ "kernel.xml" }; m_kernel = new DefaultKernel(); - try - { - Runtime.getRuntime().addShutdownHook( - new Thread() - { - public void run() - { - try - { - m_kernel.stop(); - } - catch( Throwable e ) - { - // ignore it - } - finally - { - m_kernel.dispose(); - } - } - } - ); - } - catch( IllegalStateException ise ) - { - if( m_kernel != null ) - { - try - { - m_kernel.stop(); - } - catch( Throwable e ) - { - // ignore it - } - finally - { - m_kernel.dispose(); - } - } - return; - } - File base = getWorkingDirectory(); DefaultContext context = new DefaultContext(); context.put( "urn:urn:avalon:home", base ); @@ -132,16 +89,13 @@ m_kernel.initialize(); m_kernel.start(); } - catch( KernelException e ) - { - // its already been logged - } catch( Throwable e ) { final String error = "Controller deployment failure."; - ExceptionHelper.printException( error, e, null, true ); + ExceptionHelper.printException( error, e ); m_kernel.dispose(); } + } /**
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>