📄 mbeanexporter.java
字号:
/*
* Copyright 2002-2007 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.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.management.InstanceNotFoundException;
import javax.management.JMException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.modelmbean.ModelMBean;
import javax.management.modelmbean.ModelMBeanInfo;
import javax.management.modelmbean.RequiredModelMBean;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.beans.factory.BeanClassLoaderAware;
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.export.notification.ModelMBeanNotificationPublisher;
import org.springframework.jmx.export.notification.NotificationPublisherAware;
import org.springframework.jmx.support.JmxUtils;
import org.springframework.jmx.support.MBeanRegistrationSupport;
import org.springframework.jmx.support.ObjectNameManager;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
/**
* JMX exporter that allows for exposing any <i>Spring-managed bean</i>
* 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 MBeanExporter 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 MBeanExporter will create the management information using the
* supplied {@link MBeanInfoAssembler} implementation.
*
* <p>A list of {@link MBeanExporterListener MBeanExporterListeners}
* can be registered via the
* {@link #setListeners(MBeanExporterListener[]) listeners} property,
* allowing application code to be notified of MBean registration and
* unregistration events.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @author Rick Evans
* @since 1.2
* @see #setBeans
* @see #setAutodetect
* @see #setAssembler
* @see #setListeners
* @see org.springframework.jmx.export.assembler.MBeanInfoAssembler
* @see MBeanExporterListener
*/
public class MBeanExporter extends MBeanRegistrationSupport
implements MBeanExportOperations, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
/**
* Autodetection mode indicating that no autodetection should be used.
*/
public static final int AUTODETECT_NONE = 0;
/**
* Autodetection mode indicating that only valid MBeans should be autodetected.
*/
public static final int AUTODETECT_MBEAN = 1;
/**
* Autodetection mode indicating that only the {@link MBeanInfoAssembler} should be able
* to autodetect beans.
*/
public static final int AUTODETECT_ASSEMBLER = 2;
/**
* Autodetection mode indicating that all autodetection mechanisms should be used.
*/
public static final int AUTODETECT_ALL = AUTODETECT_MBEAN | AUTODETECT_ASSEMBLER;
/**
* Wildcard used to map a {@link javax.management.NotificationListener}
* to all MBeans registered by the <code>MBeanExporter</code>.
*/
private static final String WILDCARD = "*";
/** Constant for the JMX <code>mr_type</code> "ObjectReference" */
private static final String MR_TYPE_OBJECT_REFERENCE = "ObjectReference";
/** Prefix for the autodetect constants defined in this class */
private static final String CONSTANT_PREFIX_AUTODETECT = "AUTODETECT_";
/** Constants instance for this class */
private static final Constants constants = new Constants(MBeanExporter.class);
/** The beans to be exposed as JMX managed resources, with JMX names as keys */
private Map beans;
/** The autodetect mode to use for this MBeanExporter */
private int autodetectMode = AUTODETECT_NONE;
/** Indicates whether Spring should modify generated ObjectNames */
private boolean ensureUniqueRuntimeObjectNames = true;
/** Indicates whether Spring should expose the managed resource ClassLoader in the MBean */
private boolean exposeManagedResourceClassLoader = false;
/** A set of bean names that should be excluded from autodetection */
private Set excludedBeans;
/** The MBeanExporterListeners registered with this exporter. */
private MBeanExporterListener[] listeners;
/** The NotificationListeners to register for the MBeans registered by this exporter */
private NotificationListenerBean[] notificationListeners = new NotificationListenerBean[0];
/** Stores the MBeanInfoAssembler to use for this exporter */
private MBeanInfoAssembler assembler = new SimpleReflectiveMBeanInfoAssembler();
/** The strategy to use for creating ObjectNames for an object */
private ObjectNamingStrategy namingStrategy = new KeyNamingStrategy();
/** Stores the ClassLoader to use for generating lazy-init proxies */
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
/** Stores the BeanFactory for use in autodetection process */
private ListableBeanFactory beanFactory;
/**
* 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 JMX names as keys and 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 #isMBean
* @deprecated in favor of {@link #setAutodetectModeName(String)}
*/
public void setAutodetect(boolean autodetect) {
this.autodetectMode = (autodetect ? AUTODETECT_ALL : AUTODETECT_NONE);
}
/**
* Sets the autodetection mode to use.
* @exception IllegalArgumentException if the supplied value is not
* one of the <code>AUTODETECT_</code> constants
* @see #setAutodetectModeName(String)
* @see #AUTODETECT_ALL
* @see #AUTODETECT_ASSEMBLER
* @see #AUTODETECT_MBEAN
* @see #AUTODETECT_NONE
*/
public void setAutodetectMode(int autodetectMode) {
if (!constants.getValues(CONSTANT_PREFIX_AUTODETECT).contains(new Integer(autodetectMode))) {
throw new IllegalArgumentException("Only values of autodetect constants allowed");
}
this.autodetectMode = autodetectMode;
}
/**
* Sets the autodetection mode to use by name.
* @exception IllegalArgumentException if the supplied value is not resolvable
* to one of the <code>AUTODETECT_</code> constants or is <code>null</code>
* @see #setAutodetectMode(int)
* @see #AUTODETECT_ALL
* @see #AUTODETECT_ASSEMBLER
* @see #AUTODETECT_MBEAN
* @see #AUTODETECT_NONE
*/
public void setAutodetectModeName(String constantName) {
if (constantName == null || !constantName.startsWith(CONSTANT_PREFIX_AUTODETECT)) {
throw new IllegalArgumentException("Only autodetect constants allowed");
}
this.autodetectMode = constants.asNumber(constantName).intValue();
}
/**
* 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 participate 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 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);
}
/**
* Indicates whether Spring should ensure that {@link ObjectName ObjectNames} generated by
* the configured {@link ObjectNamingStrategy} for runtime-registered MBeans should be modified
* to ensure uniqueness for every instance of managed <code>Class</code>. Default value is
* <code>true</code>.
* @see JmxUtils#appendIdentityToObjectName(javax.management.ObjectName, Object)
*/
public void setEnsureUniqueRuntimeObjectNames(boolean ensureUniqueRuntimeObjectNames) {
this.ensureUniqueRuntimeObjectNames = ensureUniqueRuntimeObjectNames;
}
/**
* Indicates whether or not the managed resource should be exposed as the
* {@link Thread#getContextClassLoader() thread context ClassLoader} before allowing
* any invocations on the MBean to occur. Default value is <code>false</code>.
*/
public void setExposeManagedResourceClassLoader(boolean exposeManagedResourceClassLoader) {
this.exposeManagedResourceClassLoader = exposeManagedResourceClassLoader;
}
/**
* Set the {@link NotificationListenerBean NotificationListenerBeans} containing the
* {@link javax.management.NotificationListener NotificationListeners} that will be registered
* with the {@link MBeanServer}.
* @see #setNotificationListenerMappings(java.util.Map)
* @see NotificationListenerBean
*/
public void setNotificationListeners(NotificationListenerBean[] notificationListeners) {
this.notificationListeners = notificationListeners;
}
/**
* Set the {@link NotificationListener NotificationListeners} to register with the
* {@link javax.management.MBeanServer}. The key of each entry in the <code>Map</code> is
* a String representation of the {@link javax.management.ObjectName} of the MBean the
* listener should be registered for. Specifying an asterisk (<code>*</code>) will cause
* the listener to be associated with all MBeans registered by this class at startup time.
* <p>The value of each entry is the {@link javax.management.NotificationListener} to register.
* For more advanced options such as registering
* {@link javax.management.NotificationFilter NotificationFilters} and
* handback objects see {@link #setNotificationListeners(NotificationListenerBean[])}.
*/
public void setNotificationListenerMappings(Map listeners) {
Assert.notNull(listeners, "Property 'notificationListenerMappings' must not be null");
List notificationListeners = new ArrayList(listeners.size());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -