1 16 17 package org.springframework.jmx.export.notification; 18 19 import javax.management.AttributeChangeNotification ; 20 import javax.management.MBeanException ; 21 import javax.management.Notification ; 22 import javax.management.ObjectName ; 23 import javax.management.modelmbean.ModelMBean ; 24 25 import org.springframework.util.Assert; 26 27 39 public class ModelMBeanNotificationPublisher implements NotificationPublisher { 40 41 45 private final ModelMBean modelMBean; 46 47 50 private final ObjectName objectName; 51 52 55 private final Object managedResource; 56 57 58 67 public ModelMBeanNotificationPublisher(ModelMBean modelMBean, ObjectName objectName, Object managedResource) { 68 Assert.notNull(modelMBean, "The 'modelMBean' parameter must not be null."); 69 Assert.notNull(objectName, "The 'objectName' parameter must not be null."); 70 Assert.notNull(managedResource, "The 'managedResource' parameter must not be null."); 71 this.modelMBean = modelMBean; 72 this.objectName = objectName; 73 this.managedResource = managedResource; 74 } 75 76 77 84 public void sendNotification(Notification notification) { 85 Assert.notNull(notification, "Notification must not be null"); 86 replaceNotificationSourceIfNecessary(notification); 87 try { 88 if (notification instanceof AttributeChangeNotification ) { 89 this.modelMBean.sendAttributeChangeNotification((AttributeChangeNotification ) notification); 90 } 91 else { 92 this.modelMBean.sendNotification(notification); 93 } 94 } 95 catch (MBeanException ex) { 96 throw new UnableToSendNotificationException("Unable to send notification [" + notification + "]", ex); 97 } 98 } 99 100 107 private void replaceNotificationSourceIfNecessary(Notification notification) { 108 if (notification.getSource() == null 109 || notification.getSource().equals(this.managedResource)) { 110 notification.setSource(this.objectName); 111 } 112 } 113 114 } 115 | Popular Tags |