Search in sources :

Example 6 with QueryExp

use of javax.management.QueryExp in project geode by apache.

the class LocalProcessController method buildQueryExp.

/**
   * Builds the QueryExp used to identify the target MBean.
   * 
   * @param pidAttribute the name of the MBean attribute with the process id to compare against
   * @param attributes the names of additional MBean attributes to compare with expected values
   * @param values the expected values of the specified MBean attributes
   *
   * @return the main QueryExp for matching the target MBean
   */
private QueryExp buildQueryExp(final String pidAttribute, final String[] attributes, final Object[] values) {
    final QueryExp optionalAttributes = buildOptionalQueryExp(attributes, values);
    QueryExp constraint;
    if (optionalAttributes != null) {
        constraint = Query.and(optionalAttributes, Query.eq(Query.attr(pidAttribute), Query.value(this.pid)));
    } else {
        constraint = Query.eq(Query.attr(pidAttribute), Query.value(this.pid));
    }
    return constraint;
}
Also used : QueryExp(javax.management.QueryExp)

Example 7 with QueryExp

use of javax.management.QueryExp in project geode by apache.

the class LocalProcessController method invokeOperationOnTargetMBean.

/**
   * Connects to the process and use its MBean to stop it.
   * 
   * @param namePattern the name pattern of the MBean to use for stopping
   * @param pidAttribute the name of the MBean attribute with the process id to compare against
   * @param methodName the name of the MBean operation to invoke
   * @param attributes the names of the MBean attributes to compare with expected values
   * @param values the expected values of the specified MBean attributes
   *
   * @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
   *         in the process
   * @throws IOException if a communication problem occurred when talking to the MBean server
   * @throws MBeanInvocationFailedException if failed to invoke stop on the MBean for any reason
   * @throws PidUnavailableException if parsing the pid from the RuntimeMXBean name fails
   */
private Object invokeOperationOnTargetMBean(final ObjectName namePattern, final String pidAttribute, final String methodName, final String[] attributes, final Object[] values) throws ConnectionFailedException, IOException, MBeanInvocationFailedException, PidUnavailableException {
    ObjectName objectName = namePattern;
    connect();
    try {
        final QueryExp constraint = buildQueryExp(pidAttribute, attributes, values);
        final Set<ObjectName> mbeanNames = this.server.queryNames(namePattern, constraint);
        if (mbeanNames.isEmpty()) {
            throw new MBeanInvocationFailedException("Failed to find mbean matching '" + namePattern + "' with attribute '" + pidAttribute + "' of value '" + this.pid + "'");
        }
        if (mbeanNames.size() > 1) {
            throw new MBeanInvocationFailedException("Found more than one mbean matching '" + namePattern + "' with attribute '" + pidAttribute + "' of value '" + this.pid + "'");
        }
        objectName = mbeanNames.iterator().next();
        return invoke(objectName, methodName);
    } catch (InstanceNotFoundException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } catch (MBeanException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } catch (ReflectionException e) {
        throw new MBeanInvocationFailedException("Failed to invoke " + methodName + " on " + objectName, e);
    } finally {
        disconnect();
    }
}
Also used : ReflectionException(javax.management.ReflectionException) QueryExp(javax.management.QueryExp) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName)

Example 8 with QueryExp

use of javax.management.QueryExp in project geode by apache.

the class AbstractCommandsController method getMemberMXBean.

/**
   * Gets the MemberMXBean from the JVM Platform MBeanServer for the specified member, identified by
   * name or ID in the GemFire cluster.
   * 
   * @param memberNameId a String indicating the name or ID of the GemFire member.
   * @return a proxy to the GemFire member's MemberMXBean.
   * @throws IllegalStateException if no MemberMXBean could be found for GemFire member with ID or
   *         name.
   * @throws RuntimeException wrapping the MalformedObjectNameException if the ObjectName pattern is
   *         malformed.
   * @see #getMBeanServer()
   * @see #isMemberMXBeanFound(java.util.Collection)
   * @see javax.management.ObjectName
   * @see javax.management.QueryExp
   * @see javax.management.MBeanServer#queryNames(javax.management.ObjectName,
   *      javax.management.QueryExp)
   * @see javax.management.JMX#newMXBeanProxy(javax.management.MBeanServerConnection,
   *      javax.management.ObjectName, Class)
   * @see org.apache.geode.management.MemberMXBean
   */
protected MemberMXBean getMemberMXBean(final String memberNameId) {
    try {
        final MBeanServer connection = getMBeanServer();
        final String objectNamePattern = ManagementConstants.OBJECTNAME__PREFIX.concat("type=Member,*");
        // NOTE throws a MalformedObjectNameException, but this should not happen since we constructed
        // the ObjectName above
        final ObjectName objectName = ObjectName.getInstance(objectNamePattern);
        final QueryExp query = Query.or(Query.eq(Query.attr("Name"), Query.value(memberNameId)), Query.eq(Query.attr("Id"), Query.value(memberNameId)));
        final Set<ObjectName> objectNames = connection.queryNames(objectName, query);
        assertState(isMemberMXBeanFound(objectNames), "No MemberMXBean with ObjectName (%1$s) based on Query (%2$s) was found in the Platform MBeanServer for member (%3$s)!", objectName, query, memberNameId);
        return JMX.newMXBeanProxy(connection, objectNames.iterator().next(), MemberMXBean.class);
    } catch (MalformedObjectNameException e) {
        throw new RuntimeException(e);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) QueryExp(javax.management.QueryExp) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 9 with QueryExp

use of javax.management.QueryExp in project geode by apache.

the class LocalProcessControllerJUnitTest method testProcessMBean.

@Test
public void testProcessMBean() throws Exception {
    // validate basics of the ProcessMBean
    Set<ObjectName> mbeanNames = this.server.queryNames(objectName, null);
    assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
    assertEquals(1, mbeanNames.size());
    final ObjectName name = mbeanNames.iterator().next();
    final MBeanInfo info = this.server.getMBeanInfo(name);
    final MBeanOperationInfo[] operInfo = info.getOperations();
    assertEquals(1, operInfo.length);
    assertEquals("stop", operInfo[0].getName());
    final MBeanAttributeInfo[] attrInfo = info.getAttributes();
    assertEquals(2, attrInfo.length);
    // The order of these attributes is indeterminate
    assertTrue("Pid".equals(attrInfo[0].getName()) || "Process".equals(attrInfo[0].getName()));
    assertTrue("Pid".equals(attrInfo[1].getName()) || "Process".equals(attrInfo[1].getName()));
    assertNotNull(this.server.getAttribute(name, "Pid"));
    assertNotNull(this.server.getAttribute(name, "Process"));
    assertEquals(pid, this.server.getAttribute(name, "Pid"));
    assertEquals(true, this.server.getAttribute(name, "Process"));
    // validate query using only Pid attribute
    QueryExp constraint = Query.eq(Query.attr("Pid"), Query.value(pid));
    mbeanNames = this.server.queryNames(objectName, constraint);
    assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
    // validate query with wrong Pid finds nothing
    constraint = Query.eq(Query.attr("Pid"), Query.value(pid + 1));
    mbeanNames = this.server.queryNames(objectName, constraint);
    assertTrue("Found matching mbeans", mbeanNames.isEmpty());
    // validate query using both attributes
    constraint = Query.and(Query.eq(Query.attr("Process"), Query.value(true)), Query.eq(Query.attr("Pid"), Query.value(pid)));
    mbeanNames = this.server.queryNames(objectName, constraint);
    assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
    // validate query with wrong attribute finds nothing
    constraint = Query.and(Query.eq(Query.attr("Process"), Query.value(false)), Query.eq(Query.attr("Pid"), Query.value(pid)));
    mbeanNames = this.server.queryNames(objectName, constraint);
    assertTrue("Found matching mbeans", mbeanNames.isEmpty());
}
Also used : MBeanInfo(javax.management.MBeanInfo) QueryExp(javax.management.QueryExp) MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 10 with QueryExp

use of javax.management.QueryExp in project geode by apache.

the class QueryNamesOverHttpDUnitTest method testQueryNameOverHttp.

@Test
public void testQueryNameOverHttp() throws Exception {
    LinkIndex links = new LinkIndex();
    links.add(new Link("mbean-query", new URI("http://localhost:" + locatorRule.getHttpPort() + "/gemfire/v1/mbean/query"), HttpMethod.POST));
    RestHttpOperationInvoker invoker = new RestHttpOperationInvoker(links, mock(Gfsh.class), new HashMap<>());
    ObjectName objectName = ObjectName.getInstance("GemFire:type=Member,*");
    QueryExp query = Query.eq(Query.attr("Name"), Query.value("mock"));
    Set<ObjectName> names = invoker.queryNames(objectName, query);
    assertTrue(names.isEmpty());
}
Also used : LinkIndex(org.apache.geode.management.internal.web.domain.LinkIndex) RestHttpOperationInvoker(org.apache.geode.management.internal.web.shell.RestHttpOperationInvoker) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) QueryExp(javax.management.QueryExp) URI(java.net.URI) Link(org.apache.geode.management.internal.web.domain.Link) ObjectName(javax.management.ObjectName) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

QueryExp (javax.management.QueryExp)21 ObjectName (javax.management.ObjectName)18 MBeanServer (javax.management.MBeanServer)7 Test (org.junit.Test)5 AttributeValueExp (javax.management.AttributeValueExp)3 StringValueExp (javax.management.StringValueExp)3 UnitTest (org.apache.geode.test.junit.categories.UnitTest)3 InstanceNotFoundException (javax.management.InstanceNotFoundException)2 MBeanException (javax.management.MBeanException)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 ObjectInstance (javax.management.ObjectInstance)2 ReflectionException (javax.management.ReflectionException)2 JMXConnector (javax.management.remote.JMXConnector)2 JMXServiceURL (javax.management.remote.JMXServiceURL)2 JmxReporter (com.codahale.metrics.JmxReporter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 URI (java.net.URI)1