mcconnell    2004/01/14 08:32:40

  Modified:    
merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl
                        Deployer.java
               merlin/kernel/api/src/java/org/apache/avalon/merlin
                        Kernel.java
               merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl
                        DefaultFactory.java DefaultKernel.java
  Added:       merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl
                        SimpleFIFO.java
  Log:
  Update the kernel to include an event notification thread.
  
  Revision  Changes    Path
  1.5       +2 -1      
avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl/Deployer.java
  
  Index: Deployer.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl/Deployer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Deployer.java     13 Jan 2004 11:41:22 -0000      1.4
  +++ Deployer.java     14 Jan 2004 16:32:39 -0000      1.5
  @@ -56,6 +56,7 @@
   import org.apache.avalon.composition.model.ContainmentModel;
   
   import org.apache.avalon.framework.logger.Logger;
  +import org.apache.avalon.framework.activity.Disposable;
   
   /**
    * Runnable deployment thread that handles the deployment of an 
  
  
  
  1.3       +7 -1      
avalon/merlin/kernel/api/src/java/org/apache/avalon/merlin/Kernel.java
  
  Index: Kernel.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/kernel/api/src/java/org/apache/avalon/merlin/Kernel.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Kernel.java       13 Jan 2004 18:39:38 -0000      1.2
  +++ Kernel.java       14 Jan 2004 16:32:39 -0000      1.3
  @@ -99,4 +99,10 @@
       */
       Appliance locate( String path ) throws KernelException;
   
  +   /**
  +    * Return the current state of the kernel.
  +    * @return the kernel state
  +    */
  +    int getState();
  +
   }
  
  
  
  1.14      +5 -46     
