mcconnell    2003/03/06 15:23:16

  Modified:    merlin/src/java/org/apache/avalon/merlin/kernel Kernel.java
               merlin/src/java/org/apache/avalon/merlin/kernel/impl
                        DefaultKernel.java KernelLoader.java
               merlin/src/test/org/apache/avalon/merlin/kernel
                        KernelTestCase.java
  Log:
  Seperation of kernel establishment for block installation.  The kerenel is not 
created folowing which any number of blocks can be installed.
  
  Revision  Changes    Path
  1.5       +16 -9     
avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/kernel/Kernel.java
  
  Index: Kernel.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/kernel/Kernel.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Kernel.java       7 Feb 2003 15:39:35 -0000       1.4
  +++ Kernel.java       6 Mar 2003 23:23:15 -0000       1.5
  @@ -50,6 +50,10 @@
   
   package org.apache.avalon.merlin.kernel;
   
  +import java.net.URL;
  +
  +import org.apache.avalon.merlin.block.Block;
  +
   /**
    * A service that provides support for the establishment and management of a set 
    * of component container.
  @@ -65,14 +69,17 @@
       // static
       //=======================================================================
   
  -    /**
  -     * The context key for the working directory.
  -     */
  -    public static final String DIR_KEY = "urn:assembly:home";
  +   /**
  +    * Install a block into the kernel.
  +    * @param base the URL of the block configuration or a jar file 
  +    *   containing a block descriptor
  +    * @param config a possibly null URL of a configuration file
  +    *   containing component configuration targets
  +    * @return the installed block
  +    * @exception KernelException if an installation error if raised
  +    * @exception NullPointerException if the supplied base URL is null
  +    */
  +    Block install( URL base, URL config ) throws KernelException;
   
  -    /**
  -     * The context key for the path to the configuration.
  -     */
  -    public static final String PATH_KEY = "path";
   
   }
  
  
  
  1.16      +125 -75   
avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/kernel/impl/DefaultKernel.java
  
  Index: DefaultKernel.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/kernel/impl/DefaultKernel.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- DefaultKernel.java        6 Mar 2003 12:03:31 -0000       1.15
  +++ DefaultKernel.java        6 Mar 2003 23:23:15 -0000       1.16
  @@ -138,7 +138,7 @@
    * @see Block
    */
   
  -public class DefaultKernel extends AbstractLogEnabled implements Kernel, 
Contextualizable, Configurable, Initializable, Startable, Disposable
  +public class DefaultKernel extends AbstractLogEnabled implements Kernel, 
