Example usage for javax.management StringValueExp StringValueExp

List of usage examples for javax.management StringValueExp StringValueExp

Introduction

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

Prototype

public StringValueExp(String val) 

Source Link

Document

Creates a new StringValueExp representing the given string.

Usage

From source file:org.onehippo.forge.camel.util.CamelContextUtils.java

/**
 * Finds the {@code ManagedCamelContextMBean} having {@code camelContextId} from the platform MBean server and invokes the {@code operation}
 * with {@code params} of which signature is like {@code signature} on the found {@code ManagedCamelContext} MBean.
 * If a blank {@code camelContextId} provided, then it invokes the operation on the first found {@code ManagedCamelContextMBean} MBean.
 * <p>Example code:</p>// w w  w  .j a  va 2s.  c  o m
 * <pre>
 * CamelContextUtils.invokeManagedCamelContextMBean("camel-1", "sendBody", new Object [] { "direct:test", body }, new String {} { String.class.getName(), Object.class.getName() });;
 * </pre>
 * @param camelContextId camel context ID
 * @param operation operation name
 * @param params parameters array
 * @param signature signature of parameters
 * @return operation return
 */
public static Object invokeManagedCamelContextMBean(final String camelContextId, final String operation,
        Object[] params, String[] signature) {
    Object ret = null;

    try {
        QueryExp expr = Query.isInstanceOf(new StringValueExp(CAMEL_CONTEXT_MBEAN_INSTANCE_TYPE));
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();

        ObjectName objectName;
        if (StringUtils.isNotBlank(camelContextId)) {
            objectName = new ObjectName("org.apache.camel:name=" + camelContextId);
        } else {
            objectName = new ObjectName("org.apache.camel:*");
        }

        Set<ObjectName> objectNames = mbeanServer.queryNames(objectName, expr);

        if (!objectNames.isEmpty()) {
            final ObjectName firstObjectName = objectNames.iterator().next();
            ret = mbeanServer.invoke(firstObjectName, operation, params, signature);
        } else {
            throw new RuntimeException("No CamelContext found by name: " + objectName);
        }
    } catch (MalformedObjectNameException e) {
        throw new RuntimeException("MalformedObjectNameException: " + e, e);
    } catch (InstanceNotFoundException e) {
        throw new RuntimeException("InstanceNotFoundException: " + e, e);
    } catch (ReflectionException e) {
        throw new RuntimeException("ReflectionException: " + e, e);
    } catch (MBeanException e) {
        throw new RuntimeException("MBeanException: " + e, e);
    }

    return ret;
}