Example usage for javax.management MBeanServerConnection invoke

List of usage examples for javax.management MBeanServerConnection invoke

Introduction

In this page you can find the example usage for javax.management MBeanServerConnection invoke.

Prototype

public Object invoke(ObjectName name, String operationName, Object params[], String signature[])
        throws InstanceNotFoundException, MBeanException, ReflectionException, IOException;

Source Link

Document

Invokes an operation on an MBean.

Because of the need for a signature to differentiate possibly-overloaded operations, it is much simpler to invoke operations through an JMX#newMBeanProxy(MBeanServerConnection,ObjectName,Class) MBean proxy where possible.

Usage

From source file:org.jboss.test.cluster.classloader.leak.test.ClassloaderLeakTestBase.java

private void removeClassLoader(String key) throws Exception {
    try {//  ww w  .  j ava2 s.c o m
        ObjectName on = new ObjectName(ClassLoaderTrackerMBean.OBJECT_NAME);
        Object[] params = { key };
        String[] signature = new String[] { String.class.getName() };
        MBeanServerConnection[] adaptors = getAdaptors();
        for (MBeanServerConnection adaptor : adaptors) {
            adaptor.invoke(on, "removeClassLoader", params, signature);
        }
    } catch (Exception e) {
        log.error("Caught exception removing classloader under key " + key, e);
    }
}

From source file:org.hyperic.hq.product.jmx.MxUtil.java

public static Object invoke(Properties config, String objectName, String method, Object[] args, String[] sig)
        throws MetricUnreachableException, MetricNotFoundException, PluginException {

    JMXConnector connector = null;

    try {//from   www.  j a v  a2 s  . c om
        connector = getMBeanConnector(config);
        MBeanServerConnection mServer = connector.getMBeanServerConnection();
        ObjectName obj = new ObjectName(objectName);
        MBeanInfo info = mServer.getMBeanInfo(obj);

        if (sig.length == 0) {
            MBeanUtil.OperationParams params = MBeanUtil.getOperationParams(info, method, args);
            if (params.isAttribute) {
                if (method.startsWith("set")) {
                    return setAttribute(mServer, obj, method, params.arguments[0]);
                } else {
                    return getAttribute(mServer, obj, method);
                }
            }
            sig = params.signature;
            args = params.arguments;
        }

        return mServer.invoke(obj, method, args, sig);
    } catch (RemoteException e) {
        throw unreachable(config, e);
    } catch (MalformedObjectNameException e) {
        throw invalidObjectName(objectName, e);
    } catch (InstanceNotFoundException e) {
        throw objectNotFound(objectName, e);
    } catch (ReflectionException e) {
        throw error(objectName, e, method);
    } catch (IntrospectionException e) {
        throw error(objectName, e, method);
    } catch (MBeanException e) {
        throw error(objectName, e, method);
    } catch (IOException e) {
        throw error(objectName, e, method);
    } finally {
        close(connector, objectName, method);
    }
}

From source file:org.rhq.enterprise.server.plugin.pc.perspective.PerspectiveServerPluginManager.java

private void deployWar(ServerPluginEnvironment env, String name, InputStream iStream) throws Exception {
    // Save the war file to the plugins data directory. This survives restarts and will
    // act as our deploy directory.
    File deployFile = writeWarToFile(getDeployFile(env, name), iStream);

    // get reference to MBean server
    Context ic = new InitialContext();
    MBeanServerConnection server = (MBeanServerConnection) ic.lookup("jmx/invoker/RMIAdaptor");

    // get reference to MainDeployer MBean
    ObjectName mainDeployer = new ObjectName("jboss.system:service=MainDeployer");

    server.invoke(mainDeployer, "deploy", new Object[] { deployFile.getAbsolutePath() },
            new String[] { String.class.getName() });
}

From source file:dk.netarkivet.common.utils.JMXUtils.java

