001/*
002 * Copyright 2002-2017 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 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 * <p>Note: This class supports Spring bean names as
036 * {@link #setMappedObjectNames "mappedObjectNames"} as well, as alternative
037 * to specifying JMX object names. Note that only beans exported by the
038 * same {@link MBeanExporter} are supported for such bean names.
039 *
040 * @author Rob Harrop
041 * @author Juergen Hoeller
042 * @since 2.0
043 * @see MBeanExporter#setNotificationListeners
044 */
045public class NotificationListenerBean extends NotificationListenerHolder implements InitializingBean {
046
047        /**
048         * Create a new instance of the {@link NotificationListenerBean} class.
049         */
050        public NotificationListenerBean() {
051        }
052
053        /**
054         * Create a new instance of the {@link NotificationListenerBean} class.
055         * @param notificationListener the encapsulated listener
056         */
057        public NotificationListenerBean(NotificationListener notificationListener) {
058                Assert.notNull(notificationListener, "NotificationListener must not be null");
059                setNotificationListener(notificationListener);
060        }
061
062
063        @Override
064        public void afterPropertiesSet() {
065                if (getNotificationListener() == null) {
066                        throw new IllegalArgumentException("Property 'notificationListener' is required");
067                }
068        }
069
070        void replaceObjectName(Object originalName, Object newName) {
071                if (this.mappedObjectNames != null && this.mappedObjectNames.contains(originalName)) {
072                        this.mappedObjectNames.remove(originalName);
073                        this.mappedObjectNames.add(newName);
074                }
075        }
076
077}