All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.weakref.jmx.guice.SetExportBinder Maven / Gradle / Ivy

There is a newer version: 1.26
Show newest version
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));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy