ManagedResource annotation supports placeholders for String attributes

In particular, the specified object name may use a placeholder for its domain part now, allowing for several instances of the MBean to be registered against the same MBeanServer from different applications.

Issue: SPR-8244
This commit is contained in:
Juergen Hoeller
2012-10-31 02:07:09 +01:00
committed by unknown
parent 8f8e517c0d
commit 0e0200769d
8 changed files with 69 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 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.
@@ -16,12 +16,11 @@
package org.springframework.jmx.export.annotation;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.annotation.AnnotationBeanUtils;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.jmx.export.metadata.InvalidMetadataException;
import org.springframework.jmx.export.metadata.JmxAttributeSource;
@@ -32,6 +31,7 @@ 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.StringUtils;
import org.springframework.util.StringValueResolver;
/**
* Implementation of the <code>JmxAttributeSource</code> interface that
@@ -45,7 +45,15 @@ import org.springframework.util.StringUtils;
* @see org.springframework.jmx.export.annotation.ManagedAttribute
* @see org.springframework.jmx.export.annotation.ManagedOperation
*/
public class AnnotationJmxAttributeSource implements JmxAttributeSource {
public class AnnotationJmxAttributeSource implements JmxAttributeSource, EmbeddedValueResolverAware {
private StringValueResolver embeddedValueResolver;
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.embeddedValueResolver = resolver;
}
public ManagedResource getManagedResource(Class<?> beanClass) throws InvalidMetadataException {
org.springframework.jmx.export.annotation.ManagedResource ann =
@@ -54,9 +62,13 @@ public class AnnotationJmxAttributeSource implements JmxAttributeSource {
return null;
}
ManagedResource managedResource = new ManagedResource();
AnnotationBeanUtils.copyPropertiesToBean(ann, managedResource);
AnnotationBeanUtils.copyPropertiesToBean(ann, managedResource, this.embeddedValueResolver);
if (!"".equals(ann.value()) && !StringUtils.hasLength(managedResource.getObjectName())) {
managedResource.setObjectName(ann.value());
String value = ann.value();
if (this.embeddedValueResolver != null) {
value = this.embeddedValueResolver.resolveStringValue(value);
}
managedResource.setObjectName(value);
}
return managedResource;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2012 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.
@@ -16,9 +16,11 @@
package org.springframework.jmx.export.annotation;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler;
import org.springframework.jmx.export.naming.MetadataNamingStrategy;
import org.springframework.util.StringValueResolver;
/**
* Convenient subclass of Spring's standard {@link MBeanExporter},
@@ -32,7 +34,7 @@ import org.springframework.jmx.export.naming.MetadataNamingStrategy;
* @author Juergen Hoeller
* @since 2.5
*/
public class AnnotationMBeanExporter extends MBeanExporter {
public class AnnotationMBeanExporter extends MBeanExporter implements EmbeddedValueResolverAware {
private final AnnotationJmxAttributeSource annotationSource =
new AnnotationJmxAttributeSource();
@@ -63,4 +65,8 @@ public class AnnotationMBeanExporter extends MBeanExporter {
this.metadataNamingStrategy.setDefaultDomain(defaultDomain);
}
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.annotationSource.setEmbeddedValueResolver(resolver);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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.
@@ -164,6 +164,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
*/
protected static final String FIELD_METRIC_CATEGORY = "metricCategory";
/**
* Default value for the JMX field "currencyTimeLimit".
*/
@@ -223,7 +224,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
* Return whether strict casing for attributes is enabled.
*/
protected boolean isUseStrictCasing() {
return useStrictCasing;
return this.useStrictCasing;
}
/**
@@ -250,7 +251,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
* Return whether to expose the JMX descriptor field "class" for managed operations.
*/
protected boolean isExposeClassDescriptor() {
return exposeClassDescriptor;
return this.exposeClassDescriptor;
}
@@ -447,7 +448,6 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
*/
protected abstract boolean includeOperation(Method method, String beanKey);
/**
* Get the description for a particular attribute.
* <p>The default implementation returns a description for the operation