/**
- * Set the application available flag for this Context.
- *
- * @param available The new application available flag
- */
- public void setAvailable(boolean available);
-
-
- /**
* Return the Locale to character set mapper for this Context.
*/
public CharsetMapper getCharsetMapper();
public static final String PERIODIC_EVENT = "periodic";
+ /**
+ * The LifecycleEvent type for the "configure" event. Use by those
+ * components that use a separate component to perform configuration and
+ * need to signal when configuration should be performed - usually after
+ * {@link #BEFORE_START_EVENT} and before {@link #START_EVENT}.
+ */
+ public static final String CONFIGURE_EVENT = "configure";
+
+
// --------------------------------------------------------- Public Methods
*
* @return The current state of the source component.
*/
- // TODO Remove this comment once all components that implement Lifecycle
- // have had this method added
- //public LifecycleState getState();
+ public LifecycleState getState();
}
import org.apache.catalina.Globals;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
-import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.LifecycleState;
import org.apache.catalina.Loader;
import org.apache.catalina.Manager;
import org.apache.catalina.Pipeline;
import org.apache.catalina.Valve;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
-import org.apache.catalina.util.LifecycleSupport;
+import org.apache.catalina.util.LifecycleBase;
import org.apache.tomcat.util.res.StringManager;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
* @author Craig R. McClanahan
*/
-public abstract class ContainerBase
+public abstract class ContainerBase extends LifecycleBase
implements Container, MBeanRegistration {
private static final org.apache.juli.logging.Log log=
/**
- * The lifecycle event support for this component.
- */
- protected LifecycleSupport lifecycle = new LifecycleSupport(this);
-
-
- /**
* The container event listeners for this Container.
*/
protected ArrayList<ContainerListener> listeners = new ArrayList<ContainerListener>();
/**
- * Has this component been started?
+ * Has this component been initialized?
*/
- protected boolean started = false;
-
protected boolean initialized=false;
/**
this.loader = loader;
// Stop the old component if necessary
- if (started && (oldLoader != null) &&
+ if (getState().isAvailable() && (oldLoader != null) &&
(oldLoader instanceof Lifecycle)) {
try {
((Lifecycle) oldLoader).stop();
// Start the new component if necessary
if (loader != null)
loader.setContainer(this);
- if (started && (loader != null) &&
+ if (getState().isAvailable() && (loader != null) &&
(loader instanceof Lifecycle)) {
try {
((Lifecycle) loader).start();
this.manager = manager;
// Stop the old component if necessary
- if (started && (oldManager != null) &&
+ if (getState().isAvailable() && (oldManager != null) &&
(oldManager instanceof Lifecycle)) {
try {
((Lifecycle) oldManager).stop();
// Start the new component if necessary
if (manager != null)
manager.setContainer(this);
- if (started && (manager != null) &&
+ if (getState().isAvailable() && (manager != null) &&
(manager instanceof Lifecycle)) {
try {
((Lifecycle) manager).start();
this.cluster = cluster;
// Stop the old component if necessary
- if (started && (oldCluster != null) &&
+ if (getState().isAvailable() && (oldCluster != null) &&
(oldCluster instanceof Lifecycle)) {
try {
((Lifecycle) oldCluster).stop();
if (cluster != null)
cluster.setContainer(this);
- if (started && (cluster != null) &&
+ if (getState().isAvailable() && (cluster != null) &&
(cluster instanceof Lifecycle)) {
try {
((Lifecycle) cluster).start();
this.realm = realm;
// Stop the old component if necessary
- if (started && (oldRealm != null) &&
+ if (getState().isAvailable() && (oldRealm != null) &&
(oldRealm instanceof Lifecycle)) {
try {
((Lifecycle) oldRealm).stop();
// Start the new component if necessary
if (realm != null)
realm.setContainer(this);
- if (started && (realm != null) &&
+ if (getState().isAvailable() && (realm != null) &&
(realm instanceof Lifecycle)) {
try {
((Lifecycle) realm).start();
children.put(child.getName(), child);
// Start child
- if (started && startChildren) {
+ if (getState().isAvailable() && startChildren) {
boolean success = false;
try {
child.start();
children.remove(child.getName());
}
- if (started) {
+ if (getState().isAvailable()) {
try {
- if( child instanceof ContainerBase ) {
- if( ((ContainerBase)child).started ) {
- child.stop();
- }
- } else {
+ if (child.getState().isAvailable()) {
child.stop();
}
} catch (LifecycleException e) {
}
- // ------------------------------------------------------ Lifecycle Methods
-
-
- /**
- * Add a lifecycle event listener to this component.
- *
- * @param listener The listener to add
- */
- public void addLifecycleListener(LifecycleListener listener) {
-
- lifecycle.addLifecycleListener(listener);
-
- }
-
-
- /**
- * Get the lifecycle listeners associated with this lifecycle. If this
- * Lifecycle has no listeners registered, a zero-length array is returned.
- */
- public LifecycleListener[] findLifecycleListeners() {
-
- return lifecycle.findLifecycleListeners();
-
- }
-
-
- /**
- * Remove a lifecycle event listener from this component.
- *
- * @param listener The listener to remove
- */
- public void removeLifecycleListener(LifecycleListener listener) {
-
- lifecycle.removeLifecycleListener(listener);
-
- }
-
-
/**
- * Prepare for active use of the public methods of this Component.
+ * Start this component and implement the requirements
+ * of {@link LifecycleBase#startInternal()}.
*
* @exception LifecycleException if this component detects a fatal error
- * that prevents it from being started
+ * that prevents this component from being used
*/
- public synchronized void start() throws LifecycleException {
-
- // Validate and update our current component state
- if (started) {
- if(log.isInfoEnabled())
- log.info(sm.getString("containerBase.alreadyStarted", logName()));
- return;
- }
-
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
-
- started = true;
+ @Override
+ protected synchronized void startInternal() throws LifecycleException {
// Start our subordinate components, if any
if ((loader != null) && (loader instanceof Lifecycle))
if (pipeline instanceof Lifecycle)
((Lifecycle) pipeline).start();
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(START_EVENT, null);
+
+ setState(LifecycleState.STARTING);
// Start our thread
threadStart();
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
-
}
/**
- * Gracefully shut down active use of the public methods of this Component.
+ * Stop this component and implement the requirements
+ * of {@link LifecycleBase#stopInternal()}.
*
* @exception LifecycleException if this component detects a fatal error
- * that needs to be reported
+ * that prevents this component from being used
*/
- public synchronized void stop() throws LifecycleException {
-
- // Validate and update our current component state
- if (!started) {
- if(log.isInfoEnabled())
- log.info(sm.getString("containerBase.notStarted", logName()));
- return;
- }
-
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);
+ @Override
+ protected synchronized void stopInternal() throws LifecycleException {
// Stop our thread
threadStop();
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(STOP_EVENT, null);
- started = false;
+ setState(LifecycleState.STOPPING);
// Stop the Valves in our pipeline (including the basic), if any
if (pipeline instanceof Lifecycle) {
if ((loader != null) && (loader instanceof Lifecycle)) {
((Lifecycle) loader).stop();
}
-
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
-
}
/** Init method, part of the MBean lifecycle.
}
public void destroy() throws Exception {
- if( started ) {
+ if (getState().isAvailable()) {
stop();
}
initialized=false;
*/
public void backgroundProcess() {
- if (!started)
+ if (!getState().isAvailable())
return;
if (cluster != null) {
}
current = current.getNext();
}
- lifecycle.fireLifecycleEvent(Lifecycle.PERIODIC_EVENT, null);
+ fireLifecycleEvent(Lifecycle.PERIODIC_EVENT, null);
}
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.LifecycleState;
import org.apache.catalina.Loader;
import org.apache.catalina.Manager;
import org.apache.catalina.Pipeline;
import org.apache.catalina.startup.TldConfig;
import org.apache.catalina.util.CharsetMapper;
import org.apache.catalina.util.ExtensionValidator;
+import org.apache.catalina.util.LifecycleBase;
import org.apache.catalina.util.RequestUtil;
import org.apache.catalina.util.URLEncoder;
import org.apache.juli.logging.Log;
/**
- * The application available flag for this Context.
- */
- private boolean available = false;
-
- /**
* The broadcaster that sends j2ee notifications.
*/
private NotificationBroadcasterSupport broadcaster = null;
*/
public boolean getAvailable() {
- return (this.available);
-
- }
-
-
- /**
- * Set the application available flag for this Context.
- *
- * @param available The new application available flag
- */
- public void setAvailable(boolean available) {
-
- boolean oldAvailable = this.available;
- this.available = available;
- support.firePropertyChange("available",
- oldAvailable,
- this.available);
+ // TODO Remove this method entirely
+ return getState().isAvailable();
}
@Override
public synchronized void setResources(DirContext resources) {
- if (started) {
+ if (getState().isAvailable()) {
throw new IllegalStateException
(sm.getString("standardContext.resources.started"));
}
this.workDir = workDir;
- if (started) {
+ if (getState().isAvailable()) {
postWorkDirectory();
}
}
public synchronized void reload() {
// Validate our current component state
- if (!started)
+ if (!getState().isAvailable())
throw new IllegalStateException
(sm.getString("containerBase.notStarted", logName()));
/**
- * Start this Context component.
+ * Start this component and implement the requirements
+ * of {@link LifecycleBase#startInternal()}.
*
- * @exception LifecycleException if a startup error occurs
+ * @exception LifecycleException if this component detects a fatal error
+ * that prevents this component from being used
*/
@Override
- public synchronized void start() throws LifecycleException {
- //if (lazy ) return;
- if (started) {
- if(log.isInfoEnabled())
- log.info(sm.getString("containerBase.alreadyStarted", logName()));
- return;
- }
+ protected synchronized void startInternal() throws LifecycleException {
+
if( !initialized ) {
try {
init();
Registry.getRegistry(null, null).unregisterComponent(oname);
}
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
-
- setAvailable(false);
setConfigured(false);
boolean ok = true;
if (ok) {
- started = true;
-
// Start our subordinate components, if any
if ((loader != null) && (loader instanceof Lifecycle))
((Lifecycle) loader).start();
}
// Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(START_EVENT, null);
+ fireLifecycleEvent(Lifecycle.CONFIGURE_EVENT, null);
// Acquire clustered manager
Manager contextManager = null;
postWelcomeFiles();
}
- if (ok) {
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
- }
-
// Configure and call application event listeners
if (ok) {
if (!listenerStart()) {
if (ok) {
if (log.isDebugEnabled())
log.debug("Starting completed");
- setAvailable(true);
} else {
log.error(sm.getString("standardContext.startFailed", getName()));
- try {
- stop();
- } catch (Throwable t) {
- log.error(sm.getString("standardContext.startCleanup"), t);
- }
- setAvailable(false);
}
// JMX registration
}
// Reinitializing if something went wrong
- if (!ok && started) {
- stop();
+ if (!ok) {
+ setState(LifecycleState.FAILED);
+ } else {
+ setState(LifecycleState.STARTING);
}
-
- //cacheContext();
}
private Map<String, Map<String, String>> buildInjectionMap(NamingResources namingResources) {
}
/**
- * Stop this Context component.
+ * Stop this component and implement the requirements
+ * of {@link LifecycleBase#stopInternal()}.
*
- * @exception LifecycleException if a shutdown error occurs
+ * @exception LifecycleException if this component detects a fatal error
+ * that prevents this component from being used
*/
@Override
- public synchronized void stop() throws LifecycleException {
-
- // Validate and update our current component state
- if (!started) {
- if(log.isInfoEnabled())
- log.info(sm.getString("containerBase.notStarted", logName()));
- return;
- }
+ protected synchronized void stopInternal() throws LifecycleException {
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);
-
// Send j2ee.state.stopping notification
if (this.getObjectName() != null) {
Notification notification =
broadcaster.sendNotification(notification);
}
- // Mark this application as unavailable while we shut down
- setAvailable(false);
+ setState(LifecycleState.STOPPING);
// Binding thread
ClassLoader oldCCL = bindThread();
// Normal container shutdown processing
if (log.isDebugEnabled())
log.debug("Processing standard container shutdown");
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(STOP_EVENT, null);
- started = false;
// Stop the Valves in our pipeline (including the basic), if any
if (pipeline instanceof Lifecycle) {
//reset the instance manager
instanceManager = null;
- // Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
-
if (log.isDebugEnabled())
log.debug("Stopping complete");
super.destroy();
// Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(DESTROY_EVENT, null);
+ fireLifecycleEvent(DESTROY_EVENT, null);
synchronized (instanceListenersLock) {
instanceListeners = new String[0];
@Override
public void preDeregister() throws Exception {
- if( started ) {
+ if (getState().isAvailable()) {
try {
stop();
} catch( Exception ex ) {
super.init();
// Notify our interested LifecycleListeners
- lifecycle.fireLifecycleEvent(INIT_EVENT, null);
+ fireLifecycleEvent(INIT_EVENT, null);
// Send j2ee.state.starting notification
if (this.getObjectName() != null) {
import org.apache.catalina.Realm;
import org.apache.catalina.Service;
import org.apache.catalina.realm.JAASRealm;
+import org.apache.catalina.util.LifecycleBase;
import org.apache.catalina.util.ServerInfo;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
}
/**
- * Start this Engine component.
+ * Start this component and implement the requirements
+ * of {@link LifecycleBase#startInternal()}.
*
- * @exception LifecycleException if a startup error occurs
+ * @exception LifecycleException if this component detects a fatal error
+ * that prevents this component from being used
*/
@Override
- public void start() throws LifecycleException {
- if( started ) {
- return;
- }
+ protected synchronized void startInternal() throws LifecycleException {
+
if( !initialized ) {
init();
}
}
// Standard container startup
- super.start();
-
+ super.startInternal();
}
+
+ /**
+ * Stop this component and implement the requirements
+ * of {@link LifecycleBase#stopInternal()}.
+ *
+ * @exception LifecycleException if this component detects a fatal error
+ * that prevents this component from being used
+ */
@Override
- public void stop() throws LifecycleException {
- super.stop();
+ protected synchronized void stopInternal() throws LifecycleException {
+
+ super.stopInternal();
+
if( mbeans != null ) {
try {
Registry.getRegistry(null, null).invoke(mbeans, "stop", false);
import org.apache.catalina.Valve;
import org.apache.catalina.loader.WebappClassLoader;
import org.apache.catalina.startup.HostConfig;
+import org.apache.catalina.util.LifecycleBase;
import org.apache.catalina.valves.ValveBase;
import org.apache.tomcat.util.modeler.Registry;
}
/**
- * Start this host.
+ * Start this component and implement the requirements
+ * of {@link LifecycleBase#startInternal()}.
*
* @exception LifecycleException if this component detects a fatal error
- * that prevents it from being started
+ * that prevents this component from being used
*/
@Override
- public synchronized void start() throws LifecycleException {
- if( started ) {
- return;
- }
+ protected synchronized void startInternal() throws LifecycleException {
+
if( ! initialized )
init();
else
log.debug(sm.getString("standardHost.validationDisabled"));
}
- super.start();
-
+
+ super.startInternal();
}
import org.apache.catalina.security.SecurityUtil;
import org.apache.catalina.util.Enumerator;
import org.apache.catalina.util.InstanceSupport;
+import org.apache.catalina.util.LifecycleBase;
import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.PeriodicEventListener;
import org.apache.tomcat.util.log.SystemLogHandler;
public void backgroundProcess() {
super.backgroundProcess();
- if (!started)
+ if (!getState().isAvailable())
return;
if (getServlet() != null && (getServlet() instanceof PeriodicEventListener)) {
/**
- * Start this component, pre-loading the servlet if the load-on-startup
- * value is set appropriately.
+ * Start this component and implement the requirements
+ * of {@link LifecycleBase#startInternal()}.
*
- * @exception LifecycleException if a fatal error occurs during startup
+ * @exception LifecycleException if this component detects a fatal error
+ * that prevents this component from being used
*/
@Override
- public void start() throws LifecycleException {
+ protected synchronized void startInternal() throws LifecycleException {
// Send j2ee.state.starting notification
if (this.getObjectName() != null) {
}
// Start up this component
- super.start();
+ super.startInternal();
if( oname != null )
registerJMX((StandardContext)getParent());
/**
- * Stop this component, gracefully shutting down the servlet if it has
- * been initialized.
+ * Stop this component and implement the requirements
+ * of {@link LifecycleBase#stopInternal()}.
*
- * @exception LifecycleException if a fatal error occurs during shutdown
+ * @exception LifecycleException if this component detects a fatal error
+ * that prevents this component from being used
*/
@Override
- public void stop() throws LifecycleException {
+ protected synchronized void stopInternal() throws LifecycleException {
setAvailable(Long.MAX_VALUE);
}
// Shut down this component
- super.stop();
+ super.stopInternal();
// Send j2ee.state.stoppped notification
if (this.getObjectName() != null) {
import org.apache.catalina.Globals;
import javax.servlet.ServletContext;
import java.util.AbstractMap;
-import org.apache.catalina.LifecycleEvent;
-import org.apache.catalina.LifecycleListener;
import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.catalina.util.Enumerator;
+import org.apache.catalina.util.LifecycleBase;
import org.apache.catalina.tribes.tipis.AbstractReplicatedMap.MapOwner;
import org.apache.juli.logging.Log;
* @author Filip Hanik
* @version 1.0
*/
-public class ReplicatedContext extends StandardContext implements LifecycleListener,MapOwner {
+public class ReplicatedContext extends StandardContext implements MapOwner {
private int mapSendOptions = Channel.SEND_OPTIONS_DEFAULT;
private static final Log log = LogFactory.getLog( ReplicatedContext.class );
- protected boolean startComplete = false;
protected static long DEFAULT_REPL_TIMEOUT = 15000;//15 seconds
- public void lifecycleEvent(LifecycleEvent event) {
- if (AFTER_START_EVENT.equals(event.getType()))
- startComplete = true;
- }
-
+ /**
+ * Start this component and implement the requirements
+ * of {@link LifecycleBase#startInternal()}.
+ *
+ * @exception LifecycleException if this component detects a fatal error
+ * that prevents this component from being used
+ */
@Override
- public synchronized void start() throws LifecycleException {
- if ( this.started ) return;
+ protected synchronized void startInternal() throws LifecycleException {
+
if( !initialized ) {
try {
init();
throw new LifecycleException("Error initializaing ", ex);
}
}
- super.addLifecycleListener(this);
+
try {
CatalinaCluster catclust = (CatalinaCluster)this.getCluster();
if (this.context == null) this.context = new ReplApplContext(this);
((ReplApplContext)this.context).setAttributeMap(map);
if (getAltDDName() != null) context.setAttribute(Globals.ALT_DD_ATTR, getAltDDName());
}
- super.start();
+ super.startInternal();
} catch ( Exception x ) {
log.error("Unable to start ReplicatedContext",x);
throw new LifecycleException("Failed to start ReplicatedContext",x);
}
}
+ /**
+ * Stop this component and implement the requirements
+ * of {@link LifecycleBase#stopInternal()}.
+ *
+ * @exception LifecycleException if this component detects a fatal error
+ * that prevents this component from being used
+ */
@Override
- public synchronized void stop() throws LifecycleException
- {
- if ( !this.started ) return;
+ protected synchronized void stopInternal() throws LifecycleException {
+
+ super.stopInternal();
+
AbstractMap<String,Object> map =
((ReplApplContext)this.context).getAttributeMap();
if ( map!=null && map instanceof ReplicatedMap) {
((ReplicatedMap)map).breakdown();
}
- try {
- super.lifecycle.removeLifecycleListener(this);
- } catch ( Exception x ){
- log.error("Unable to stop ReplicatedContext",x);
- throw new LifecycleException("Failed to stop ReplicatedContext",x);
- } finally {
- this.startComplete = false;
- super.stop();
- }
-
-
}
@Override
public void setAttribute(String name, Object value) {
- if ( (!getParent().startComplete) || "org.apache.jasper.runtime.JspApplicationContextImpl".equals(name) ){
+ if ( (!getParent().getState().isAvailable()) || "org.apache.jasper.runtime.JspApplicationContextImpl".equals(name) ){
tomcatAttributes.put(name,value);
} else
super.setAttribute(name,value);
}
// Process the event that has occurred
- if (event.getType().equals(Lifecycle.START_EVENT)) {
- start();
+ if (event.getType().equals(Lifecycle.CONFIGURE_EVENT)) {
+ configure();
} else if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
beforeStart();
} else if (event.getType().equals(Lifecycle.AFTER_START_EVENT)) {
/**
- * Process a "start" event for this Context.
+ * Process a "contextConfig" event for this Context.
*/
- protected synchronized void start() {
+ protected synchronized void configure() {
// Called from StandardContext.start()
if (log.isDebugEnabled())
if (event.getType().equals(Lifecycle.INIT_EVENT)) {
init();
- } else if (event.getType().equals(Lifecycle.START_EVENT)) {
+ } else if (event.getType().equals(Lifecycle.CONFIGURE_EVENT)) {
try {
execute();
} catch (Exception e) {
public void lifecycleEvent(LifecycleEvent event) {
try {
Context context = (Context) event.getLifecycle();
- if (event.getType().equals(Lifecycle.START_EVENT)) {
+ if (event.getType().equals(Lifecycle.CONFIGURE_EVENT)) {
context.setConfigured(true);
}
} catch (ClassCastException e) {