001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.notification;
023:
024: import javax.management.ListenerNotFoundException;
025: import javax.management.NotificationBroadcaster;
026: import javax.management.NotificationEmitter;
027: import javax.management.NotificationFilter;
028: import javax.management.NotificationListener;
029: import javax.management.ObjectName;
030:
031: /**
032: * The notification listener registration for a listener in
033: * the mbeanserver. The listener is proxied so we can
034: * replace the source of the notification with the object name.<p>
035: *
036: * We also handle the registration with the broadcaster.
037: *
038: * @see org.jboss.mx.notification.ListenerRegistry
039: * @see org.jboss.mx.notification.ListenerRegistrationFactory
040: *
041: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
042: * @version $Revision: 57200 $
043: */
044: public class MBeanServerListenerRegistration extends
045: DefaultListenerRegistration {
046: // Attributes ----------------------------------------------------
047:
048: /**
049: * The notification listener proxy
050: */
051: private NotificationListener proxy;
052:
053: private NotificationFilter filterProxy;
054:
055: /**
056: * The notification broadcaster
057: */
058: private NotificationBroadcaster broadcaster;
059:
060: // Constructor ---------------------------------------------------
061:
062: /**
063: * Create a listener registration
064: *
065: * @param name the object name to use as the notifiation source
066: * @param broadcaster the notification broadcaster
067: * @param listener the notification listener
068: * @param filter the notification filter
069: * @param handback the handback object a
070: */
071: public MBeanServerListenerRegistration(ObjectName name,
072: NotificationBroadcaster broadcaster,
073: NotificationListener listener, NotificationFilter filter,
074: Object handback) {
075: super (listener, filter, handback);
076: proxy = (NotificationListener) NotificationListenerProxy
077: .newInstance(name, listener);
078: this .broadcaster = broadcaster;
079: this .filterProxy = (filter == null) ? null
080: : new NotificationFilterProxy(name, filter);
081: broadcaster.addNotificationListener(proxy, filterProxy,
082: handback);
083: }
084:
085: // Public --------------------------------------------------------
086:
087: // ListenerRegistration Implementation ---------------------------
088:
089: public NotificationListener getListener() {
090: return proxy;
091: }
092:
093: public NotificationFilter getFilter() {
094: return filterProxy;
095: }
096:
097: public void removed() {
098: try {
099: if (broadcaster instanceof NotificationEmitter)
100: ((NotificationEmitter) broadcaster)
101: .removeNotificationListener(getListener(),
102: getFilter(), getHandback());
103: else
104: broadcaster.removeNotificationListener(getListener());
105: } catch (ListenerNotFoundException ignored) {
106: }
107: }
108: }
|