/** Execute a command on a bean.
 *
 * @param connection Connection to the server holding the bean.
 * @param beanName Name of the bean./* ww w. j av a2s .  c o m*/
 * @param command Command to execute.
 * @param arguments Arguments to the command.  Only string arguments are
 * possible at the moment.
 * @return The return value of the executed command.
 */
public static Object executeCommand(MBeanServerConnection connection, String beanName, String command,
        String... arguments) {
    ArgumentNotValid.checkNotNull(connection, "MBeanServerConnection connection");
    ArgumentNotValid.checkNotNullOrEmpty(beanName, "String beanName");
    ArgumentNotValid.checkNotNullOrEmpty(command, "String command");
    ArgumentNotValid.checkNotNull(arguments, "String... arguments");

    log.debug(
            "Preparing to execute " + command + " with args " + Arrays.toString(arguments) + " on " + beanName);
    final int maxJmxRetries = getMaxTries();
    try {
        final String[] signature = new String[arguments.length];
        Arrays.fill(signature, String.class.getName());
        // The first time we attempt to connect to an mbean, we might have
        // to wait a bit for it to appear
        Throwable lastException;
        int tries = 0;
        do {
            tries++;
            try {
                Object ret = connection.invoke(getBeanName(beanName), command, arguments, signature);
                log.debug("Executed command " + command + " returned " + ret);
                return ret;
            } catch (InstanceNotFoundException e) {
                lastException = e;
                if (tries < maxJmxRetries) {
                    TimeUtils.exponentialBackoffSleep(tries);
                }
            } catch (IOException e) {
                log.warn("Exception thrown while executing " + command + " with args "
                        + Arrays.toString(arguments) + " on " + beanName, e);
                lastException = e;
                if (tries < maxJmxRetries) {
                    TimeUtils.exponentialBackoffSleep(tries);
                }
            }
        } while (tries < maxJmxRetries);
        throw new IOFailure("Failed to find MBean " + beanName + " for executing " + command + " after " + tries
                + " attempts", lastException);
    } catch (MBeanException e) {
        throw new IOFailure("MBean exception for " + beanName, e);
    } catch (ReflectionException e) {
        throw new IOFailure("Reflection exception for " + beanName, e);
    }
}

From source file:org.jboss.as.test.integration.domain.rbac.JmxRBACProviderHostScopedRolesTestCase.java

private void doOperation(boolean successExpected, String objectName, String operationName,
        JmxManagementInterface jmx) throws Exception {
    MBeanServerConnection connection = jmx.getConnection();
    ObjectName domain = new ObjectName(objectName);
    try {/*  w ww  .  ja  v  a  2  s  . c  om*/
        connection.invoke(domain, operationName, ArrayUtils.EMPTY_OBJECT_ARRAY, ArrayUtils.EMPTY_STRING_ARRAY);
        assertTrue("Failure was expected but success happened", successExpected);
    } catch (JMRuntimeException e) {
        if (e.getMessage().contains("WFLYJMX0037")) {
            assertFalse("Success was expected but failure happened: " + e, successExpected);
        } else {
            throw e;
        }
    }
}

From source file:org.jboss.test.classloader.leak.test.ClassloaderLeakTestBase.java

private void flushSecurityCache(String domain) throws Exception {
    log.debug("Flushing security cache " + domain);
    MBeanServerConnection adaptor = delegate.getServer();
    ObjectName on = new ObjectName(ClassLoaderTrackerMBean.OBJECT_NAME);
    Object[] params = { domain };
    String[] signature = new String[] { String.class.getName() };
    adaptor.invoke(on, "flushSecurityCache", params, signature);
}

From source file:org.jboss.test.classloader.leak.test.ClassloaderLeakTestBase.java

private boolean hasClassLoaderBeenReleased(String key) throws Exception {
    MBeanServerConnection adaptor = delegate.getServer();
    ObjectName on = new ObjectName(ClassLoaderTrackerMBean.OBJECT_NAME);
    Object[] params = { key };/*from  ww w  .  j  av  a 2  s .co  m*/
    String[] signature = new String[] { String.class.getName() };
    return ((Boolean) adaptor.invoke(on, "hasClassLoaderBeenReleased", params, signature)).booleanValue();
}

