org.springside.modules.utils.jmx.JmxClientTemplate.java Source code

Java tutorial

Introduction

Here is the source code for org.springside.modules.utils.jmx.JmxClientTemplate.java

Source

/**
 * Copyright (c) 2005-2010 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id$
 */
package org.springside.modules.utils.jmx;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.management.Attribute;
import javax.management.JMException;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

/**
 * JMX?.
 * 
 * ?
 * 1.JMX Server,?.
 * 2.??MBeanMBean?(MbeanClass).
 * 3.???MBean(MBeanClass).
 * 
 * @author ben
 * @author calvin
 */
public class JmxClientTemplate {

    private JMXConnector connector;
    private MBeanServerConnection connection;
    private AtomicBoolean connected = new AtomicBoolean(false);

    /**
     *  ??. 
     */
    public JmxClientTemplate(final String serviceUrl) throws IOException {
        initConnector(serviceUrl, null, null);
    }

    /**
     * ??.
     */
    public JmxClientTemplate(final String serviceUrl, final String userName, final String passwd)
            throws IOException {
        initConnector(serviceUrl, userName, passwd);
    }

    /**
     * JMX Server.
     */

    private void initConnector(final String serviceUrl, final String userName, final String passwd)
            throws IOException {
        JMXServiceURL url = new JMXServiceURL(serviceUrl);

        boolean hasCredentlals = StringUtils.isNotBlank(userName);
        if (hasCredentlals) {
            Map environment = Collections.singletonMap(JMXConnector.CREDENTIALS, new String[] { userName, passwd });
            connector = JMXConnectorFactory.connect(url, environment);
        } else {
            connector = JMXConnectorFactory.connect(url);
        }

        connection = connector.getMBeanServerConnection();
        connected.set(true);
    }

    /**
     * JMX.
     */
    public void close() throws IOException {
        connector.close();
        connected.set(false);
        connection = null;
    }

    /**
     * MBean?.
     */
    public <T> T createMBeanProxy(final String mbeanName, final Class<T> mBeanInterface) {
        Assert.hasText(mbeanName, "mbeanName?");
        assertConnected();

        ObjectName objectName = buildObjectName(mbeanName);
        return MBeanServerInvocationHandler.newProxyInstance(connection, objectName, mBeanInterface, false);
    }

    /**
     * ???MBean(MBeanClass).
     * 
     * attributeName?.
     */
    public Object getAttribute(final String mbeanName, final String attributeName) {
        Assert.hasText(mbeanName, "mbeanName?");
        Assert.hasText(attributeName, "attributeName?");
        assertConnected();

        try {
            ObjectName objectName = buildObjectName(mbeanName);
            return connection.getAttribute(objectName, attributeName);
        } catch (JMException e) {
            throw new IllegalArgumentException("??", e);
        } catch (IOException e) {
            throw new IllegalStateException("", e);
        }
    }

    /**
     * ??MBean(MBeanClass).
     * 
     * attributeName?.
     */
    public void setAttribute(final String mbeanName, final String attributeName, final Object value) {
        Assert.hasText(mbeanName, "mbeanName?");
        Assert.hasText(attributeName, "attributeName?");
        assertConnected();

        try {
            ObjectName objectName = buildObjectName(mbeanName);
            Attribute attribute = new Attribute(attributeName, value);
            connection.setAttribute(objectName, attribute);
        } catch (JMException e) {
            throw new IllegalArgumentException("??", e);
        } catch (IOException e) {
            throw new IllegalStateException("", e);
        }
    }

    /**
     * ??MBean(MBeanClass).
     * 
     * ?.
     */
    public void inoke(final String mbeanName, final String methodName) {
        invoke(mbeanName, methodName, new String[] {}, new Object[] {});
    }

    /**
     * ??MBean(MBeanClass).
     * 
     * @param paramClassNames ?Class??.
     */
    public Object invoke(final String mbeanName, final String methodName, final String[] paramClassNames,
            final Object[] paramValues) {
        Assert.hasText(mbeanName, "mbeanName?");
        Assert.hasText(methodName, "methodName?");
        assertConnected();

        try {
            ObjectName objectName = buildObjectName(mbeanName);
            return connection.invoke(objectName, methodName, paramValues, paramClassNames);
        } catch (JMException e) {
            throw new IllegalArgumentException("??", e);
        } catch (IOException e) {
            throw new IllegalStateException("", e);
        }
    }

    /**
     * ??MBean(MBeanClass).
     * 
     * @param paramClasses ?Class.
     */

    public Object invoke(final String mbeanName, final String methodName, final Class[] paramClasses,
            final Object[] paramValues) {
        String[] paramClassNames = new String[paramClasses.length];
        for (int i = 0; i < paramClasses.length; i++) {
            paramClassNames[i] = paramClasses[i].getName();
        }

        return invoke(mbeanName, methodName, paramClassNames, paramValues);
    }

    /**
     * ?Connection.
     */
    private void assertConnected() {
        if (!connected.get()) {
            throw new IllegalStateException("connector");
        }
    }

    /**
     * ObjectName,?unchecked exception.
     */
    private ObjectName buildObjectName(final String mbeanName) {
        try {
            return new ObjectName(mbeanName);
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException("mbeanName:" + mbeanName + "?,?ObjectName.",
                    e);
        }
    }
}