tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@locus.apache.org
Subject cvs commit: jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core ContainerBase.java
Date Tue, 18 Apr 2000 04:53:41 GMT
craigmcc    00/04/17 21:53:41

  Modified:    proposals/catalina/src/share/org/apache/tomcat/core
                        ContainerBase.java
  Log:
  Clean up synchronization and lifecycle (start/stop) issues related to
  adding and removing subcomponents.
  
  Add convenience logging calls to record lifecycle exceptions, etc.
  
  Revision  Changes    Path
  1.11      +237 -37   jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/ContainerBase.java
  
  Index: ContainerBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/ContainerBase.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ContainerBase.java	2000/04/17 02:34:34	1.10
  +++ ContainerBase.java	2000/04/18 04:53:41	1.11
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/ContainerBase.java,v
1.10 2000/04/17 02:34:34 craigmcc Exp $
  - * $Revision: 1.10 $
  - * $Date: 2000/04/17 02:34:34 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/ContainerBase.java,v
1.11 2000/04/18 04:53:41 craigmcc Exp $
  + * $Revision: 1.11 $
  + * $Date: 2000/04/18 04:53:41 $
    *
    * ====================================================================
    *
  @@ -149,7 +149,7 @@
    * class comments of the implementation class.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.10 $ $Date: 2000/04/17 02:34:34 $
  + * @version $Revision: 1.11 $ $Date: 2000/04/18 04:53:41 $
    */
   
   public abstract class ContainerBase
  @@ -270,15 +270,36 @@
        *
        * @param valve The new basic Valve
        */
  -    public void setBasic(Valve valve) {
  +    public synchronized void setBasic(Valve valve) {
   
  -	// FIXME - Synchronize as necessary
  -	// FIXME - Lifecycle support (on old and new) if already started
  -
  +	// Are we actually changing components?
   	Valve oldBasic = this.basic;
  -	valve.setContainer((Container) this);
  -	valve.setNext(null);
  +	if (oldBasic == valve)
  +	    return;
  +
  +	// Stop the old component if necessary
  +	if (started && (oldBasic != null) && (oldBasic instanceof Lifecycle))
{
  +	    try {
  +		((Lifecycle) oldBasic).stop();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setBasic: stop: ", e);
  +	    }
  +	}
   
  +	// Start the new component if necessary
  +	if (valve != null) {
  +	    valve.setContainer((Container) this);
  +	    valve.setNext(null);
  +	}
  +	if (started && (valve != null) && (valve instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) valve).start();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setBasic: start: ", e);
  +	    }
  +	}
  +
  +	// Link the new component into the pipeline
   	if (basic == null) {
   	    Valve previous = getLast();
   	    if (previous != null)
  @@ -295,6 +316,7 @@
   	    basic = valve;
   	}
   
  +	// Report this property change to interested listeners
   	support.firePropertyChange("basic", oldBasic, this.basic);
   
       }
  @@ -346,12 +368,37 @@
        *
        * @param loader The newly associated loader
        */
  -    public void setLoader(Loader loader) {
  +    public synchronized void setLoader(Loader loader) {
   
  +	// Are we actually changing components?
   	Loader oldLoader = this.loader;
  +	if (oldLoader == loader)
  +	    return;
  +
  +	// Stop the old component if necessary
  +	if (started && (oldLoader != null) &&
  +	    (oldLoader instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) oldLoader).stop();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setLoader: stop: ", e);
  +	    }
  +	}
  +
  +	// Start the new component if necessary
  +	if (loader != null)
  +	    loader.setContainer(this);
  +	if (started && (loader != null) &&
  +	    (loader instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) loader).start();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setLoader: start: ", e);
  +	    }
  +	}
  +
  +	// Perform the property change and report it
   	this.loader = loader;
  -	if (this.loader != null)
  -	    this.loader.setContainer(this);
   	support.firePropertyChange("loader", oldLoader, this.loader);
   
       }
  @@ -378,12 +425,37 @@
        *
        * @param logger The newly associated Logger
        */
  -    public void setLogger(Logger logger) {
  +    public synchronized void setLogger(Logger logger) {
   
  +	// Are we actually changing components?
   	Logger oldLogger = this.logger;
  +	if (oldLogger == logger)
  +	    return;
  +
  +	// Stop the old component if necessary
  +	if (started && (oldLogger != null) &&
  +	    (oldLogger instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) oldLogger).stop();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setLogger: stop: ", e);
  +	    }
  +	}
  +
  +	// Start the new component if necessary
  +	if (logger != null)
  +	    logger.setContainer(this);
  +	if (started && (logger != null) &&
  +	    (logger instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) logger).start();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setLogger: start: ", e);
  +	    }
  +	}
  +
  +	// Perform the property change and report it
   	this.logger = logger;
  -	if (this.logger != null)
  -	    this.logger.setContainer(this);
   	support.firePropertyChange("logger", oldLogger, this.logger);
   
       }
  @@ -410,12 +482,37 @@
        *
        * @param manager The newly associated Manager
        */
  -    public void setManager(Manager manager) {
  +    public synchronized void setManager(Manager manager) {
   
  +	// Are we actually changing components?
   	Manager oldManager = this.manager;
  +	if (oldManager == manager)
  +	    return;
  +
  +	// Stop the old component if necessary
  +	if (started && (oldManager != null) &&
  +	    (oldManager instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) oldManager).stop();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setManager: stop: ", e);
  +	    }
  +	}
  +
  +	// Start the new component if necessary
  +	if (manager != null)
  +	    manager.setContainer(this);
  +	if (started && (manager != null) &&
  +	    (manager instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) manager).start();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setManager: start: ", e);
  +	    }
  +	}
  +
  +	// Perform the property change and report it
   	this.manager = manager;
  -	if (this.manager != null)
  -	    this.manager.setContainer(this);
   	support.firePropertyChange("manager", oldManager, this.manager);
   
       }
  @@ -505,12 +602,37 @@
        *
        * @param realm The newly associated Realm
        */
  -    public void setRealm(Realm realm) {
  +    public synchronized void setRealm(Realm realm) {
   
  +	// Are we actually changing components?
   	Realm oldRealm = this.realm;
  +	if (oldRealm == realm)
  +	    return;
  +
  +	// Stop the old component if necessary
  +	if (started && (oldRealm != null) &&
  +	    (oldRealm instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) oldRealm).stop();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setRealm: stop: ", e);
  +	    }
  +	}
  +
  +	// Start the new component if necessary
  +	if (realm != null)
  +	    realm.setContainer(this);
  +	if (started && (realm != null) &&
  +	    (realm instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) realm).start();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setRealm: start: ", e);
  +	    }
  +	}
  +
  +	// Perform the property change and report it
   	this.realm = realm;
  -	if (this.realm != null)
  -	    this.realm.setContainer(this);
   	support.firePropertyChange("realm", oldRealm, this.realm);
   
       }
  @@ -537,12 +659,37 @@
        *
        * @param resources The newly associated Resources
        */
  -    public void setResources(Resources resources) {
  +    public synchronized void setResources(Resources resources) {
   
  +	// Are we actually changing components?
   	Resources oldResources = this.resources;
  +	if (oldResources == resources)
  +	    return;
  +
  +	// Stop the old component if necessary
  +	if (started && (oldResources != null) &&
  +	    (oldResources instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) oldResources).stop();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setResources: stop: ", e);
  +	    }
  +	}
  +
  +	// Start the new component if necessary
  +	if (resources != null)
  +	    resources.setContainer(this);
  +	if (started && (resources != null) &&
  +	    (resources instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) resources).start();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.setResources: start: ", e);
  +	    }
  +	}
  +
  +	// Perform the property change and report it
   	this.resources = resources;
  -	if (this.resources != null)
  -	    this.resources.setContainer(this);
   	support.firePropertyChange("resources", oldResources, this.resources);
   
       }
  @@ -600,6 +747,13 @@
   						   child.getName() +
   						   "' is not unique");
   	    child.setParent((Container) this);	// May throw IAE
  +	    if (child instanceof Lifecycle) {
  +		try {
  +		    ((Lifecycle) child).start();
  +		} catch (LifecycleException e) {
  +		    log("ContainerBase.addChild: start: ", e);
  +		}
  +	    }
   	    children.put(child.getName(), child);
   	    fireContainerEvent(ADD_CHILD_EVENT, child);
   	}
  @@ -679,6 +833,13 @@
   	    if (children.get(child.getName()) == null)
   		return;
   	    children.remove(child.getName());
  +	    if (child instanceof Lifecycle) {
  +		try {
  +		    ((Lifecycle) child).stop();
  +		} catch (LifecycleException e) {
  +		    log("ContainerBase.removeChild: stop: ", e);
  +		}
  +	    }
   	    child.setParent(null);
   	    fireContainerEvent(REMOVE_CHILD_EVENT, child);
   	}
  @@ -745,7 +906,7 @@
        * @exception LifecycleException if this component detects a fatal error
        *  that prevents it from being started
        */
  -    public void start() throws LifecycleException {
  +    public synchronized void start() throws LifecycleException {
   
   	// Validate and update our current component state
   	if (started)
  @@ -784,7 +945,7 @@
        * @exception LifecycleException if this component detects a fatal error
        *  that needs to be reported
        */
  -    public void stop() throws LifecycleException {
  +    public synchronized void stop() throws LifecycleException {
   
   	// Validate and update our current component state
   	if (!started)
  @@ -839,15 +1000,20 @@
        * @exception IllegalStateException if the specified Valve is already
        *  associated with a different Container
        */
  -    public void addValve(Valve valve) {
  -
  -	// FIXME - Synchronize as necesary
  -	// FIXME - Call valve.start() if Lifecycle implemented and
  -	// this Container is already started
  +    public synchronized void addValve(Valve valve) {
   
  +	// Start the new component if necessary
   	valve.setContainer((Container) this);
   	valve.setNext(basic);
  +	if (started && (valve != null) && (valve instanceof Lifecycle)) {
  +	    try {
  +		((Lifecycle) valve).start();
  +	    } catch (LifecycleException e) {
  +		log("ContainerBase.addValve: start: ", e);
  +	    }
  +	}
   
  +	// Link the new component into the pipeline
   	if (basic == null) {
   	    Valve last = getLast();
   	    if (last == null) {
  @@ -868,6 +1034,7 @@
   	    }
   	}
   
  +	// Report this pipeline change to interested listeners
   	fireContainerEvent(ADD_VALVE_EVENT, valve);
   
       }
  @@ -890,12 +1057,8 @@
        *
        * @param valve Valve to be removed
        */
  -    public void removeValve(Valve valve) {
  +    public synchronized void removeValve(Valve valve) {
   
  -	// FIXME - Synchronize as necessary
  -	// FIXME - Call valve.stop() if Lifecycle implemented and
  -	// this Container is already started
  -
   	Valve current = first;
   	while (current != null) {
   	    if (current != valve) {
  @@ -916,6 +1079,13 @@
   		if (next != null)
   		    next.setPrevious(previous);
   	    }
  +	    if (started && (valve != null) && (valve instanceof Lifecycle)) {
  +		try {
  +		    ((Lifecycle) valve).stop();
  +		} catch (LifecycleException e) {
  +		    log("ContainerBase.removeValve: stop: ", e);
  +		}
  +	    }
   	    fireContainerEvent(REMOVE_VALVE_EVENT, valve);
   	    break;
   	}
  @@ -943,6 +1113,36 @@
   	}
   	for (int i = 0; i < list.size(); i++)
   	    ((ContainerListener) list.elementAt(i)).containerEvent(event);
  +
  +    }
  +
  +
  +    /**
  +     * Log the specified message to our current Logger (if any).
  +     *
  +     * @param message Message to be logged
  +     */
  +    protected void log(String message) {
  +
  +	Logger logger = getLogger();
  +	if (logger != null)
  +	    logger.log(message);
  +
  +    }
  +
  +
  +    /**
  +     * Log the specified message and exception to our current Logger
  +     * (if any).
  +     *
  +     * @param message Message to be logged
  +     * @param throwable Related exception
  +     */
  +    protected void log(String message, Throwable throwable) {
  +
  +	Logger logger = getLogger();
  +	if (logger != null)
  +	    logger.log(message, throwable);
   
       }
   
  
  
  

Mime
View raw message