001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 */
016package org.kuali.common.util.spring.event;
017
018import static com.google.common.base.Preconditions.checkArgument;
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import java.util.List;
022
023import org.kuali.common.util.execute.Executable;
024import org.kuali.common.util.log.LoggerUtils;
025import org.slf4j.Logger;
026import org.springframework.context.ApplicationEvent;
027import org.springframework.context.event.SmartApplicationListener;
028
029import com.google.common.collect.ImmutableList;
030
031/**
032 * <p>
033 * Associate an executable with one or more Spring framework application events.
034 * </p>
035 * 
036 * <p>
037 * If an application event gets fired where both {@code supportsEventType} and {@code supportsSourceType} return {@code true}, {@code onApplicationEvent} invokes the executable.
038 * </p>
039 * 
040 * <p>
041 * The default behavior of {@code supportsEventType} and {@code supportsSourceType} is to always return true irrespective of what application event was fired.
042 * </p>
043 * 
044 * <p>
045 * To be more discriminatory, provide values for {@code supportedSourceTypes} and {@code supportedEventTypes}.
046 * </p>
047 * 
048 * <p>
049 * To limit execution to a specific event type, eg {@code ContextRefreshedEvent}:
050 * </p>
051 * 
052 * <pre>
053 * ExecutableApplicationListener.builder(executable).supportedEventType(ContextRefreshedEvent.class).build()
054 * </pre>
055 */
056public final class GenericExecutableApplicationListener implements SmartApplicationListener {
057
058        private static final Logger logger = LoggerUtils.make();
059
060        private final Executable executable;
061        private final int order;
062        private final List<Class<?>> supportedSourceTypes;
063        private final List<Class<? extends ApplicationEvent>> supportedEventTypes;
064
065        @Override
066        public void onApplicationEvent(ApplicationEvent event) {
067                logger.info("Received event: [{}]", event.getClass().getCanonicalName());
068                executable.execute();
069        }
070
071        @Override
072        public int getOrder() {
073                return order;
074        }
075
076        @Override
077        public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
078                return supportedEventTypes.isEmpty() || supportedEventTypes.contains(eventType);
079        }
080
081        @Override
082        public boolean supportsSourceType(Class<?> sourceType) {
083                return supportedSourceTypes.isEmpty() || supportedSourceTypes.contains(sourceType);
084        }
085
086        private GenericExecutableApplicationListener(Builder builder) {
087                this.executable = builder.executable;
088                this.order = builder.order;
089                this.supportedSourceTypes = builder.supportedSourceTypes;
090                this.supportedEventTypes = builder.supportedEventTypes;
091        }
092
093        public static Builder builder(Executable executable) {
094                return new Builder(executable);
095        }
096
097        public static class Builder {
098
099                // Required
100                private final Executable executable;
101
102                // Optional
103                private int order = 0; // Lower values mean higher priority, higher values mean lower priority
104                private List<Class<?>> supportedSourceTypes = ImmutableList.of();
105                private List<Class<? extends ApplicationEvent>> supportedEventTypes = ImmutableList.of();
106
107                public Builder(Executable executable) {
108                        this.executable = executable;
109                }
110
111                public GenericExecutableApplicationListener build() {
112                        this.supportedEventTypes = ImmutableList.copyOf(supportedEventTypes);
113                        this.supportedSourceTypes = ImmutableList.copyOf(supportedSourceTypes);
114                        GenericExecutableApplicationListener instance = new GenericExecutableApplicationListener(this);
115                        validate(instance);
116                        return instance;
117                }
118
119                private void validate(GenericExecutableApplicationListener instance) {
120                        checkNotNull(instance.getExecutable(), "executable cannot be null");
121                        checkNotNull(instance.getSupportedEventTypes(), "supportedEventTypes cannot be null");
122                        checkNotNull(instance.getSupportedSourceTypes(), "supportedSourceTypes cannot be null");
123                        checkArgument(ImmutableList.class.isAssignableFrom(instance.getSupportedEventTypes().getClass()), "supportedEventTypes must be immutable");
124                        checkArgument(ImmutableList.class.isAssignableFrom(instance.getSupportedSourceTypes().getClass()), "supportedSourceTypes must be immutable");
125                }
126
127                public Builder order(int order) {
128                        this.order = order;
129                        return this;
130                }
131
132                public Builder supportedSourceTypes(List<Class<?>> supportedSourceTypes) {
133                        this.supportedSourceTypes = supportedSourceTypes;
134                        return this;
135                }
136
137                public Builder supportedSourceType(Class<?> supportedSourceType) {
138                        return supportedSourceTypes(ImmutableList.<Class<?>> of(supportedSourceType));
139                }
140
141                public Builder supportedEventTypes(List<Class<? extends ApplicationEvent>> supportedEventTypes) {
142                        this.supportedEventTypes = supportedEventTypes;
143                        return this;
144                }
145
146                public Builder supportedEventType(Class<? extends ApplicationEvent> supportedEventType) {
147                        return supportedEventTypes(ImmutableList.<Class<? extends ApplicationEvent>> of(supportedEventType));
148                }
149
150                public int getOrder() {
151                        return order;
152                }
153
154                public void setOrder(int order) {
155                        this.order = order;
156                }
157
158                public List<Class<?>> getSupportedSourceTypes() {
159                        return supportedSourceTypes;
160                }
161
162                public void setSupportedSourceTypes(List<Class<?>> supportedSourceTypes) {
163                        this.supportedSourceTypes = supportedSourceTypes;
164                }
165
166                public List<Class<? extends ApplicationEvent>> getSupportedEventTypes() {
167                        return supportedEventTypes;
168                }
169
170                public void setSupportedEventTypes(List<Class<? extends ApplicationEvent>> supportedEventTypes) {
171                        this.supportedEventTypes = supportedEventTypes;
172                }
173
174                public Executable getExecutable() {
175                        return executable;
176                }
177        }
178
179        public Executable getExecutable() {
180                return executable;
181        }
182
183        public List<Class<?>> getSupportedSourceTypes() {
184                return supportedSourceTypes;
185        }
186
187        public List<Class<? extends ApplicationEvent>> getSupportedEventTypes() {
188                return supportedEventTypes;
189        }
190
191}