avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l...@apache.org
Subject cvs commit: avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/handler PrepareHandlerCommand.java
Date Wed, 11 Jun 2003 19:14:43 GMT
leif        2003/06/11 12:14:42

  Modified:    fortress/src/java/org/apache/avalon/fortress
                        ContainerManagerConstants.java
               fortress/src/java/org/apache/avalon/fortress/util
                        ContextManager.java FortressConfig.java
               fortress/src/java/org/apache/avalon/fortress/impl/handler
                        PrepareHandlerCommand.java
  Added:       fortress/src/java/org/apache/avalon/fortress/util
                        FortressCommandFailureHandler.java
  Log:
  Add the ability to override the CommandFailureHandler for the container.  This
  makes it possible for user code to trap and handle any component initialization
  problems as they see fit.
  
  Revision  Changes    Path
  1.7       +8 -1      avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/ContainerManagerConstants.java
  
  Index: ContainerManagerConstants.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/ContainerManagerConstants.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ContainerManagerConstants.java	28 May 2003 16:10:56 -0000	1.6
  +++ ContainerManagerConstants.java	11 Jun 2003 19:14:42 -0000	1.7
  @@ -52,6 +52,8 @@
   import org.apache.avalon.framework.logger.Logger;
   import org.apache.avalon.framework.service.ServiceManager;
   
  +import org.apache.excalibur.event.command.CommandFailureHandler;
  +
   /**
    * Provides constants used to access the Context object for impl
    * managers. A impl manager can assume that all these elements are
  @@ -66,6 +68,11 @@
        * Class: The class of the impl.
        */
       String CONTAINER_CLASS = "impl.class";
  +
  +    /**
  +     * Class: The class of the command failure handler impl.
  +     */
  +    String COMMAND_FAILURE_HANDLER_CLASS = CommandFailureHandler.class.getName();
   
       /**
        * ComponentLocator: The component manager to give to the impl.
  
  
  
  1.45      +20 -2     avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/util/ContextManager.java
  
  Index: ContextManager.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/util/ContextManager.java,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- ContextManager.java	11 Jun 2003 14:18:06 -0000	1.44
  +++ ContextManager.java	11 Jun 2003 19:14:42 -0000	1.45
  @@ -77,6 +77,7 @@
   import org.apache.avalon.framework.service.ServiceException;
   import org.apache.excalibur.event.Sink;
   import org.apache.excalibur.event.Queue;
  +import org.apache.excalibur.event.command.CommandFailureHandler;
   import org.apache.excalibur.event.command.CommandManager;
   import org.apache.excalibur.event.command.TPCThreadManager;
   import org.apache.excalibur.event.command.ThreadManager;
  @@ -460,9 +461,26 @@
       {
           final CommandManager cm = new CommandManager();
           final ThreadManager tm = new TPCThreadManager();
  -
  +        
           assumeOwnership( cm );
           assumeOwnership( tm );
  +
  +        // Set the CommandFailureHandler
  +        Class failureHandlerClass;
  +        try
  +        {
  +            failureHandlerClass = (Class)m_rootContext.get( COMMAND_FAILURE_HANDLER_CLASS
);
  +        }
  +        catch ( ContextException ce )
  +        {
  +            // Not set.  Use the default.
  +            failureHandlerClass = FortressCommandFailureHandler.class;
  +        }
  +        CommandFailureHandler fh = (CommandFailureHandler)failureHandlerClass.newInstance();
  +        final Logger fhLogger = m_loggerManager.getLoggerForCategory( "system.command"
);
  +        ContainerUtil.enableLogging( fh, fhLogger );
  +        ContainerUtil.initialize( fh );
  +        cm.setCommandFailureHandler( fh );
   
           // Get the context Logger Manager
           final Logger tmLogger = m_loggerManager.getLoggerForCategory( "system.threadmgr"
);
  
  
  
  1.19      +40 -2     avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/util/FortressConfig.java
  
  Index: FortressConfig.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/util/FortressConfig.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- FortressConfig.java	4 Jun 2003 13:19:41 -0000	1.18
  +++ FortressConfig.java	11 Jun 2003 19:14:42 -0000	1.19
  @@ -119,6 +119,8 @@
           {
               defaultContext.put( ContextManagerConstants.CONTAINER_CLASS,
                   DefaultContainer.class );
  +            defaultContext.put( ContextManagerConstants.COMMAND_FAILURE_HANDLER_CLASS,
  +                FortressCommandFailureHandler.class );
           }
           catch ( Exception e )
           {
  @@ -179,12 +181,48 @@
               classLoader = Thread.currentThread().getContextClassLoader();
           }
   
  -        m_context.put( ContextManagerConstants.CONTAINER_CLASS, classLoader.loadClass(
containerClass ) );
  +        setContainerClass( classLoader.loadClass( containerClass ) );
       }
   
       public void setContainerClass( final Class containerClass )
       {
           m_context.put( ContextManagerConstants.CONTAINER_CLASS, containerClass );
  +    }
  +    
  +    /**
  +     * Sets a class whose instance will be used to override the default
  +     *  CommandFailureHandler used by the container.  This makes it possible
  +     *  for applications to decide how they wish to handle failures.
  +     *
  +     * @param commandFailureHandlerClass Name of the CommandFailureHandler class to use.
  +     */
  +    public void setCommandFailureHandlerClass( final String commandFailureHandlerClass
)
  +        throws ClassNotFoundException
  +    {
  +        ClassLoader classLoader;
  +        try
  +        {
  +            classLoader = (ClassLoader) m_context.get( ClassLoader.class.getName() );
  +        }
  +        catch ( ContextException ce )
  +        {
  +            classLoader = Thread.currentThread().getContextClassLoader();
  +        }
  +
  +        setCommandFailureHandlerClass( classLoader.loadClass( commandFailureHandlerClass
) );
  +    }
  +
  +    /**
  +     * Sets a class whose instance will be used to override the default
  +     *  CommandFailureHandler used by the container.  This makes it possible
  +     *  for applications to decide how they wish to handle failures.
  +     *
  +     * @param commandFailureHandlerClass The CommandFailureHandler class to use.
  +     */
  +    public void setCommandFailureHandlerClass( final Class commandFailureHandlerClass )
  +    {
  +        m_context.put(
  +            ContextManagerConstants.COMMAND_FAILURE_HANDLER_CLASS, commandFailureHandlerClass
);
       }
   
       public void setContainerConfiguration( final Configuration config )
  
  
  
  1.1                  avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/util/FortressCommandFailureHandler.java
  
  Index: FortressCommandFailureHandler.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 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", "Avalon", "Excalibur" 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
      apache@apache.org.
  
   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.fortress.util;
  
  import org.apache.avalon.fortress.impl.handler.ComponentHandler;
  import org.apache.avalon.fortress.impl.handler.PrepareHandlerCommand;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  
  import org.apache.excalibur.event.command.Command;
  import org.apache.excalibur.event.command.CommandFailureHandler;
  
  /**
   * The default CommandFailureHandler used by Fortress to log any
   *  failures encountered while executing commands.
   *
   * @author <a href="leif.at.apache.org">Leif Mortenson</a>
   * @version CVS $Revision: 1.1 $ $Date: 2003/06/11 19:14:42 $
   * @since 4.1
   */
  public class FortressCommandFailureHandler
      extends AbstractLogEnabled
      implements CommandFailureHandler
  {
      /**
       * Handle a command failure.  If a command throws an exception, it has failed.  The
       * CommandManager will call this method so that we can handle the problem effectively.
       *
       * @param command    The original Command object that failed
       * @param throwable  The throwable that caused the failure
       *
       * @return <code>true</code> if the CommandManager should cease to process
commands.
       */
      public boolean handleCommandFailure( final Command command, final Throwable throwable
)
      {
          if ( command instanceof PrepareHandlerCommand )
          {
              PrepareHandlerCommand phc = (PrepareHandlerCommand)command;
              ComponentHandler handler = phc.getHandler();
              
              if ( getLogger().isErrorEnabled() )
              {
                  getLogger().error( "Could not prepare ComponentHandler for: "
                      + handler.getComponentClass().getName(), throwable );
              }
          }
          else
          {
              if ( getLogger().isErrorEnabled() )
              {
                  getLogger().error( "Command failed: " + command, throwable );
              }
          }
          
          // This handler never requests that commands cease to be processed.
          return false;
      }
  }
  
  
  
  
  1.10      +12 -2     avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/handler/PrepareHandlerCommand.java
  
  Index: PrepareHandlerCommand.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/handler/PrepareHandlerCommand.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- PrepareHandlerCommand.java	18 Apr 2003 20:02:29 -0000	1.9
  +++ PrepareHandlerCommand.java	11 Jun 2003 19:14:42 -0000	1.10
  @@ -75,6 +75,16 @@
           m_handler = handler;
           m_logger = ( null == logger ) ? new NullLogger() : logger;
       }
  +    
  +    /**
  +     * Returns a reference to the ComponentHandler being prepared.
  +     *
  +     * @return The ComponentHandler.
  +     */
  +    public ComponentHandler getHandler()
  +    {
  +        return m_handler;
  +    }
   
       /**
        * Invoke execution of the handler
  @@ -91,7 +101,7 @@
           {
               if ( m_logger.isErrorEnabled() )
               {
  -                m_logger.error( "Could not prepare ComponentHandler for: " + m_handler.getComponentClass().getName(),
e );
  +                //m_logger.error( "[REMOVE THIS] Could not prepare ComponentHandler for:
" + m_handler.getComponentClass().getName(), e );
               }
   
               throw e;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message