From: markt Date: Sat, 1 May 2010 13:31:46 +0000 (+0000) Subject: Add init() and destroy() to the Lifecycle interface X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=1be74afa16b083f050c82f6c51a47b0a0986b992;p=tomcat7.0 Add init() and destroy() to the Lifecycle interface Note that this commit breaks the shutdown hook. I'll fix that in a following commit. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@940008 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/Context.java b/java/org/apache/catalina/Context.java index 6ea4f3b80..0e650be73 100644 --- a/java/org/apache/catalina/Context.java +++ b/java/org/apache/catalina/Context.java @@ -603,11 +603,6 @@ public interface Context extends Container { */ public boolean getLogEffectiveWebXml(); - /** - * Has this context been initialized? - */ - public boolean isInitialized(); - // --------------------------------------------------------- Public Methods diff --git a/java/org/apache/catalina/Lifecycle.java b/java/org/apache/catalina/Lifecycle.java index 58ccd691f..f49a389fe 100644 --- a/java/org/apache/catalina/Lifecycle.java +++ b/java/org/apache/catalina/Lifecycle.java @@ -27,30 +27,37 @@ package org.apache.catalina; *
* The valid state transitions for components that support Lifecycle are: *
- *                            --------------------<--------------------------
- *                            |                                             |
- *               start()      |        auto          auto         stop()    |       
- * NEW ------------->--- STARTING_PREP -->- STARTING -->- STARTED -->---    |
- *  |                                                        |         |    |
- *  |                                            auto        |         |    |
- *  |stop()       ---------<----- MUST_STOP --<---------------         |    |
- *  |             |                                                    |    |
- *  |             ---------------------------<--------------------------    ^
- *  |             |                                                         |
- *  |             |        auto          auto                start()        |
- *  |        STOPPING_PREP -->- STOPPING -->- STOPPED -------------->--------
- *  |             ^                              ^
- *  |             |stop()                        |
- *  |             |                              |
- *  |          FAILED                            |
- *  |                                            |
- *  --->------------------------------>-----------
+ *                                  --------------------<-----------------------
+ *                                  |                                          |
+ *    init()           start()      |        auto          auto         stop() |
+ * NEW ->-- INITIALIZED -->-- STARTING_PREP -->- STARTING -->- STARTED -->---  |
+ * | |                              ^                             |         |  |
+ * | |        start()               |                             |         |  |
+ * | ----------->--------------------                             |         |  |
+ * |                                                              |         |  |
+ * |                   auto                    auto               |         |  |
+ * |stop()       ---------<----- MUST_STOP ---------------------<--         |  |
+ * |             |                                                          |  |
+ * |             ---------------------------<--------------------------------  ^
+ * |             |                                                             |
+ * |             |         auto            auto                 start()        |
+ * |        STOPPING_PREP --->-- STOPPING --->-- STOPPED ---------->------------
+ * |             ^                                |   ^
+ * |             |stop()                          |   |
+ * |             |                       destroy()|   |
+ * |             |    destroy()                   |   |
+ * |          FAILED ---->------ DESTROYED ----<---   |
+ * |                                                  |
+ * --->------------------------------>-----------------
  *   
  * Any state can transition to FAILED.
  * 
  * Calling start() while a component is in states STARTING_PREP, STARTING or
  * STARTED has no effect.
  * 
+ * Calling start() while a component is in state NEW will cause init() to be
+ * called immediately the start() method is entered.
+ * 
  * Calling stop() while a component is in states STOPPING_PREP, STOPPING or
  * STOPPED has no effect.
  * 
