📄 mbeanutils.java
字号:
/*
* $Header: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/mbeans/MBeanUtils.java,v 1.23 2004/01/26 20:19:11 remm Exp $
* $Revision: 1.23 $
* $Date: 2004/01/26 20:19:11 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 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.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Hashtable;
import javax.management.Attribute;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.modelmbean.ModelMBean;
import org.apache.catalina.Connector;
import org.apache.catalina.Contained;
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.DefaultContext;
import org.apache.catalina.Engine;
import org.apache.catalina.Group;
import org.apache.catalina.Host;
import org.apache.catalina.Loader;
import org.apache.catalina.Logger;
import org.apache.catalina.Manager;
import org.apache.catalina.Realm;
import org.apache.catalina.Role;
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.User;
import org.apache.catalina.UserDatabase;
import org.apache.catalina.Valve;
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.catalina.valves.ValveBase;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.modeler.ManagedBean;
import org.apache.commons.modeler.Registry;
/**
* Public utility methods in support of the server side MBeans implementation.
*
* @author Craig R. McClanahan
* @author Amy Roh
* @version $Revision: 1.23 $ $Date: 2004/01/26 20:19:11 $
*/
public class MBeanUtils {
private static Log log = LogFactory.getLog(MBeanUtils.class);
// ------------------------------------------------------- Static Variables
/**
* The set of exceptions to the normal rules used by
* <code>createManagedBean()</code>. The first element of each pair
* is a class name, and the second element is the managed bean name.
*/
private static String exceptions[][] = {
{ "org.apache.ajp.tomcat4.Ajp13Connector",
"Ajp13Connector" },
{ "org.apache.coyote.tomcat4.Ajp13Connector",
"CoyoteConnector" },
{ "org.apache.catalina.core.StandardDefaultContext",
"DefaultContext" },
{ "org.apache.catalina.users.JDBCGroup",
"Group" },
{ "org.apache.catalina.users.JDBCRole",
"Role" },
{ "org.apache.catalina.users.JDBCUser",
"User" },
{ "org.apache.catalina.users.MemoryGroup",
"Group" },
{ "org.apache.catalina.users.MemoryRole",
"Role" },
{ "org.apache.catalina.users.MemoryUser",
"User" },
};
/**
* The configuration information registry for our managed beans.
*/
private static Registry registry = createRegistry();
/**
* The <code>MBeanServer</code> for this application.
*/
private static MBeanServer mserver = createServer();
// --------------------------------------------------------- Static Methods
/**
* Translates a string into x-www-form-urlencoded format
*
* @param t string to be encoded
* @return encoded string
*/
private static final String encodeStr(String t) {
return URLEncoder.encode(t);
}
/**
* Create and return the name of the <code>ManagedBean</code> that
* corresponds to this Catalina component.
*
* @param component The component for which to create a name
*/
static String createManagedName(Object component) {
// Deal with exceptions to the standard rule
String className = component.getClass().getName();
for (int i = 0; i < exceptions.length; i++) {
if (className.equals(exceptions[i][0])) {
return (exceptions[i][1]);
}
}
// Perform the standard transformation
int period = className.lastIndexOf('.');
if (period >= 0)
className = className.substring(period + 1);
return (className);
}
/**
* Create, register, and return an MBean for this
* <code>Connector</code> object.
*
* @param connector The Connector to be managed
*
* @exception Exception if an MBean cannot be created or registered
*/
static ModelMBean createMBean(Connector connector)
throws Exception {
String mname = createManagedName(connector);
ManagedBean managed = registry.findManagedBean(mname);
if (managed == null) {
Exception e = new Exception("ManagedBean is not found with "+mname);
throw new MBeanException(e);
}
String domain = managed.getDomain();
if (domain == null)
domain = mserver.getDefaultDomain();
ModelMBean mbean = managed.createMBean(connector);
ObjectName oname = createObjectName(domain, connector);
if( mserver.isRegistered( oname )) {
mserver.unregisterMBean(oname);
}
mserver.registerMBean(mbean, oname);
return (mbean);
}
/**
* Create, register, and return an MBean for this
* <code>Context</code> object.
*
* @param context The Context to be managed
*
* @exception Exception if an MBean cannot be created or registered
*/
static ModelMBean createMBean(Context context)
throws Exception {
String mname = createManagedName(context);
ManagedBean managed = registry.findManagedBean(mname);
if (managed == null) {
Exception e = new Exception("ManagedBean is not found with "+mname);
throw new MBeanException(e);
}
String domain = managed.getDomain();
if (domain == null)
domain = mserver.getDefaultDomain();
ModelMBean mbean = managed.createMBean(context);
ObjectName oname = createObjectName(domain, context);
if( mserver.isRegistered(oname)) {
log.debug("Already registered " + oname);
mserver.unregisterMBean(oname);
}
mserver.registerMBean(mbean, oname);
return (mbean);
}
/**
* Create, register, and return an MBean for this
* <code>ContextEnvironment</code> object.
*
* @param environment The ContextEnvironment to be managed
*
* @exception Exception if an MBean cannot be created or registered
*/
static ModelMBean createMBean(ContextEnvironment environment)
throws Exception {
String mname = createManagedName(environment);
ManagedBean managed = registry.findManagedBean(mname);
if (managed == null) {
Exception e = new Exception("ManagedBean is not found with "+mname);
throw new MBeanException(e);
}
String domain = managed.getDomain();
if (domain == null)
domain = mserver.getDefaultDomain();
ModelMBean mbean = managed.createMBean(environment);
ObjectName oname = createObjectName(domain, environment);
if( mserver.isRegistered( oname )) {
mserver.unregisterMBean(oname);
}
mserver.registerMBean(mbean, oname);
return (mbean);
}
/**
* Create, register, and return an MBean for this
* <code>ContextResource</code> object.
*
* @param resource The ContextResource to be managed
*
* @exception Exception if an MBean cannot be created or registered
*/
static ModelMBean createMBean(ContextResource resource)
throws Exception {
String mname = createManagedName(resource);
ManagedBean managed = registry.findManagedBean(mname);
if (managed == null) {
Exception e = new Exception("ManagedBean is not found with "+mname);
throw new MBeanException(e);
}
String domain = managed.getDomain();
if (domain == null)
domain = mserver.getDefaultDomain();
ModelMBean mbean = managed.createMBean(resource);
ObjectName oname = createObjectName(domain, resource);
if( mserver.isRegistered( oname )) {
mserver.unregisterMBean(oname);
}
mserver.registerMBean(mbean, oname);
return (mbean);
}
/**
* Create, register, and return an MBean for this
* <code>ContextResourceLink</code> object.
*
* @param resourceLink The ContextResourceLink to be managed
*
* @exception Exception if an MBean cannot be created or registered
*/
static ModelMBean createMBean(ContextResourceLink resourceLink)
throws Exception {
String mname = createManagedName(resourceLink);
ManagedBean managed = registry.findManagedBean(mname);
if (managed == null) {
Exception e = new Exception("ManagedBean is not found with "+mname);
throw new MBeanException(e);
}
String domain = managed.getDomain();
if (domain == null)
domain = mserver.getDefaultDomain();
ModelMBean mbean = managed.createMBean(resourceLink);
ObjectName oname = createObjectName(domain, resourceLink);
if( mserver.isRegistered( oname )) {
mserver.unregisterMBean(oname);
}
mserver.registerMBean(mbean, oname);
return (mbean);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -