/**
   * Create MBean for Object. Attempts to create an MBean for the object by searching the package
   * and class name space. For example an object of the type
   *
   * <PRE>
   *   class com.acme.MyClass extends com.acme.util.BaseClass
   * </PRE>
   *
   * Then this method would look for the following classes:
   *
   * <UL>
   *   <LI>com.acme.MyClassMBean
   *   <LI>com.acme.jmx.MyClassMBean
   *   <LI>com.acme.util.BaseClassMBean
   *   <LI>com.acme.util.jmx.BaseClassMBean
   * </UL>
   *
   * @param o The object
   * @return A new instance of an MBean for the object or null.
   */
  public static ModelMBean mbeanFor(Object o) {
    try {
      Class oClass = o.getClass();
      ClassLoader loader = oClass.getClassLoader();

      ModelMBean mbean = null;
      boolean jmx = false;
      Class[] interfaces = null;
      int i = 0;

      while (mbean == null && oClass != null) {
        Class focus = interfaces == null ? oClass : interfaces[i];
        String pName = focus.getPackage().getName();
        String cName = focus.getName().substring(pName.length() + 1);
        String mName = pName + (jmx ? ".jmx." : ".") + cName + "MBean";

        try {
          Class mClass = loader.loadClass(mName);
          if (log.isTraceEnabled()) log.trace("mbeanFor " + o + " mClass=" + mClass);
          mbean = (ModelMBean) mClass.newInstance();
          mbean.setManagedResource(o, "objectReference");
          if (log.isDebugEnabled()) log.debug("mbeanFor " + o + " is " + mbean);
          return mbean;
        } catch (ClassNotFoundException e) {
          if (e.toString().endsWith("MBean")) {
            if (log.isTraceEnabled()) log.trace(e.toString());
          } else log.warn(LogSupport.EXCEPTION, e);
        } catch (Error e) {
          log.warn(LogSupport.EXCEPTION, e);
          mbean = null;
        } catch (Exception e) {
          log.warn(LogSupport.EXCEPTION, e);
          mbean = null;
        }

        if (jmx) {
          if (interfaces != null) {
            i++;
            if (i >= interfaces.length) {
              interfaces = null;
              oClass = oClass.getSuperclass();
            }
          } else {
            interfaces = oClass.getInterfaces();
            i = 0;
            if (interfaces == null || interfaces.length == 0) {
              interfaces = null;
              oClass = oClass.getSuperclass();
            }
          }
        }
        jmx = !jmx;
      }
    } catch (Exception e) {
      LogSupport.ignore(log, e);
    }
    return null;
  }
Exemple #2
0
  /**
   * Create MBean for Object. Attempts to create an MBean for the object by searching the package
   * and class name space. For example an object of the type
   *
   * <PRE>
   * class com.acme.MyClass extends com.acme.util.BaseClass implements com.acme.Iface
   * </PRE>
   *
   * Then this method would look for the following classes:
   *
   * <UL>
   *   <LI>com.acme.jmx.MyClassMBean
   *   <LI>com.acme.util.jmx.BaseClassMBean
   *   <LI>org.eclipse.jetty.jmx.ObjectMBean
   * </UL>
   *
   * @param o The object
   * @return A new instance of an MBean for the object or null.
   */
  public static Object mbeanFor(Object o) {
    try {
      Class oClass = o.getClass();
      Object mbean = null;

      while (mbean == null && oClass != null) {
        String pName = oClass.getPackage().getName();
        String cName = oClass.getName().substring(pName.length() + 1);
        String mName = pName + ".jmx." + cName + "MBean";

        try {
          Class mClass =
              (Object.class.equals(oClass))
                  ? oClass = ObjectMBean.class
                  : Loader.loadClass(oClass, mName, true);
          if (LOG.isDebugEnabled()) LOG.debug("mbeanFor " + o + " mClass=" + mClass);

          try {
            Constructor constructor = mClass.getConstructor(OBJ_ARG);
            mbean = constructor.newInstance(new Object[] {o});
          } catch (Exception e) {
            LOG.ignore(e);
            if (ModelMBean.class.isAssignableFrom(mClass)) {
              mbean = mClass.newInstance();
              ((ModelMBean) mbean).setManagedResource(o, "objectReference");
            }
          }

          if (LOG.isDebugEnabled()) LOG.debug("mbeanFor " + o + " is " + mbean);
          return mbean;
        } catch (ClassNotFoundException e) {
          // The code below was modified to fix bugs 332200 and JETTY-1416
          // The issue was caused by additional information added to the
          // message after the class name when running in Apache Felix,
          // as well as before the class name when running in JBoss.
          if (e.getMessage().contains(mName)) LOG.ignore(e);
          else LOG.warn(e);
        } catch (Error e) {
          LOG.warn(e);
          mbean = null;
        } catch (Exception e) {
          LOG.warn(e);
          mbean = null;
        }

        oClass = oClass.getSuperclass();
      }
    } catch (Exception e) {
      LOG.ignore(e);
    }
    return null;
  }
