use of org.apache.catalina.Lifecycle in project tomcat by apache.
the class ThreadLocalLeakPreventionListener method lifecycleEvent.
/**
* Listens for {@link LifecycleEvent} for the start of the {@link Server} to
* initialize itself and then for after_stop events of each {@link Context}.
*/
@Override
public void lifecycleEvent(LifecycleEvent event) {
try {
Lifecycle lifecycle = event.getLifecycle();
if (Lifecycle.AFTER_START_EVENT.equals(event.getType()) && lifecycle instanceof Server) {
// when the server starts, we register ourself as listener for
// all context
// as well as container event listener so that we know when new
// Context are deployed
Server server = (Server) lifecycle;
registerListenersForServer(server);
}
if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType()) && lifecycle instanceof Server) {
// Server is shutting down, so thread pools will be shut down so
// there is no need to clean the threads
serverStopping = true;
}
if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType()) && lifecycle instanceof Context) {
stopIdleThreads((Context) lifecycle);
}
} catch (Exception e) {
String msg = sm.getString("threadLocalLeakPreventionListener.lifecycleEvent.error", event);
log.error(msg, e);
}
}
use of org.apache.catalina.Lifecycle in project tomcat by apache.
the class StandardContext method setManager.
@Override
public void setManager(Manager manager) {
Lock writeLock = managerLock.writeLock();
writeLock.lock();
Manager oldManager = null;
try {
// Change components if necessary
oldManager = this.manager;
if (oldManager == manager)
return;
this.manager = manager;
// Stop the old component if necessary
if (oldManager instanceof Lifecycle) {
try {
((Lifecycle) oldManager).stop();
((Lifecycle) oldManager).destroy();
} catch (LifecycleException e) {
log.error("StandardContext.setManager: stop-destroy: ", e);
}
}
// Start the new component if necessary
if (manager != null) {
manager.setContext(this);
}
if (getState().isAvailable() && manager instanceof Lifecycle) {
try {
((Lifecycle) manager).start();
} catch (LifecycleException e) {
log.error("StandardContext.setManager: start: ", e);
}
}
} finally {
writeLock.unlock();
}
// Report this property change to interested listeners
support.firePropertyChange("manager", oldManager, manager);
}
use of org.apache.catalina.Lifecycle in project tomcat by apache.
the class StandardPipeline method setBasic.
/**
* <p>Set the Valve instance that has been distinguished as the basic
* Valve for this Pipeline (if any). Prior to setting the basic Valve,
* the Valve's <code>setContainer()</code> will be called, if it
* implements <code>Contained</code>, with the owning Container as an
* argument. The method may throw an <code>IllegalArgumentException</code>
* if this Valve chooses not to be associated with this Container, or
* <code>IllegalStateException</code> if it is already associated with
* a different Container.</p>
*
* @param valve Valve to be distinguished as the basic Valve
*/
@Override
public void setBasic(Valve valve) {
// Change components if necessary
Valve oldBasic = this.basic;
if (oldBasic == valve)
return;
// Stop the old component if necessary
if (oldBasic != null) {
if (getState().isAvailable() && (oldBasic instanceof Lifecycle)) {
try {
((Lifecycle) oldBasic).stop();
} catch (LifecycleException e) {
log.error("StandardPipeline.setBasic: stop", e);
}
}
if (oldBasic instanceof Contained) {
try {
((Contained) oldBasic).setContainer(null);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
}
}
// Start the new component if necessary
if (valve == null)
return;
if (valve instanceof Contained) {
((Contained) valve).setContainer(this.container);
}
if (getState().isAvailable() && valve instanceof Lifecycle) {
try {
((Lifecycle) valve).start();
} catch (LifecycleException e) {
log.error("StandardPipeline.setBasic: start", e);
return;
}
}
// Update the pipeline
Valve current = first;
while (current != null) {
if (current.getNext() == oldBasic) {
current.setNext(valve);
break;
}
current = current.getNext();
}
this.basic = valve;
}
use of org.apache.catalina.Lifecycle in project tomcat by apache.
the class StandardPipeline method startInternal.
/**
* Start {@link Valve}s) in this pipeline 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
protected synchronized void startInternal() throws LifecycleException {
// Start the Valves in our pipeline (including the basic), if any
Valve current = first;
if (current == null) {
current = basic;
}
while (current != null) {
if (current instanceof Lifecycle)
((Lifecycle) current).start();
current = current.getNext();
}
setState(LifecycleState.STARTING);
}
use of org.apache.catalina.Lifecycle in project tomcat by apache.
the class StandardHostSF method storeChildren.
/**
* Store the specified Host properties and children
* (Listener,Alias,Realm,Valve,Cluster, Context)
*
* @param aWriter
* PrintWriter to which we are storing
* @param indent
* Number of spaces to indent this element
* @param aHost
* Host whose properties are being stored
*
* @exception Exception
* if an exception occurs while storing
*/
@Override
public void storeChildren(PrintWriter aWriter, int indent, Object aHost, StoreDescription parentDesc) throws Exception {
if (aHost instanceof StandardHost) {
StandardHost host = (StandardHost) aHost;
// Store nested <Listener> elements
LifecycleListener[] listeners = ((Lifecycle) host).findLifecycleListeners();
storeElementArray(aWriter, indent, listeners);
// Store nested <Alias> elements
String[] aliases = host.findAliases();
getStoreAppender().printTagArray(aWriter, "Alias", indent + 2, aliases);
// Store nested <Realm> element
Realm realm = host.getRealm();
if (realm != null) {
Realm parentRealm = null;
if (host.getParent() != null) {
parentRealm = host.getParent().getRealm();
}
if (realm != parentRealm) {
storeElement(aWriter, indent, realm);
}
}
// Store nested <Valve> elements
Valve[] valves = host.getPipeline().getValves();
if (valves != null && valves.length > 0) {
List<Valve> hostValves = new ArrayList<>();
for (int i = 0; i < valves.length; i++) {
if (!(valves[i] instanceof ClusterValve))
hostValves.add(valves[i]);
}
storeElementArray(aWriter, indent, hostValves.toArray());
}
// store all <Cluster> elements
Cluster cluster = host.getCluster();
if (cluster != null) {
Cluster parentCluster = null;
if (host.getParent() != null) {
parentCluster = host.getParent().getCluster();
}
if (cluster != parentCluster) {
storeElement(aWriter, indent, cluster);
}
}
// store all <Context> elements
Container[] children = host.findChildren();
storeElementArray(aWriter, indent, children);
}
}
Aggregations