mx4j/src/core/javax/management/modelmbean RequiredModelMBean.java,1.20,1.21

Jeremy Boynes <[email protected]> Sun, 13 Feb 2005 16:36:18 +0000
Newsgroups gmane.comp.java.mx4j.cvs
Message-ID <[email protected]>
Update of /cvsroot/mx4j/mx4j/src/core/javax/management/modelmbean
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26074/src/core/javax/management/modelmbean

Modified Files:
	RequiredModelMBean.java 
Log Message:
Fix for [ 1121844 ] RequiredModelMBean must list default notifications
Fix for [ 1121843 ] RequiredModelMBean should have one notification broadcaster
Also fixes problems with argument checking and ensures correct Exception is thrown

Index: RequiredModelMBean.java
===================================================================
RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/modelmbean/RequiredModelMBean.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** RequiredModelMBean.java	9 Feb 2005 07:02:39 -0000	1.20
--- RequiredModelMBean.java	13 Feb 2005 16:36:15 -0000	1.21
***************
*** 27,31 ****
  import javax.management.MBeanNotificationInfo;
  import javax.management.MBeanRegistration;
- import javax.management.MBeanRegistrationException;
  import javax.management.MBeanServer;
  import javax.management.MalformedObjectNameException;
--- 27,30 ----
***************
*** 58,61 ****
--- 57,81 ----
  {
     private static final String OBJECT_RESOURCE_TYPE = "ObjectReference";
+    private static final ModelMBeanNotificationInfo[] DEFAULT_NOTIFICATIONS;
+    private static final ModelMBeanInfoSupport DEFAULT_INFO;
+    static
+    {
+       String[] fields = new String[]{
+          "name=GENERIC",
+          "descriptorType=notification",
+          "log=T",
+          "severity=6",
+          "displayName=jmx.modelmbean.generic"};
+       ModelMBeanNotificationInfo genericInfo = new ModelMBeanNotificationInfo(new String[]{"jmx.modelmbean.generic"}, "GENERIC", "", new DescriptorSupport(fields));
+       fields = new String[]{
+          "name=ATTRIBUTE_CHANGE",
+          "descriptorType=notification",
+          "log=T",
+          "severity=6",
+          "displayName=jmx.attribute.change"};
+       ModelMBeanNotificationInfo attributeChangeInfo = new ModelMBeanNotificationInfo(new String[]{"jmx.attribute.change"}, "ATTRIBUTE_CHANGE", "", new DescriptorSupport(fields));
+       DEFAULT_NOTIFICATIONS = new ModelMBeanNotificationInfo[]{genericInfo, attributeChangeInfo};
+       DEFAULT_INFO = new ModelMBeanInfoSupport(RequiredModelMBean.class.getName(), null, null, null, null, DEFAULT_NOTIFICATIONS);
+    }
  
     private static final int ALWAYS_STALE = 1;
***************
*** 71,90 ****
     private MBeanServer m_mbeanServer;
     private Object m_managedResource;
-    private boolean m_canBeRegistered;
     private ModelMBeanInfo m_modelMBeanInfo;
-    private NotificationBroadcasterSupport m_attributeChangeBroadcaster = new NotificationBroadcasterSupport();
     private NotificationBroadcasterSupport m_generalBroadcaster = new NotificationBroadcasterSupport();
  
     public RequiredModelMBean() throws MBeanException, RuntimeOperationsException
     {
!       try
!       {
!          load();
!       }
!       catch (Exception x)
!       {
!          Logger logger = getLogger();
!          logger.warn("Cannot restore previously saved status", x);
!       }
     }
  
--- 91,100 ----
     private MBeanServer m_mbeanServer;
     private Object m_managedResource;
     private ModelMBeanInfo m_modelMBeanInfo;
     private NotificationBroadcasterSupport m_generalBroadcaster = new NotificationBroadcasterSupport();
  
     public RequiredModelMBean() throws MBeanException, RuntimeOperationsException
     {
!       m_modelMBeanInfo = DEFAULT_INFO;
     }
  
***************
*** 104,116 ****
     public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
     {
!       if (m_canBeRegistered)
!       {
!          m_mbeanServer = server;
!          return name;
!       }
!       else
!       {
!          throw new MBeanRegistrationException(new IllegalStateException("ModelMBean cannot be registered until setModelMBeanInfo has been called"));
!       }
     }
  
--- 114,120 ----
     public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
     {
!       if (name == null) throw new IllegalArgumentException("ObjectName is null");
!       m_mbeanServer = server;
!       return name;
     }
  
***************
*** 137,149 ****
     public void setModelMBeanInfo(ModelMBeanInfo modelMBeanInfo) throws MBeanException, RuntimeOperationsException
     {
        if (modelMBeanInfo == null) throw new RuntimeOperationsException(new IllegalArgumentException("ModelMBeanInfo cannot be null"));
        if (!isModelMBeanInfoValid(modelMBeanInfo)) throw new RuntimeOperationsException(new IllegalArgumentException("ModelMBeanInfo is invalid"));
  
!       m_modelMBeanInfo = (ModelMBeanInfo)modelMBeanInfo.clone();
  
        Logger logger = getLogger();
        if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("ModelMBeanInfo successfully set to: " + m_modelMBeanInfo);
-       // Only now the MBean can be registered in the MBeanServer
-       m_canBeRegistered = true;
     }
  
--- 141,153 ----
     public void setModelMBeanInfo(ModelMBeanInfo modelMBeanInfo) throws MBeanException, RuntimeOperationsException
     {
+       if (m_mbeanServer != null) throw new RuntimeOperationsException(new IllegalStateException("setModelMBeanInfo cannot be called when ModelMBean is registered"));
        if (modelMBeanInfo == null) throw new RuntimeOperationsException(new IllegalArgumentException("ModelMBeanInfo cannot be null"));
        if (!isModelMBeanInfoValid(modelMBeanInfo)) throw new RuntimeOperationsException(new IllegalArgumentException("ModelMBeanInfo is invalid"));
  
!       // todo add in default notifications
!       m_modelMBeanInfo = new ModelMBeanInfoSupport(modelMBeanInfo);
  
        Logger logger = getLogger();
        if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("ModelMBeanInfo successfully set to: " + m_modelMBeanInfo);
     }
  
***************
*** 157,161 ****
     public void setManagedResource(Object resource, String resourceType) throws MBeanException, RuntimeOperationsException, InstanceNotFoundException, InvalidTargetObjectTypeException
     {
-       if (resource == null) throw new RuntimeOperationsException(new IllegalArgumentException("Managed resource cannot be null"));
        if (!isResourceTypeSupported(resourceType)) throw new InvalidTargetObjectTypeException(resourceType);
  
--- 161,164 ----
***************
*** 183,190 ****
     public void addAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, IllegalArgumentException
     {
!       if (listener == null) throw new RuntimeOperationsException(new IllegalArgumentException("Listener cannot be null"));
        AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
        if (attributeName != null)
        {
           filter.enableAttribute(attributeName);
        }
--- 186,194 ----
     public void addAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, IllegalArgumentException
     {
!       if (listener == null) throw new IllegalArgumentException("Listener cannot be null");
        AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
        if (attributeName != null)
        {
+          if (m_modelMBeanInfo.getAttribute(attributeName) == null) throw new RuntimeOperationsException(new IllegalArgumentException("No attribute named " + attributeName));
           filter.enableAttribute(attributeName);
        }
***************
*** 207,210 ****
--- 211,215 ----
     public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws IllegalArgumentException
     {
+       if (listener == null) throw new IllegalArgumentException("listener cannot be null");
        m_generalBroadcaster.addNotificationListener(listener, filter, handback);
     }
***************
*** 212,220 ****
     public MBeanNotificationInfo[] getNotificationInfo()
     {
!       return m_modelMBeanInfo.getNotifications();
     }
  
     public void removeAttributeChangeNotificationListener(NotificationListener listener, String attributeName) throws MBeanException, RuntimeOperationsException, ListenerNotFoundException
     {
        removeAttributeChangeNotificationListener(listener, attributeName, null);
     }
--- 217,252 ----
     public MBeanNotificationInfo[] getNotificationInfo()
     {
!       MBeanNotificationInfo[] notifications = m_modelMBeanInfo.getNotifications();
!       // todo we should add these in setModelInfo()
!       boolean hasGeneric = false;
!       boolean hasAttribute = false;
!       int toAdd = 2;
!       for (int i=0; i < notifications.length; i++) {
!          String name = notifications[i].getName();
!          if ("GENERIC".equals(name))
!          {
!             hasGeneric = true;
!             toAdd -= 1;
!             if (hasAttribute) break;
!          }
!          else if ("ATTRIBUTE_CHANGE".equals(name))
!          {
!             hasAttribute = true;
!             toAdd -= 1;
!             if (hasGeneric) break;
!          }
!       }
! 
!       // clone the array, adding in missing notifications
!       MBeanNotificationInfo[] newNotifications = new MBeanNotificationInfo[notifications.length + toAdd];
!       System.arraycopy(notifications, 0, newNotifications, toAdd, notifications.length);
!       if (!hasAttribute) newNotifications[--toAdd] = DEFAULT_NOTIFICATIONS[1];
!       if (!hasGeneric) newNotifications[--toAdd] = DEFAULT_NOTIFICATIONS[0];
!       return newNotifications;
     }
  
     public void removeAttributeChangeNotificationListener(NotificationListener listener, String attributeName) throws MBeanException, RuntimeOperationsException, ListenerNotFoundException
     {
+       //todo there may be a problem here if the original handback was not null - should we go directly to removeListener(NotificationListener) ?
        removeAttributeChangeNotificationListener(listener, attributeName, null);
     }
***************
*** 223,230 ****
     private void removeAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, ListenerNotFoundException
     {
!       if (listener == null) throw new RuntimeOperationsException(new IllegalArgumentException("Listener cannot be null"));
        AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
        if (attributeName != null)
        {
           filter.enableAttribute(attributeName);
        }
--- 255,263 ----
     private void removeAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, ListenerNotFoundException
     {
!       if (listener == null) throw new ListenerNotFoundException("listener is null");
        AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
        if (attributeName != null)
        {
+          if (m_modelMBeanInfo.getAttribute(attributeName) == null) throw new RuntimeOperationsException(new IllegalArgumentException("No attribute named " + attributeName));
           filter.enableAttribute(attributeName);
        }
***************
*** 288,292 ****
     public void sendNotification(String message) throws MBeanException, RuntimeOperationsException
     {
!       Notification notification = new Notification("jmx.modelmbean.general", this, 1, message);
        sendNotification(notification);
     }
--- 321,326 ----
     public void sendNotification(String message) throws MBeanException, RuntimeOperationsException
     {
!       if (message == null) throw new RuntimeOperationsException(new IllegalArgumentException("message is null"));
!       Notification notification = new Notification("jmx.modelmbean.generic", this, 1, message);
        sendNotification(notification);
     }
***************
*** 294,297 ****
--- 328,332 ----
     public void sendNotification(Notification notification) throws MBeanException, RuntimeOperationsException
     {
+       if (notification == null) throw new RuntimeOperationsException(new IllegalArgumentException("notification is null"));
        m_generalBroadcaster.sendNotification(notification);
     }
***************
*** 1095,1099 ****
     private NotificationBroadcasterSupport getAttributeChangeBroadcaster()
     {
!       return m_attributeChangeBroadcaster;
     }
  
--- 1130,1134 ----
     private NotificationBroadcasterSupport getAttributeChangeBroadcaster()
     {
!       return m_generalBroadcaster;
     }
  



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click