From source file:org.jboss.test.classloader.leak.test.ClassloaderLeakTestBase.java

@SuppressWarnings("unchecked")
private List<String> hasClassLoaders(String[] keys) throws Exception {
    MBeanServerConnection adaptor = delegate.getServer();
    ObjectName on = new ObjectName(ClassLoaderTrackerMBean.OBJECT_NAME);
    Object[] params = { keys };/*from   w w w  . ja  va 2 s  .  co m*/
    String[] signature = new String[] { String[].class.getName() };
    return ((List) adaptor.invoke(on, "hasClassLoaders", params, signature));
}

From source file:org.jboss.test.classloader.leak.test.ClassloaderLeakTestBase.java

private void removeClassLoader(String key) throws Exception {
    try {//from ww  w .  ja  va  2s.  co m
        MBeanServerConnection adaptor = delegate.getServer();
        ObjectName on = new ObjectName(ClassLoaderTrackerMBean.OBJECT_NAME);
        Object[] params = { key };
        String[] signature = new String[] { String.class.getName() };
        adaptor.invoke(on, "removeClassLoader", params, signature);
    } catch (Exception e) {
        log.error("Caught exception removing classloader under key " + key, e);
    }
}

From source file:io.fabric8.test.smoke.JolokiaEndpointTestBase.java

@Test
public void testMXBeanEndpoint() throws Exception {

    ContainerManager cntManager = ContainerManagerLocator.getContainerManager();
    Container cnt = cntManager.getCurrentContainer();

    ServiceEndpoint sep = cnt.getServiceEndpoint(URLServiceEndpoint.JMX_SERVICE_ENDPOINT_IDENTITY);
    JMXServiceEndpoint jmxEndpoint = sep.adapt(JMXServiceEndpoint.class);
    String serviceURL = jmxEndpoint.getServiceURL();
    Assert.assertNotNull("JMX URL not null", serviceURL);

    // Get the local MBeanServer
    MBeanServer server = ServiceLocator.getRequiredService(MBeanServer.class);
    server.registerMBean(new Simple(), SimpleMXBean.OBJECT_NAME);
    try {//from   w w w .j av a 2  s .  c om
        String[] userpass = RuntimeType.KARAF == RuntimeType.getRuntimeType() ? karafJmx : otherJmx;
        JMXConnector jmxConnector = jmxEndpoint.getJMXConnector(userpass[0], userpass[1], 200,
                TimeUnit.MILLISECONDS);
        MBeanServerConnection con = jmxConnector.getMBeanServerConnection();
        try {
            // Simple string echo
            Object[] params = new Object[] { "Kermit" };
            String[] signature = new String[] { String.class.getName() };
            Object result = con.invoke(SimpleMXBean.OBJECT_NAME, "echo", params, signature);
            Assert.assertEquals("Hello: Kermit", result);

            // Set Bean attribute using CompositeData
            Bean bean = new Bean("Hello", "Foo");
            CompositeData cdata = OpenTypeGenerator.toCompositeData(bean);
            con.setAttribute(SimpleMXBean.OBJECT_NAME, new Attribute("Bean", cdata));

            // Get Bean attribute using CompositeData
            cdata = (CompositeData) con.getAttribute(SimpleMXBean.OBJECT_NAME, "Bean");
            Assert.assertEquals(bean, OpenTypeGenerator.fromCompositeData(Bean.class, cdata));

            // Simple Bean echo using CompositeData
            params = new Object[] { cdata };
            signature = new String[] { CompositeData.class.getName() };
            cdata = (CompositeData) con.invoke(SimpleMXBean.OBJECT_NAME, "echoBean", params, signature);
            Assert.assertEquals(bean, OpenTypeGenerator.fromCompositeData(Bean.class, cdata));
        } finally {
            jmxConnector.close();
        }
    } finally {
        server.unregisterMBean(SimpleMXBean.OBJECT_NAME);
    }
}