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

io.dropwizard.metrics5.jmx.DefaultObjectNameFactory Maven / Gradle / Ivy

package io.dropwizard.metrics5.jmx;

import io.dropwizard.metrics5.MetricName;
import java.util.Hashtable;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultObjectNameFactory implements ObjectNameFactory {

    private static final Logger LOGGER = LoggerFactory.getLogger(JmxReporter.class);

    @Override
    public ObjectName createName(String type, String domain, MetricName name) {
        try {
            ObjectName objectName;
            Hashtable properties = new Hashtable<>();

            properties.put("name", name.getKey());
            properties.put("type", type);
            properties.putAll(name.getTags());
            objectName = new ObjectName(domain, properties);

            /*
             * The only way we can find out if we need to quote the properties is by
             * checking an ObjectName that we've constructed.
             */
            if (objectName.isDomainPattern()) {
                domain = ObjectName.quote(domain);
            }
            if (objectName.isPropertyValuePattern("name")) {
                properties.put("name", ObjectName.quote(name.getKey()));
            }
            if (objectName.isPropertyValuePattern("type")) {
                properties.put("type", ObjectName.quote(type));
            }
            objectName = new ObjectName(domain, properties);

            return objectName;
        } catch (MalformedObjectNameException e) {
            try {
                return new ObjectName(domain, "name", ObjectName.quote(name.getKey()));
            } catch (MalformedObjectNameException e1) {
                LOGGER.warn("Unable to register {} {}", type, name, e1);
                throw new RuntimeException(e1);
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy