KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > notification > ModelMBeanNotificationPublisher


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jmx.export.notification;
18
19 import javax.management.AttributeChangeNotification JavaDoc;
20 import javax.management.MBeanException JavaDoc;
21 import javax.management.Notification JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23 import javax.management.modelmbean.ModelMBean JavaDoc;
24
25 import org.springframework.util.Assert;
26
27 /**
28  * {@link NotificationPublisher} implementation that uses the infrastructure
29  * provided by the {@link ModelMBean} interface to track
30  * {@link javax.management.NotificationListener javax.management.NotificationListeners}
31  * and send {@link Notification Notifications} to those listeners.
32  *
33  * @author Rob Harrop
34  * @author Rick Evans
35  * @since 2.0
36  * @see ModelMBean
37  * @see NotificationPublisherAware
38  */

39 public class ModelMBeanNotificationPublisher implements NotificationPublisher {
40
41     /**
42      * The {@link ModelMBean} instance wrapping the managed resource into which this
43      * <code>NotificationPublisher</code> will be injected.
44      */

45     private final ModelMBean JavaDoc modelMBean;
46
47     /**
48      * The {@link ObjectName} associated with the {@link ModelMBean modelMBean}.
49      */

50     private final ObjectName JavaDoc objectName;
51
52     /**
53      * The managed resource associated with the {@link ModelMBean modelMBean}.
54      */

55     private final Object JavaDoc managedResource;
56
57
58     /**
59      * Creates a new instance of the {@link ModelMBeanNotificationPublisher} class
60      * that will publish all {@link javax.management.Notification Notifications}
61      * to the supplied {@link ModelMBean}.
62      * @param modelMBean the target {@link ModelMBean}; must not be <code>null</code>
63      * @param objectName the {@link ObjectName} of the source {@link ModelMBean}
64      * @param managedResource the managed resource exposed by the supplied {@link ModelMBean}
65      * @throws IllegalArgumentException if any of the parameters is <code>null</code>
66      */

67     public ModelMBeanNotificationPublisher(ModelMBean JavaDoc modelMBean, ObjectName JavaDoc objectName, Object JavaDoc 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     /**
78      * Sends the supplied {@link Notification} using the wrapped
79      * {@link ModelMBean} instance.
80      * @param notification the {@link Notification} to be sent
81      * @throws IllegalArgumentException if the supplied <code>notification</code> is <code>null</code>
82      * @throws UnableToSendNotificationException if the supplied <code>notification</code> could not be sent
83      */

84     public void sendNotification(Notification JavaDoc notification) {
85         Assert.notNull(notification, "Notification must not be null");
86         replaceNotificationSourceIfNecessary(notification);
87         try {
88             if (notification instanceof AttributeChangeNotification JavaDoc) {
89                 this.modelMBean.sendAttributeChangeNotification((AttributeChangeNotification JavaDoc) notification);
90             }
91             else {
92                 this.modelMBean.sendNotification(notification);
93             }
94         }
95         catch (MBeanException JavaDoc ex) {
96             throw new UnableToSendNotificationException("Unable to send notification [" + notification + "]", ex);
97         }
98     }
99
100     /**
101      * From the {@link Notification class level Javadoc}:
102      * <p><i>'It is strongly recommended that notification senders use the object name
103      * rather than a reference to the MBean object as the source.'</i>
104      *
105      * @param notification the {@link Notification} whose {@link javax.management.Notification#getSource()} might need massaging
106      */

107     private void replaceNotificationSourceIfNecessary(Notification JavaDoc notification) {
108         if (notification.getSource() == null
109                 || notification.getSource().equals(this.managedResource)) {
110             notification.setSource(this.objectName);
111         }
112     }
113
114 }
115
Popular Tags