Return-Path: Delivered-To: apmail-jakarta-avalon-cvs-archive@apache.org Received: (qmail 1936 invoked from network); 12 Jul 2002 12:05:33 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 12 Jul 2002 12:05:33 -0000 Received: (qmail 17321 invoked by uid 97); 12 Jul 2002 12:05:45 -0000 Delivered-To: qmlist-jakarta-archive-avalon-cvs@jakarta.apache.org Received: (qmail 17305 invoked by uid 97); 12 Jul 2002 12:05:45 -0000 Mailing-List: contact avalon-cvs-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon CVS List" Reply-To: "Avalon Developers List" Delivered-To: mailing list avalon-cvs@jakarta.apache.org Received: (qmail 17293 invoked by uid 97); 12 Jul 2002 12:05:44 -0000 X-Antivirus: nagoya (v4198 created Apr 24 2002) Date: 12 Jul 2002 12:05:28 -0000 Message-ID: <20020712120528.41582.qmail@icarus.apache.org> From: mcconnell@apache.org To: jakarta-avalon-excalibur-cvs@apache.org Subject: cvs commit: jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/container LifecycleHelper.java LifecycleException.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N mcconnell 2002/07/12 05:05:28 Added: assembly/src/java/org/apache/excalibur/merlin/container LifecycleHelper.java LifecycleException.java Log: rationalization Revision Changes Path 1.1 jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/container/LifecycleHelper.java Index: LifecycleHelper.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.excalibur.merlin.container; import org.apache.avalon.excalibur.i18n.ResourceManager; import org.apache.avalon.excalibur.i18n.Resources; import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.activity.Startable; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.container.ContainerUtil; import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.logger.LogEnabled; import org.apache.avalon.framework.logger.Logger; import org.apache.avalon.framework.parameters.Parameterizable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.service.ServiceManager; import org.apache.avalon.framework.service.Serviceable; import org.apache.excalibur.merlin.model.Profile; /** * This is a class to help an Application manage the lifecycle of a component. * The implementation provides support for the processing of a component through * each lifecycle stage, and manage errors in a consistent way. * * @author Peter Donald * @author Stephen McConnell */ class LifecycleHelper extends AbstractLogEnabled { private static final Resources REZ = ResourceManager.getPackageResources( LifecycleHelper.class ); //Constants to designate stages private static final int STAGE_CREATE = 0; private static final int STAGE_LOGGER = 1; private static final int STAGE_CONTEXT = 2; private static final int STAGE_COMPOSE = 3; private static final int STAGE_CONFIG = 4; private static final int STAGE_PARAMETER = 5; private static final int STAGE_INIT = 6; private static final int STAGE_START = 7; private static final int STAGE_STOP = 8; private static final int STAGE_DISPOSE = 9; private static final int STAGE_DESTROY = 10; /** * Method to run a component through it's startup phase. * Errors that occur during startup will be logged appropriately and * cause exceptions with useful messages to be raised. * * @param name the name of the component * @param profile representing the instance criteria * @param provider the resource provider * @throws LifecycleException if an error occurs when the component passes * through a specific lifecycle stage */ public Object startup( final String name, final Profile profile, final ResourceProvider provider ) throws LifecycleException { int stage = 0; try { //Creation stage stage = STAGE_CREATE; notice( name, stage ); final Object object = provider.createObject( profile ); //LogEnabled stage stage = STAGE_LOGGER; if( object instanceof LogEnabled ) { notice( name, stage ); final Logger logger = provider.createLogger( profile ); ContainerUtil.enableLogging( object, logger ); } //Contextualize stage stage = STAGE_CONTEXT; if( object instanceof Contextualizable ) { notice( name, stage ); final Context context = provider.createContext( profile ); ContainerUtil.contextualize( object, context ); } //Composition stage stage = STAGE_COMPOSE; if( object instanceof Serviceable ) { notice( name, stage ); final ServiceManager manager = provider.createServiceManager( profile); ContainerUtil.service( object, manager ); } else if( object instanceof Composable ) { notice( name, stage ); final ComponentManager componentManager = provider.createComponentManager( profile ); ContainerUtil.compose( object, componentManager ); } //Configuring stage stage = STAGE_CONFIG; if( object instanceof Configurable ) { notice( name, stage ); final Configuration configuration = provider.createConfiguration( profile ); ContainerUtil.configure( object, configuration ); } //Parameterizing stage stage = STAGE_PARAMETER; if( object instanceof Parameterizable ) { notice( name, stage ); final Parameters parameters = provider.createParameters( profile ); ContainerUtil.parameterize( object, parameters ); } //Initialize stage stage = STAGE_INIT; if( object instanceof Initializable ) { notice( name, stage ); ContainerUtil.initialize( object ); } //Start stage stage = STAGE_START; if( object instanceof Startable ) { notice( name, stage ); ContainerUtil.start( object ); } return object; } catch( final Throwable t ) { fail( name, stage, t ); //fail() throws an exception so next //line will never be executed return null; } } /** * Method to run a component through it's shutdown phase. * Errors that occur during shutdown will be logged appropraitely. * * @param name the name of the component * @param object the component to shutdown */ public void shutdown( final String name, final Object object ) throws LifecycleException { //Stage at which failure occured int stage = 0; //Failure exception Throwable failure = null; //Stoppable stage if( object instanceof Startable ) { notice( name, STAGE_STOP ); try { ContainerUtil.stop( object ); } catch( final Throwable t ) { safeFail( name, STAGE_STOP, t ); failure = t; stage = STAGE_STOP; } } //Disposable stage if( object instanceof Disposable ) { notice( name, STAGE_DISPOSE ); try { ContainerUtil.dispose( object ); } catch( final Throwable t ) { safeFail( name, STAGE_DISPOSE, t ); failure = t; stage = STAGE_DISPOSE; } } notice( name, STAGE_DESTROY ); if( null != failure ) { fail( name, stage, failure ); } } /** * Utility method to report that a lifecycle stage is about to be processed. * * @param name the name of component that is the subject of the notice * @param stage the lifecycle processing stage */ private void notice( final String name, final int stage ) { if( getLogger().isDebugEnabled() ) { final String message = REZ.getString( "lifecycle.stage.notice", name, new Integer( stage ) ); getLogger().debug( message ); } } /** * Utility method to report that there was an error processing * specified lifecycle stage. * * @param name the name of component that caused failure * @param stage the lefecycle stage * @param t the exception thrown */ private void safeFail( final String name, final int stage, final Throwable t ) { //final String reason = t.getMessage(); final String reason = t.toString(); final String message = REZ.getString( "lifecycle.fail.error", name, new Integer( stage ), reason ); getLogger().error( message ); } /** * Utility method to report that there was an error processing * specified lifecycle stage. It will also re-throw an exception * with a better error message. * * @param name the name of block that caused failure * @param stage the stage * @param t the exception thrown * @throws LifecycleException containing error */ private void fail( final String name, final int stage, final Throwable t ) throws LifecycleException { //final String reason = t.getMessage(); final String reason = t.toString(); final String message = REZ.getString( "lifecycle.fail.error", name, new Integer( stage ), reason ); getLogger().error( message ); throw new LifecycleException( message, t ); } } 1.1 jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/container/LifecycleException.java Index: LifecycleException.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.excalibur.merlin.container; import org.apache.avalon.framework.CascadingException; import org.apache.excalibur.meta.verifier.VerifyException; /** * Exception to indicate error processing a component through its lifecycle. * * @author Peter Donald * @author Stephen McConnell * @version $Revision: 1.1 $ $Date: 2002/07/12 12:05:28 $ */ public final class LifecycleException extends CascadingException { /** * Construct a new VerifyException instance. * * @param message The detail message for this exception. */ public LifecycleException( final String message ) { this( message, null ); } /** * Construct a new VerifyException instance. * * @param message The detail message for this exception. * @param throwable the root cause of the exception */ public LifecycleException( final String message, final Throwable throwable ) { super( message, throwable ); } } -- To unsubscribe, e-mail: For additional commands, e-mail: