Skip to content

Commit

Permalink
replace target with target supplier
Browse files Browse the repository at this point in the history
  • Loading branch information
arhimondr committed Aug 30, 2015
1 parent 8d81782 commit 963a28c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 82 deletions.
72 changes: 46 additions & 26 deletions src/main/java/org/weakref/jmx/MBeanAttributeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import static org.weakref.jmx.ReflectionUtils.isValidGetter;
import static org.weakref.jmx.ReflectionUtils.isValidSetter;

import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;

import javax.management.Descriptor;
import javax.management.ImmutableDescriptor;
import javax.management.MBeanAttributeInfo;
Expand All @@ -31,7 +34,7 @@
public class MBeanAttributeBuilder
{
private final static Pattern getterOrSetterPattern = Pattern.compile("(get|set|is)(.+)");
private Object target;
private Supplier targetSupplier;
private String name;
private Method concreteGetter;
private Method annotatedGetter;
Expand All @@ -40,10 +43,10 @@ public class MBeanAttributeBuilder
private boolean flatten;
private boolean nested;

public MBeanAttributeBuilder onInstance(Object target)
public MBeanAttributeBuilder withTargetSupplier(Supplier targetSupplier)
{
if (target == null) throw new NullPointerException("target is null");
this.target = target;
if (targetSupplier == null) throw new NullPointerException("targetSupplier is null");
this.targetSupplier = targetSupplier;
return this;
}

Expand Down Expand Up @@ -116,7 +119,7 @@ public MBeanAttributeBuilder nested()

public Collection<? extends MBeanFeature> build()
{
if (target == null) {
if (targetSupplier == null) {
throw new IllegalArgumentException("JmxAttribute must have a target object");
}

Expand All @@ -132,18 +135,13 @@ public Collection<? extends MBeanFeature> build()
throw new IllegalArgumentException("Flattened JmxAttribute must have a concrete getter");
}

Object value = null;
try {
value = concreteGetter.invoke(target);
}
catch (Exception e) {
// todo log me
}
if (value == null) {
return Collections.emptySet();
Class targetType = getNestedTargetType(concreteGetter);
if(targetType == null){
return ImmutableList.of();
}
Supplier nestedObjectSupplier = createNestedObjectSupplier(targetType, concreteGetter);

MBean mbean = new MBeanBuilder(value).build();
MBean mbean = MBeanBuilder.from(targetType, nestedObjectSupplier).build();
ArrayList<MBeanFeature> features = new ArrayList<MBeanFeature>();
features.addAll(mbean.getAttributes());
features.addAll(mbean.getOperations());
Expand All @@ -155,18 +153,13 @@ else if (nested || AnnotationUtils.isNested(annotatedGetter)) {
throw new IllegalArgumentException("Nested JmxAttribute must have a concrete getter");
}

Object value = null;
try {
value = concreteGetter.invoke(target);
}
catch (Exception e) {
// todo log me
}
if (value == null) {
return Collections.emptySet();
Class targetType = getNestedTargetType(concreteGetter);
if(targetType == null){
return ImmutableList.of();
}
Supplier nestedObjectSupplier = createNestedObjectSupplier(targetType, concreteGetter);

MBean mbean = new MBeanBuilder(value).build();
MBean mbean = MBeanBuilder.from(targetType, nestedObjectSupplier).build();
ArrayList<MBeanFeature> features = new ArrayList<MBeanFeature>();
for (MBeanAttribute attribute : mbean.getAttributes()) {
features.add(new NestedMBeanAttribute(attributeName, attribute));
Expand Down Expand Up @@ -219,7 +212,7 @@ else if (nested || AnnotationUtils.isNested(annotatedGetter)) {
descriptor);


return Collections.singleton(new ReflectionMBeanAttribute(mbeanAttributeInfo, target, concreteGetter, concreteSetter));
return Collections.singleton(new ReflectionMBeanAttribute(mbeanAttributeInfo, targetSupplier, concreteGetter, concreteSetter));
}
}

Expand All @@ -242,4 +235,31 @@ private static String getAttributeName(Method... methods)
}
return null;
}

private Class getNestedTargetType(Method concreteGetter)
{
try {
Object value = concreteGetter.invoke(targetSupplier.get());
return value.getClass();
}
catch (Exception e) {
// todo log me
return null;
}
}

private Supplier createNestedObjectSupplier(final Class requiredType, final Method concreteGetter){
return new Supplier() {
public Object get()
{
try {
return requiredType.cast(concreteGetter.invoke(targetSupplier.get()));
}
catch (Exception e) {
// todo log me
return null;
}
}
};
}
}
45 changes: 23 additions & 22 deletions src/main/java/org/weakref/jmx/MBeanBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,42 @@
import static org.weakref.jmx.ReflectionUtils.isGetter;
import static org.weakref.jmx.ReflectionUtils.isSetter;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static com.google.common.base.Preconditions.checkNotNull;

class MBeanBuilder
{
private String className;
private final List<MBeanAttributeBuilder> attributeBuilders = new ArrayList<MBeanAttributeBuilder>();
private final List<MBeanOperationBuilder> operationBuilders = new ArrayList<MBeanOperationBuilder>();
private String description;

public MBeanBuilder(String className)
{
this.className = className;
}

public static MBeanBuilder from(String className)
public static MBeanBuilder from(Object object)
{
return new MBeanBuilder(className);
return from(object.getClass(), Suppliers.ofInstance(object));
}

public static MBeanBuilder from(Object object)
static MBeanBuilder from(Class targetType, Supplier targetSupplier)
{
return new MBeanBuilder(object);
return new MBeanBuilder(targetType, targetSupplier);
}

public MBeanBuilder(Object target)
private MBeanBuilder(Class targetType, Supplier targetSupplier)
{
if (target == null) {
throw new NullPointerException("target is null");
}
checkNotNull(targetType, "targetType is null");
checkNotNull(targetSupplier, "targetSupplier is null");

Map<String, MBeanAttributeBuilder> attributeBuilders = new TreeMap<String, MBeanAttributeBuilder>();

for (Map.Entry<Method, Method> entry : AnnotationUtils.findManagedMethods(target.getClass()).entrySet()) {
for (Map.Entry<Method, Method> entry : AnnotationUtils.findManagedMethods(targetType).entrySet()) {
Method concreteMethod = entry.getKey();
Method annotatedMethod = entry.getValue();

Expand All @@ -64,9 +63,9 @@ public MBeanBuilder(Object target)

MBeanAttributeBuilder attributeBuilder = attributeBuilders.get(attributeName);
if (attributeBuilder == null) {
attributeBuilder = new MBeanAttributeBuilder().named(attributeName).onInstance(target);
attributeBuilder = new MBeanAttributeBuilder().named(attributeName).withTargetSupplier(targetSupplier);
}

if (isGetter(concreteMethod)) {
attributeBuilder = attributeBuilder
.withConcreteGetter(concreteMethod)
Expand All @@ -84,7 +83,7 @@ else if (isSetter(concreteMethod)) {
// TODO: change this so that we are not making assumptions about mutability or side effects
// in the builder
addOperation()
.onInstance(target)
.withTargetSupplier(targetSupplier)
.withConcreteMethod(concreteMethod)
.withAnnotatedMethod(annotatedMethod)
.build();
Expand All @@ -95,8 +94,8 @@ else if (isSetter(concreteMethod)) {
this.attributeBuilders.add(attributeBuilder);
}

className = target.getClass().getName();
description = AnnotationUtils.getDescription(target.getClass().getAnnotations());
className = targetType.getName();
description = AnnotationUtils.getDescription(targetType.getAnnotations());
}

public MBeanBuilder withDescription(String description)
Expand All @@ -105,13 +104,15 @@ public MBeanBuilder withDescription(String description)
return this;
}

public MBeanAttributeBuilder addAttribute() {
public MBeanAttributeBuilder addAttribute()
{
MBeanAttributeBuilder builder = new MBeanAttributeBuilder();
attributeBuilders.add(builder);
return builder;
}

public MBeanOperationBuilder addOperation() {
public MBeanOperationBuilder addOperation()
{
MBeanOperationBuilder builder = new MBeanOperationBuilder();
operationBuilders.add(builder);
return builder;
Expand All @@ -134,7 +135,7 @@ public MBean build()
for (MBeanOperationBuilder operationBuilder : operationBuilders) {
operations.add(operationBuilder.build());
}

return new MBean(className, description, attributes, operations);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/weakref/jmx/MBeanExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void export(String name, Object object)
public void export(ObjectName objectName, Object object)
{
try {
MBeanBuilder builder = new MBeanBuilder(object);
MBeanBuilder builder = MBeanBuilder.from(object);
MBean mbean = builder.build();

synchronized(exportedObjects) {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/weakref/jmx/MBeanOperationBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
*/
package org.weakref.jmx;

import com.google.common.base.Supplier;
import com.thoughtworks.paranamer.BytecodeReadingParanamer;
import com.thoughtworks.paranamer.Paranamer;

import javax.management.Descriptor;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class MBeanOperationBuilder
{
private Object target;
private Supplier targetSupplier;
private String name;
private Method concreteMethod;
private Method annotatedMethod;

public MBeanOperationBuilder onInstance(Object target)
public MBeanOperationBuilder withTargetSupplier(Supplier targetSupplier)
{
if (target == null) throw new NullPointerException("target is null");
this.target = target;
if (targetSupplier == null) throw new NullPointerException("targetSupplier is null");
this.targetSupplier = targetSupplier;
return this;
}

Expand Down Expand Up @@ -65,7 +67,7 @@ public MBeanOperationBuilder withAnnotatedMethod(Method annotatedMethod)

public MBeanOperation build()
{
if (target == null) {
if (targetSupplier == null) {
throw new IllegalArgumentException("JmxOperation must have a target object");
}

Expand Down Expand Up @@ -126,6 +128,6 @@ public MBeanOperation build()
MBeanOperationInfo.UNKNOWN,
descriptor);

return new ReflectionMBeanOperation(mbeanOperationInfo, target, concreteMethod);
return new ReflectionMBeanOperation(mbeanOperationInfo, targetSupplier, concreteMethod);
}
}
27 changes: 14 additions & 13 deletions src/main/java/org/weakref/jmx/ReflectionMBeanAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package org.weakref.jmx;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.weakref.jmx.ReflectionUtils.invoke;
import com.google.common.base.Supplier;

import javax.management.AttributeNotFoundException;
import javax.management.InvalidAttributeValueException;
Expand All @@ -27,21 +29,21 @@
class ReflectionMBeanAttribute implements MBeanAttribute
{
private final MBeanAttributeInfo info;
private final Object target;
private final Supplier targetSupplier;
private final String name;
private final Method getter;
private final Method setter;

public ReflectionMBeanAttribute(MBeanAttributeInfo info, Object target, Method getter, Method setter)
public ReflectionMBeanAttribute(MBeanAttributeInfo info, Supplier targetSupplier, Method getter, Method setter)
{
if (info == null) {
throw new NullPointerException("info is null");
}
if (target == null) {
throw new NullPointerException("target is null");
if (targetSupplier == null) {
throw new NullPointerException("targetSupplier is null");
}
this.info = info;
this.target = target;
this.targetSupplier = targetSupplier;
this.name = info.getName();
this.getter = getter;
this.setter = setter;
Expand All @@ -52,11 +54,6 @@ public MBeanAttributeInfo getInfo()
return info;
}

public Object getTarget()
{
return target;
}

public String getName()
{
return name;
Expand All @@ -68,8 +65,7 @@ public Object getValue()
if (getter == null) {
throw new AttributeNotFoundException(name + " is write-only");
}
Object result = invoke(target, getter);
return result;
return invoke(getTarget(), getter);
}

public void setValue(Object value)
Expand All @@ -81,6 +77,11 @@ public void setValue(Object value)
if (!ReflectionUtils.isAssignable(value, setter.getParameterTypes()[0])) {
throw new InvalidAttributeValueException("Can not assign " + value.getClass() + " to attribute " + name);
}
invoke(target, setter, value);
invoke(getTarget(), setter, value);
}

private Object getTarget()
{
return checkNotNull(targetSupplier.get(), "target is null");
}
}
Loading

0 comments on commit 963a28c

Please sign in to comment.