01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.context.event;
18:
19: import org.springframework.context.ApplicationEvent;
20: import org.springframework.context.ApplicationListener;
21:
22: /**
23: * {@link org.springframework.context.ApplicationListener} decorator that filters
24: * events from a specified event source, invoking its delegate listener for
25: * matching {@link org.springframework.context.ApplicationEvent} objects only.
26: *
27: * <p>Can also be used as base class, overriding the {@link #onApplicationEventInternal}
28: * method instead of specifying a delegate listener.
29: *
30: * @author Juergen Hoeller
31: * @since 2.0.5
32: */
33: public class SourceFilteringListener implements ApplicationListener {
34:
35: private final Object source;
36:
37: private ApplicationListener delegate;
38:
39: /**
40: * Create a SourceFilteringListener for the given event source.
41: * @param source the event source that this listener filters for,
42: * only processing events from this source
43: * @param delegate the delegate listener to invoke with event
44: * from the specified source
45: */
46: public SourceFilteringListener(Object source,
47: ApplicationListener delegate) {
48: this .source = source;
49: this .delegate = delegate;
50: }
51:
52: /**
53: * Create a SourceFilteringListener for the given event source,
54: * expecting subclasses to override the {@link #onApplicationEventInternal}
55: * method (instead of specifying a delegate listener).
56: * @param source the event source that this listener filters for,
57: * only processing events from this source
58: */
59: protected SourceFilteringListener(Object source) {
60: this .source = source;
61: }
62:
63: public void onApplicationEvent(ApplicationEvent event) {
64: if (event.getSource() == this .source) {
65: onApplicationEventInternal(event);
66: }
67: }
68:
69: /**
70: * Actually process the event, after having filtered according to the
71: * desired event source already.
72: * <p>The default implementation invokes the specified delegate, if any.
73: * @param event the event to process (matching the specified source)
74: */
75: protected void onApplicationEventInternal(ApplicationEvent event) {
76: if (this .delegate == null) {
77: throw new IllegalStateException(
78: "Must specify a delegate object or override the onApplicationEventInternal method");
79: }
80: this.delegate.onApplicationEvent(event);
81: }
82:
83: }
|