org.weakref.jmx.guice.SetExportBinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmxutils Show documentation
Show all versions of jmxutils Show documentation
Exporting JMX mbeans made easy
package org.weakref.jmx.guice;
import org.weakref.jmx.internal.guava.base.Throwables;
import com.google.inject.multibindings.Multibinder;
import org.weakref.jmx.ObjectNames;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
public class SetExportBinder
{
private final Multibinder> binder;
private final Class clazz;
SetExportBinder(Multibinder> binder, Class clazz)
{
this.binder = binder;
this.clazz = clazz;
}
public void withGeneratedName(final NamingFunction itemNamingFunction)
{
ObjectNameFunction objectNameFunction = new ObjectNameFunction()
{
public ObjectName name(T object)
{
try {
String itemName = itemNamingFunction.name(object);
return new ObjectName(ObjectNames.generatedNameOf(clazz, itemName));
}
catch (MalformedObjectNameException e) {
throw Throwables.propagate(e);
}
}
};
binder.addBinding().toInstance(new SetMapping(clazz, objectNameFunction));
}
public void withGeneratedName(final ObjectNameFunction itemNamingFunction)
{
ObjectNameFunction objectNameFunction = new ObjectNameFunction()
{
public ObjectName name(T object)
{
return itemNamingFunction.name(object);
}
};
binder.addBinding().toInstance(new SetMapping(clazz, objectNameFunction));
}
}