@@ -60,7 +67,12 @@ package org.apache.catalina;
  * try to stop all sub-components - even those it didn't start.
  * 
  * MUST_STOP is used to indicate that the {@link #stop()} should be called on
- * the component as soon as {@link #start()} exits.
+ * the component as soon as {@link #start()} exits. It is typically used when a
+ * component has failed to start.
+ * 
+ * MUST_DESTROY is used to indicate that the {@link #stop()} should be called on
+ * the component as soon as {@link #stop()} exits. It is typically used when a
+ * component is not designed to be restarted.
  * 
  * Attempting any other transition will throw {@link LifecycleException}.
  * 
@@ -179,6 +191,20 @@ public interface Lifecycle {
 
 
     /**
+     * Prepare the component for starting. This method should perform any
+     * initialization required post object creation. The following
+     * {@link LifecycleEvent}s will be fired in the following order:
+     * 
    + *
  1. INIT_EVENT: On the successful completion of component + * initialization.
  2. + *
+ * + * @exception LifecycleException if this component detects a fatal error + * that prevents this component from being used + */ + public void init() throws LifecycleException; + + /** * Prepare for the beginning of active use of the public methods of this * component. This method should be called before any of the public * methods of this component are utilized. The following @@ -227,6 +253,19 @@ public interface Lifecycle { */ public void stop() throws LifecycleException; + /** + * Prepare to discard the object. The following {@link LifecycleEvent}s will + * be fired in the following order: + *
    + *
  1. DESTROY_EVENT: On the successful completion of component + * destruction.
  2. + *
+ * + * @exception LifecycleException if this component detects a fatal error + * that prevents this component from being used + */ + public void destroy() throws LifecycleException; + /** * Obtain the current state of the source component. diff --git a/java/org/apache/catalina/LifecycleState.java b/java/org/apache/catalina/LifecycleState.java index a569dc97f..c108cae78 100644 --- a/java/org/apache/catalina/LifecycleState.java +++ b/java/org/apache/catalina/LifecycleState.java @@ -23,14 +23,17 @@ package org.apache.catalina; */ public enum LifecycleState { NEW(false, null), + INITIALIZED(false, Lifecycle.INIT_EVENT), STARTING_PREP(false, Lifecycle.BEFORE_START_EVENT), STARTING(true, Lifecycle.START_EVENT), STARTED(true, Lifecycle.AFTER_START_EVENT), STOPPING_PREP(true, Lifecycle.BEFORE_STOP_EVENT), STOPPING(false, Lifecycle.STOP_EVENT), STOPPED(false, Lifecycle.AFTER_STOP_EVENT), + DESTROYED(false, Lifecycle.DESTROY_EVENT), FAILED(false, null), - MUST_STOP(true, null); + MUST_STOP(true, null), + MUST_DESTROY(true, null); private final boolean available; private final String lifecycleEvent; diff --git a/java/org/apache/catalina/Server.java b/java/org/apache/catalina/Server.java index 4f8cf244c..a2a0b7de2 100644 --- a/java/org/apache/catalina/Server.java +++ b/java/org/apache/catalina/Server.java @@ -155,13 +155,4 @@ public interface Server extends Lifecycle { * @param service The Service to be removed */ public void removeService(Service service); - - /** - * Invoke a pre-startup initialization. This is used to allow connectors - * to bind to restricted ports under Unix operating environments. - * - * @exception LifecycleException If this server was already initialized. - */ - public void initialize() - throws LifecycleException; } diff --git a/java/org/apache/catalina/Service.java b/java/org/apache/catalina/Service.java index 14329793a..6643e8f46 100644 --- a/java/org/apache/catalina/Service.java +++ b/java/org/apache/catalina/Service.java @@ -110,14 +110,6 @@ public interface Service extends Lifecycle { public void removeConnector(Connector connector); /** - * Invoke a pre-startup initialization. This is used to allow connectors - * to bind to restricted ports under Unix operating environments. - * - * @exception LifecycleException If this server was already initialized. - */ - public void initialize() throws LifecycleException; - - /** * Adds a named executor to the service * @param ex Executor */ diff --git a/java/org/apache/catalina/connector/Connector.java b/java/org/apache/catalina/connector/Connector.java index 65f901c01..8b932b651 100644 --- a/java/org/apache/catalina/connector/Connector.java +++ b/java/org/apache/catalina/connector/Connector.java @@ -189,12 +189,6 @@ public class Connector extends LifecycleBase implements MBeanRegistration { /** - * Has this component been initialized yet? - */ - protected boolean initialized = false; - - - /** * The shutdown signal to our background thread */ protected boolean stopped = false; @@ -857,51 +851,6 @@ public class Connector extends LifecycleBase implements MBeanRegistration { return _oname; } - /** - * Initialize this connector (create ServerSocket here!) - */ - public void initialize() - throws LifecycleException - { - if (initialized) { - if(log.isInfoEnabled()) - log.info(sm.getString("coyoteConnector.alreadyInitialized")); - return; - } - - this.initialized = true; - - if (oname == null) { - try { - // we are loaded directly, via API - and no name was given to us - // Engine name is used as domain name for MBeans - oname = createObjectName(container.getName(), "Connector"); - Registry.getRegistry(null, null) - .registerComponent(this, oname, null); - controller=oname; - } catch (Exception e) { - log.error( "Error registering connector ", e); - } - if(log.isDebugEnabled()) - log.debug("Creating name for connector " + oname); - } - - // Initializa adapter - adapter = new CoyoteAdapter(this); - protocolHandler.setAdapter(adapter); - - IntrospectionUtils.setProperty(protocolHandler, "jkHome", - System.getProperty("catalina.base")); - - try { - protocolHandler.init(); - } catch (Exception e) { - throw new LifecycleException - (sm.getString - ("coyoteConnector.protocolHandlerInitializationFailed", e)); - } - } - /** * Pause the connector. @@ -938,8 +887,6 @@ public class Connector extends LifecycleBase implements MBeanRegistration { */ @Override protected void startInternal() throws LifecycleException { - if( !initialized ) - initialize(); setState(LifecycleState.STARTING); @@ -1109,9 +1056,6 @@ public class Connector extends LifecycleBase implements MBeanRegistration { log.debug("Found engine " + obj + " " + obj.getClass()); container=(Container)obj; - // Internal initialize - we now have the Engine - initialize(); - if(log.isDebugEnabled()) log.debug("Initialized"); // As a side effect we'll get the container field set @@ -1123,19 +1067,47 @@ public class Connector extends LifecycleBase implements MBeanRegistration { } } - public void init() throws Exception { + @Override + protected void initInternal() throws LifecycleException { - if( this.getService() != null ) { - if(log.isDebugEnabled()) - log.debug( "Already configured" ); - return; - } if( container==null ) { findContainer(); } + + if (oname == null) { + try { + // we are loaded directly, via API - and no name was given to us + // Engine name is used as domain name for MBeans + oname = createObjectName(container.getName(), "Connector"); + Registry.getRegistry(null, null) + .registerComponent(this, oname, null); + controller=oname; + } catch (Exception e) { + log.error( "Error registering connector ", e); + } + if(log.isDebugEnabled()) + log.debug("Creating name for connector " + oname); + } + + // Initializa adapter + adapter = new CoyoteAdapter(this); + protocolHandler.setAdapter(adapter); + + IntrospectionUtils.setProperty(protocolHandler, "jkHome", + System.getProperty("catalina.base")); + + try { + protocolHandler.init(); + } catch (Exception e) { + throw new LifecycleException + (sm.getString + ("coyoteConnector.protocolHandlerInitializationFailed", e)); + } + } - public void destroy() throws Exception { + @Override + protected void destroyInternal() { if( oname!=null && controller==oname ) { if(log.isDebugEnabled()) log.debug("Unregister itself " + oname ); diff --git a/java/org/apache/catalina/connector/LocalStrings.properties b/java/org/apache/catalina/connector/LocalStrings.properties index cf0847103..48abb02f9 100644 --- a/java/org/apache/catalina/connector/LocalStrings.properties +++ b/java/org/apache/catalina/connector/LocalStrings.properties @@ -17,7 +17,6 @@ # # CoyoteConnector # -coyoteConnector.alreadyInitialized=The connector has already been initialized coyoteConnector.cannotRegisterProtocol=Cannot register MBean for the Protocol coyoteConnector.protocolHandlerDestroyFailed=Protocol handler destroy failed: {0} coyoteConnector.protocolHandlerInitializationFailed=Protocol handler initialization failed: {0} diff --git a/java/org/apache/catalina/connector/LocalStrings_es.properties b/java/org/apache/catalina/connector/LocalStrings_es.properties index 891207cad..9d2af1765 100644 --- a/java/org/apache/catalina/connector/LocalStrings_es.properties +++ b/java/org/apache/catalina/connector/LocalStrings_es.properties @@ -14,7 +14,6 @@ # limitations under the License. # # CoyoteConnector -coyoteConnector.alreadyInitialized = Ya ha sido inicializado el conector coyoteConnector.cannotRegisterProtocol = No puedo registrar MBean para el Protocolo coyoteConnector.protocolHandlerDestroyFailed = Fall\u00F3 la destrucci\u00F3n del manejador de protocolo\: {0} coyoteConnector.protocolHandlerInitializationFailed = Fall\u00F3 la inicializaci\u00F3n del manejador de protocolo\: {0} diff --git a/java/org/apache/catalina/connector/LocalStrings_fr.properties b/java/org/apache/catalina/connector/LocalStrings_fr.properties index d70582fc3..93d3b351c 100644 --- a/java/org/apache/catalina/connector/LocalStrings_fr.properties +++ b/java/org/apache/catalina/connector/LocalStrings_fr.properties @@ -18,7 +18,6 @@ # CoyoteConnector # -coyoteConnector.alreadyInitialized=Le connecteur a d\u00e9j\u00e0 \u00e9t\u00e9 initialis\u00e9 coyoteConnector.cannotRegisterProtocol=Impossible d''enregistrer le MBean pour le Protocol coyoteConnector.protocolHandlerDestroyFailed=La destruction du gestionnaire de protocole a \u00e9chou\u00e9: {0} coyoteConnector.protocolHandlerInitializationFailed=L''initialisation du gestionnaire de protocole a \u00e9chou\u00e9: {0} diff --git a/java/org/apache/catalina/connector/LocalStrings_ja.properties b/java/org/apache/catalina/connector/LocalStrings_ja.properties index 8d1eb77aa..30c7a7572 100644 --- a/java/org/apache/catalina/connector/LocalStrings_ja.properties +++ b/java/org/apache/catalina/connector/LocalStrings_ja.properties @@ -18,7 +18,6 @@ # CoyoteConnector # -coyoteConnector.alreadyInitialized=\u30b3\u30cd\u30af\u30bf\u306f\u65e2\u306b\u521d\u671f\u5316\u3055\u308c\u3066\u3044\u307e\u3059 coyoteConnector.cannotRegisterProtocol=\u305d\u306e\u30d7\u30ed\u30c8\u30b3\u30eb\u306bMBean\u3092\u767b\u9332\u3067\u304d\u307e\u305b\u3093 coyoteConnector.protocolHandlerDestroyFailed=\u30d7\u30ed\u30c8\u30b3\u30eb\u30cf\u30f3\u30c9\u30e9\u306e\u5ec3\u68c4\u306b\u5931\u6557\u3057\u307e\u3057\u305f: {0} coyoteConnector.protocolHandlerInitializationFailed=\u30d7\u30ed\u30c8\u30b3\u30eb\u30cf\u30f3\u30c9\u30e9\u306e\u521d\u671f\u5316\u306b\u5931\u6557\u3057\u307e\u3057\u305f: {0} diff --git a/java/org/apache/catalina/core/ApplicationServletRegistration.java b/java/org/apache/catalina/core/ApplicationServletRegistration.java index f49edb165..dd4a372bc 100644 --- a/java/org/apache/catalina/core/ApplicationServletRegistration.java +++ b/java/org/apache/catalina/core/ApplicationServletRegistration.java @@ -28,6 +28,7 @@ import javax.servlet.ServletRegistration; import javax.servlet.ServletSecurityElement; import org.apache.catalina.Context; +import org.apache.catalina.LifecycleState; import org.apache.catalina.Wrapper; import org.apache.catalina.deploy.SecurityCollection; import org.apache.catalina.deploy.SecurityConstraint; @@ -151,7 +152,7 @@ public class ApplicationServletRegistration getName(), context.getPath())); } - if (context.isInitialized()) { + if (!context.getState().equals(LifecycleState.STARTING_PREP)) { throw new IllegalStateException(sm.getString( "applicationServletRegistration.setServletSecurity.ise", getName(), context.getPath())); diff --git a/java/org/apache/catalina/core/ContainerBase.java b/java/org/apache/catalina/core/ContainerBase.java index f18d9aa15..476e8b977 100644 --- a/java/org/apache/catalina/core/ContainerBase.java +++ b/java/org/apache/catalina/core/ContainerBase.java @@ -29,10 +29,13 @@ import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; +import javax.management.InstanceNotFoundException; +import javax.management.MBeanException; import javax.management.MBeanRegistration; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; +import javax.management.ReflectionException; import javax.naming.directory.DirContext; import javax.servlet.ServletException; @@ -248,11 +251,6 @@ public abstract class ContainerBase extends LifecycleBase /** - * Has this component been initialized? - */ - protected boolean initialized=false; - - /** * Will children be started automatically when they are added. */ protected boolean startChildren = true; @@ -1061,32 +1059,40 @@ public abstract class ContainerBase extends LifecycleBase * * @throws Exception */ - public void init() throws Exception { - - if( this.getParent() == null ) { - // "Life" update - ObjectName parentName=getParentName(); - - //log.info("Register " + parentName ); - if( parentName != null && - mserver.isRegistered(parentName)) - { - mserver.invoke(parentName, "addChild", new Object[] { this }, - new String[] {"org.apache.catalina.Container"}); + @Override + protected void initInternal() throws LifecycleException{ + + try { + if( this.getParent() == null ) { + // "Life" update + ObjectName parentName; + parentName = getParentName(); + + //log.info("Register " + parentName ); + if( parentName != null && + mserver.isRegistered(parentName)) + { + mserver.invoke(parentName, "addChild", new Object[] { this }, + new String[] {"org.apache.catalina.Container"}); + } } + } catch (MalformedObjectNameException e) { + throw new LifecycleException(e); + } catch (InstanceNotFoundException e) { + throw new LifecycleException(e); + } catch (ReflectionException e) { + throw new LifecycleException(e); + } catch (MBeanException e) { + throw new LifecycleException(e); } - initialized=true; } public ObjectName getParentName() throws MalformedObjectNameException { return null; } - public void destroy() throws Exception { - if (getState().isAvailable()) { - stop(); - } - initialized=false; + @Override + protected void destroyInternal() throws LifecycleException { // unregister this component if ( oname != null ) { @@ -1106,12 +1112,6 @@ public abstract class ContainerBase extends LifecycleBase parent.removeChild(this); } - // Stop our child containers, if any - Container children[] = findChildren(); - for (int i = 0; i < children.length; i++) { - removeChild(children[i]); - } - } // ------------------------------------------------------- Pipeline Methods diff --git a/java/org/apache/catalina/core/LocalStrings.properties b/java/org/apache/catalina/core/LocalStrings.properties index 65121ad98..3d23c0d91 100644 --- a/java/org/apache/catalina/core/LocalStrings.properties +++ b/java/org/apache/catalina/core/LocalStrings.properties @@ -188,10 +188,8 @@ standardHost.warRequired=URL to web application archive is required standardHost.warURL=Invalid URL for web application archive: {0} standardHost.validationEnabled=XML validation enabled standardHost.validationDisabled=XML validation disabled -standardServer.initialize.initialized=This server has already been initialized standardServer.shutdownViaPort=A valid shutdown command was received via the shutdown port. Stopping the Server instance. standardService.connector.failed=Failed to start connector [{0}] -standardService.initialize.initialized=This service has already been initialized standardService.initialize.failed=Service initializing at {0} failed standardService.register.failed=Error registering Service at domain {0} standardService.start.name=Starting service {0} diff --git a/java/org/apache/catalina/core/LocalStrings_es.properties b/java/org/apache/catalina/core/LocalStrings_es.properties index 0877bd282..1427b5c95 100644 --- a/java/org/apache/catalina/core/LocalStrings_es.properties +++ b/java/org/apache/catalina/core/LocalStrings_es.properties @@ -162,8 +162,6 @@ standardHost.warRequired = Es necesario poner la URL a archivo de aplicaci\u00F3 standardHost.warURL = URL inv\u00E1lida para archivo de aplicaci\u00F3n web\: {0} standardHost.validationEnabled = Activada la validaci\u00F3n XML standardHost.validationDisabled = Desactivada la validaci\u00F3n XML -standardServer.initialize.initialized = Ya se ha inicializado este servidor -standardService.initialize.initialized = Ya ha sido inicializado este servicio standardService.initialize.failed = Servicio inicializando en {0} fall\u00F3 standardService.register.failed = Error registrando servicio en dominio {0} standardService.start.name = Arrancando servicio {0} diff --git a/java/org/apache/catalina/core/LocalStrings_fr.properties b/java/org/apache/catalina/core/LocalStrings_fr.properties index 9fabbf01b..8016ee98a 100644 --- a/java/org/apache/catalina/core/LocalStrings_fr.properties +++ b/java/org/apache/catalina/core/LocalStrings_fr.properties @@ -135,8 +135,6 @@ standardHost.stop=Arr\u00eat de l''application web pour le chemin de contexte {0 standardHost.unfoundContext=Impossible de trouver un contexte pour l''URI {0} demand\u00e9e standardHost.warRequired=Une URL vers l''archive d''application web (war) est n\u00e9cessaire standardHost.warURL=URL vers l''archive d''application web (war) invalide: {0} -standardServer.initialize.initialized=Ce serveur a d\u00e9j\u00e0 \u00e9t\u00e9 initialis\u00e9 -standardService.initialize.initialized=Ce service a d\u00e9j\u00e0 \u00e9t\u00e9 initialis\u00e9 standardService.start.name=D\u00e9marrage du service {0} standardService.stop.name=Arr\u00eat du service {0} standardWrapper.allocate=Erreur d''allocation \u00e0 une instance de servlet diff --git a/java/org/apache/catalina/core/LocalStrings_ja.properties b/java/org/apache/catalina/core/LocalStrings_ja.properties index 0afb169d2..24dc48419 100644 --- a/java/org/apache/catalina/core/LocalStrings_ja.properties +++ b/java/org/apache/catalina/core/LocalStrings_ja.properties @@ -142,8 +142,6 @@ standardHost.warRequired=Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30 standardHost.warURL=Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30a2\u30fc\u30ab\u30a4\u30d6\u306b\u5bfe\u3059\u308b\u7121\u52b9\u306aURL\u3067\u3059: {0} standardHost.validationEnabled=XML\u691c\u8a3c\u306f\u6709\u52b9\u3067\u3059 standardHost.validationDisabled=XML\u691c\u8a3c\u306f\u7121\u52b9\u3067\u3059 -standardServer.initialize.initialized=\u3053\u306e\u30b5\u30fc\u30d0\u306f\u65e2\u306b\u521d\u671f\u5316\u3055\u308c\u3066\u3044\u307e\u3059 -standardService.initialize.initialized=\u3053\u306e\u30b5\u30fc\u30d3\u30b9\u306f\u65e2\u306b\u521d\u671f\u5316\u3055\u308c\u3066\u3044\u307e\u3059 standardService.start.name=\u30b5\u30fc\u30d3\u30b9 {0} \u3092\u8d77\u52d5\u3057\u307e\u3059 standardService.stop.name=\u30b5\u30fc\u30d3\u30b9 {0} \u3092\u505c\u6b62\u3057\u307e\u3059 standardWrapper.allocate=\u30b5\u30fc\u30d6\u30ec\u30c3\u30c8\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u5272\u308a\u5f53\u3066\u4e2d\u306e\u30a8\u30e9\u30fc\u3067\u3059 diff --git a/java/org/apache/catalina/core/StandardContext.java b/java/org/apache/catalina/core/StandardContext.java index df4f06911..0629e847a 100644 --- a/java/org/apache/catalina/core/StandardContext.java +++ b/java/org/apache/catalina/core/StandardContext.java @@ -2348,13 +2348,6 @@ public class StandardContext } - /** - * Has this context been initialized? - */ - public boolean isInitialized() { - return initialized; - } - // -------------------------------------------------------- Context Methods @@ -4532,13 +4525,6 @@ public class StandardContext @Override protected synchronized void startInternal() throws LifecycleException { - if( !initialized ) { - try { - init(); - } catch( Exception ex ) { - throw new LifecycleException("Error initializaing ", ex); - } - } if(log.isDebugEnabled()) log.debug("Starting " + ("".equals(getName()) ? "ROOT" : getName())); @@ -5027,7 +5013,7 @@ public class StandardContext * */ @Override - public void destroy() throws Exception { + protected void destroyInternal() throws LifecycleException { if( oname != null ) { // Send j2ee.object.deleted notification Notification notification = @@ -5035,7 +5021,7 @@ public class StandardContext sequenceNumber++); broadcaster.sendNotification(notification); } - super.destroy(); + super.destroyInternal(); // Notify our interested LifecycleListeners fireLifecycleEvent(DESTROY_EVENT, null); @@ -5659,10 +5645,15 @@ public class StandardContext } @Override - public void init() throws Exception { + protected void initInternal() throws LifecycleException { if( this.getParent() == null ) { - ObjectName parentName=getParentName(); + ObjectName parentName; + try { + parentName = getParentName(); + } catch (MalformedObjectNameException e1) { + throw new LifecycleException(e1); + } if( ! mserver.isRegistered(parentName)) { if(log.isDebugEnabled()) @@ -5670,8 +5661,12 @@ public class StandardContext StandardHost host=new StandardHost(); host.setName(hostName); host.setAutoDeploy(false); - Registry.getRegistry(null, null) - .registerComponent(host, parentName, null); + try { + Registry.getRegistry(null, null) + .registerComponent(host, parentName, null); + } catch (Exception e) { + throw new LifecycleException(e); + } // We could do it the hard way... //mserver.invoke(parentName, "init", new Object[] {}, new String[] {} ); // or same thing easier: @@ -5695,7 +5690,7 @@ public class StandardContext } } catch (Exception e) { log.warn("Error creating ContextConfig for " + parentName, e); - throw e; + throw new LifecycleException(e); } this.addLifecycleListener(config); @@ -5706,19 +5701,14 @@ public class StandardContext mserver.invoke(parentName, "addChild", new Object[] { this }, new String[] {"org.apache.catalina.Container"}); } catch (Exception e) { - destroy(); - throw e; - } - // It's possible that addChild may have started us - if( initialized ) { - return; + throw new LifecycleException(e); } } if (processTlds) { this.addLifecycleListener(new TldConfig()); } - super.init(); + super.initInternal(); // Notify our interested LifecycleListeners fireLifecycleEvent(INIT_EVENT, null); diff --git a/java/org/apache/catalina/core/StandardEngine.java b/java/org/apache/catalina/core/StandardEngine.java index 1a72d3600..9361d9c13 100644 --- a/java/org/apache/catalina/core/StandardEngine.java +++ b/java/org/apache/catalina/core/StandardEngine.java @@ -295,12 +295,8 @@ public class StandardEngine } - private boolean initialized=false; - @Override - public void init() { - if( initialized ) return; - initialized=true; + protected void initInternal() { if( oname==null ) { // not registered in JMX yet - standalone mode @@ -360,7 +356,7 @@ public class StandardEngine try { service=new StandardService(); service.setContainer( this ); - service.initialize(); + service.init(); } catch( Throwable t ) { log.error(t); } @@ -369,9 +365,7 @@ public class StandardEngine } @Override - public void destroy() throws LifecycleException { - if( ! initialized ) return; - initialized=false; + protected void destroyInternal() throws LifecycleException { // if we created it, make sure it's also destroyed // this call implizit this.stop() @@ -402,6 +396,7 @@ public class StandardEngine // registry - and stop using the static. Registry.getRegistry(null, null).resetMetadata(); + super.destroyInternal(); } /** @@ -414,10 +409,6 @@ public class StandardEngine @Override protected synchronized void startInternal() throws LifecycleException { - if( !initialized ) { - init(); - } - // Look for a realm - that may have been configured earlier. // If the realm is added after context - it'll set itself. if( realm == null ) { diff --git a/java/org/apache/catalina/core/StandardHost.java b/java/org/apache/catalina/core/StandardHost.java index 657b39c20..61394569b 100644 --- a/java/org/apache/catalina/core/StandardHost.java +++ b/java/org/apache/catalina/core/StandardHost.java @@ -803,9 +803,6 @@ public class StandardHost @Override protected synchronized void startInternal() throws LifecycleException { - if( ! initialized ) - init(); - // Look for a realm - that may have been configured earlier. // If the realm is added after context - it'll set itself. if( realm == null ) { @@ -885,12 +882,8 @@ public class StandardHost } } - private boolean initialized=false; - @Override - public void init() { - if( initialized ) return; - initialized=true; + protected void initInternal() { // already registered. if( getParent() == null ) { @@ -932,10 +925,10 @@ public class StandardHost } @Override - public void destroy() throws Exception { + public void destroyInternal() throws LifecycleException { // destroy our child containers, if any Container children[] = findChildren(); - super.destroy(); + super.destroyInternal(); for (int i = 0; i < children.length; i++) { if(children[i] instanceof StandardContext) ((StandardContext)children[i]).destroy(); diff --git a/java/org/apache/catalina/core/StandardPipeline.java b/java/org/apache/catalina/core/StandardPipeline.java index 6fc3d47d4..7fdc3bf0a 100644 --- a/java/org/apache/catalina/core/StandardPipeline.java +++ b/java/org/apache/catalina/core/StandardPipeline.java @@ -157,6 +157,12 @@ public class StandardPipeline extends LifecycleBase } + @Override + protected void initInternal() { + // NOOP + } + + /** * Start {@link Valve}s) in this pipeline and implement the requirements * of {@link LifecycleBase#startInternal()}. @@ -209,6 +215,12 @@ public class StandardPipeline extends LifecycleBase } + @Override + protected void destroyInternal() { + // NOOP + } + + /** * Return a String representation of this component. */ diff --git a/java/org/apache/catalina/core/StandardServer.java b/java/org/apache/catalina/core/StandardServer.java index c933ce2d9..3922d3450 100644 --- a/java/org/apache/catalina/core/StandardServer.java +++ b/java/org/apache/catalina/core/StandardServer.java @@ -151,12 +151,6 @@ public final class StandardServer extends LifecycleBase /** - * Has this component been initialized? - */ - private boolean initialized = false; - - - /** * The property change support for this component. */ protected PropertyChangeSupport support = new PropertyChangeSupport(this); @@ -321,14 +315,6 @@ public final class StandardServer extends LifecycleBase results[services.length] = service; services = results; - if (initialized) { - try { - service.initialize(); - } catch (LifecycleException e) { - log.error(e); - } - } - if (getState().isAvailable()) { try { service.start(); @@ -689,23 +675,12 @@ public final class StandardServer extends LifecycleBase } - public void init() throws Exception { - initialize(); - } - /** * Invoke a pre-startup initialization. This is used to allow connectors * to bind to restricted ports under Unix operating environments. */ - public void initialize() - throws LifecycleException - { - if (initialized) { - log.info(sm.getString("standardServer.initialize.initialized")); - return; - } - fireLifecycleEvent(INIT_EVENT, null); - initialized = true; + @Override + protected void initInternal() throws LifecycleException { if( oname==null ) { try { @@ -729,10 +704,14 @@ public final class StandardServer extends LifecycleBase // Initialize our defined Services for (int i = 0; i < services.length; i++) { - services[i].initialize(); + services[i].init(); } } + protected void destroyInternal() { + // NOOP + } + protected String type; protected String domain; protected String suffix; diff --git a/java/org/apache/catalina/core/StandardService.java b/java/org/apache/catalina/core/StandardService.java index 95656c167..cf0d28633 100644 --- a/java/org/apache/catalina/core/StandardService.java +++ b/java/org/apache/catalina/core/StandardService.java @@ -106,12 +106,6 @@ public class StandardService extends LifecycleBase protected Container container = null; - /** - * Has this component been initialized? - */ - protected boolean initialized = false; - - // ------------------------------------------------------------- Properties @@ -247,14 +241,6 @@ public class StandardService extends LifecycleBase results[connectors.length] = connector; connectors = results; - if (initialized) { - try { - connector.initialize(); - } catch (LifecycleException e) { - log.error("Connector.initialize", e); - } - } - if (getState().isAvailable()) { try { ((Lifecycle) connector).start(); @@ -443,9 +429,6 @@ public class StandardService extends LifecycleBase @Override protected void startInternal() throws LifecycleException { - if( ! initialized ) - init(); - if(log.isInfoEnabled()) log.info(sm.getString("standardService.start.name", this.name)); setState(LifecycleState.STARTING); @@ -544,22 +527,20 @@ public class StandardService extends LifecycleBase * Invoke a pre-startup initialization. This is used to allow connectors * to bind to restricted ports under Unix operating environments. */ - public void initialize() - throws LifecycleException - { - // Service shouldn't be used with embedded, so it doesn't matter - if (initialized) { - if(log.isInfoEnabled()) - log.info(sm.getString("standardService.initialize.initialized")); - return; - } - initialized = true; + @Override + protected void initInternal() throws LifecycleException { if( oname==null ) { try { // Hack - Server should be deprecated... Container engine=this.getContainer(); - domain=engine.getName(); + + if (engine == null) { + // TODO - Get this form elsewhere + domain = "Catalina"; + } else { + domain = engine.getName(); + } oname=new ObjectName(domain + ":type=Service,serviceName="+name); this.controller=oname; Registry.getRegistry(null, null) @@ -590,7 +571,7 @@ public class StandardService extends LifecycleBase synchronized (connectors) { for (int i = 0; i < connectors.length; i++) { try { - connectors[i].initialize(); + connectors[i].init(); } catch (Exception e) { log.error(sm.getString( "standardService.connector.failed", @@ -600,19 +581,11 @@ public class StandardService extends LifecycleBase } } - public void destroy() throws LifecycleException { - if(getState().isAvailable()) stop(); + @Override + protected void destroyInternal() { // FIXME unregister should be here probably -- stop doing that ? } - public void init() { - try { - initialize(); - } catch( Throwable t ) { - log.error(sm.getString("standardService.initialize.failed",domain),t); - } - } - protected String type; protected String domain; protected String suffix; diff --git a/java/org/apache/catalina/core/StandardThreadExecutor.java b/java/org/apache/catalina/core/StandardThreadExecutor.java index 3b9a5d6b0..0a201fcde 100644 --- a/java/org/apache/catalina/core/StandardThreadExecutor.java +++ b/java/org/apache/catalina/core/StandardThreadExecutor.java @@ -93,6 +93,12 @@ public class StandardThreadExecutor extends LifecycleBase // ---------------------------------------------- Public Methods + @Override + protected void initInternal() { + // NOOP + } + + /** * Start the component and implement the requirements * of {@link LifecycleBase#startInternal()}. @@ -130,6 +136,13 @@ public class StandardThreadExecutor extends LifecycleBase executor = null; taskqueue = null; } + + + @Override + protected void destroyInternal() { + // NOOP + } + public void execute(Runnable command, long timeout, TimeUnit unit) { if ( executor != null ) { diff --git a/java/org/apache/catalina/ha/context/ReplicatedContext.java b/java/org/apache/catalina/ha/context/ReplicatedContext.java index 0bd53e71c..c71a933c2 100644 --- a/java/org/apache/catalina/ha/context/ReplicatedContext.java +++ b/java/org/apache/catalina/ha/context/ReplicatedContext.java @@ -54,14 +54,6 @@ public class ReplicatedContext extends StandardContext implements MapOwner { @Override protected synchronized void startInternal() throws LifecycleException { - if( !initialized ) { - try { - init(); - } catch( Exception ex ) { - throw new LifecycleException("Error initializaing ", ex); - } - } - try { CatalinaCluster catclust = (CatalinaCluster)this.getCluster(); if (this.context == null) this.context = new ReplApplContext(this); diff --git a/java/org/apache/catalina/ha/session/BackupManager.java b/java/org/apache/catalina/ha/session/BackupManager.java index 3fbc72cfb..c21c8538f 100644 --- a/java/org/apache/catalina/ha/session/BackupManager.java +++ b/java/org/apache/catalina/ha/session/BackupManager.java @@ -200,8 +200,6 @@ public class BackupManager extends StandardManager implements ClusterManager, Ma @Override protected synchronized void startInternal() throws LifecycleException { - if (!initialized) init(); - // Force initialization of the random number generator generateSessionId(); @@ -256,9 +254,7 @@ public class BackupManager extends StandardManager implements ClusterManager, Ma cluster.removeManager(this); this.random = null; - if( initialized ) { - destroy(); - } + setState(LifecycleState.MUST_DESTROY); } @Override diff --git a/java/org/apache/catalina/ha/session/DeltaManager.java b/java/org/apache/catalina/ha/session/DeltaManager.java index 4b78a674d..1c61021a0 100644 --- a/java/org/apache/catalina/ha/session/DeltaManager.java +++ b/java/org/apache/catalina/ha/session/DeltaManager.java @@ -761,7 +761,6 @@ public class DeltaManager extends ClusterManagerBase{ */ @Override protected synchronized void startInternal() throws LifecycleException { - if (!initialized) init(); // Force initialization of the random number generator generateSessionId(); @@ -988,9 +987,8 @@ public class DeltaManager extends ClusterManagerBase{ this.random = null; getCluster().removeManager(this); replicationValve = null; - if (initialized) { - destroy(); - } + + setState(LifecycleState.MUST_DESTROY); } // ----------------------------------------- PropertyChangeListener Methods diff --git a/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java b/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java index eb6eb15e5..74e512bea 100644 --- a/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java +++ b/java/org/apache/catalina/ha/tcp/SimpleTcpCluster.java @@ -613,6 +613,12 @@ public class SimpleTcpCluster extends LifecycleBase // ------------------------------------------------------ public + @Override + protected void initInternal() { + // NOOP + } + + /** * Start Cluster and implement the requirements * of {@link LifecycleBase#startInternal()}. @@ -732,6 +738,12 @@ public class SimpleTcpCluster extends LifecycleBase } + @Override + protected void destroyInternal() { + // NOOP + } + + /** * Return a String rendering of this object. */ diff --git a/java/org/apache/catalina/loader/WebappClassLoader.java b/java/org/apache/catalina/loader/WebappClassLoader.java index 6577c9be9..5c1c67838 100644 --- a/java/org/apache/catalina/loader/WebappClassLoader.java +++ b/java/org/apache/catalina/loader/WebappClassLoader.java @@ -1757,6 +1757,12 @@ public class WebappClassLoader } + @Override + public void init() { + // NOOP + } + + /** * Start the class loader. * @@ -1837,6 +1843,12 @@ public class WebappClassLoader } + @Override + public void destroy() { + // NOOP + } + + /** * Used to periodically signal to the classloader to release * JAR resources. diff --git a/java/org/apache/catalina/loader/WebappLoader.java b/java/org/apache/catalina/loader/WebappLoader.java index 149bf9f3c..7b798c5ba 100644 --- a/java/org/apache/catalina/loader/WebappLoader.java +++ b/java/org/apache/catalina/loader/WebappLoader.java @@ -526,10 +526,8 @@ public class WebappLoader extends LifecycleBase } - private boolean initialized=false; - - public void init() { - initialized=true; + @Override + protected void initInternal() { if( oname==null ) { // not registered yet - standalone or API @@ -558,14 +556,13 @@ public class WebappLoader extends LifecycleBase } } - public void destroy() { + @Override + protected void destroyInternal() { if( controller==oname ) { // Self-registration, undo it Registry.getRegistry(null, null).unregisterComponent(oname); oname = null; } - initialized = false; - } /** @@ -578,8 +575,6 @@ public class WebappLoader extends LifecycleBase @Override protected void startInternal() throws LifecycleException { - if( ! initialized ) init(); - if (log.isDebugEnabled()) log.debug(sm.getString("webappLoader.starting")); @@ -702,7 +697,7 @@ public class WebappLoader extends LifecycleBase classLoader = null; - destroy(); + setState(LifecycleState.MUST_DESTROY); } diff --git a/java/org/apache/catalina/realm/RealmBase.java b/java/org/apache/catalina/realm/RealmBase.java index 573db55a2..544db085f 100644 --- a/java/org/apache/catalina/realm/RealmBase.java +++ b/java/org/apache/catalina/realm/RealmBase.java @@ -968,10 +968,6 @@ public abstract class RealmBase extends LifecycleBase @Override protected void startInternal() throws LifecycleException { - if( !initialized ) { - init(); - } - // Create a MessageDigest instance for credentials, if desired if (digest != null) { try { @@ -1002,7 +998,7 @@ public abstract class RealmBase extends LifecycleBase // Clean up allocated resources md = null; - destroy(); + setState(LifecycleState.MUST_DESTROY); } @@ -1018,7 +1014,8 @@ public abstract class RealmBase extends LifecycleBase } - public void destroy() { + @Override + protected void destroyInternal() { // unregister this realm if ( oname!=null ) { @@ -1305,17 +1302,14 @@ public abstract class RealmBase extends LifecycleBase // NOOP in base class } - protected boolean initialized=false; - - public void init() { - if( initialized && container != null ) return; + @Override + protected void initInternal() { // We want logger as soon as possible if (container != null) { this.containerLog = container.getLogger(); } - initialized=true; if( container== null ) { ObjectName parent=null; // Register with the parent diff --git a/java/org/apache/catalina/session/ManagerBase.java b/java/org/apache/catalina/session/ManagerBase.java index 2c19d60b0..c66e08ae7 100644 --- a/java/org/apache/catalina/session/ManagerBase.java +++ b/java/org/apache/catalina/session/ManagerBase.java @@ -191,8 +191,6 @@ public abstract class ManagerBase extends LifecycleBase // number of duplicated session ids - anything >0 means we have problems protected int duplicates=0; - protected boolean initialized=false; - /** * Processing time during session expiration. */ @@ -712,7 +710,8 @@ public abstract class ManagerBase extends LifecycleBase } - public void destroy() { + @Override + protected void destroyInternal() { if( oname != null ) Registry.getRegistry(null, null).unregisterComponent(oname); if (randomIS!=null) { @@ -724,13 +723,11 @@ public abstract class ManagerBase extends LifecycleBase randomIS=null; } - initialized=false; oname = null; } - public void init() { - if( initialized ) return; - initialized=true; + @Override + protected void initInternal() { if( oname==null ) { try { diff --git a/java/org/apache/catalina/session/PersistentManagerBase.java b/java/org/apache/catalina/session/PersistentManagerBase.java index d9bac7b70..03dd77bf8 100644 --- a/java/org/apache/catalina/session/PersistentManagerBase.java +++ b/java/org/apache/catalina/session/PersistentManagerBase.java @@ -909,9 +909,6 @@ public abstract class PersistentManagerBase extends ManagerBase @Override protected synchronized void startInternal() throws LifecycleException { - if( ! initialized ) - init(); - // Force initialization of the random number generator if (log.isDebugEnabled()) log.debug("Force random number initialization starting"); @@ -962,8 +959,7 @@ public abstract class PersistentManagerBase extends ManagerBase // Require a new random number generator if we are restarted this.random = null; - if( initialized ) - destroy(); + setState(LifecycleState.MUST_DESTROY); } diff --git a/java/org/apache/catalina/session/StandardManager.java b/java/org/apache/catalina/session/StandardManager.java index 5daaaae35..363acfd86 100644 --- a/java/org/apache/catalina/session/StandardManager.java +++ b/java/org/apache/catalina/session/StandardManager.java @@ -570,9 +570,6 @@ public class StandardManager extends ManagerBase @Override protected synchronized void startInternal() throws LifecycleException { - if( ! initialized ) - init(); - // Force initialization of the random number generator if (log.isDebugEnabled()) log.debug("Force random number initialization starting"); @@ -633,9 +630,7 @@ public class StandardManager extends ManagerBase // Require a new random number generator if we are restarted this.random = null; - if( initialized ) { - destroy(); - } + setState(LifecycleState.MUST_DESTROY); } diff --git a/java/org/apache/catalina/session/StoreBase.java b/java/org/apache/catalina/session/StoreBase.java index 14ad0816e..ee0946a9b 100644 --- a/java/org/apache/catalina/session/StoreBase.java +++ b/java/org/apache/catalina/session/StoreBase.java @@ -182,6 +182,12 @@ public abstract class StoreBase extends LifecycleBase implements Store { } + @Override + protected void initInternal() { + // NOOP + } + + /** * Start this component and implement the requirements * of {@link LifecycleBase#startInternal()}. @@ -210,6 +216,12 @@ public abstract class StoreBase extends LifecycleBase implements Store { } + @Override + protected void destroyInternal() { + // NOOP + } + + /** * Return a String rendering of this object. */ diff --git a/java/org/apache/catalina/startup/Catalina.java b/java/org/apache/catalina/startup/Catalina.java index c83c194e8..3343e5161 100644 --- a/java/org/apache/catalina/startup/Catalina.java +++ b/java/org/apache/catalina/startup/Catalina.java @@ -501,7 +501,7 @@ public class Catalina extends Embedded { // Start the new server try { - getServer().initialize(); + getServer().init(); } catch (LifecycleException e) { if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) throw new java.lang.Error(e); @@ -535,8 +535,8 @@ public class Catalina extends Embedded { } @Override - public void destroy() { - + protected void destroyInternal() { + // NOOP } /** diff --git a/java/org/apache/catalina/startup/Embedded.java b/java/org/apache/catalina/startup/Embedded.java index 2f7b5fa43..998479d17 100644 --- a/java/org/apache/catalina/startup/Embedded.java +++ b/java/org/apache/catalina/startup/Embedded.java @@ -813,7 +813,6 @@ public class Embedded extends StandardService { setState(LifecycleState.STARTING); lifecycle.fireLifecycleEvent(START_EVENT, null); - initialized = true; // Start our defined Engines first for (int i = 0; i < engines.length; i++) { @@ -822,7 +821,6 @@ public class Embedded extends StandardService { // Start our defined Connectors second for (int i = 0; i < connectors.length; i++) { - connectors[i].initialize(); ((Lifecycle) connectors[i]).start(); } diff --git a/java/org/apache/catalina/startup/Tomcat.java b/java/org/apache/catalina/startup/Tomcat.java index 5d9f12617..785b8602f 100644 --- a/java/org/apache/catalina/startup/Tomcat.java +++ b/java/org/apache/catalina/startup/Tomcat.java @@ -288,7 +288,6 @@ public class Tomcat { public void start() throws LifecycleException { getServer(); getConnector(); - server.initialize(); server.start(); } diff --git a/java/org/apache/catalina/util/LifecycleBase.java b/java/org/apache/catalina/util/LifecycleBase.java index 66653a3f6..a519713b0 100644 --- a/java/org/apache/catalina/util/LifecycleBase.java +++ b/java/org/apache/catalina/util/LifecycleBase.java @@ -90,40 +90,51 @@ public abstract class LifecycleBase implements Lifecycle { } + public synchronized final void init() throws LifecycleException { + if (!state.equals(LifecycleState.NEW)) { + invalidTransition(Lifecycle.INIT_EVENT); + } + + // TODO - Check for JMX support and register if required + + initInternal(); + + setState(LifecycleState.INITIALIZED); + } + + + protected abstract void initInternal() throws LifecycleException; + /** * {@inheritDoc} */ @Override - public final void start() throws LifecycleException { + public synchronized final void start() throws LifecycleException { - synchronized (this) { - if (LifecycleState.STARTING_PREP.equals(state) || - LifecycleState.STARTING.equals(state) || - LifecycleState.STARTED.equals(state)) { - - if (log.isDebugEnabled()) { - Exception e = new LifecycleException(); - log.debug(sm.getString("lifecycleBase.alreadyStarted", - toString()), e); - } else if (log.isInfoEnabled()) { - log.info(sm.getString("lifecycleBase.alreadyStarted", - toString())); - } - - return; - } + if (LifecycleState.STARTING_PREP.equals(state) || + LifecycleState.STARTING.equals(state) || + LifecycleState.STARTED.equals(state)) { - if (!state.equals(LifecycleState.NEW) && - !state.equals(LifecycleState.STOPPED)) { - invalidTransition(Lifecycle.BEFORE_START_EVENT); + if (log.isDebugEnabled()) { + Exception e = new LifecycleException(); + log.debug(sm.getString("lifecycleBase.alreadyStarted", + toString()), e); + } else if (log.isInfoEnabled()) { + log.info(sm.getString("lifecycleBase.alreadyStarted", + toString())); } - - // Set state and fire event separately rather than via setState() - // so event fires outside of sync boundary - state = LifecycleState.STARTING_PREP; + + return; } - lifecycle.fireLifecycleEvent(Lifecycle.BEFORE_START_EVENT, null); + if (state.equals(LifecycleState.NEW)) { + init(); + } else if (!state.equals(LifecycleState.INITIALIZED) && + !state.equals(LifecycleState.STOPPED)) { + invalidTransition(Lifecycle.BEFORE_START_EVENT); + } + + setState(LifecycleState.STARTING_PREP); startInternal(); @@ -166,52 +177,53 @@ public abstract class LifecycleBase implements Lifecycle { * {@inheritDoc} */ @Override - public final void stop() throws LifecycleException { - - synchronized (this) { - if (LifecycleState.STOPPING_PREP.equals(state) || - LifecycleState.STOPPING.equals(state) || - LifecycleState.STOPPED.equals(state)) { - - if (log.isDebugEnabled()) { - Exception e = new LifecycleException(); - log.debug(sm.getString("lifecycleBase.alreadyStopped", - toString()), e); - } else if (log.isInfoEnabled()) { - log.info(sm.getString("lifecycleBase.alreadyStopped", - toString())); - } - - return; + public synchronized final void stop() throws LifecycleException { + + if (LifecycleState.STOPPING_PREP.equals(state) || + LifecycleState.STOPPING.equals(state) || + LifecycleState.STOPPED.equals(state)) { + + if (log.isDebugEnabled()) { + Exception e = new LifecycleException(); + log.debug(sm.getString("lifecycleBase.alreadyStopped", + toString()), e); + } else if (log.isInfoEnabled()) { + log.info(sm.getString("lifecycleBase.alreadyStopped", + toString())); } - if (state.equals(LifecycleState.NEW)) { - state = LifecycleState.STOPPED; - return; - } + return; + } + + if (state.equals(LifecycleState.NEW)) { + state = LifecycleState.STOPPED; + return; + } - if (!state.equals(LifecycleState.STARTED) && - !state.equals(LifecycleState.FAILED) && - !state.equals(LifecycleState.MUST_STOP)) { - invalidTransition(Lifecycle.BEFORE_STOP_EVENT); - } - - // Set state and fire event separately rather than via setState() - // so event fires outside of sync boundary - state = LifecycleState.STOPPING_PREP; + if (!state.equals(LifecycleState.STARTED) && + !state.equals(LifecycleState.FAILED) && + !state.equals(LifecycleState.MUST_STOP)) { + invalidTransition(Lifecycle.BEFORE_STOP_EVENT); } - lifecycle.fireLifecycleEvent(Lifecycle.BEFORE_STOP_EVENT, null); + setState(LifecycleState.STOPPING_PREP); stopInternal(); - // Shouldn't be necessary but acts as a check that sub-classes are doing - // what they are supposed to. - if (!state.equals(LifecycleState.STOPPING)) { - invalidTransition(Lifecycle.AFTER_STOP_EVENT); - } + if (state.equals(LifecycleState.MUST_DESTROY)) { + // Complete stop process first + setState(LifecycleState.STOPPED); - setState(LifecycleState.STOPPED); + destroy(); + } else { + // Shouldn't be necessary but acts as a check that sub-classes are doing + // what they are supposed to. + if (!state.equals(LifecycleState.STOPPING)) { + invalidTransition(Lifecycle.AFTER_STOP_EVENT); + } + + setState(LifecycleState.STOPPED); + } } @@ -229,6 +241,22 @@ public abstract class LifecycleBase implements Lifecycle { protected abstract void stopInternal() throws LifecycleException; + public synchronized final void destroy() throws LifecycleException { + if (!state.equals(LifecycleState.STOPPED) && + !state.equals(LifecycleState.FAILED)) { + invalidTransition(Lifecycle.DESTROY_EVENT); + } + + // TODO - Check for JMX support and de-register if required + + destroyInternal(); + + setState(LifecycleState.DESTROYED); + } + + + protected abstract void destroyInternal() throws LifecycleException; + /** * {@inheritDoc} */ @@ -244,7 +272,7 @@ public abstract class LifecycleBase implements Lifecycle { * * @param state The new state for this component */ - protected void setState(LifecycleState state) { + protected synchronized void setState(LifecycleState state) { setState(state, null); } diff --git a/java/org/apache/catalina/valves/ValveBase.java b/java/org/apache/catalina/valves/ValveBase.java index 4408bc090..af86ccfaf 100644 --- a/java/org/apache/catalina/valves/ValveBase.java +++ b/java/org/apache/catalina/valves/ValveBase.java @@ -227,6 +227,10 @@ public abstract class ValveBase extends LifecycleBase } + protected void initInternal() { + // NOOP + } + /** * Start this component and implement the requirements * of {@link LifecycleBase#startInternal()}. @@ -255,6 +259,12 @@ public abstract class ValveBase extends LifecycleBase } + @Override + protected void destroyInternal() { + // NOOP + } + + /** * Return a String rendering of this object. */