ApplicationListenerMethodAdapter.java 12.2 KB
Newer Older
1
/*
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.context.event;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
21
import java.lang.reflect.Proxy;
22
import java.lang.reflect.UndeclaredThrowableException;
23
import java.util.ArrayList;
24
import java.util.Collection;
25 26
import java.util.Collections;
import java.util.List;
27 28 29 30

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

31
import org.springframework.aop.support.AopUtils;
32 33 34 35 36 37
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.ResolvableType;
38
import org.springframework.core.annotation.AnnotatedElementUtils;
39
import org.springframework.core.annotation.Order;
40
import org.springframework.lang.Nullable;
41
import org.springframework.util.Assert;
42
import org.springframework.util.ObjectUtils;
43 44 45 46 47 48 49
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

/**
 * {@link GenericApplicationListener} adapter that delegates the processing of
 * an event to an {@link EventListener} annotated method.
 *
50 51
 * <p>Delegates to {@link #processEvent(ApplicationEvent)} to give sub-classes
 * a chance to deviate from the default. Unwraps the content of a
52 53 54
 * {@link PayloadApplicationEvent} if necessary to allow method declaration
 * to define any arbitrary event type. If a condition is defined, it is
 * evaluated prior to invoking the underlying method.
55 56
 *
 * @author Stephane Nicoll
57
 * @author Juergen Hoeller
58
 * @author Sam Brannen
59 60 61 62 63 64 65 66 67 68
 * @since 4.2
 */
public class ApplicationListenerMethodAdapter implements GenericApplicationListener {

	protected final Log logger = LogFactory.getLog(getClass());

	private final String beanName;

	private final Method method;

69
	private final Method targetMethod;
70

71
	private final AnnotatedElementKey methodKey;
72

73
	private final List<ResolvableType> declaredEventTypes;
74

75
	@Nullable
76 77 78 79
	private final String condition;

	private final int order;

80
	@Nullable
81 82
	private ApplicationContext applicationContext;

83
	@Nullable
84 85
	private EventExpressionEvaluator evaluator;

J
Juergen Hoeller 已提交
86

87 88
	public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
		this.beanName = beanName;
89 90 91 92
		this.method = BridgeMethodResolver.findBridgedMethod(method);
		this.targetMethod = (!Proxy.isProxyClass(targetClass) ?
				AopUtils.getMostSpecificMethod(method, targetClass) : this.method);
		this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass);
93

94
		EventListener ann = AnnotatedElementUtils.findMergedAnnotation(this.targetMethod, EventListener.class);
95 96 97 98 99 100
		this.declaredEventTypes = resolveDeclaredEventTypes(method, ann);
		this.condition = (ann != null ? ann.condition() : null);
		this.order = resolveOrder(method);
	}


101
	private List<ResolvableType> resolveDeclaredEventTypes(Method method, @Nullable EventListener ann) {
102 103 104 105 106
		int count = method.getParameterCount();
		if (count > 1) {
			throw new IllegalStateException(
					"Maximum one parameter is allowed for event listener method: " + method);
		}
107 108 109 110 111 112 113 114 115

		if (ann != null) {
			Class<?>[] classes = ann.classes();
			if (classes.length > 0) {
				List<ResolvableType> types = new ArrayList<>(classes.length);
				for (Class<?> eventType : classes) {
					types.add(ResolvableType.forClass(eventType));
				}
				return types;
116 117
			}
		}
118 119 120 121

		if (count == 0) {
			throw new IllegalStateException(
					"Event parameter is mandatory for event listener method: " + method);
122
		}
123
		return Collections.singletonList(ResolvableType.forMethodParameter(method, 0));
124 125
	}

126 127 128 129
	private int resolveOrder(Method method) {
		Order ann = AnnotatedElementUtils.findMergedAnnotation(method, Order.class);
		return (ann != null ? ann.value() : 0);
	}
J
Juergen Hoeller 已提交
130

131 132 133 134 135 136 137 138
	/**
	 * Initialize this instance.
	 */
	void init(ApplicationContext applicationContext, EventExpressionEvaluator evaluator) {
		this.applicationContext = applicationContext;
		this.evaluator = evaluator;
	}

J
Juergen Hoeller 已提交
139

