Example usage for org.springframework.jmx.support MBeanServerConnectionFactoryBean setEnvironment

List of usage examples for org.springframework.jmx.support MBeanServerConnectionFactoryBean setEnvironment

Introduction

In this page you can find the example usage for org.springframework.jmx.support MBeanServerConnectionFactoryBean setEnvironment.

Prototype

public void setEnvironment(Properties environment) 

Source Link

Document

Set the environment properties used to construct the JMXConnector as java.util.Properties (String key/value pairs).

Usage

From source file:com.haulmont.cuba.web.jmx.JmxConnectionHelper.java

protected static <T> T withConnection(JmxInstance instance, JmxAction<T> action) {
    try {//ww  w.  j a  v a2 s .c o m
        if (Objects.equals(instance.getId(), LOCAL_JMX_INSTANCE_ID)) {
            return action.perform(instance, getLocalConnection());
        } else {
            MBeanServerConnectionFactoryBean factoryBean = new MBeanServerConnectionFactoryBean();
            String address = instance.getAddress();
            if (!address.startsWith("service:")) {
                address = "service:jmx:rmi:///jndi/rmi://" + address + "/jmxrmi";
            }
            factoryBean.setServiceUrl(address);

            String username = instance.getLogin();
            if (StringUtils.isNotEmpty(username)) {
                Properties properties = new Properties();
                properties.put("jmx.remote.credentials", new String[] { username, instance.getPassword() });
                factoryBean.setEnvironment(properties);
            }

            factoryBean.afterPropertiesSet();

            MBeanServerConnection connection = factoryBean.getObject();
            T result;
            try {
                result = action.perform(instance, connection);
            } finally {
                try {
                    factoryBean.destroy();
                } catch (Exception ignored) {
                }
            }
            return result;
        }
    } catch (Exception e) {
        throw new JmxControlException(e);
    }
}