📄 containerbase.java
字号:
* @exception IllegalStateException if this Container does not support
* child Containers
*/
public void addChild(Container child) {
if (System.getSecurityManager() != null) {
PrivilegedAction dp =
new PrivilegedAddChild(child);
AccessController.doPrivileged(dp);
} else {
addChildInternal(child);
}
}
private void addChildInternal(Container child) {
if( log.isDebugEnabled() )
log.debug("Add child " + child + " " + this);
synchronized(children) {
if (children.get(child.getName()) != null)
throw new IllegalArgumentException("addChild: Child name '" +
child.getName() +
"' is not unique");
child.setParent(this); // May throw IAE
if (started && (child instanceof Lifecycle)) {
try {
((Lifecycle) child).start();
} catch (LifecycleException e) {
log.error("ContainerBase.addChild: start: ", e);
throw new IllegalStateException
("ContainerBase.addChild: start: " + e);
}
}
children.put(child.getName(), child);
fireContainerEvent(ADD_CHILD_EVENT, child);
}
}
/**
* Add a container event listener to this component.
*
* @param listener The listener to add
*/
public void addContainerListener(ContainerListener listener) {
synchronized (listeners) {
listeners.add(listener);
}
}
/**
* Add a property change listener to this component.
*
* @param listener The listener to add
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
/**
* Return the child Container, associated with this Container, with
* the specified name (if any); otherwise, return <code>null</code>
*
* @param name Name of the child Container to be retrieved
*/
public Container findChild(String name) {
if (name == null)
return (null);
synchronized (children) { // Required by post-start changes
return ((Container) children.get(name));
}
}
/**
* Return the set of children Containers associated with this Container.
* If this Container has no children, a zero-length array is returned.
*/
public Container[] findChildren() {
synchronized (children) {
Container results[] = new Container[children.size()];
return ((Container[]) children.values().toArray(results));
}
}
/**
* Return the set of container listeners associated with this Container.
* If this Container has no registered container listeners, a zero-length
* array is returned.
*/
public ContainerListener[] findContainerListeners() {
synchronized (listeners) {
ContainerListener[] results =
new ContainerListener[listeners.size()];
return ((ContainerListener[]) listeners.toArray(results));
}
}
/**
* Process the specified Request, to produce the corresponding Response,
* by invoking the first Valve in our pipeline (if any), or the basic
* Valve otherwise.
*
* @param request Request to be processed
* @param response Response to be produced
*
* @exception IllegalStateException if neither a pipeline or a basic
* Valve have been configured for this Container
* @exception IOException if an input/output error occurred while
* processing
* @exception ServletException if a ServletException was thrown
* while processing this request
*/
public final void invoke(Request request, Response response)
throws IOException, ServletException {
pipeline.invoke(request, response);
}
/**
* Remove an existing child Container from association with this parent
* Container.
*
* @param child Existing child Container to be removed
*/
public void removeChild(Container child) {
synchronized(children) {
if (children.get(child.getName()) == null)
return;
children.remove(child.getName());
}
if (started && (child instanceof Lifecycle)) {
try {
if( child instanceof ContainerBase ) {
if( ((ContainerBase)child).started ) {
((Lifecycle) child).stop();
}
} else {
((Lifecycle) child).stop();
}
} catch (LifecycleException e) {
log.error("ContainerBase.removeChild: stop: ", e);
}
}
fireContainerEvent(REMOVE_CHILD_EVENT, child);
// child.setParent(null);
}
/**
* Remove a container event listener from this component.
*
* @param listener The listener to remove
*/
public void removeContainerListener(ContainerListener listener) {
synchronized (listeners) {
listeners.remove(listener);
}
}
/**
* Remove a property change listener from this component.
*
* @param listener The listener to remove
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
support.removePropertyChangeListener(listener);
}
// ------------------------------------------------------ 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.
*
* @exception LifecycleException if this component detects a fatal error
* that prevents it from being started
*/
public synchronized void start() throws LifecycleException {
// Validate and update our current component state
if (started) {
log.info(sm.getString("containerBase.alreadyStarted", logName()));
return;
}
if( logger instanceof LoggerBase ) {
LoggerBase lb=(LoggerBase)logger;
if( lb.getObjectName()==null ) {
ObjectName lname=lb.createObjectName();
try {
Registry.getRegistry(null, null)
.registerComponent(lb, lname, null);
} catch( Exception ex ) {
log.error( "Can't register logger " + lname, ex);
}
}
}
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
started = true;
// Start our subordinate components, if any
if ((loader != null) && (loader instanceof Lifecycle))
((Lifecycle) loader).start();
if ((logger != null) && (logger instanceof Lifecycle))
((Lifecycle) logger).start();
if ((manager != null) && (manager instanceof Lifecycle))
((Lifecycle) manager).start();
if ((cluster != null) && (cluster instanceof Lifecycle))
((Lifecycle) cluster).start();
if ((realm != null) && (realm instanceof Lifecycle))
((Lifecycle) realm).start();
if ((resources != null) && (resources instanceof Lifecycle))
((Lifecycle) resources).start();
// Start our child containers, if any
Container children[] = findChildren();
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof Lifecycle)
((Lifecycle) children[i]).start();
}
// Start the Valves in our pipeline (including the basic), if any
if (pipeline instanceof Lifecycle)
((Lifecycle) pipeline).start();
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(START_EVENT, null);
// 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.
*
* @exception LifecycleException if this component detects a fatal error
* that needs to be reported
*/
public synchronized void stop() throws LifecycleException {
// Validate and update our current component state
if (!started) {
log.info(sm.getString("containerBase.notStarted", logName()));
return;
}
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);
// Stop our thread
threadStop();
// 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) {
((Lifecycle) pipeline).stop();
}
// Stop our child containers, if any
Container children[] = findChildren();
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof Lifecycle)
((Lifecycle) children[i]).stop();
}
// Remove children - so next start can work
children = findChildren();
for (int i = 0; i < children.length; i++) {
removeChild(children[i]);
}
// Stop our subordinate components, if any
if ((resources != null) && (resources instanceof Lifecycle)) {
((Lifecycle) resources).stop();
}
if ((realm != null) && (realm instanceof Lifecycle)) {
((Lifecycle) realm).stop();
}
if ((cluster != null) && (cluster instanceof Lifecycle)) {
((Lifecycle) cluster).stop();
}
if ((manager != null) && (manager instanceof Lifecycle)) {
((Lifecycle) manager).stop();
}
if ((logger != null) && (logger instanceof Lifecycle)) {
((Lifecycle) logger).stop();
}
if ((loader != null) && (loader instanceof Lifecycle)) {
((Lifecycle) loader).stop();
}
if( logger instanceof LoggerBase ) {
LoggerBase lb=(LoggerBase)logger;
if( lb.getObjectName()!=null ) {
try {
Registry.getRegistry(null, null)
.unregisterComponent(lb.getObjectName());
} catch( Exception ex ) {
log.error( "Can't unregister logger " + lb.getObjectName(), ex);
}
}
}
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
}
/** Init method, part of the MBean lifecycle.
* If the container was added via JMX, it'll register itself with the
* parent, using the ObjectName conventions to locate the parent.
*
* If the container was added directly and it doesn't have an ObjectName,
* it'll create a name and register itself with the JMX console. On destroy(),
* the object will unregister.
*
* @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"});
}
}
initialized=true;
}
public ObjectName getParentName() throws MalformedObjectNameException {
return null;
}
public void destroy() throws Exception {
if( started ) {
stop();
}
initialized=false;
// unregister this component
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -