📄 abstractreflectivembeaninfoassembler.java
字号:
* @see #populateOperationDescriptor
*/
protected ModelMBeanOperationInfo[] getOperationInfo(Object managedBean, String beanKey) {
Method[] methods = getClassToExpose(managedBean).getMethods();
List infos = new ArrayList();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
ModelMBeanOperationInfo info = null;
if (method.getDeclaringClass() == Object.class) {
continue;
}
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
if (pd != null) {
if ((method.equals(pd.getReadMethod()) && includeReadAttribute(method, beanKey)) ||
(method.equals(pd.getWriteMethod()) && includeWriteAttribute(method, beanKey))) {
// Attributes need to have their methods exposed as
// operations to the JMX server as well.
info = createModelMBeanOperationInfo(method, pd.getName(), beanKey);
Descriptor desc = info.getDescriptor();
if (method.equals(pd.getReadMethod())) {
desc.setField(FIELD_ROLE, ROLE_GETTER);
}
else {
desc.setField(FIELD_ROLE, ROLE_SETTER);
}
desc.setField(FIELD_VISIBILITY, ATTRIBUTE_OPERATION_VISIBILITY);
if (isExposeClassDescriptor()) {
desc.setField(FIELD_CLASS, getClassForDescriptor(managedBean).getName());
}
info.setDescriptor(desc);
}
}
else if (includeOperation(method, beanKey)) {
info = createModelMBeanOperationInfo(method, method.getName(), beanKey);
Descriptor desc = info.getDescriptor();
desc.setField(FIELD_ROLE, ROLE_OPERATION);
if (isExposeClassDescriptor()) {
desc.setField(FIELD_CLASS, getClassForDescriptor(managedBean).getName());
}
populateOperationDescriptor(desc, method, beanKey);
info.setDescriptor(desc);
}
if (info != null) {
infos.add(info);
}
}
return (ModelMBeanOperationInfo[]) infos.toArray(new ModelMBeanOperationInfo[infos.size()]);
}
/**
* Creates an instance of <code>ModelMBeanOperationInfo</code> for the
* given method. Populates the parameter info for the operation.
* @param method the <code>Method</code> to create a <code>ModelMBeanOperationInfo</code> for
* @param name the name for the operation info
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @return the <code>ModelMBeanOperationInfo</code>
*/
protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) {
MBeanParameterInfo[] params = getOperationParameters(method, beanKey);
if (params.length == 0) {
return new ModelMBeanOperationInfo(getOperationDescription(method, beanKey), method);
}
else {
return new ModelMBeanOperationInfo(name,
getOperationDescription(method, beanKey),
getOperationParameters(method, beanKey),
method.getReturnType().getName(),
MBeanOperationInfo.UNKNOWN);
}
}
/**
* Return the class to be used for the JMX descriptor field "class".
* Only applied when the "exposeClassDescriptor" property is "true".
* <p>Default implementation returns the first implemented interface
* for a JDK proxy, and the target class else.
* @param managedBean the bean instance (might be an AOP proxy)
* @return the class to expose in the descriptor field "class"
* @see #setExposeClassDescriptor
* @see #getClassToExpose(Class)
* @see org.springframework.aop.framework.AopProxyUtils#proxiedUserInterfaces(Object)
*/
protected Class getClassForDescriptor(Object managedBean) {
if (AopUtils.isJdkDynamicProxy(managedBean)) {
return AopProxyUtils.proxiedUserInterfaces(managedBean)[0];
}
return getClassToExpose(managedBean);
}
/**
* Allows subclasses to vote on the inclusion of a particular attribute accessor.
* @param method the accessor <code>Method</code>
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @return <code>true</code> if the accessor should be included in the management interface,
* otherwise <code>false<code>
*/
protected abstract boolean includeReadAttribute(Method method, String beanKey);
/**
* Allows subclasses to vote on the inclusion of a particular attribute mutator.
* @param method the mutator <code>Method</code>.
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @return <code>true</code> if the mutator should be included in the management interface,
* otherwise <code>false<code>
*/
protected abstract boolean includeWriteAttribute(Method method, String beanKey);
/**
* Allows subclasses to vote on the inclusion of a particular operation.
* @param method the operation method
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @return whether the operation should be included in the management interface
*/
protected abstract boolean includeOperation(Method method, String beanKey);
/**
* Get the description for a particular attribute.
* <p>Default implementation returns a description for the operation
* that is the name of corresponding <code>Method</code>.
* @param propertyDescriptor the PropertyDescriptor for the attribute
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @return the description for the attribute
*/
protected String getAttributeDescription(PropertyDescriptor propertyDescriptor, String beanKey) {
return propertyDescriptor.getDisplayName();
}
/**
* Get the description for a particular operation.
* <p>Default implementation returns a description for the operation
* that is the name of corresponding <code>Method</code>.
* @param method the operation method
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @return the description for the operation
*/
protected String getOperationDescription(Method method, String beanKey) {
return method.getName();
}
/**
* Create parameter info for the given method. Default implementation
* returns an empty arry of <code>MBeanParameterInfo</code>.
* @param method the <code>Method</code> to get the parameter information for
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @return the <code>MBeanParameterInfo</code> array
*/
protected MBeanParameterInfo[] getOperationParameters(Method method, String beanKey) {
return new MBeanParameterInfo[0];
}
/**
* Allows subclasses to add extra fields to the <code>Descriptor</code> for an
* MBean. Default implementation sets the <code>currencyTimeLimit</code> field to
* the specified "defaultCurrencyTimeLimit", if any (by default none).
* @param descriptor the <code>Descriptor</code> for the MBean resource.
* @param managedBean the bean instance (might be an AOP proxy)
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @see #setDefaultCurrencyTimeLimit(Integer)
* @see #applyDefaultCurrencyTimeLimit(javax.management.Descriptor)
*/
protected void populateMBeanDescriptor(Descriptor descriptor, Object managedBean, String beanKey) {
applyDefaultCurrencyTimeLimit(descriptor);
}
/**
* Allows subclasses to add extra fields to the <code>Descriptor</code> for a particular
* attribute. Default implementation sets the <code>currencyTimeLimit</code> field to
* the specified "defaultCurrencyTimeLimit", if any (by default none).
* @param desc the attribute descriptor
* @param getter the accessor method for the attribute
* @param setter the mutator method for the attribute
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @see #setDefaultCurrencyTimeLimit(Integer)
* @see #applyDefaultCurrencyTimeLimit(javax.management.Descriptor)
*/
protected void populateAttributeDescriptor(Descriptor desc, Method getter, Method setter, String beanKey) {
applyDefaultCurrencyTimeLimit(desc);
}
/**
* Allows subclasses to add extra fields to the <code>Descriptor</code> for a particular
* operation. Default implementation sets the <code>currencyTimeLimit</code> field to
* the specified "defaultCurrencyTimeLimit", if any (by default none).
* @param desc the operation descriptor
* @param method the method corresponding to the operation
* @param beanKey the key associated with the MBean in the beans map
* of the <code>MBeanExporter</code>
* @see #setDefaultCurrencyTimeLimit(Integer)
* @see #applyDefaultCurrencyTimeLimit(javax.management.Descriptor)
*/
protected void populateOperationDescriptor(Descriptor desc, Method method, String beanKey) {
applyDefaultCurrencyTimeLimit(desc);
}
/**
* Set the <code>currencyTimeLimit</code> field to the specified
* "defaultCurrencyTimeLimit", if any (by default none).
* @param desc the JMX attribute or operation descriptor
* @see #setDefaultCurrencyTimeLimit(Integer)
*/
protected final void applyDefaultCurrencyTimeLimit(Descriptor desc) {
if (getDefaultCurrencyTimeLimit() != null) {
desc.setField(FIELD_CURRENCY_TIME_LIMIT, getDefaultCurrencyTimeLimit().toString());
}
}
/**
* Apply the given JMX "currencyTimeLimit" value to the given descriptor.
* <p>Default implementation sets a value <code>>0</code> as-is (as number of cache seconds),
* turns a value of <code>0</code> into <code>Integer.MAX_VALUE</code> ("always cache")
* and sets the "defaultCurrencyTimeLimit" (if any, indicating "never cache") in case of
* a value <code><0</code>. This follows the recommendation in the JMX 1.2 specification.
* @param desc the JMX attribute or operation descriptor
* @param currencyTimeLimit the "currencyTimeLimit" value to apply
* @see #setDefaultCurrencyTimeLimit(Integer)
* @see #applyDefaultCurrencyTimeLimit(javax.management.Descriptor)
*/
protected void applyCurrencyTimeLimit(Descriptor desc, int currencyTimeLimit) {
if (currencyTimeLimit > 0) {
// number of cache seconds
desc.setField(FIELD_CURRENCY_TIME_LIMIT, Integer.toString(currencyTimeLimit));
}
else if (currencyTimeLimit == 0) {
// "always cache"
desc.setField(FIELD_CURRENCY_TIME_LIMIT, Integer.toString(Integer.MAX_VALUE));
}
else {
// "never cache"
applyDefaultCurrencyTimeLimit(desc);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -