org.weakref.jmx.guice.MapExportBinder 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;
import java.util.Map;
public class MapExportBinder
{
protected final Multibinder> binder;
protected final Class keyClass;
private final Class valueClass;
MapExportBinder(Multibinder> binder, Class keyClass, Class valueClass)
{
this.binder = binder;
this.keyClass = keyClass;
this.valueClass = valueClass;
}
public void withGeneratedName(final NamingFunction valueNamingFunction)
{
ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
{
public ObjectName name(Map.Entry entry)
{
try {
String itemName = valueNamingFunction.name(entry.getValue());
return new ObjectName(ObjectNames.generatedNameOf(valueClass, itemName));
}
catch (MalformedObjectNameException e) {
throw Throwables.propagate(e);
}
}
};
binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
}
public void withGeneratedName(final ObjectNameFunction valueNamingFunction)
{
ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
{
public ObjectName name(Map.Entry entry)
{
return valueNamingFunction.name(entry.getValue());
}
};
binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
}
public void withGeneratedName(final MapNamingFunction valueNamingFunction)
{
ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
{
public ObjectName name(Map.Entry entry)
{
try {
String itemName = valueNamingFunction.name(entry.getKey(), entry.getValue());
return new ObjectName(ObjectNames.generatedNameOf(valueClass, itemName));
}
catch (MalformedObjectNameException e) {
throw Throwables.propagate(e);
}
}
};
binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
}
public void withGeneratedName(final MapObjectNameFunction valueNamingFunction)
{
ObjectNameFunction> objectNameFunction = new ObjectNameFunction>()
{
public ObjectName name(Map.Entry entry)
{
return valueNamingFunction.name(entry.getKey(), entry.getValue());
}
};
binder.addBinding().toInstance(new MapMapping(keyClass, valueClass, objectNameFunction));
}
}