Example usage for javax.management MBeanServer invoke

List of usage examples for javax.management MBeanServer invoke

Introduction

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

Prototype

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

Source Link

Usage

From source file:org.apache.geode.management.internal.web.controllers.ShellCommandsController.java

@RequestMapping(method = RequestMethod.POST, value = "/mbean/operation")
public ResponseEntity<?> invoke(@RequestParam("resourceName") final String resourceName,
        @RequestParam("operationName") final String operationName,
        @RequestParam(value = "signature", required = false) String[] signature,
        @RequestParam(value = "parameters", required = false) Object[] parameters)
        throws MalformedObjectNameException, MBeanException, InstanceNotFoundException, ReflectionException,
        IOException {//  w ww. jav a 2 s.  com
    // Exceptions are caught by the @ExceptionHandler AbstractCommandsController.handleAppException
    signature = (signature != null ? signature : ArrayUtils.EMPTY_STRING_ARRAY);
    parameters = (parameters != null ? parameters : ObjectUtils.EMPTY_OBJECT_ARRAY);
    MBeanServer mBeanServer = getMBeanServer();
    ObjectName objectName = ObjectName.getInstance(decode(resourceName));
    final Object result = mBeanServer.invoke(objectName, decode(operationName), parameters, signature);
    byte[] serializedResult = IOUtils.serializeObject(result);
    return new ResponseEntity<>(serializedResult, HttpStatus.OK);
}

From source file:org.apache.marmotta.platform.core.services.config.ConfigurationServiceImpl.java

/**
 * Shutdown the application server running this web application; tries to determine the kind of server we are
 * running under and send the proper shutdown signal before exiting with System.exit
 *///from  ww  w . j a v  a 2s .  c  om
@Override
public void performServerShutdown() {
    try {
        MBeanServer server = (MBeanServer) Class.forName("org.apache.catalina.mbeans.MBeanUtils")
                .getMethod("createServer").invoke(null);
        ObjectName name;
        if (isTomcat6()) {
            // Tomcat 6.x
            name = new ObjectName("Catalina:type=Service,serviceName=Catalina");
            server.invoke(name, "stop", new Object[0], new String[0]);
            log.warn("shutting down Apache Tomcat server on user request");
        } else if (isTomcat7()) {
            // Tomcat 7.x
            name = new ObjectName("Catalina", "type", "Service");
            server.invoke(name, "stop", new Object[0], new String[0]);
            log.warn("shutting down Apache Tomcat server on user request");
        }
    } catch (Exception ex) {
        log.error("shutting down other servers than Apache Tomcat is not supported", ex);
    }

    // ensure complete shutdown
    System.exit(0);

}

From source file:org.apache.openejb.server.cli.command.LocalJMXCommand.java

private void invoke(final String value) {
    if (!value.contains("(") || !value.contains(")")) {
        streamManager.writeErr("method should follow the format: <methoName>(<arg1>,<arg2>,...)");
        return;/*from  w ww.  jav a2  s .co m*/
    }

    int open = value.indexOf("(");
    int close = value.lastIndexOf(")");

    final String name = value.substring(0, open).trim();
    final String rawArgs = value.substring(open + 1, close).trim();
    final ObjectName on;
    try {
        on = new ObjectName(value.substring(close + 1).trim());
    } catch (MalformedObjectNameException e) {
        streamManager.writeErr(e);
        return;
    }

    final MBeanServer server = LocalMBeanServer.get();
    final String[] args;
    if (rawArgs == null || rawArgs.isEmpty()) {
        args = new String[0];
    } else {
        args = rawArgs.split(",");
    }

    try {
        final MBeanInfo minfo = server.getMBeanInfo(on);
        final MBeanOperationInfo[] methods = minfo.getOperations();

        MBeanOperationInfo operation = null;
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals(name)) {
                operation = methods[i];
                break;
            }
        }

        if (operation == null) {
            streamManager.writeErr("can't find operation '" + name + "'");
            return;
        }

        final Object[] passedArgs = new Object[args.length];
        final String[] passedArgTypes = new String[args.length];
        for (int i = 0; i < passedArgs.length; i++) {
            final String expected = operation.getSignature()[i].getType();
            if (!String.class.getName().equals(expected)) {
                passedArgs[i] = PropertyEditors.getValue(expected, args[i],
                        Thread.currentThread().getContextClassLoader());
            } else {
                passedArgs[i] = args[i];
            }
            passedArgTypes[i] = expected;
        }

        streamManager.writeOut(stringify(server.invoke(on, name, passedArgs, passedArgTypes)));
    } catch (Exception e) {
        streamManager.writeErr(e);
        return;
    }
}