Contextualizable, Configurable, Initializable
   {
       //==============================================================
       // static
  @@ -161,6 +161,11 @@
        */
       private static PoolManager m_pool;
   
  +    /**
  +     * The block loader.
  +     */
  +    private DefaultBlockLoader m_installer;
  +
      /**
       * The kernel configuration profile.
       */
  @@ -219,7 +224,7 @@
       /**
        * The list of blocks established by the kernel.
        */
  -    private List m_blocks = new ArrayList();   
  +    private List m_blocks = new ArrayList();
   
       /**
        * The root block.
  @@ -251,6 +256,11 @@
       */
       private String m_debug;
   
  +   /**
  +    * Flag holding the initalized state of the kernel.
  +    */
  +    private boolean m_initialized = false;
  +
   
       //==============================================================
       // Contextualizable
  @@ -309,14 +319,24 @@
           m_root = (File) context.get( "urn:merlin:system" );
           m_common = (ClassLoader) context.get( "urn:merlin:classloader.common" );
           m_loader = (ClassLoader) context.get( "urn:merlin:classloader.system" );
  -        m_url = (URL) context.get( "urn:merlin:block.url" );
  -        if( context.hasEntry( "urn:merlin:block.config" ) )
  -        {
  -            m_override = (URL) context.get( "urn:merlin:block.config" );
  -        }
           if( context.hasEntry( "urn:merlin:debug" ) )
           {
               m_debug = (String) context.get( "urn:merlin:debug" );
  +            System.out.println("### DEBUG: " + m_debug );
  +        }
  +
  +        //
  +        // get the root block if an only if it is declared
  +        // in the context
  +        //
  +
  +        if( context.hasEntry( "urn:merlin:block.url" ) )
  +        {
  +            m_url = (URL) context.get( "urn:merlin:block.url" );
  +            if( context.hasEntry( "urn:merlin:block.config" ) )
  +            {
  +                m_override = (URL) context.get( "urn:merlin:block.config" );
  +            }
           }
       }
   
  @@ -347,6 +367,12 @@
       */
       public void initialize() throws Exception
       {
  +        if( m_initialized )
  +        {
  +            final String error = 
  +              "Kernel has already been initialized.";
  +            throw new IllegalStateException( error );
  +        }
   
           if( m_home == null )
           {
  @@ -406,7 +432,8 @@
   
           try
           {
  -            m_engine = bootstrapEngine( m_logging, m_pool, m_config.getChild( 
"engine" ), m_loader );
  +            Configuration engine = m_config.getChild( "engine" );
  +            m_engine = bootstrapEngine( m_logging, m_pool, engine, m_loader );
           }
           catch( Throwable e )
           {
  @@ -424,20 +451,20 @@
           // we are now ready to assemble the block 
           //
   
  -        DefaultBlockLoader loader = new DefaultBlockLoader();
  +        m_installer = new DefaultBlockLoader();
           try
           {
  -            loader.enableLogging( getLogger() );
  +            m_installer.enableLogging( getLogger() );
               DefaultLocator context = new DefaultLocator();
               context.put( "urn:assembly:engine", m_engine );
               context.put( "urn:assembly:system-context", getSystemContext() );
               context.makeReadOnly();
  -            loader.contextualize( context );
  -            loader.initialize();
  +            m_installer.contextualize( context );
  +            m_installer.initialize();
           }
           catch( Throwable e )
           {
  -            final String error = "Block installation phase failure.";
  +            final String error = "Block installer establishmet failure.";
               String log = ExceptionHelper.packException( error, e );
               if( getLogger().isErrorEnabled() )
               {
  @@ -446,19 +473,55 @@
               throw new KernelException( error, e );
           }
   
  -        if( getLogger().isInfoEnabled() )
  +        m_initialized = true;
  +
  +        //
  +        // install a block if a block URL was included in the context argument
  +        //
  +
  +        if( m_url != null )
  +        {
  +            install( m_url, m_override );
  +        }
  +    }
  +
  +    //==============================================================
  +    // Kernel
  +    //==============================================================
  +
  +   /**
  +    * Install a block into the kernel.
  +    * @param base the URL of the block configuration or a jar file 
  +    *   containing a block descriptor
  +    * @param config a possibly null URL of a configuration file
  +    *   containing component configuration targets
  +    * @return the installed block
  +    * @exception KernelException if an installation error if raised
  +    * @exception NullPointerException if the supplied base URL is null
  +    */
  +    public Block install( URL base, URL config ) throws KernelException
  +    {
  +        if( base == null )
           {
  -            getLogger().info( "commencing block installation phase" );
  +            throw new NullPointerException( "base" );
  +        }
  +
  +        if( !m_initialized )
  +        {
  +            final String error = 
  +              "Kernel has not been initialized.";
  +            throw new IllegalStateException( error );
           }
   
           if( getLogger().isDebugEnabled() )
           {
  -            getLogger().debug(  "installing block: " + m_url );
  +            getLogger().debug(  "installing block: " + base );
           }
   
  +        Block block = null;
           try
           {
  -            m_block = loader.install( m_url, m_override );
  +            block = m_installer.install( base, config );
           }
           catch( Throwable e )
           {
  @@ -479,12 +542,12 @@
   
           try
           {
  -            m_block.assemble( new DependencyGraph( m_graph ) );
  +            block.assemble( new DependencyGraph( m_graph ) );
           }
           catch( Throwable e )
           {
               final String error = 
  -              "Unable to deploy block: " + m_block.getName() + " due to an assembly 
failure.";
  +              "Unable to deploy block: " + block.getName() + " due to an assembly 
failure.";
               String log = ExceptionHelper.packException( error, e );
               if( getLogger().isErrorEnabled() )
               {
  @@ -505,84 +568,69 @@
               buffer.append( "\n" );
               getLogger().debug( buffer.toString() );
           }
  -    }
   
  -    //==============================================================
  -    // Startable
  -    //==============================================================
  -
  -   /**
  -    * Start the kernel.
  -    *
  -    * @exception Exception if an startup error occurs.
  -    */
  -    public void start() throws Exception 
  -    {
  -        if( getLogger().isInfoEnabled() )
  -        {
  -            getLogger().info( "commencing deployment phase" );
  -        }
  -
  -        if( m_block != null )
  -        {
  -            m_block.deploy();
  -        }
  -
  -        if( getLogger().isInfoEnabled() )
  +        try
           {
  -            getLogger().info( "deployment complete" );
  +            block.deploy();
           }
  -    }
  -
  -   /**
  -    * Stop the kernel.
  -    *
  -    * @exception Exception if an shutdown error occurs.
  -    */
  -    public void stop() throws Exception 
  -    {
  -        if( getLogger() != null ) 
  +        catch( Throwable e )
           {
  -            if( getLogger().isInfoEnabled() )
  +            final String error = 
  +              "Unable to deploy block: " + block.getName() + " due to an deployment 
failure.";
  +            String log = ExceptionHelper.packException( error, e );
  +            if( getLogger().isErrorEnabled() )
               {
  -                getLogger().info( "commencing decommissioning phase" );
  +                getLogger().error( log );
               }
  +            throw new KernelException( error, e );
           }
   
  -        if( m_block != null )
  -        {
  -            m_block.decommission();
  -        }
  +        m_blocks.add( block );
  +        return block;
       }
   
       //==============================================================
  -    // Disposable
  +    // DefaultKernel
       //==============================================================
   
      /**
  -    * Disposal of the kernel.
  +    * Shutdown the kernel during which orderly shutdown of all 
  +    * installed blocks is undertaken.
       */
  -    public void dispose()
  +    public void shutdown()
       {
           if( getLogger() != null ) 
           {
               if( getLogger().isInfoEnabled() )
               {
  -                getLogger().info( "commencing termination phase" );
  +                getLogger().info( "commencing decommissioning phase" );
               }
           }
   
  -        if( m_block != null )
  +        Block[] blocks = (Block[]) m_blocks.toArray( new Block[0] );
  +        for( int i=0; i<blocks.length; i++ )
           {
  -            m_block.terminate();
  +            Block block = blocks[i];
  +            try
  +            {
  +                block.decommission();
  +                block.terminate();
  +            }
  +            catch( Throwable e )
  +            {
  +                if( getLogger().isWarnEnabled() )
  +                {
  +                    final String error = 
  +                      "Ignoring block decommissioning error.";
  +                    getLogger().warn( error, e );
  +                }        
  +            }
  +            m_blocks.remove( block );
           }
   
  -        if( getLogger() != null )
  +        if( getLogger().isInfoEnabled() )
           {
  -            if( getLogger().isInfoEnabled() )
  -            {
  -                getLogger().info( "bye" );
  -            }
  +            getLogger().info( "bye" );
           }
       }
   
  @@ -691,18 +739,20 @@
               Locator system = getSystemContext();
               DefaultLocator context = new DefaultLocator( system );
               context.put( "urn:assembly:engine.bootstrap", "true" );
  -            //context.put( "urn:assembly:engine.base", m_url );
  +            context.put( "urn:assembly:home", m_home );
  +            context.put( "urn:assembly:system", m_root );
               if( lib != null )
               {
                   LibraryDescriptor extensions =
                     CREATOR.createLibraryDescriptor( config.getChild( "library" ) );
                   context.put( "urn:assembly:engine.extensions", extensions );
               }
  -            context.put( "urn:assembly:engine.classpath", classpath );
  +            if( classpath != null )
  +            {
  +                context.put( "urn:assembly:engine.classpath", classpath );
  +            }
               context.put( "urn:assembly:logging.manager", logging );
               context.put( "urn:assembly:threads.manager", pool );
  -            context.put( "urn:assembly:home", m_home );
  -            context.put( "urn:assembly:system", m_root );
               context.makeReadOnly();
               engine.contextualize( context );
               engine.initialize();
  @@ -728,7 +778,7 @@
               URL[] urls = m_block.getURLs();
               for( int i=0; i<urls.length; i++ )
               {
  -                buffer.append( "\n  " + urls[i].toString() );
  +                buffer.append( "\n" + urls[i].toString() );
               }
               buffer.append( "\n" );
               return buffer.toString();
  
  
  
  1.7       +4 -15     
avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/kernel/impl/KernelLoader.java
  
  Index: KernelLoader.java
  ===================================================================
  RCS file: 
/home/cvs/avalon-sandbox/merlin/src/java/org/apache/avalon/merlin/kernel/impl/KernelLoader.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- KernelLoader.java 6 Mar 2003 09:17:42 -0000       1.6
  +++ KernelLoader.java 6 Mar 2003 23:23:15 -0000       1.7
  @@ -75,20 +75,13 @@
                       {
                           if( m_kernel != null )
                           {
  -                            m_kernel.stop();
  +                            m_kernel.shutdown();
                           }
                       }
                       catch( Throwable e )
                       {
                           // ignore it
                       }
  -                    finally
  -                    {
  -                        if( m_kernel != null )
  -                        {
  -                            m_kernel.dispose();
  -                        }
  -                    }
                   }
                 }
               );
  @@ -99,16 +92,12 @@
                {
                    try
                    {
  -                     m_kernel.stop();
  +                     m_kernel.shutdown();
                    }
                    catch( Throwable e )
                    {
                        // ignore it
                    }
  -                 finally
  -                 {
  -                     m_kernel.dispose();
  -                 }
                }
                return;
           }
  @@ -124,6 +113,7 @@
           //
   
           DefaultLocator context = new DefaultLocator( map );
  +
           context.makeReadOnly();
           try
           {
  @@ -131,7 +121,6 @@
               m_kernel.contextualize( context );
               m_kernel.configure( getKernelConfiguration( profile ) );
               m_kernel.initialize();
  -            m_kernel.start();
           }
           catch( KernelException e )
           {
  @@ -146,7 +135,7 @@
               final String error = "Controller deployment failure.";
               String message = ExceptionHelper.packException( error, e );
               System.err.println( message );
  -            m_kernel.dispose();
  +            m_kernel.shutdown();
           }
       }
   
  
  
  
  1.8       +9 -5      
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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- KernelTestCase.java       7 Feb 2003 23:08:56 -0000       1.7
  +++ KernelTestCase.java       6 Mar 2003 23:23:16 -0000       1.8
  @@ -58,24 +58,28 @@
           String[] args = new String[]{ "kernel.xml" };
           m_kernel = new DefaultKernel();
   
  +        ClassLoader loader = Thread.currentThread().getContextClassLoader();
  +
           File base = getWorkingDirectory();
           DefaultLocator context = new DefaultLocator();
  -        context.put( "urn:assembly:home", base );
  +        context.put( "urn:merlin:home", base );
  +        context.put( "urn:merlin:system", base );
  +        context.put( "urn:merlin:classloader.common", loader );
  +        context.put( "urn:merlin:classloader.system", loader );
  +        context.put( "urn:merlin:debug", "WARN" );
           context.makeReadOnly();
   
           try
           {
               m_kernel.contextualize( context );
               m_kernel.initialize();
  -            m_kernel.start();
  -            m_kernel.stop();
  -            m_kernel.dispose();
  +            m_kernel.shutdown();
           }
           catch( Throwable e )
           {
               final String error = "Controller deployment failure.";
               ExceptionHelper.printException( error, e );
  -            m_kernel.dispose();
  +            assertTrue( false );
           }
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to