KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > StandardEmitterMBean


1 /*
2  * @(#)StandardEmitterMBean.java 1.7 06/06/20
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management;
9
10 /**
11  * <p>An MBean whose management interface is determined by reflection
12  * on a Java interface, and that emits notifications.</p>
13  *
14  * <p>The following example shows how to use the public constructor
15  * {@link #StandardEmitterMBean(Object, Class, NotificationEmitter)
16  * StandardEmitterMBean(implementation, mbeanInterface, emitter)} to
17  * create an MBean emitting notifications with any
18  * implementation class name <i>Impl</i>, with a management
19  * interface defined (as for current Standard MBeans) by any interface
20  * <i>Intf</i>, and with any implementation of the interface
21  * {@link NotificationEmitter}. The example uses the class
22  * {@link NotificationBroadcasterSupport} as an implementation
23  * of the interface {@link NotificationEmitter}.</p>
24  *
25  * <pre>
26  * MBeanServer mbs;
27  * ...
28  * final String[] types = new String[] {"sun.disc.space","sun.disc.alarm"};
29  * final MBeanNotificationInfo info = new MBeanNotificationInfo(
30  * types,
31  * Notification.class.getName(),
32  * "Notification about disc info.");
33  * final NotificationEmitter emitter =
34  * new NotificationBroadcasterSupport(info);
35  *
36  * final Intf impl = new Impl(...);
37  * final Object mbean = new StandardEmitterMBean(
38  * impl, Intf.class, emitter);
39  * mbs.registerMBean(mbean, objectName);
40  * </pre>
41  *
42  * @see StandardMBean
43  *
44  * @since 1.6
45  */