From source file:org.dspace.kernel.DSpaceKernelManager.java

/**
 * Get the kernel.  This will be a single instance for the JVM, but
 * the method will retrieve the same instance regardless of this 
 * object instance./*from w  w  w  .  j  av a2s .  com*/
 *
 * @param name this is the name of this kernel instance.  If you do
 * not know what this is then use null.
 * @return the DSpace kernel
 * @throws IllegalStateException if the kernel is not available or not running
 */
public DSpaceKernel getKernel(String name) {

    // Are we getting a named kernel?
    if (!StringUtils.isEmpty(name)) {
        String checkedName = checkName(name);

        if (namedKernelMap.containsKey(checkedName)) {
            return namedKernelMap.get(checkedName);
        }

        if (defaultKernel != null && checkedName.equals(defaultKernel.getMBeanName())) {
            return defaultKernel;
        }

        synchronized (lock) {
            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
            try {
                ObjectName kernelName = new ObjectName(checkedName);
                DSpaceKernel namedKernel = (DSpaceKernel) mbs.invoke(kernelName, "getManagedBean", null, null);
                if (namedKernel == null || !namedKernel.isRunning()) {
                    throw new IllegalStateException(
                            "The DSpace kernel is not started yet, please start it before attempting to use it");
                }

                namedKernelMap.put(checkedName, namedKernel);
                return namedKernel;
            } catch (InstanceNotFoundException e) {
                throw new IllegalStateException(e);
            } catch (MBeanException e) {
                throw new IllegalStateException(e);
            } catch (ReflectionException e) {
                throw new IllegalStateException(e);
            } catch (MalformedObjectNameException e) {
                throw new IllegalStateException(e);
            } catch (NullPointerException e) {
                throw new IllegalStateException(e);
            }
        }
    }

    return defaultKernel;
}

From source file:org.eclipse.ecr.core.storage.sql.reload.RepositoryReloader.java

public static void flushJCAPool() throws Exception {
    MBeanServer jboss = locateJBoss();
    if (jboss != null) {
        jboss.invoke(new ObjectName("jboss.jca:name=NXRepository/default,service=ManagedConnectionPool"),
                "flush", new Object[0], new String[0]);
    } else { // try tomcat (jtajca nuxeo plugin)
        Class<?> cl = null;
        try {/* w  w w . java  2s .c om*/
            cl = Class.forName("org.eclipse.ecr.runtime.jtajca.NuxeoContainer");
        } catch (ClassNotFoundException e) { // not tomcat or not jtajca
                                             // enabled
                                             // do nothing
        }
        if (cl != null) {
            cl.getMethod("resetConnectionManager").invoke(null);
        }
    }
}

From source file:org.eclipse.virgo.web.test.AbstractWebIntegrationTests.java

@AfterClass
public static void cleanup() throws Exception {
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    ObjectName objectName = new ObjectName(
            "org.eclipse.virgo.kernel:type=ArtifactModel,artifact-type=plan,name=org.eclipse.virgo.web.tomcat,version="
                    + CURRENT_VERSION + ",region=global");

    try {//  www.j av a 2s. c  om
        mBeanServer.invoke(objectName, "stop", null, null);
        mBeanServer.invoke(objectName, "uninstall", null, null);
    } catch (JMException _) {
    }
}

From source file:org.jahia.bin.errors.ErrorFileDumper.java

public static File performHeapDump()
        throws MalformedObjectNameException, InstanceNotFoundException, ReflectionException, MBeanException {
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    ObjectName hotSpotDiagnostic = new ObjectName("com.sun.management:type=HotSpotDiagnostic");
    if (!mBeanServer.isRegistered(hotSpotDiagnostic)) {
        throw new UnsupportedOperationException("Unable to find the "
                + "'com.sun.management:type=HotSpotDiagnostic'" + " managed bean to perform heap dump");
    }//from  w w w  .ja v a 2 s  . c om
    File hprofFilePath = getNextHeapDumpFile();
    mBeanServer.invoke(hotSpotDiagnostic, "dumpHeap", new Object[] { hprofFilePath.getPath(), Boolean.TRUE },
            new String[] { String.class.getName(), boolean.class.getName() });
    return hprofFilePath;
}

From source file:org.josso.gateway.event.security.JMXSSOEventManagerImpl.java

/**
 * This method invokes the fireSSOEvent of the MBean registered under the "oname" name.
 *
 * @param event/*from w ww.ja  v a2 s . c o m*/
 */
public void fireSSOEvent(SSOEvent event) {
    try {
        // Invoke fireSSOEvent on MBean ...
        MBeanServer server = getMBeanServer();

        // Build a JMX Notification based on the SSOEvent.
        Notification notification = buildNotification(event);

        server.invoke(mbeanOname, "fireJMXSSOEvent", new Object[] { notification },
                new String[] { "javax.management.Notification" });

    } catch (Exception e) {
        logger.error("Can't send SSO Event : " + e.getMessage(), e);
    }
}

From source file:org.jumpmind.symmetric.SymmetricWebServer.java

protected void registerHttpJmxAdaptor(int jmxPort) throws Exception {
    if (AppUtils.isSystemPropertySet(SystemConstants.SYSPROP_JMX_HTTP_CONSOLE_ENABLED, true) && jmxEnabled) {
        log.info("Starting JMX HTTP console on port {}", jmxPort);
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = getHttpJmxAdaptorName();
        mbeanServer.createMBean(HttpAdaptor.class.getName(), name);
        if (!AppUtils.isSystemPropertySet(SystemConstants.SYSPROP_JMX_HTTP_CONSOLE_LOCALHOST_ENABLED, true)) {
            mbeanServer.setAttribute(name, new Attribute("Host", "0.0.0.0"));
        } else if (StringUtils.isNotBlank(host)) {
            mbeanServer.setAttribute(name, new Attribute("Host", host));
        }//from   w w  w.  j a  v  a2s . c om
        mbeanServer.setAttribute(name, new Attribute("Port", new Integer(jmxPort)));
        ObjectName processorName = getXslJmxAdaptorName();
        mbeanServer.createMBean(XSLTProcessor.class.getName(), processorName);
        mbeanServer.setAttribute(name, new Attribute("ProcessorName", processorName));
        mbeanServer.invoke(name, "start", null, null);
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.reload.RepositoryReloader.java

public static void flushJCAPool() throws Exception {
    MBeanServer jboss = locateJBoss();
    if (jboss != null) {
        jboss.invoke(new ObjectName("jboss.jca:name=NXRepository/default,service=ManagedConnectionPool"),
                "flush", new Object[0], new String[0]);
    } else { // try tomcat (jtajca nuxeo plugin)
        Class<?> cl = null;
        try {/* www  . j a va2  s. c  om*/
            cl = Class.forName("org.nuxeo.runtime.jtajca.NuxeoContainer");
        } catch (ClassNotFoundException e) { // not tomcat or not jtajca
                                             // enabled
                                             // do nothing
        }
        if (cl != null) {
            cl.getMethod("resetConnectionManager").invoke(null);
        }
    }
}