JDK-5108792 : Add abstract base class to make coding DynamicMBeans easier
  • Type: Enhancement
  • Component: core-svc
  • Sub-Component: javax.management
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2004-09-29
  • Updated: 2008-09-17
  • Resolved: 2008-09-17
Related Reports
Relates :  
Description
Coding a DynamicMBean is harder than it needs to be.  Constructing the MBeanInfo completely by hand is tedious and error-prone.  The getAttributes and setAttributes methods have to be written explicitly, even if each of them just consists of calling getAttribute or setAttribute n times.  The getAttribute and setAttribute methods frequently consist of a set of lines like this:

  if (attrName.equals("foo"))
    return getFoo();
  else if (attrName.equals("bar"))
    return getBar();
  ...
  else
    throw new AttributeNotFoundException(attrName);

There should be an easier way.

The AbstractDynamicMBean class defined by the MX4J project would be a good starting point.
http://cvs.sourceforge.net/viewcvs.py/mx4j/mx4j/src/core/mx4j/AbstractDynamicMBean.java?view=markup

Comments
EVALUATION In practice it appears that most cases where this might be useful are better addressed by javax.management.StandardMBean. Although if you do need to code a DynamicMBean, you often end up coding getAttributes in terms of getAttribute and setAttributes in terms of setAttribute, people don't often code DynamicMBeans so an abstract class would probably not pull its weight.
17-09-2008

WORK AROUND If you know the set of attributes and operations that you want in your dynamic MBean at compile time, you can often just put them in an MBean interface and subclass javax.management.StandardMBean to override getAttribute(String) etc with the extra functionality you need.
09-12-2005

EVALUATION The hardest part here is getting the specification right, striking a balance between simplicity and ease of use on the one hand, and expressive power on the other. Functionality in the base class should be justified by realistic examples of when it is useful. ###@###.### 2004-09-29
29-09-2004