metadatambeaninfoassembler.java
来自「spring framework 2.5.4源代码」· Java 代码 · 共 385 行 · 第 1/2 页
JAVA
385 行
/*
* 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.assembler;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import javax.management.Descriptor;
import javax.management.MBeanParameterInfo;
import javax.management.modelmbean.ModelMBeanNotificationInfo;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jmx.export.metadata.InvalidMetadataException;
import org.springframework.jmx.export.metadata.JmxAttributeSource;
import org.springframework.jmx.export.metadata.JmxMetadataUtils;
import org.springframework.jmx.export.metadata.ManagedAttribute;
import org.springframework.jmx.export.metadata.ManagedNotification;
import org.springframework.jmx.export.metadata.ManagedOperation;
import org.springframework.jmx.export.metadata.ManagedOperationParameter;
import org.springframework.jmx.export.metadata.ManagedResource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Implementation of the {@link org.springframework.jmx.export.assembler.MBeanInfoAssembler}
* interface that reads the management interface information from source level metadata.
*
* <p>Uses the {@link JmxAttributeSource} strategy interface, so that
* metadata can be read using any supported implementation. Out of the box,
* two strategies are included:
* <ul>
* <li><code>AttributesJmxAttributeSource</code>, for Commons Attributes
* <li><code>AnnotationJmxAttributeSource</code>, for JDK 1.5+ annotations
* </ul>
*
* @author Rob Harrop
* @author Juergen Hoeller
* @since 1.2
* @see #setAttributeSource
* @see org.springframework.jmx.export.metadata.AttributesJmxAttributeSource
* @see org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource
*/
public class MetadataMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssembler
implements AutodetectCapableMBeanInfoAssembler, InitializingBean {
private JmxAttributeSource attributeSource;
/**
* Create a new <code>MetadataMBeanInfoAssembler<code> which needs to be
* configured through the {@link #setAttributeSource} method.
*/
public MetadataMBeanInfoAssembler() {
}
/**
* Create a new <code>MetadataMBeanInfoAssembler<code> for the given
* <code>JmxAttributeSource</code>.
* @param attributeSource the JmxAttributeSource to use
*/
public MetadataMBeanInfoAssembler(JmxAttributeSource attributeSource) {
Assert.notNull(attributeSource, "JmxAttributeSource must not be null");
this.attributeSource = attributeSource;
}
/**
* Set the <code>JmxAttributeSource</code> implementation to use for
* reading the metadata from the bean class.
* @see org.springframework.jmx.export.metadata.AttributesJmxAttributeSource
* @see org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource
*/
public void setAttributeSource(JmxAttributeSource attributeSource) {
Assert.notNull(attributeSource, "JmxAttributeSource must not be null");
this.attributeSource = attributeSource;
}
public void afterPropertiesSet() {
if (this.attributeSource == null) {
throw new IllegalArgumentException("Property 'attributeSource' is required");
}
}
/**
* Throws an IllegalArgumentException if it encounters a JDK dynamic proxy.
* Metadata can only be read from target classes and CGLIB proxies!
*/
protected void checkManagedBean(Object managedBean) throws IllegalArgumentException {
if (AopUtils.isJdkDynamicProxy(managedBean)) {
throw new IllegalArgumentException(
"MetadataMBeanInfoAssembler does not support JDK dynamic proxies - " +
"export the target beans directly or use CGLIB proxies instead");
}
}
/**
* Used for autodetection of beans. Checks to see if the bean's class has a
* <code>ManagedResource</code> attribute. If so it will add it list of included beans.
* @param beanClass the class of the bean
* @param beanName the name of the bean in the bean factory
*/
public boolean includeBean(Class beanClass, String beanName) {
return (this.attributeSource.getManagedResource(getClassToExpose(beanClass)) != null);
}
/**
* Vote on the inclusion of an attribute accessor.
* @param method the accessor method
* @param beanKey the key associated with the MBean in the beans map
* @return whether the method has the appropriate metadata
*/
protected boolean includeReadAttribute(Method method, String beanKey) {
return hasManagedAttribute(method);
}
/**
* Votes on the inclusion of an attribute mutator.
* @param method the mutator method
* @param beanKey the key associated with the MBean in the beans map
* @return whether the method has the appropriate metadata
*/
protected boolean includeWriteAttribute(Method method, String beanKey) {
return hasManagedAttribute(method);
}
/**
* Votes on the inclusion of an operation.
* @param method the operation method
* @param beanKey the key associated with the MBean in the beans map
* @return whether the method has the appropriate metadata
*/
protected boolean includeOperation(Method method, String beanKey) {
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
if (pd != null) {
return hasManagedAttribute(method);
}
else {
return hasManagedOperation(method);
}
}
/**
* Checks to see if the given Method has the <code>ManagedAttribute</code> attribute.
*/
private boolean hasManagedAttribute(Method method) {
return (this.attributeSource.getManagedAttribute(method) != null);
}
/**
* Checks to see if the given Method has the <code>ManagedOperation</code> attribute.
* @param method the method to check
*/
private boolean hasManagedOperation(Method method) {
return (this.attributeSource.getManagedOperation(method) != null);
}
/**
* Reads managed resource description from the source level metadata.
* Returns an empty <code>String</code> if no description can be found.
*/
protected String getDescription(Object managedBean, String beanKey) {
ManagedResource mr = this.attributeSource.getManagedResource(getClassToExpose(managedBean));
return (mr != null ? mr.getDescription() : "");
}
/**
* Creates a description for the attribute corresponding to this property
* descriptor. Attempts to create the description using metadata from either
* the getter or setter attributes, otherwise uses the property name.
*/
protected String getAttributeDescription(PropertyDescriptor propertyDescriptor, String beanKey) {
Method readMethod = propertyDescriptor.getReadMethod();
Method writeMethod = propertyDescriptor.getWriteMethod();
ManagedAttribute getter =
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?