001/*
002 * Copyright 2002-2012 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 *      https://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
017package org.springframework.jmx.export;
018
019import javax.management.NotificationListener;
020
021import org.springframework.beans.factory.InitializingBean;
022import org.springframework.jmx.support.NotificationListenerHolder;
023import org.springframework.util.Assert;
024
025/**
026 * Helper class that aggregates a {@link javax.management.NotificationListener},
027 * a {@link javax.management.NotificationFilter}, and an arbitrary handback
028 * object.
029 *
030 * <p>Also provides support for associating the encapsulated
031 * {@link javax.management.NotificationListener} with any number of
032 * MBeans from which it wishes to receive
033 * {@link javax.management.Notification Notifications} via the
034 * {@link #setMappedObjectNames mappedObjectNames} property.
035 *
036 * <p>Note: This class supports Spring bean names as
037 * {@link #setMappedObjectNames "mappedObjectNames"} as well, as alternative
038 * to specifying JMX object names. Note that only beans exported by the
039 * same {@link MBeanExporter} are supported for such bean names.
040 *
041 * @author Rob Harrop
042 * @author Juergen Hoeller
043 * @since 2.0
044 * @see MBeanExporter#setNotificationListeners
045 */
046public class NotificationListenerBean extends NotificationListenerHolder implements InitializingBean {
047
048        /**
049         * Create a new instance of the {@link NotificationListenerBean} class.
050         */
051        public NotificationListenerBean() {
052        }
053
054        /**
055         * Create a new instance of the {@link NotificationListenerBean} class.
056         * @param notificationListener the encapsulated listener
057         */
058        public NotificationListenerBean(NotificationListener notificationListener) {
059                Assert.notNull(notificationListener, "NotificationListener must not be null");
060                setNotificationListener(notificationListener);
061        }
062
063
064        @Override
065        public void afterPropertiesSet() {
066                if (getNotificationListener() == null) {
067                        throw new IllegalArgumentException("Property 'notificationListener' is required");
068                }
069        }
070
071        void replaceObjectName(Object originalName, Object newName) {
072                if (this.mappedObjectNames != null && this.mappedObjectNames.contains(originalName)) {
073                        this.mappedObjectNames.remove(originalName);
074                        this.mappedObjectNames.add(newName);
075                }
076        }
077
078}