📄 mbeanexporter.java
字号:
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed 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.springframework.jmx.export;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.JMException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.modelmbean.InvalidTargetObjectTypeException;
import javax.management.modelmbean.ModelMBean;
import javax.management.modelmbean.ModelMBeanInfo;
import javax.management.modelmbean.RequiredModelMBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Constants;
import org.springframework.jmx.export.assembler.AutodetectCapableMBeanInfoAssembler;
import org.springframework.jmx.export.assembler.MBeanInfoAssembler;
import org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler;
import org.springframework.jmx.export.naming.KeyNamingStrategy;
import org.springframework.jmx.export.naming.ObjectNamingStrategy;
import org.springframework.jmx.export.naming.SelfNaming;
import org.springframework.jmx.support.JmxUtils;
import org.springframework.util.ObjectUtils;
/**
* A bean that allows for any Spring-managed bean to be exposed to a JMX
* <code>MBeanServer</code>, without the need to define any JMX-specific
* information in the bean classes.
*
* <p>If the bean implements one of the JMX management interfaces then
* <code>MBeanExporter</code> can simply register the MBean with the server
* automatically, through its autodetection process.
*
* <p>If the bean does not implement one of the JMX management interfaces then
* <code>MBeanExporter</code> will create the management information using the
* supplied <code>MBeanInfoAssembler</code> implementation.
*
* <p>A list of <code>MBeanExporterListener</code>s can be registered via the
* <code>listeners</code> property, allowing application code to be notified
* of MBean registration and unregistration events.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @since 1.2
* @see #setBeans
* @see #setAutodetect
* @see #setAssembler
* @see #setListeners
* @see org.springframework.jmx.export.assembler.MBeanInfoAssembler
* @see MBeanExporterListener
*/
public class MBeanExporter implements BeanFactoryAware, InitializingBean, DisposableBean {
/**
* Constant indicating that registration should fail when
* attempting to register an MBean under a name that already exists.
* <p>This is the default registration behavior.
*/
public static final int REGISTRATION_FAIL_ON_EXISTING = 0;
/**
* Constant indicating that registration should ignore the affected MBean
* when attempting to register an MBean under a name that already exists.
*/
public static final int REGISTRATION_IGNORE_EXISTING = 1;
/**
* Constant indicating that registration should replace the affected MBean
* when attempting to register an MBean under a name that already exists.
*/
public static final int REGISTRATION_REPLACE_EXISTING = 2;
/**
* Constant for the JMX <code>mr_type</code> "ObjectReference".
*/
private static final String MR_TYPE_OBJECT_REFERENCE = "ObjectReference";
/**
* Constants for this class.
*/
private static final Constants constants = new Constants(MBeanExporter.class);
/**
* <code>Log</code> instance for this class.
*/
protected final Log logger = LogFactory.getLog(getClass());
/**
* The <code>MBeanServer</code> instance being used to register beans.
*/
private MBeanServer server;
/**
* The beans to be exposed as JMX managed resources.
*/
private Map beans;
/**
* Whether to autodetect MBeans in the bean factory.
*/
private boolean autodetect = false;
/**
* A list of bean names that should be excluded from autodetection.
*/
private Set excludedBeans;
/**
* Stores the <code>MBeanInfoAssembler</code> to use for this exporter.
*/
private MBeanInfoAssembler assembler = new SimpleReflectiveMBeanInfoAssembler();
/**
* The strategy to use for creating <code>ObjectName</code>s for an object.
*/
private ObjectNamingStrategy namingStrategy = new KeyNamingStrategy();
/**
* Stores the <code>BeanFactory</code> for use in autodetection process.
*/
private ListableBeanFactory beanFactory;
/**
* The beans that have been registered by this exporter.
*/
private Set registeredBeans;
/**
* The <code>MBeanExporterListeners</code> registered with this exporter.
*/
private MBeanExporterListener[] listeners;
/**
* The action take when registering an MBean and finding that it already exists.
* By default an exception is raised.
*/
private int registrationBehavior = REGISTRATION_FAIL_ON_EXISTING;
/**
* Specify the instance <code>MBeanServer</code> with which all beans should
* be registered. The <code>MBeanExporter</code> will attempt to locate an
* existing <code>MBeanServer</code> if none is supplied.
*/
public void setServer(MBeanServer server) {
this.server = server;
}
/**
* Supply a <code>Map</code> of beans to be registered with the JMX
* <code>MBeanServer</code>.
* <p>The String keys are the basis for the creation of JMX object names.
* By default, a JMX <code>ObjectName</code> will be created straight
* from the given key. This can be customized through specifying a
* custom <code>NamingStrategy</code>.
* <p>Both bean instances and bean names are allowed as values.
* Bean instances are typically linked in through bean references.
* Bean names will be resolved as beans in the current factory, respecting
* lazy-init markers (that is, not triggering initialization of such beans).
* @param beans Map with bean instances or bean names as values
* @see #setNamingStrategy
* @see org.springframework.jmx.export.naming.KeyNamingStrategy
* @see javax.management.ObjectName#ObjectName(String)
*/
public void setBeans(Map beans) {
this.beans = beans;
}
/**
* Set whether to autodetect MBeans in the bean factory that this exporter
* runs in. Will also ask an <code>AutodetectCapableMBeanInfoAssembler</code>
* if available.
* <p>This feature is turned off by default. Explicitly specify "true" here
* to enable autodetection.
* @see #setAssembler
* @see AutodetectCapableMBeanInfoAssembler
* @see org.springframework.jmx.support.JmxUtils#isMBean
*/
public void setAutodetect(boolean autodetect) {
this.autodetect = autodetect;
}
/**
* Set the list of names for beans that should be excluded from autodetection.
*/
public void setExcludedBeans(String[] excludedBeans) {
this.excludedBeans = (excludedBeans != null ? new HashSet(Arrays.asList(excludedBeans)) : null);
}
/**
* Set the implementation of the <code>MBeanInfoAssembler</code> interface to use
* for this exporter. Default is a <code>SimpleReflectiveMBeanInfoAssembler</code>.
* <p>The passed-in assembler can optionally implement the
* <code>AutodetectCapableMBeanInfoAssembler</code> interface, which enables it
* to particiapte in the exporter's MBean autodetection process.
* @see org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler
* @see org.springframework.jmx.export.assembler.AutodetectCapableMBeanInfoAssembler
* @see org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler
* @see #setAutodetect
*/
public void setAssembler(MBeanInfoAssembler assembler) {
this.assembler = assembler;
}
/**
* Set the implementation of the <code>ObjectNamingStrategy</code> interface
* to use for this exporter. Default is a <code>KeyNamingStrategy</code>.
* @see org.springframework.jmx.export.naming.KeyNamingStrategy
* @see org.springframework.jmx.export.naming.MetadataNamingStrategy
*/
public void setNamingStrategy(ObjectNamingStrategy namingStrategy) {
this.namingStrategy = namingStrategy;
}
/**
* Set the <code>MBeanExporterListener</code>s that should be notified
* of MBean registration and unregistration events.
* @see MBeanExporterListener
*/
public void setListeners(MBeanExporterListener[] listeners) {
this.listeners = listeners;
}
/**
* Set the registration behavior by the name of the corresponding constant,
* e.g. "REGISTRATION_IGNORE_EXISTING".
* @see #setRegistrationBehavior
* @see #REGISTRATION_FAIL_ON_EXISTING
* @see #REGISTRATION_IGNORE_EXISTING
* @see #REGISTRATION_REPLACE_EXISTING
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -