📄 serverlifecyclelistener.java
字号:
/*
* $Header: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/mbeans/ServerLifecycleListener.java,v 1.12 2003/12/09 00:51:47 amyroh Exp $
* $Revision: 1.12 $
* $Date: 2003/12/09 00:51:47 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.catalina.mbeans;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.StringTokenizer;
import javax.management.MBeanException;
import org.apache.catalina.Connector;
import org.apache.catalina.Container;
import org.apache.catalina.ContainerEvent;
import org.apache.catalina.ContainerListener;
import org.apache.catalina.Context;
import org.apache.catalina.DefaultContext;
import org.apache.catalina.Engine;
import org.apache.catalina.Globals;
import org.apache.catalina.Host;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Loader;
import org.apache.catalina.Logger;
import org.apache.catalina.Manager;
import org.apache.catalina.Realm;
import org.apache.catalina.Server;
import org.apache.catalina.ServerFactory;
import org.apache.catalina.Service;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.core.StandardService;
import org.apache.catalina.deploy.ContextEnvironment;
import org.apache.catalina.deploy.ContextResource;
import org.apache.catalina.deploy.ContextResourceLink;
import org.apache.catalina.deploy.NamingResources;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Implementation of <code>LifecycleListener</code> that
* instantiates the set of MBeans associated with the components of a
* running instance of Catalina.
*
* @author Craig R. McClanahan
* @author Amy Roh
* @version $Revision: 1.12 $ $Date: 2003/12/09 00:51:47 $
*/
public class ServerLifecycleListener
implements ContainerListener, LifecycleListener, PropertyChangeListener {
private static Log log = LogFactory.getLog(ServerLifecycleListener.class);
// ------------------------------------------------------------- Properties
/**
* The debugging detail level for this component.
*/
protected int debug = 0;
public int getDebug() {
return (this.debug);
}
public void setDebug(int debug) {
this.debug = debug;
}
/**
* Semicolon separated list of paths containing MBean desciptor resources.
*/
protected String descriptors = null;
public String getDescriptors() {
return (this.descriptors);
}
public void setDescriptors(String descriptors) {
this.descriptors = descriptors;
}
/**
* MX4J adaptor name.
*/
protected String adaptor = null;
public String getAdaptor() {
return (this.adaptor);
}
public void setAdaptor(String adaptor) {
this.adaptor = adaptor;
}
/**
* MX4J jrmp/iiop listen host
*/
protected String adaptorHost = null;
public String getAdaptorHost() {
return (this.adaptorHost);
}
public void setAdaptorHost(String adaptorHost) {
this.adaptorHost = adaptorHost;
}
/**
* MX4J jrmp/iiop listen port
*/
protected int adaptorPort = -1;
public int getAdaptorPort() {
return (this.adaptorPort);
}
public void setAdaptorPort(int adaptorPort) {
this.adaptorPort = adaptorPort;
}
// ---------------------------------------------- ContainerListener Methods
/**
* Handle a <code>ContainerEvent</code> from one of the Containers we are
* interested in.
*
* @param event The event that has occurred
*/
public void containerEvent(ContainerEvent event) {
try {
String type = event.getType();
if (Container.ADD_CHILD_EVENT.equals(type)) {
processContainerAddChild(event.getContainer(),
(Container) event.getData());
} else if (Container.REMOVE_CHILD_EVENT.equals(type)) {
processContainerRemoveChild(event.getContainer(),
(Container) event.getData());
}
} catch (Exception e) {
log.error("Exception processing event " + event, e);
}
}
// ---------------------------------------------- LifecycleListener Methods
/**
* Primary entry point for startup and shutdown events.
*
* @param event The event that has occurred
*/
public void lifecycleEvent(LifecycleEvent event) {
Lifecycle lifecycle = event.getLifecycle();
if (Lifecycle.START_EVENT.equals(event.getType())) {
if (lifecycle instanceof Server) {
// Loading additional MBean descriptors
loadMBeanDescriptors();
createMBeans();
if (adaptor != null) {
try {
MBeanUtils.createRMIAdaptor(adaptor, adaptorHost, adaptorPort);
} catch (Exception e) {
log.error("createAdaptor: Exception", e);
}
}
}
// We are embedded.
if( lifecycle instanceof Service ) {
try {
MBeanFactory factory = new MBeanFactory();
createMBeans(factory);
loadMBeanDescriptors();
createMBeans((Service)lifecycle);
} catch( Exception ex ) {
log.error("Create mbean factory");
}
}
/*
// Ignore events from StandardContext objects to avoid
// reregistering the context
if (lifecycle instanceof StandardContext)
return;
createMBeans();
*/
} else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
try {
if (lifecycle instanceof Server) {
destroyMBeans((Server)lifecycle);
}
if (lifecycle instanceof Service) {
destroyMBeans((Service)lifecycle);
}
} catch (MBeanException t) {
Exception e = t.getTargetException();
if (e == null) {
e = t;
}
log.error("destroyMBeans: MBeanException", e);
} catch (Throwable t) {
log.error("destroyMBeans: Throwable", t);
}
// FIXME: RMI adaptor should be stopped; however, this is
// undocumented in MX4J, and reports exist in the MX4J bug DB that
// this doesn't work
}
if ((Context.RELOAD_EVENT.equals(event.getType()))
|| (Lifecycle.START_EVENT.equals(event.getType()))) {
// Give context a new handle to the MBean server if the
// context has been reloaded since reloading causes the
// context to lose its previous handle to the server
if (lifecycle instanceof StandardContext) {
// If the context is privileged, give a reference to it
// in a servlet context attribute
StandardContext context = (StandardContext)lifecycle;
if (context.getPrivileged()) {
context.getServletContext().setAttribute
(Globals.MBEAN_REGISTRY_ATTR,
MBeanUtils.createRegistry());
context.getServletContext().setAttribute
(Globals.MBEAN_SERVER_ATTR,
MBeanUtils.createServer());
}
}
}
}
// ----------------------------------------- PropertyChangeListener Methods
/**
* Handle a <code>PropertyChangeEvent</code> from one of the Containers
* we are interested in.
*
* @param event The event that has occurred
*/
public void propertyChange(PropertyChangeEvent event) {
if (event.getSource() instanceof Container) {
try {
processContainerPropertyChange((Container) event.getSource(),
event.getPropertyName(),
event.getOldValue(),
event.getNewValue());
} catch (Exception e) {
log.error("Exception handling Container property change", e);
}
} else if (event.getSource() instanceof DefaultContext) {
try {
processDefaultContextPropertyChange
((DefaultContext) event.getSource(),
event.getPropertyName(),
event.getOldValue(),
event.getNewValue());
} catch (Exception e) {
log.error("Exception handling DefaultContext property change", e);
}
} else if (event.getSource() instanceof NamingResources) {
try {
processNamingResourcesPropertyChange
((NamingResources) event.getSource(),
event.getPropertyName(),
event.getOldValue(),
event.getNewValue());
} catch (Exception e) {
log.error("Exception handling NamingResources property change", e);
}
} else if (event.getSource() instanceof Server) {
try {
processServerPropertyChange((Server) event.getSource(),
event.getPropertyName(),
event.getOldValue(),
event.getNewValue());
} catch (Exception e) {
log.error("Exception handing Server property change", e);
}
} else if (event.getSource() instanceof Service) {
try {
processServicePropertyChange((Service) event.getSource(),
event.getPropertyName(),
event.getOldValue(),
event.getNewValue());
} catch (Exception e) {
log.error("Exception handing Service property change", e);
}
}
}
// ------------------------------------------------------ Protected Methods
/**
* Load additional MBean descriptor resources.
*/
protected void loadMBeanDescriptors() {
if (descriptors != null) {
StringTokenizer tokenizer = new StringTokenizer(descriptors, ";");
while (tokenizer.hasMoreTokens()) {
String resource = tokenizer.nextToken();
MBeanUtils.loadMBeanDescriptors(resource);
}
}
}
/**
* Create the MBeans that correspond to every existing node of our tree.
*/
protected void createMBeans() {
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -