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;
018:
019: import javax.management.NotificationFilter;
020: import javax.management.NotificationListener;
021:
022: import org.springframework.beans.factory.InitializingBean;
023:
024: /**
025: * Helper class that aggregates a {@link javax.management.NotificationListener},
026: * a {@link javax.management.NotificationFilter}, and an arbitrary handback
027: * object.
028: *
029: * <p>Also provides support for associating the encapsulated
030: * {@link javax.management.NotificationListener} with any number of
031: * MBeans from which it wishes to receive
032: * {@link javax.management.Notification Notifications} via the
033: * {@link #setMappedObjectNames mappedObjectNames} property.
034: *
035: * @author Rob Harrop
036: * @author Juergen Hoeller
037: * @since 2.0
038: */
039: public class NotificationListenerBean implements InitializingBean {
040:
041: private NotificationListener notificationListener;
042:
043: private NotificationFilter notificationFilter;
044:
045: private Object handback;
046:
047: private String[] mappedObjectNames;
048:
049: /**
050: * Create a new instance of the {@link NotificationListenerBean} class.
051: */
052: public NotificationListenerBean() {
053: }
054:
055: /**
056: * Create a new instance of the {@link NotificationListenerBean} class.
057: * @param notificationListener the encapsulated listener
058: */
059: public NotificationListenerBean(
060: NotificationListener notificationListener) {
061: this .notificationListener = notificationListener;
062: }
063:
064: /**
065: * Set the {@link javax.management.NotificationListener}.
066: * @param notificationListener said {@link javax.management.NotificationListener}
067: */
068: public void setNotificationListener(
069: NotificationListener notificationListener) {
070: this .notificationListener = notificationListener;
071: }
072:
073: /**
074: * Get the {@link javax.management.NotificationListener}.
075: * @return said {@link javax.management.NotificationListener}
076: */
077: public NotificationListener getNotificationListener() {
078: return this .notificationListener;
079: }
080:
081: /**
082: * Set the {@link javax.management.NotificationFilter} associated
083: * with the encapsulated {@link #getNotificationFilter() NotificationFilter}.
084: * <p>May be <code>null</code>.
085: * @param notificationFilter said {@link javax.management.NotificationFilter}
086: */
087: public void setNotificationFilter(
088: NotificationFilter notificationFilter) {
089: this .notificationFilter = notificationFilter;
090: }
091:
092: /**
093: * Return the {@link javax.management.NotificationFilter} associated
094: * with the encapsulated {@link #getNotificationFilter() NotificationFilter}.
095: * <p>May be <code>null</code>.
096: * @return said {@link javax.management.NotificationFilter}
097: */
098: public NotificationFilter getNotificationFilter() {
099: return this .notificationFilter;
100: }
101:
102: /**
103: * Set the (arbitrary) object that will be 'handed back' as-is by an
104: * {@link javax.management.NotificationBroadcaster} when notifying
105: * any {@link javax.management.NotificationListener}.
106: * <p>May be <code>null</code>.
107: * @param handback the handback object.
108: * @see javax.management.NotificationListener#handleNotification(javax.management.Notification, Object)
109: */
110: public void setHandback(Object handback) {
111: this .handback = handback;
112: }
113:
114: /**
115: * Return the (arbitrary) object that will be 'handed back' as-is by an
116: * {@link javax.management.NotificationBroadcaster} when notifying
117: * any {@link javax.management.NotificationListener}.
118: * @return the handback object
119: * @see javax.management.NotificationListener#handleNotification(javax.management.Notification, Object)
120: */
121: public Object getHandback() {
122: return this .handback;
123: }
124:
125: /**
126: * Set the {@link javax.management.ObjectName}-style name of the single MBean
127: * that the encapsulated {@link #getNotificationFilter() NotificationFilter}
128: * will be registered with to listen for
129: * {@link javax.management.Notification Notifications}.
130: */
131: public void setMappedObjectName(String mappedObjectName) {
132: setMappedObjectNames(mappedObjectName != null ? new String[] { mappedObjectName }
133: : null);
134: }
135:
136: /**
137: * Set an array of {@link javax.management.ObjectName}-style names of the MBeans
138: * that the encapsulated {@link #getNotificationFilter() NotificationFilter}
139: * will be registered with to listen for
140: * {@link javax.management.Notification Notifications}.
141: */
142: public void setMappedObjectNames(String[] mappedObjectNames) {
143: this .mappedObjectNames = mappedObjectNames;
144: }
145:
146: /**
147: * Return the list of {@link javax.management.ObjectName} String representations for
148: * which the encapsulated {@link #getNotificationFilter() NotificationFilter} will
149: * be registered as a listener for {@link javax.management.Notification Notifications}.
150: */
151: public String[] getMappedObjectNames() {
152: return this .mappedObjectNames;
153: }
154:
155: public void afterPropertiesSet() {
156: if (this .notificationListener == null) {
157: throw new IllegalArgumentException(
158: "Property 'notificationListener' is required");
159: }
160: }
161:
162: }
|