140 141
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
142 143 144
		processEvent(event);
	}

J
Juergen Hoeller 已提交
145 146 147 148 149 150
	@Override
	public boolean supportsEventType(ResolvableType eventType) {
		for (ResolvableType declaredEventType : this.declaredEventTypes) {
			if (declaredEventType.isAssignableFrom(eventType)) {
				return true;
			}
151
			if (PayloadApplicationEvent.class.isAssignableFrom(eventType.toClass())) {
J
Juergen Hoeller 已提交
152 153 154 155 156 157 158 159 160 161
				ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
				if (declaredEventType.isAssignableFrom(payloadType)) {
					return true;
				}
			}
		}
		return eventType.hasUnresolvableGenerics();
	}

	@Override
162
	public boolean supportsSourceType(@Nullable Class<?> sourceType) {
J
Juergen Hoeller 已提交
163 164 165 166 167
		return true;
	}

	@Override
	public int getOrder() {
168
		return this.order;
J
Juergen Hoeller 已提交
169 170 171
	}


172 173 174 175 176
	/**
	 * Process the specified {@link ApplicationEvent}, checking if the condition
	 * match and handling non-null result, if any.
	 */
	public void processEvent(ApplicationEvent event) {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		Object[] args = resolveArguments(event);
		if (shouldHandle(event, args)) {
			Object result = doInvoke(args);
			if (result != null) {
				handleResult(result);
			}
			else {
				logger.trace("No result object given - no result to handle");
			}
		}
	}

	/**
	 * Resolve the method arguments to use for the specified {@link ApplicationEvent}.
	 * <p>These arguments will be used to invoke the method handled by this instance. Can
	 * return {@code null} to indicate that no suitable arguments could be resolved and
	 * therefore the method should not be invoked at all for the specified event.
	 */
195
	@Nullable
196
	protected Object[] resolveArguments(ApplicationEvent event) {
197 198 199 200
		ResolvableType declaredEventType = getResolvableType(event);
		if (declaredEventType == null) {
			return null;
		}
201
		if (this.method.getParameterCount() == 0) {
202 203
			return new Object[0];
		}
204
		if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.toClass()) &&
205
				event instanceof PayloadApplicationEvent) {
206
			return new Object[] {((PayloadApplicationEvent) event).getPayload()};
207 208 209 210 211 212 213
		}
		else {
			return new Object[] {event};
		}
	}

	protected void handleResult(Object result) {
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
		if (result.getClass().isArray()) {
			Object[] events = ObjectUtils.toObjectArray(result);
			for (Object event : events) {
				publishEvent(event);
			}
		}
		else if (result instanceof Collection<?>) {
			Collection<?> events = (Collection<?>) result;
			for (Object event : events) {
				publishEvent(event);
			}
		}
		else {
			publishEvent(result);
		}
	}

231
	private void publishEvent(@Nullable Object event) {
232
		if (event != null) {
233
			Assert.notNull(this.applicationContext, "ApplicationContext must not be null");
234 235
			this.applicationContext.publishEvent(event);
		}
236 237
	}

238
	private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) {
239 240 241
		if (args == null) {
			return false;
		}
242
		String condition = getCondition();
243
		if (StringUtils.hasText(condition)) {
244 245 246
			Assert.notNull(this.evaluator, "EventExpressionEvaluator must not be null");
			return this.evaluator.condition(
					condition, event, this.targetMethod, this.methodKey, args, this.applicationContext);
247 248 249 250 251 252 253
		}
		return true;
	}

	/**
	 * Invoke the event listener method with the given argument values.
	 */
254
	@Nullable
255 256
	protected Object doInvoke(Object... args) {
		Object bean = getTargetBean();
257
		ReflectionUtils.makeAccessible(this.method);
258
		try {
259
			return this.method.invoke(bean, args);
260 261
		}
		catch (IllegalArgumentException ex) {
262
			assertTargetBean(this.method, bean, args);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
			throw new IllegalStateException(getInvocationErrorMessage(bean, ex.getMessage(), args), ex);
		}
		catch (IllegalAccessException ex) {
			throw new IllegalStateException(getInvocationErrorMessage(bean, ex.getMessage(), args), ex);
		}
		catch (InvocationTargetException ex) {
			// Throw underlying exception
			Throwable targetException = ex.getTargetException();
			if (targetException instanceof RuntimeException) {
				throw (RuntimeException) targetException;
			}
			else {
				String msg = getInvocationErrorMessage(bean, "Failed to invoke event listener method", args);
				throw new UndeclaredThrowableException(targetException, msg);
			}
		}
	}

	/**
	 * Return the target bean instance to use.
	 */
	protected Object getTargetBean() {
J
Juergen Hoeller 已提交
285
		Assert.notNull(this.applicationContext, "ApplicationContext must no be null");
286 287 288
		return this.applicationContext.getBean(this.beanName);
	}

