コード例 #1
0
 @Override
 public boolean isInstanceOf(ObjectName name, String className)
     throws InstanceNotFoundException, IOException {
   try {
     return connection.isInstanceOf(name, className);
   } catch (IOException e) {
     checkConnection();
     return connection.isInstanceOf(name, className);
   }
 }
コード例 #2
0
  /**
   * Returns a proxy for a platform MXBean interface of a given <a href="#MXBeanNames">MXBean
   * name</a> that forwards its method calls through the given <tt>MBeanServerConnection</tt>.
   *
   * <p>This method is equivalent to:
   *
   * <blockquote>
   *
   * {@link java.lang.reflect.Proxy#newProxyInstance
   * Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(), new Class[] { mxbeanInterface },
   * handler)</tt>
   *
   * </blockquote>
   *
   * where <tt>handler</tt> is an {@link java.lang.reflect.InvocationHandler InvocationHandler} to
   * which method invocations to the MXBean interface are dispatched. This <tt>handler</tt> converts
   * an input parameter from an MXBean data type to its mapped open type before forwarding to the
   * <tt>MBeanServer</tt> and converts a return value from an MXBean method call through the
   * <tt>MBeanServer</tt> from an open type to the corresponding return type declared in the MXBean
   * interface.
   *
   * <p>If the MXBean is a notification emitter (i.e., it implements {@link
   * javax.management.NotificationEmitter NotificationEmitter}), both the <tt>mxbeanInterface</tt>
   * and <tt>NotificationEmitter</tt> will be implemented by this proxy.
   *
   * <p><b>Notes:</b>
   *
   * <ol>
   *   <li>Using an MXBean proxy is a convenience remote access to a platform MXBean of a running
   *       virtual machine. All method calls to the MXBean proxy are forwarded to an
   *       <tt>MBeanServerConnection</tt> where {@link java.io.IOException IOException} may be
   *       thrown when the communication problem occurs with the connector server. An application
   *       remotely accesses the platform MXBeans using proxy should prepare to catch
   *       <tt>IOException</tt> as if accessing with the <tt>MBeanServerConnector</tt> interface.
   *   <li>When a client application is designed to remotely access MXBeans for a running virtual
   *       machine whose version is different than the version on which the application is running,
   *       it should prepare to catch {@link java.io.InvalidObjectException InvalidObjectException}
   *       which is thrown when an MXBean proxy receives a name of an enum constant which is missing
   *       in the enum class loaded in the client application.
   *   <li>{@link javax.management.MBeanServerInvocationHandler MBeanServerInvocationHandler} or its
   *       {@link javax.management.MBeanServerInvocationHandler#newProxyInstance newProxyInstance}
   *       method cannot be used to create a proxy for a platform MXBean. The proxy object created
   *       by <tt>MBeanServerInvocationHandler</tt> does not handle the properties of the platform
   *       MXBeans described in the <a href="#MXBean">class specification</a>.
   * </ol>
   *
   * @param connection the <tt>MBeanServerConnection</tt> to forward to.
   * @param mxbeanName the name of a platform MXBean within <tt>connection</tt> to forward to.
   *     <tt>mxbeanName</tt> must be in the format of {@link ObjectName ObjectName}.
   * @param mxbeanInterface the MXBean interface to be implemented by the proxy.
   * @throws IllegalArgumentException if
   *     <ul>
   *       <li><tt>mxbeanName</tt> is not with a valid {@link ObjectName ObjectName} format, or
   *       <li>the named MXBean in the <tt>connection</tt> is not a MXBean provided by the platform,
   *           or
   *       <li>the named MXBean is not registered in the <tt>MBeanServerConnection</tt>, or
   *       <li>the named MXBean is not an instance of the given <tt>mxbeanInterface</tt>
   *     </ul>
   *
   * @throws java.io.IOException if a communication problem occurred when accessing the
   *     <tt>MBeanServerConnection</tt>.
   */
  public static <T> T newPlatformMXBeanProxy(
      MBeanServerConnection connection, String mxbeanName, Class<T> mxbeanInterface)
      throws java.io.IOException {

    final Class interfaceClass = mxbeanInterface;
    // Only allow MXBean interfaces from rt.jar loaded by the
    // bootstrap class loader
    final ClassLoader loader =
        AccessController.doPrivileged(
            new PrivilegedAction<ClassLoader>() {
              public ClassLoader run() {
                return interfaceClass.getClassLoader();
              }
            });
    if (loader != null) {
      throw new IllegalArgumentException(mxbeanName + " is not a platform MXBean");
    }

    try {
      final ObjectName objName = new ObjectName(mxbeanName);
      if (!connection.isInstanceOf(objName, interfaceClass.getName())) {
        throw new IllegalArgumentException(mxbeanName + " is not an instance of " + interfaceClass);
      }

      final Class[] interfaces;
      // check if the registered MBean is a notification emitter
      boolean emitter = connection.isInstanceOf(objName, NOTIF_EMITTER);

      // create an MXBean proxy
      return JMX.newMXBeanProxy(connection, objName, mxbeanInterface, emitter);
    } catch (InstanceNotFoundException e) {
      final IllegalArgumentException iae =
          new IllegalArgumentException(mxbeanName + " not found in the connection.");
      iae.initCause(e);
      throw iae;
    } catch (MalformedObjectNameException e) {
      final IllegalArgumentException iae =
          new IllegalArgumentException(mxbeanName + " is not a valid ObjectName format.");
      iae.initCause(e);
      throw iae;
    }
  }
コード例 #3
0
ファイル: JMXInvokerImpl.java プロジェクト: AndersWelen/Buzz
  public boolean isMBeanStarted(String bean) {
    MBeanServerConnection server = getMBeanServerConnection();

    boolean debug = LOG.isDebugEnabled();

    try {
      // If not a JBoss life-cycle extended MBean, It's "Started"!
      if (!server.isInstanceOf(new ObjectName(bean), "org.jboss.system.ServiceMBean")) {
        if (debug) {
          LOG.debug("MBean: " + bean + " is not an org.jboss.system.ServiceMBean. Returning true");
        }
        return true;
      }
    } catch (InstanceNotFoundException e) {
      throw new RuntimeException("Failed to check MBean: " + bean, e);
    } catch (MalformedObjectNameException e) {
      throw new RuntimeException("Failed to check MBean: " + bean, e);
    } catch (NullPointerException e) {
      throw new RuntimeException("Failed to check MBean: " + bean, e);
    } catch (IOException e) {
      throw new RuntimeException("Failed to check MBean: " + bean, e);
    }

    // Check state
    try {
      Integer state = (Integer) getAttributeValue(bean, "State");
      if (state == ServiceMBean.STARTED) {
        if (debug) {
          LOG.debug("MBean: " + bean + " is started. Returning true");
        }
        return true;
      }
    } catch (RuntimeException e) {
      // State attribute not present. Assuming it started
      if (e.getCause() instanceof AttributeNotFoundException) {
        if (debug) {
          LOG.debug("MBean: " + bean + " is missing the attribute. Returning true");
        }
        return true;
      }
      throw e;
    }

    if (debug) {
      LOG.debug("MBean: " + bean + " is not started. Returning false");
    }
    return false;
  }