niclas      2004/01/04 04:00:28

  Modified:    
merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl
                        AbstractBlock.java
  Added:       
merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl
                        Deployer.java DeploymentRequest.java
                        SimpleFIFO.java
  Removed:     
merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl
                        BlockThread.java
  Log:
  New Deployment system, which doesn't create an additional threads, and is somewhat 
immune to components that hangs during deployment. If the thread can be interrupted, 
the Deployer will throw a DeploymentException (for the rest of the activation to 
handle). If the thread can not be interrupted, it throws a FatalDeploymentException, 
which the activation system should signal upwards to kill the JVM, as there probably 
is a uncontrollable thread running. Any Exceptions or Errors thrown at the component 
level is still passed on, untampered with.
  
  Revision  Changes    Path
  1.11      +24 -74    
avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl/AbstractBlock.java
  
  Index: AbstractBlock.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl/AbstractBlock.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- AbstractBlock.java        1 Jan 2004 13:05:05 -0000       1.10
  +++ AbstractBlock.java        4 Jan 2004 12:00:28 -0000       1.11
  @@ -63,21 +63,25 @@
   import org.apache.avalon.activation.appliance.BlockContext;
   import org.apache.avalon.activation.appliance.Composite;
   import org.apache.avalon.activation.appliance.DependencyGraph;
  -import org.apache.avalon.activation.appliance.DeploymentException;
   import org.apache.avalon.activation.appliance.Engine;
   import org.apache.avalon.activation.appliance.NoProviderDefinitionException;
   import org.apache.avalon.activation.appliance.ServiceContext;
  +
   import org.apache.avalon.composition.data.CategoriesDirective;
   import org.apache.avalon.composition.logging.LoggingManager;
  +
   import org.apache.avalon.composition.event.CompositionEvent;
   import org.apache.avalon.composition.event.CompositionEventListener;
  +
   import org.apache.avalon.composition.model.ContainmentModel;
   import org.apache.avalon.composition.model.DependencyModel;
   import org.apache.avalon.composition.model.DeploymentModel;
   import org.apache.avalon.composition.model.Model;
   import org.apache.avalon.composition.model.StageModel;
  +
   import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.logger.Logger;
  +
   import org.apache.avalon.meta.info.DependencyDescriptor;
   import org.apache.avalon.meta.info.StageDescriptor;
   
  @@ -660,57 +664,29 @@
               {
                   return;
               }
  -        
  +            
               Appliance[] appliances = getLocalStartupSequence();
               if( getLogger().isDebugEnabled() )
               {
                   String message = listAppliances( "deployment: ", appliances );
                   getLogger().debug( message );
               }
  -
  -            for( int i=0; i<appliances.length; i++ )
  -            {
  -                Appliance appliance = appliances[i];
  -                if( appliance instanceof Block )
  -                {
  -                    Block block = (Block) appliance;
  -                    BlockThread thread = new BlockThread( block );
  -                    m_threads.put( block, thread );
  -                    thread.start();
  -
  -                    //
  -                    // wait for the thread to complete startup
  -                    //
  -
  -                    while( !thread.started() )
  -                    {
  -                        try
  -                        {
  -                            Thread.sleep( 300 );
  -                        }
  -                        catch( Throwable e )
  -                        {
  -                            // wakeup
  -                        }
  -                    }
  - 
  -                    //
  -                    // check for any errors raised during startup
  -                    //
  -
  -                    if( thread.getError() != null )
  -                    {
  -                        final String error =
  -                          "Composite deployment failure in block: [" 
  -                          + block + "]";
  -                        throw new DeploymentException( 
  -                          error, thread.getError() );  
  -                    }
  -                }
  -                else
  -                {
  -                    appliances[i].deploy();
  -                }
  +            ContainmentModel model = m_context.getContainmentModel();
  +            long timeout = model.getDeploymentTimeout();
  +        
  +            ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
  +            ClassLoader classloader = model.getClassLoaderModel().getClassLoader();
  +            Thread.currentThread().setContextClassLoader( classloader );    
  +            Deployer deployer = new Deployer( getLogger() );
  +            try
  +            {
  +                for( int i=0; i<appliances.length; i++ )
  +                    deployer.deploy( appliances[i], timeout );
  +            } finally
  +            {
  +                deployer.dispose();
  +                // restore the Old ContextClassloader.
  +                Thread.currentThread().setContextClassLoader( oldCL );
               }
               m_deployment.setEnabled( true );
           }
  @@ -737,34 +713,8 @@
               for( int i=0; i<appliances.length; i++ )
               {
                   Appliance appliance = appliances[i];
  -                if( appliance instanceof Block )
  -                {
  -                    BlockThread thread = 
  -                      (BlockThread) m_threads.get( appliance );
  -                    thread.decommission();
  -
  -                    //
  -                    // wait for the thread to complete decommissioning
  -                    //
  -
  -                    while( !thread.stopped() )
  -                    {
  -                        try
  -                        {
  -                            Thread.sleep( 300 );
  -                        }
  -                        catch( Throwable e )
  -                        {
  -                            // wakeup
  -                        }
  -                    }
  -                }
  -                else
  -                {
  -                    appliance.decommission();
  -                }
  +                appliance.decommission();
               }
  -
               m_deployment.setEnabled( false );
           }
       }
  
  
  
  1.1                  
avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl/Deployer.java
  
  Index: Deployer.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.activation.appliance.impl;
  
  import org.apache.avalon.activation.appliance.Deployable;
  
  import org.apache.avalon.composition.model.ContainmentModel;
  
  import org.apache.avalon.framework.logger.Logger;
  
  class Deployer
      implements Runnable
  {
      static private int  m_ThreadCounter = 0;
      
      private Logger      m_Logger;
      private Thread      m_DeploymentThread;
      private SimpleFIFO  m_DeploymentFIFO;
      
      Deployer( Logger logger )
      {
          m_Logger = logger;
          m_DeploymentFIFO = new SimpleFIFO();
          
          m_DeploymentThread = new Thread( this, "Deployer - " + m_ThreadCounter++ );
          m_DeploymentThread.start();
      }
  
      /** Deploys the given Deployable, and allows a maximum time
       *  for the deployment to complete.
       * @throws DeploymentException if the deployment hanged, but the
       * thread interruption was successful.
       * @throws FatalDeploymentException if the deployment hanged, and
       * the thread interruption failed.
       * @throws Exception any Exception or Error thrown by within the
       * deployment of the component is forwarded to the caller.
       * @throws InvocationTargetException if the deployment throws a
       * Throwable subclass that is NOT of type Exception or Error.
       **/
      void deploy( Deployable deployable, long timeout )
          throws Exception
      {
          if( deployable == null ) 
              throw new NullPointerException( "deployable" );
          if( m_Logger.isDebugEnabled() )
              m_Logger.debug( "Deployer: deploy - " + deployable );
          DeploymentRequest req = new DeploymentRequest( deployable, 
m_DeploymentThread );
          m_DeploymentFIFO.put( req );
          req.waitForCompletion( timeout );
      }
  
      /** Disposal of the Deployer.
       *
       * The Deployer allocates a deployment thread, which needs to be
       * disposed of before releasing the Deployer reference.
       **/
      void dispose()
      {
          if( m_Logger.isDebugEnabled() )
              m_Logger.debug( "Deployer: dispose deployer " );
          m_DeploymentThread.interrupt();
      }
      
      public void run()
      {
          if( m_Logger.isDebugEnabled() )
              m_Logger.debug( "Deployer: DeploymentThread started." );
          try
          {
              while( true )
              {
                  DeploymentRequest req = (DeploymentRequest) m_DeploymentFIFO.get();
                  Deployable deployable = req.getDeployable();
                  try
                  {
                      deployable.deploy();
                      req.done();
                  } catch( InterruptedException e )
                  {
                      req.interrupted();
                  } catch( Throwable e )
                  {
                      req.exception( e );
                  }
              }
          } catch( InterruptedException e )
          {} // ignore, part of dispose();
          m_DeploymentThread = null;
      }
  }
  
  
  
  1.1                  
avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl/DeploymentRequest.java
  
  Index: DeploymentRequest.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.activation.appliance.impl;
  
  import java.lang.reflect.InvocationTargetException;
  
  import org.apache.avalon.activation.appliance.Deployable;
  import org.apache.avalon.activation.appliance.DeploymentException;
  import org.apache.avalon.activation.appliance.FatalDeploymentException;
  
  class DeploymentRequest
  {
      private Deployable m_Deployable;
      private boolean   m_Completed;
      private boolean   m_Interrupted;
      private Thread    m_DeploymentThread;
      private Throwable m_Exception;
      
      DeploymentRequest( Deployable deployable, Thread deploymentThread )
      {
          m_Deployable = deployable;
          m_Completed = false;
          m_Interrupted = false;
          m_Exception = null;
          m_DeploymentThread = deploymentThread;
      }
  
      Deployable getDeployable()
      {
          return m_Deployable;
      }
  
      void waitForCompletion( long timeout )
          throws Exception
      {
          synchronized( this )
          {
              wait( timeout );
              processException();
              if( m_Completed )
                  return;
              m_DeploymentThread.interrupt();
              wait( timeout );
              processException();
              if( m_Interrupted || m_Completed )
                  throw new DeploymentException( "Deployable '" + m_Deployable + "' 
hanged during deployment and was interrupted." );
              throw new FatalDeploymentException( "Deployable '" + m_Deployable + "' 
hanged during deployment and could not be interrupted." );
          }
      }
  
      private void processException()
          throws Exception
      {
          if( m_Exception != null )
          {
              if( m_Exception instanceof Exception )
                  throw (Exception) m_Exception;
              else if( m_Exception instanceof Error )
                  throw (Error) m_Exception;
              else
                  throw new InvocationTargetException( m_Exception, "Unknown Throwable 
type, neither Exception nor Error." );
          }
      }
  
      void done()
      {
          synchronized( this )
          {
              m_Completed = true;
              notifyAll();
          }
      }
  
      void interrupted()
      {
          m_Interrupted = true;
      }
  
      void exception( Throwable e )
      {
          m_Exception = e;
      }
  }
  
  
  
  
  1.1                  
avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/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.activation.appliance.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