289
	/**
290 291 292 293
	 * Return the condition to use.
	 * <p>Matches the {@code condition} attribute of the {@link EventListener}
	 * annotation or any matching attribute on a composed annotation that
	 * is meta-annotated with {@code @EventListener}.
294
	 */
295
	@Nullable
296 297 298 299
	protected String getCondition() {
		return this.condition;
	}

300 301 302 303 304 305 306 307 308
	/**
	 * Add additional details such as the bean type and method signature to
	 * the given error message.
	 * @param message error message to append the HandlerMethod details to
	 */
	protected String getDetailedErrorMessage(Object bean, String message) {
		StringBuilder sb = new StringBuilder(message).append("\n");
		sb.append("HandlerMethod details: \n");
		sb.append("Bean [").append(bean.getClass().getName()).append("]\n");
309
		sb.append("Method [").append(this.method.toGenericString()).append("]\n");
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		return sb.toString();
	}

	/**
	 * Assert that the target bean class is an instance of the class where the given
	 * method is declared. In some cases the actual bean instance at event-
	 * processing time may be a JDK dynamic proxy (lazy initialization, prototype
	 * beans, and others). Event listener beans that require proxying should prefer
	 * class-based proxy mechanisms.
	 */
	private void assertTargetBean(Method method, Object targetBean, Object[] args) {
		Class<?> methodDeclaringClass = method.getDeclaringClass();
		Class<?> targetBeanClass = targetBean.getClass();
		if (!methodDeclaringClass.isAssignableFrom(targetBeanClass)) {
			String msg = "The event listener method class '" + methodDeclaringClass.getName() +
J
Juergen Hoeller 已提交
325
					"' is not an instance of the actual bean class '" +
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
					targetBeanClass.getName() + "'. If the bean requires proxying " +
					"(e.g. due to @Transactional), please use class-based proxying.";
			throw new IllegalStateException(getInvocationErrorMessage(targetBean, msg, args));
		}
	}

	private String getInvocationErrorMessage(Object bean, String message, Object[] resolvedArgs) {
		StringBuilder sb = new StringBuilder(getDetailedErrorMessage(bean, message));
		sb.append("Resolved arguments: \n");
		for (int i = 0; i < resolvedArgs.length; i++) {
			sb.append("[").append(i).append("] ");
			if (resolvedArgs[i] == null) {
				sb.append("[null] \n");
			}
			else {
				sb.append("[type=").append(resolvedArgs[i].getClass().getName()).append("] ");
				sb.append("[value=").append(resolvedArgs[i]).append("]\n");
			}
		}
		return sb.toString();
	}

348
	@Nullable
349 350 351 352
	private ResolvableType getResolvableType(ApplicationEvent event) {
		ResolvableType payloadType = null;
		if (event instanceof PayloadApplicationEvent) {
			PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
353 354 355 356
			ResolvableType eventType = payloadEvent.getResolvableType();
			if (eventType != null) {
				payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
			}
357 358
		}
		for (ResolvableType declaredEventType : this.declaredEventTypes) {
359 360
			Class<?> eventClass = declaredEventType.toClass();
			if (!ApplicationEvent.class.isAssignableFrom(eventClass) &&
361 362
					payloadType != null && declaredEventType.isAssignableFrom(payloadType)) {
				return declaredEventType;
363
			}
364
			if (eventClass.isInstance(event)) {
365 366 367 368 369 370
				return declaredEventType;
			}
		}
		return null;
	}

J
Juergen Hoeller 已提交
371

372 373 374 375 376 377
	@Override
	public String toString() {
		return this.method.toGenericString();
	}

}