46 public class StandardEmitterMBean extends StandardMBean JavaDoc
47         implements NotificationEmitter JavaDoc {
48
49     private final NotificationEmitter JavaDoc emitter;
50     private final MBeanNotificationInfo JavaDoc[] notificationInfo;
51     
52     /**
53      * <p>Make an MBean whose management interface is specified by
54      * {@code mbeanInterface}, with the given implementation and
55      * where notifications are handled by the given {@code NotificationEmitter}.
56      * The resultant MBean implements the {@code NotificationEmitter} interface
57      * by forwarding its methods to {@code emitter}. It is legal and useful
58      * for {@code implementation} and {@code emitter} to be the same object.</p>
59      *
60      * <p>If {@code emitter} is an instance of {@code
61      * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
62      * sendNotification} method will call {@code emitter.}{@link
63      * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
64      *
65      * <p>The array returned by {@link #getNotificationInfo()} on the
66      * new MBean is a copy of the array returned by
67      * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
68      * getNotificationInfo()} at the time of construction. If the array
69      * returned by {@code emitter.getNotificationInfo()} later changes,
70      * that will have no effect on this object's
71      * {@code getNotificationInfo()}.</p>
72      *
73      * @param implementation the implementation of the MBean interface.
74      * @param mbeanInterface a Standard MBean interface.
75      * @param emitter the object that will handle notifications.
76      *
77      * @throws IllegalArgumentException if the {@code mbeanInterface}
78      * does not follow JMX design patterns for Management Interfaces, or
79      * if the given {@code implementation} does not implement the
80      * specified interface, or if {@code emitter} is null.
81      */

82     public <T> StandardEmitterMBean(T implementation, Class JavaDoc<T> mbeanInterface,
83                                     NotificationEmitter JavaDoc emitter) {
84         super(implementation, mbeanInterface, false);
85         if (emitter == null)
86             throw new IllegalArgumentException JavaDoc("Null emitter");
87         this.emitter = emitter;
88         this.notificationInfo = emitter.getNotificationInfo();
89     }
90     
91     /**
92      * <p>Make an MBean whose management interface is specified by
93      * {@code mbeanInterface}, with the given implementation and where
94      * notifications are handled by the given {@code
95      * NotificationEmitter}. This constructor can be used to make
96      * either Standard MBeans or MXBeans. The resultant MBean
97      * implements the {@code NotificationEmitter} interface by
98      * forwarding its methods to {@code emitter}. It is legal and
99      * useful for {@code implementation} and {@code emitter} to be the
100      * same object.</p>
101      *
102      * <p>If {@code emitter} is an instance of {@code
103      * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
104      * sendNotification} method will call {@code emitter.}{@link
105      * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
106      *
107      * <p>The array returned by {@link #getNotificationInfo()} on the
108      * new MBean is a copy of the array returned by
109      * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
110      * getNotificationInfo()} at the time of construction. If the array
111      * returned by {@code emitter.getNotificationInfo()} later changes,
112      * that will have no effect on this object's
113      * {@code getNotificationInfo()}.</p>
114      *
115      * @param implementation the implementation of the MBean interface.
116      * @param mbeanInterface a Standard MBean interface.
117      * @param isMXBean If true, the {@code mbeanInterface} parameter
118      * names an MXBean interface and the resultant MBean is an MXBean.
119      * @param emitter the object that will handle notifications.
120      *
121      * @throws IllegalArgumentException if the {@code mbeanInterface}
122      * does not follow JMX design patterns for Management Interfaces, or
123      * if the given {@code implementation} does not implement the
124      * specified interface, or if {@code emitter} is null.
125      */

126     public <T> StandardEmitterMBean(T implementation, Class JavaDoc<T> mbeanInterface,
127                                     boolean isMXBean,
128                                     NotificationEmitter JavaDoc emitter) {
129         super(implementation, mbeanInterface, isMXBean);
130         if (emitter == null)
131             throw new IllegalArgumentException JavaDoc("Null emitter");
132         this.emitter = emitter;
133         this.notificationInfo = emitter.getNotificationInfo();
134     }
135
136     /**
137      * <p>Make an MBean whose management interface is specified by
138      * {@code mbeanInterface}, and
139      * where notifications are handled by the given {@code NotificationEmitter}.
140      * The resultant MBean implements the {@code NotificationEmitter} interface
141      * by forwarding its methods to {@code emitter}.</p>
142      *
143      * <p>If {@code emitter} is an instance of {@code
144      * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
145      * sendNotification} method will call {@code emitter.}{@link
146      * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
147      *
148      * <p>The array returned by {@link #getNotificationInfo()} on the
149      * new MBean is a copy of the array returned by
150      * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
151      * getNotificationInfo()} at the time of construction. If the array
152      * returned by {@code emitter.getNotificationInfo()} later changes,
153      * that will have no effect on this object's
154      * {@code getNotificationInfo()}.</p>
155      *
156      * <p>This constructor must be called from a subclass that implements
157      * the given {@code mbeanInterface}.</p>
158      *
159      * @param mbeanInterface a StandardMBean interface.
160      * @param emitter the object that will handle notifications.
161      *
162      * @throws IllegalArgumentException if the {@code mbeanInterface}
163      * does not follow JMX design patterns for Management Interfaces, or
164      * if {@code this} does not implement the specified interface, or
165      * if {@code emitter} is null.
166      */

167     protected StandardEmitterMBean(Class JavaDoc<?> mbeanInterface,
168                                    NotificationEmitter JavaDoc emitter) {
169         super(mbeanInterface, false);
170         if (emitter == null)
171             throw new IllegalArgumentException JavaDoc("Null emitter");
172         this.emitter = emitter;
173         this.notificationInfo = emitter.getNotificationInfo();
174     }
175
176     /**
177      * <p>Make an MBean whose management interface is specified by
178      * {@code mbeanInterface}, and where notifications are handled by
179      * the given {@code NotificationEmitter}. This constructor can be
180      * used to make either Standard MBeans or MXBeans. The resultant
181      * MBean implements the {@code NotificationEmitter} interface by
182      * forwarding its methods to {@code emitter}.</p>
183      *
184      * <p>If {@code emitter} is an instance of {@code
185      * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
186      * sendNotification} method will call {@code emitter.}{@link
187      * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
188      *
189      * <p>The array returned by {@link #getNotificationInfo()} on the
190      * new MBean is a copy of the array returned by
191      * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
192      * getNotificationInfo()} at the time of construction. If the array
193      * returned by {@code emitter.getNotificationInfo()} later changes,
194      * that will have no effect on this object's
195      * {@code getNotificationInfo()}.</p>
196      *
197      * <p>This constructor must be called from a subclass that implements
198      * the given {@code mbeanInterface}.</p>
199      *
200      * @param mbeanInterface a StandardMBean interface.
201      * @param isMXBean If true, the {@code mbeanInterface} parameter
202      * names an MXBean interface and the resultant MBean is an MXBean.
203      * @param emitter the object that will handle notifications.
204      *
205      * @throws IllegalArgumentException if the {@code mbeanInterface}
206      * does not follow JMX design patterns for Management Interfaces, or
207      * if {@code this} does not implement the specified interface, or
208      * if {@code emitter} is null.
209      */

210     protected StandardEmitterMBean(Class JavaDoc<?> mbeanInterface, boolean isMXBean,
211                                    NotificationEmitter JavaDoc emitter) {
212         super(mbeanInterface, isMXBean);
213         if (emitter == null)
214             throw new IllegalArgumentException JavaDoc("Null emitter");
215         this.emitter = emitter;
216         this.notificationInfo = emitter.getNotificationInfo();
217     }
218
219     public void removeNotificationListener(NotificationListener JavaDoc listener)
220             throws ListenerNotFoundException JavaDoc {
221         emitter.removeNotificationListener(listener);
222     }
223
224     public void removeNotificationListener(NotificationListener JavaDoc listener,
225                                            NotificationFilter JavaDoc filter,
226                                            Object JavaDoc handback)
227             throws ListenerNotFoundException JavaDoc {
228         emitter.removeNotificationListener(listener, filter, handback);
229     }
230
231     public void addNotificationListener(NotificationListener JavaDoc listener,
232                                         NotificationFilter JavaDoc filter,
233                                         Object JavaDoc handback) {
234         emitter.addNotificationListener(listener, filter, handback);
235     }
236
237     public MBeanNotificationInfo JavaDoc[] getNotificationInfo() {
238         return notificationInfo;
239     }
240
241     /**
242      * <p>Sends a notification.</p>
243      *
244      * <p>If the {@code emitter} parameter to the constructor was an
245      * instance of {@code NotificationBroadcasterSupport} then this
246      * method will call {@code emitter.}{@link
247      * NotificationBroadcasterSupport#sendNotification
248      * sendNotification}.</p>
249      *
250      * @param n the notification to send.
251      *
252      * @throws ClassCastException if the {@code emitter} parameter to the
253      * constructor was not a {@code NotificationBroadcasterSupport}.
254      */

255     public void sendNotification(Notification JavaDoc n) {
256         if (emitter instanceof NotificationBroadcasterSupport JavaDoc)
257             ((NotificationBroadcasterSupport JavaDoc) emitter).sendNotification(n);
258         else {
259             final String JavaDoc msg =
260                 "Cannot sendNotification when emitter is not an " +
261                 "instance of NotificationBroadcasterSupport: " +
262                 emitter.getClass().getName();
263             throw new ClassCastException JavaDoc(msg);
264         }
265     }
266
267     /**
268      * <p>Get the MBeanNotificationInfo[] that will be used in the
269      * MBeanInfo returned by this MBean.</p>
270      *
271      * <p>The default implementation of this method returns
272      * {@link #getNotificationInfo()}.</p>
273      *
274      * @param info The default MBeanInfo derived by reflection.
275      * @return the MBeanNotificationInfo[] for the new MBeanInfo.
276      */

277     MBeanNotificationInfo JavaDoc[] getNotifications(MBeanInfo JavaDoc info) {
278         return getNotificationInfo();
279     }
280 }
281
Popular Tags