1 /*
2 * Copyright 2002-2006 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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 java.lang.reflect.Constructor;
20
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.context.ApplicationEvent;
26 import org.springframework.context.ApplicationEventPublisher;
27 import org.springframework.context.ApplicationEventPublisherAware;
28
29 /**
30 * {@link MethodInterceptor Interceptor} that publishes an
31 * <code>ApplicationEvent</code> to all <code>ApplicationListeners</code>
32 * registered with an <code>ApplicationEventPublisher</code> after each
33 * <i>successful</i> method invocation.
34 *
35 * <p>Note that this interceptor is only capable of publishing <i>stateless</i>
36 * events configured via the
37 * {@link #setApplicationEventClass "applicationEventClass"} property.
38 *
39 * @author Dmitriy Kopylenko
40 * @author Juergen Hoeller
41 * @author Rick Evans
42 * @see #setApplicationEventClass
43 * @see org.springframework.context.ApplicationEvent
44 * @see org.springframework.context.ApplicationListener
45 * @see org.springframework.context.ApplicationEventPublisher
46 * @see org.springframework.context.ApplicationContext
47 */
48 public class EventPublicationInterceptor
49 implements MethodInterceptor, ApplicationEventPublisherAware, InitializingBean {
50
51 private Constructor applicationEventClassConstructor;
52
53 private ApplicationEventPublisher applicationEventPublisher;
54
55
56 /**
57 * Set the application event class to publish.
58 * <p>The event class <b>must</b> have a constructor with a single
59 * <code>Object</code> argument for the event source. The interceptor
60 * will pass in the invoked object.
61 * @throws IllegalArgumentException if the supplied <code>Class</code> is
62 * <code>null</code> or if it is not an <code>ApplicationEvent</code> subclass or
63 * if it does not expose a constructor that takes a single <code>Object</code> argument
64 */
65 public void setApplicationEventClass(Class applicationEventClass) {
66 if (ApplicationEvent.class.equals(applicationEventClass) ||
67 !ApplicationEvent.class.isAssignableFrom(applicationEventClass)) {
68 throw new IllegalArgumentException("applicationEventClass needs to extend ApplicationEvent");
69 }
70 try {
71 this.applicationEventClassConstructor =
72 applicationEventClass.getConstructor(new Class[] {Object.class});
73 }
74 catch (NoSuchMethodException ex) {
75 throw new IllegalArgumentException("applicationEventClass [" +
76 applicationEventClass.getName() + "] does not have the required Object constructor: " + ex);
77 }
78 }
79
80 public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
81 this.applicationEventPublisher = applicationEventPublisher;
82 }
83
84 public void afterPropertiesSet() throws Exception {
85 if (this.applicationEventClassConstructor == null) {
86 throw new IllegalArgumentException("applicationEventClass is required");
87 }
88 }
89
90
91 public Object invoke(MethodInvocation invocation) throws Throwable {
92 Object retVal = invocation.proceed();
93
94 ApplicationEvent event = (ApplicationEvent)
95 this.applicationEventClassConstructor.newInstance(new Object[] {invocation.getThis()});
96 this.applicationEventPublisher.publishEvent(event);
97
98 return retVal;
99 }
100
101 }