Exemple #3
0
  public static void main(String[] args) throws Exception {

    boolean ok = true;

    MBeanServer mbs = MBeanServerFactory.createMBeanServer();

    // ModelMBeanAttributeInfo

    Descriptor somethingAttributeDescriptor =
        new DescriptorSupport(
            new String[] {"name=Something", "descriptorType=attribute", "getMethod=getSomething"});
    ModelMBeanAttributeInfo somethingAttributeInfo =
        new ModelMBeanAttributeInfo(
            "Something",
            "java.lang.String",
            "Something attribute",
            true,
            true,
            false,
            somethingAttributeDescriptor);

    Descriptor somethingCachedAttributeDescriptor =
        new DescriptorSupport(
            new String[] {
              "name=SomethingCached",
              "descriptorType=attribute",
              "getMethod=getSomethingCached",
              "currencyTimeLimit=5000"
            });
    ModelMBeanAttributeInfo somethingCachedAttributeInfo =
        new ModelMBeanAttributeInfo(
            "SomethingCached",
            "java.lang.String",
            "Something cached attribute",
            true,
            true,
            false,
            somethingCachedAttributeDescriptor);
    // ModelMBeanInfo

    ModelMBeanInfo mmbi =
        new ModelMBeanInfoSupport(
            Resource.class.getName(),
            "Resource MBean",
            new ModelMBeanAttributeInfo[] {somethingAttributeInfo, somethingCachedAttributeInfo},
            null,
            new ModelMBeanOperationInfo[] {},
            null);

    // RequiredModelMBean

    ModelMBean mmb = new RequiredModelMBean(mmbi);
    mmb.setManagedResource(resource, "ObjectReference");
    ObjectName mmbName = new ObjectName(":type=ResourceMBean");
    mbs.registerMBean(mmb, mmbName);

    // Run tests

    System.out.println("\nTest that we receive ServiceNotFoundException");
    try {
      Attribute attr = new Attribute("Something", "Some string");
      mbs.setAttribute(mmbName, attr);
      System.out.println("TEST FAILED: Didn't caught exception");
      ok = false;
    } catch (MBeanException mbex) {
      Exception e = mbex.getTargetException();
      if (e == null || !(e instanceof ServiceNotFoundException)) {
        System.out.println("TEST FAILED: Caught wrong exception:" + e);
        ok = false;
      } else System.out.println("Received expected ServiceNotFoundException");

    } catch (Exception e) {
      System.out.println("TEST FAILED: Caught wrong exception: " + e);
      e.printStackTrace(System.out);
      ok = false;
    }

    // Now check that when caching is enabled, setAttribute is working
    System.out.println("\nTest that we are not receiving ServiceNotFoundException");
    try {
      Attribute attr = new Attribute("SomethingCached", "Some string");
      mbs.setAttribute(mmbName, attr);
      System.out.println("No exception thrown");
    } catch (Exception e) {
      System.out.println("TEST FAILED: Caught an exception: " + e);
      e.printStackTrace(System.out);
      ok = false;
    }

    if (ok) System.out.println("Test passed");
    else {
      System.out.println("TEST FAILED");
      throw new Exception("TEST FAILED");
    }
  }