avalon/merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl/DefaultFactory.java
  
  Index: DefaultFactory.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl/DefaultFactory.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- DefaultFactory.java       13 Jan 2004 18:43:15 -0000      1.13
  +++ DefaultFactory.java       14 Jan 2004 16:32:40 -0000      1.14
  @@ -103,6 +103,7 @@
   import org.apache.avalon.framework.configuration.DefaultConfiguration;
   import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
   import org.apache.avalon.framework.parameters.Parameters;
  +import org.apache.avalon.framework.activity.Disposable;
   
   import org.apache.avalon.merlin.Kernel;
   import org.apache.avalon.merlin.KernelException;
  @@ -317,33 +318,6 @@
           Kernel kernel = new DefaultKernel( kernelContext );
           setShutdownHook( getLogger(), kernel );
   
  -        /*
  -        try
  -        {
  -            m_system = 
  -              AbstractBlock.createRootBlock( facilities );
  -        }
  -        catch( Throwable e )
  -        {
  -            final String error = 
  -              "Facilities establishment failure.";
  -            throw new KernelException( error, e );
  -        }
  -
  -        try
  -        {
  -            getLogger().debug( "system deployment" );
  -            m_system.deploy();
  -            getLogger().debug( "system ready" );
  -        }
  -        catch( Throwable e )
  -        {
  -            final String error = 
  -              "System deployment failure.";
  -            throw new KernelException( error, e );
  -        }
  -        */
  -
           //
           // install any blocks declared within the kernel context
           //
  @@ -440,25 +414,6 @@
           return kernel;
       }
   
  -    /*
  -    private Kernel createKernel(
  -      Logger logger, KernelCriteria criteria, SystemContext context, 
ContainmentModel application )
  -      throws KernelException
  -    {
  -        try
  -        {
  -            return new DefaultKernel( 
  -              logger, criteria, context, application );
  -        }
  -        catch( Throwable e )
  -        {
  -            final String error = 
  -              "Kernel establishment failure.";
  -            throw new KernelException( error, e );
  -        }
  -    }
  -    */
  -
      /**
       * If the kernel criteria includes a language code
       * then set the current local to the declared value.
  @@ -1075,6 +1030,10 @@
                     {
                         logger.debug("shutdown event");
                         kernel.shutdown();
  +                      if( kernel instanceof Disposable )
  +                      {
  +                          ((Disposable)kernel).dispose();
  +                      }
                     }
                     catch( Throwable e )
                     {
  
  
  
  1.4       +132 -21   
avalon/merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl/DefaultKernel.java
  
  Index: DefaultKernel.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl/DefaultKernel.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultKernel.java        13 Jan 2004 18:43:15 -0000      1.3
  +++ DefaultKernel.java        14 Jan 2004 16:32:40 -0000      1.4
  @@ -85,7 +85,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a>
    * @version $Revision$ $Date$
    */
  -public class DefaultKernel implements Kernel
  +public class DefaultKernel implements Kernel, Disposable
   {
   
       //--------------------------------------------------------------
  @@ -138,7 +138,6 @@
               throw new KernelError( error, e );
           }
   
  -
           try
           {
               m_application = 
  @@ -151,12 +150,11 @@
               throw new KernelError( error, e );
           }
   
  -        setState( INITIALIZED );
           if( getLogger().isDebugEnabled() )
           {
               m_context.getLogger().debug( "kernel established" );
           }
  -        setState( STOPPED );
  +        setState( INITIALIZED );
       }
   
       //--------------------------------------------------------------
  @@ -164,6 +162,15 @@
       //--------------------------------------------------------------
   
      /**
  +    * Return the current state of the kernel.
  +    * @return the kernel state
  +    */
  +    public int getState()
  +    {
  +        return m_state.getState();
  +    }
  +
  +   /**
       * Add a kernel listener.
       * @param listener the kernel listener to be added
       */
  @@ -227,8 +234,7 @@
   
           synchronized( m_state )
           {
  -            if( m_state.getState() != STOPPED ) return;
  -
  +            if( !isStartable() ) return;
               if( getLogger().isDebugEnabled() )
               {
                   getLogger().debug( "application assembly" );
  @@ -277,7 +283,7 @@
       {
           synchronized( m_state )
           {
  -            if( m_state.getState() != STARTED ) return;
  +            if( !isStoppable() ) return;
   
               setState( STOPPING );
   
  @@ -313,13 +319,6 @@
                   }
               }
               */
  -
  -            if( getLogger().isDebugEnabled() )
  -            {
  -                int n = Thread.activeCount();
  -                getLogger().debug( "active threads (" + n + ")" );
  -            }
  -
               setState( STOPPED );
           }
       }
  @@ -328,6 +327,27 @@
       // internal
       //--------------------------------------------------------------
   
  +    private boolean isStartable()
  +    {
  +        synchronized( m_state )
  +        {
  +            int state = m_state.getState();
  +            if( state == INITIALIZED ) return true;
  +            if( state == STOPPED ) return true;
  +            return false;
  +        }
  +    }
  +
  +    private boolean isStoppable()
  +    {
  +        synchronized( m_state )
  +        {
  +            int state = m_state.getState();
  +            if( state == STARTED ) return true;
  +            return false;
  +        }
  +    }
  +
       /**
        * Set the state of the kernel.  The method also triggers the 
        * emmission of a attribute change notification containing the 
  @@ -337,10 +357,6 @@
        */
        private void setState( int state )
        {
  -         if( getLogger().isDebugEnabled() )
  -         {
  -             getLogger().debug( "state: " + state );
  -         }
            m_state.setState( state );
        }
   
  @@ -350,7 +366,7 @@
       }
   
   
  -    private class State
  +    private class State implements Runnable, Disposable
       {
           private int m_state = INITIALIZING;
   
  @@ -358,9 +374,15 @@
   
           private final Kernel m_kernel;
   
  +        private final SimpleFIFO m_events = new SimpleFIFO();
  +
  +        private Thread m_notification;
  +
           State( Kernel kernel )
           {
               m_kernel = kernel;
  +            m_notification = new Thread( this );
  +            m_notification.start();
           }
   
           public void addKernelEventListener( KernelEventListener listener )
  @@ -391,9 +413,31 @@
   
               m_state = newValue;
   
  +            if( getLogger().isDebugEnabled() )
  +            {
  +                getLogger().debug( this.toString() );
  +            }
  +
               KernelStateEvent event = 
                 new KernelStateEvent( m_kernel, oldValue, newValue );
  -            fireStateChangedEvent( event );
  +            m_events.put( event );
  +        }
  +
  +        public void run()
  +        {
  +            try
  +            {
  +                while( true )
  +                {
  +                    KernelStateEvent event = (KernelStateEvent) m_events.get();
  +                    fireStateChangedEvent( event );
  +                }
  +            }
  +            catch( InterruptedException e )
  +            {
  +                // trigger by disposal
  +            }
  +            m_notification = null;
           }
   
           private void fireStateChangedEvent( final KernelStateEvent event )
  @@ -415,5 +459,72 @@
                   }
               }
           }
  +
  +        public void dispose()
  +        {
  +            if( null != m_notification )
  +            { 
  +                m_notification.interrupt();
  +            }
  +        }
  +
  +        public String toString()
  +        {
  +            int s = m_state;
  +            if( s == 0 )
  +            {
  +                return "state: initializing";
  +            }
  +            else if( s == 1 )
  +            {
  +                return "state: initialized";
  +            }
  +            else if( s == 2 )
  +            {
  +                return "state: starting";
  +            }
  +            else if( s == 3 )
  +            {
  +                return "state: assembly";
  +            }
  +            else if( s == 4 )
  +            {
  +                return "state: deployment";
  +            }
  +            else if( s == 5 )
  +            {
  +                return "state: started";
  +            }
  +            else if( s == 6 )
  +            {
  +                return "state: stopping";
  +            }
  +            else if( s == 7 )
  +            {
  +                return "state: decommissioning";
  +            }
  +            else if( s == 8 )
  +            {
  +                return "state: dissassembly";
  +            }
  +            else if( s == 9 )
  +            {
  +                return "state: stopped";
  +            }
  +            else
  +            {
  +                return "state: " + s;
  +            }
  +        }
  +    }
  +
  +    public void dispose()
  +    {
  +        if( getLogger().isDebugEnabled() )
  +        {
  +            getLogger().debug( "disposal" );
  +        }
  +        shutdown();
  +        m_state.dispose();
       }
   }
  
  
  
  1.1                  
avalon/merlin/kernel/impl/src/java/org/apache/avalon/merlin/impl/SimpleFIFO.java
  
  Index: SimpleFIFO.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.impl;
  
  import java.util.ArrayList;
  
  
  class SimpleFIFO
  {
      private ArrayList m_Queue;
      
      SimpleFIFO()
      {
          m_Queue = new ArrayList();
      }
      
      void clear()
      {
          synchronized( this )
          {
              m_Queue.clear();
          }
      }
      
      void put( Object obj )
      {
          synchronized( this )
          {
              m_Queue.add( obj );
              notifyAll();
          }
      }
      
      Object get()
          throws InterruptedException
      {
          synchronized( this )
          {
              while( m_Queue.size() == 0 )
                  wait(100);
              return m_Queue.remove(0);
          }
      }
      
      int size()
      {
          synchronized( this )
          {
              return m_Queue.size();
          }
      }
  } 
  
  
  

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

Reply via email to