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.context.event;
018
019import org.springframework.context.ApplicationEvent;
020import org.springframework.context.ApplicationListener;
021import org.springframework.core.Ordered;
022import org.springframework.core.ResolvableType;
023import org.springframework.lang.Nullable;
024
025/**
026 * {@link org.springframework.context.ApplicationListener} decorator that filters
027 * events from a specified event source, invoking its delegate listener for
028 * matching {@link org.springframework.context.ApplicationEvent} objects only.
029 *
030 * <p>Can also be used as base class, overriding the {@link #onApplicationEventInternal}
031 * method instead of specifying a delegate listener.
032 *
033 * @author Juergen Hoeller
034 * @author Stephane Nicoll
035 * @since 2.0.5
036 */
037public class SourceFilteringListener implements GenericApplicationListener, SmartApplicationListener {
038
039        private final Object source;
040
041        @Nullable
042        private GenericApplicationListener delegate;
043
044
045        /**
046         * Create a SourceFilteringListener for the given event source.
047         * @param source the event source that this listener filters for,
048         * only processing events from this source
049         * @param delegate the delegate listener to invoke with event
050         * from the specified source
051         */
052        public SourceFilteringListener(Object source, ApplicationListener<?> delegate) {
053                this.source = source;
054                this.delegate = (delegate instanceof GenericApplicationListener ?
055                                (GenericApplicationListener) delegate : new GenericApplicationListenerAdapter(delegate));
056        }
057
058        /**
059         * Create a SourceFilteringListener for the given event source,
060         * expecting subclasses to override the {@link #onApplicationEventInternal}
061         * method (instead of specifying a delegate listener).
062         * @param source the event source that this listener filters for,
063         * only processing events from this source
064         */
065        protected SourceFilteringListener(Object source) {
066                this.source = source;
067        }
068
069
070        @Override
071        public void onApplicationEvent(ApplicationEvent event) {
072                if (event.getSource() == this.source) {
073                        onApplicationEventInternal(event);
074                }
075        }
076
077        @Override
078        public boolean supportsEventType(ResolvableType eventType) {
079                return (this.delegate == null || this.delegate.supportsEventType(eventType));
080        }
081
082        @Override
083        public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
084                return supportsEventType(ResolvableType.forType(eventType));
085        }
086
087        @Override
088        public boolean supportsSourceType(@Nullable Class<?> sourceType) {
089                return (sourceType != null && sourceType.isInstance(this.source));
090        }
091
092        @Override
093        public int getOrder() {
094                return (this.delegate != null ? this.delegate.getOrder() : Ordered.LOWEST_PRECEDENCE);
095        }
096
097
098        /**
099         * Actually process the event, after having filtered according to the
100         * desired event source already.
101         * <p>The default implementation invokes the specified delegate, if any.
102         * @param event the event to process (matching the specified source)
103         */
104        protected void onApplicationEventInternal(ApplicationEvent event) {
105                if (this.delegate == null) {
106                        throw new IllegalStateException(
107                                        "Must specify a delegate object or override the onApplicationEventInternal method");
108                }
109                this.delegate.onApplicationEvent(event);
110        }
111
112}