📄 serverlifecyclelistener.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.mbeans;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.management.MBeanException;
import org.apache.catalina.Container;
import org.apache.catalina.ContainerEvent;
import org.apache.catalina.ContainerListener;
import org.apache.catalina.Context;
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.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.connector.Connector;
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.juli.logging.Log;
import org.apache.juli.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: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
*/
public class ServerLifecycleListener
implements ContainerListener, LifecycleListener, PropertyChangeListener {
private static Log log = LogFactory.getLog(ServerLifecycleListener.class);
// ------------------------------------------------------------- Properties
/**
* 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;
}
// ---------------------------------------------- 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) {
createMBeans();
}
// We are embedded.
if( lifecycle instanceof Service ) {
try {
MBeanFactory factory = new MBeanFactory();
createMBeans(factory);
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
/**
* Create the MBeans that correspond to every existing node of our tree.
*/
protected void createMBeans() {
try {
MBeanFactory factory = new MBeanFactory();
createMBeans(factory);
createMBeans(ServerFactory.getServer());
} catch (MBeanException t) {
Exception e = t.getTargetException();
if (e == null)
e = t;
log.error("createMBeans: MBeanException", e);
} catch (Throwable t) {
log.error("createMBeans: Throwable", t);
}
}
/**
* Create the MBeans for the specified Connector and its nested components.
*
* @param connector Connector for which to create MBeans
*
* @exception Exception if an exception is thrown during MBean creation
*/
protected void createMBeans(Connector connector) throws Exception {
// Create the MBean for the Connnector itself
// if (log.isDebugEnabled())
// log.debug("Creating MBean for Connector " + connector);
// MBeanUtils.createMBean(connector);
}
/**
* Create the MBeans for the specified Context and its nested components.
*
* @param context Context for which to create MBeans
*
* @exception Exception if an exception is thrown during MBean creation
*/
protected void createMBeans(Context context) throws Exception {
// Create the MBean for the Context itself
// if (log.isDebugEnabled())
// log.debug("Creating MBean for Context " + context);
// MBeanUtils.createMBean(context);
context.addContainerListener(this);
if (context instanceof StandardContext) {
((StandardContext) context).addPropertyChangeListener(this);
((StandardContext) context).addLifecycleListener(this);
}
// If the context is privileged, give a reference to it
// in a servlet context attribute
if (context.getPrivileged()) {
context.getServletContext().setAttribute
(Globals.MBEAN_REGISTRY_ATTR,
MBeanUtils.createRegistry());
context.getServletContext().setAttribute
(Globals.MBEAN_SERVER_ATTR,
MBeanUtils.createServer());
}
// Create the MBeans for the associated nested components
Loader cLoader = context.getLoader();
if (cLoader != null) {
if (log.isDebugEnabled())
log.debug("Creating MBean for Loader " + cLoader);
//MBeanUtils.createMBean(cLoader);
}
Manager cManager = context.getManager();
if (cManager != null) {
if (log.isDebugEnabled())
log.debug("Creating MBean for Manager " + cManager);
//MBeanUtils.createMBean(cManager);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -