Example #1
0
 public Object getProperty(String property) {
   try {
     return server.getAttribute(name, property);
   } catch (MBeanException e) {
     throwExceptionWithTarget("Could not access property: " + property + ". Reason: ", e);
   } catch (Exception e) {
     if (!ignoreErrors) throwException("Could not access property: " + property + ". Reason: ", e);
   }
   return null;
 }
Example #2
0
 private Object getAttribute(ObjectName objName, String attrName)
     throws MBeanException, InstanceNotFoundException, AttributeNotFoundException,
         ReflectionException, IOException {
   final NameValueMap values = getCachedAttributes(objName, Collections.singleton(attrName));
   Object value = values.get(attrName);
   if (value != null || values.containsKey(attrName)) {
     return value;
   }
   // Not in cache, presumably because it was omitted from the
   // getAttributes result because of an exception.  Following
   // call will probably provoke the same exception.
   return conn.getAttribute(objName, attrName);
 }
  /**
   * @param mbeanServer
   * @param objectName
   * @param attributeName
   * @param attributeValue if <code>null</code>, this is a read access.
   * @return if this is a read access, the returned value (may be null); if it is a write access,
   *     return <code>void.class</code>.
   * @throws IOException
   * @throws JMException
   */
  public Result invokeAttribute(
      @Nonnull MBeanServerConnection mbeanServer,
      @Nonnull ObjectName objectName,
      @Nonnull String attributeName,
      @Nullable String attributeValue)
      throws IOException, JMException {
    MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(objectName);
    MBeanAttributeInfo attributeInfo = null;
    for (MBeanAttributeInfo mai : mbeanInfo.getAttributes()) {
      if (mai.getName().equals(attributeName)) {
        attributeInfo = mai;
        break;
      }
    }
    if (attributeInfo == null) {
      throw new IllegalArgumentException(
          "No attribute '"
              + attributeName
              + "' found on '"
              + objectName
              + "'. Existing attributes: "
              + Arrays.asList(mbeanInfo.getAttributes()));
    }

    String description;
    Object resultValue;

    if (attributeValue == null) {
      if (attributeInfo.isReadable()) {
        Object attribute = mbeanServer.getAttribute(objectName, attributeName);
        logger.info("get attribute value {}:{}:{}", objectName, attributeName, attribute);
        resultValue = attribute;
        description =
            "Get attribute value " + objectName + ":" + attributeName + ": " + resultValue;
      } else {
        throw new IllegalArgumentException(
            "Attribute '"
                + attributeName
                + "' is not readable on '"
                + objectName
                + "': "
                + attributeInfo);
      }
    } else {
      if (attributeInfo.isWritable()) {
        Object value = convertValue(attributeValue, attributeInfo.getType());
        mbeanServer.setAttribute(objectName, new Attribute(attributeName, value));
        logger.info("set attribute value {}:{}:{}", objectName, attributeName, value);

        description = "Set attribute value " + objectName + ":" + attributeName + ": " + value;
        resultValue = void.class;
      } else {
        throw new IllegalArgumentException(
            "Attribute '"
                + attributeName
                + "' is not writable on '"
                + objectName
                + "': "
                + attributeInfo);
      }
    }

    return new Result(objectName, resultValue, description);
  }
Example #4
0
  private static boolean test(String proto, MBeanServer mbs, ObjectName on) throws Exception {
    System.out.println("Testing for protocol " + proto);

    JMXConnectorServer cs;
    JMXServiceURL url = new JMXServiceURL(proto, null, 0);
    try {
      cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    } catch (MalformedURLException e) {
      System.out.println("System does not recognize URL: " + url + "; ignoring");
      return true;
    }
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector client = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = client.getMBeanServerConnection();
    Object getAttributeExotic = mbsc.getAttribute(on, "Exotic");
    AttributeList getAttrs = mbsc.getAttributes(on, new String[] {"Exotic"});
    AttributeList setAttrs = new AttributeList();
    setAttrs.add(new Attribute("Exotic", new Exotic()));
    setAttrs = mbsc.setAttributes(on, setAttrs);
    Object invokeExotic = mbsc.invoke(on, "anExotic", new Object[] {}, new String[] {});
    MBeanInfo exoticMBI = mbsc.getMBeanInfo(on);

    mbsc.setAttribute(on, new Attribute("Exception", Boolean.TRUE));
    Exception getAttributeException, setAttributeException, invokeException;
    try {
      try {
        mbsc.getAttribute(on, "Exotic");
        throw noException("getAttribute");
      } catch (Exception e) {
        getAttributeException = e;
      }
      try {
        mbsc.setAttribute(on, new Attribute("Exotic", new Exotic()));
        throw noException("setAttribute");
      } catch (Exception e) {
        setAttributeException = e;
      }
      try {
        mbsc.invoke(on, "anExotic", new Object[] {}, new String[] {});
        throw noException("invoke");
      } catch (Exception e) {
        invokeException = e;
      }
    } finally {
      mbsc.setAttribute(on, new Attribute("Exception", Boolean.FALSE));
    }
    client.close();
    cs.stop();

    boolean ok = true;

    ok &= checkAttrs("getAttributes", getAttrs);
    ok &= checkAttrs("setAttributes", setAttrs);

    ok &= checkType("getAttribute", getAttributeExotic, Exotic.class);
    ok &= checkType("getAttributes", attrValue(getAttrs), Exotic.class);
    ok &= checkType("setAttributes", attrValue(setAttrs), Exotic.class);
    ok &= checkType("invoke", invokeExotic, Exotic.class);
    ok &= checkType("getMBeanInfo", exoticMBI, ExoticMBeanInfo.class);

    ok &= checkExceptionType("getAttribute", getAttributeException, ExoticException.class);
    ok &= checkExceptionType("setAttribute", setAttributeException, ExoticException.class);
    ok &= checkExceptionType("invoke", invokeException, ExoticException.class);

    if (ok) System.out.println("Test passes for protocol " + proto);
    return ok;
  }