001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jmx.export.notification;
018:
019: import javax.management.AttributeChangeNotification;
020: import javax.management.MBeanException;
021: import javax.management.Notification;
022: import javax.management.ObjectName;
023: import javax.management.modelmbean.ModelMBean;
024: import javax.management.modelmbean.ModelMBeanNotificationBroadcaster;
025:
026: import org.springframework.util.Assert;
027:
028: /**
029: * {@link NotificationPublisher} implementation that uses the infrastructure
030: * provided by the {@link ModelMBean} interface to track
031: * {@link javax.management.NotificationListener javax.management.NotificationListeners}
032: * and send {@link Notification Notifications} to those listeners.
033: *
034: * @author Rob Harrop
035: * @author Juergen Hoeller
036: * @author Rick Evans
037: * @since 2.0
038: * @see javax.management.modelmbean.ModelMBeanNotificationBroadcaster
039: * @see NotificationPublisherAware
040: */
041: public class ModelMBeanNotificationPublisher implements
042: NotificationPublisher {
043:
044: /**
045: * The {@link ModelMBean} instance wrapping the managed resource into which this
046: * <code>NotificationPublisher</code> will be injected.
047: */
048: private final ModelMBeanNotificationBroadcaster modelMBean;
049:
050: /**
051: * The {@link ObjectName} associated with the {@link ModelMBean modelMBean}.
052: */
053: private final ObjectName objectName;
054:
055: /**
056: * The managed resource associated with the {@link ModelMBean modelMBean}.
057: */
058: private final Object managedResource;
059:
060: /**
061: * Create a new instance of the {@link ModelMBeanNotificationPublisher} class
062: * that will publish all {@link javax.management.Notification Notifications}
063: * to the supplied {@link ModelMBean}.
064: * @param modelMBean the target {@link ModelMBean}; must not be <code>null</code>
065: * @param objectName the {@link ObjectName} of the source {@link ModelMBean}
066: * @param managedResource the managed resource exposed by the supplied {@link ModelMBean}
067: * @throws IllegalArgumentException if any of the parameters is <code>null</code>
068: */
069: public ModelMBeanNotificationPublisher(
070: ModelMBeanNotificationBroadcaster modelMBean,
071: ObjectName objectName, Object managedResource) {
072:
073: Assert.notNull(modelMBean, "'modelMBean' must not be null");
074: Assert.notNull(objectName, "'objectName' must not be null");
075: Assert.notNull(managedResource,
076: "'managedResource' must not be null");
077: this .modelMBean = modelMBean;
078: this .objectName = objectName;
079: this .managedResource = managedResource;
080: }
081:
082: /**
083: * Send the supplied {@link Notification} using the wrapped
084: * {@link ModelMBean} instance.
085: * @param notification the {@link Notification} to be sent
086: * @throws IllegalArgumentException if the supplied <code>notification</code> is <code>null</code>
087: * @throws UnableToSendNotificationException if the supplied <code>notification</code> could not be sent
088: */
089: public void sendNotification(Notification notification) {
090: Assert.notNull(notification, "Notification must not be null");
091: replaceNotificationSourceIfNecessary(notification);
092: try {
093: if (notification instanceof AttributeChangeNotification) {
094: this .modelMBean
095: .sendAttributeChangeNotification((AttributeChangeNotification) notification);
096: } else {
097: this .modelMBean.sendNotification(notification);
098: }
099: } catch (MBeanException ex) {
100: throw new UnableToSendNotificationException(
101: "Unable to send notification [" + notification
102: + "]", ex);
103: }
104: }
105:
106: /**
107: * From the {@link Notification javadoc}:
108: * <p><i>"It is strongly recommended that notification senders use the object name
109: * rather than a reference to the MBean object as the source."</i>
110: * @param notification the {@link Notification} whose
111: * {@link javax.management.Notification#getSource()} might need massaging
112: */
113: private void replaceNotificationSourceIfNecessary(
114: Notification notification) {
115: if (notification.getSource() == null
116: || notification.getSource()
117: .equals(this.managedResource)) {
118: notification.setSource(this.objectName);
119: }
120: }
121:
122: }
|