-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
LambdaMessageProcessor.java
151 lines (132 loc) · 5.14 KB
/
LambdaMessageProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2016-2018 the original author or authors.
*
* 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.integration.handler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* The {@link MessageProcessor} implementation for method invocation on the single method classes
* - functional interface implementations.
*
* @author Artem Bilan
* @author Gary Russell
*
* @since 5.0
*/
public class LambdaMessageProcessor implements MessageProcessor<Object>, BeanFactoryAware {
private static final Log logger = LogFactory.getLog(LambdaMessageProcessor.class); // NOSONAR lower case static
private final Object target;
private final Method method;
private final Class<?> payloadType;
private final Class<?>[] parameterTypes;
private MessageConverter messageConverter;
public LambdaMessageProcessor(Object target, Class<?> payloadType) {
Assert.notNull(target, "'target' must not be null");
this.target = target;
final AtomicReference<Method> methodValue = new AtomicReference<>();
ReflectionUtils.doWithMethods(target.getClass(),
methodValue::set,
methodCandidate -> {
boolean isCandidate = !methodCandidate.isBridge()
&& !methodCandidate.isDefault()
&& methodCandidate.getDeclaringClass() != Object.class
&& Modifier.isPublic(methodCandidate.getModifiers())
&& !Modifier.isStatic(methodCandidate.getModifiers());
if (isCandidate) {
Assert.isNull(methodValue.get(), "LambdaMessageProcessor is applicable for inline or lambda " +
"classes with single method - functional interface implementations.");
}
return isCandidate;
});
Assert.notNull(methodValue.get(), "LambdaMessageProcessor is applicable for inline or lambda " +
"classes with single method - functional interface implementations.");
this.method = methodValue.get();
this.method.setAccessible(true);
this.parameterTypes = this.method.getParameterTypes();
this.payloadType = payloadType;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.messageConverter =
beanFactory.getBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME,
MessageConverter.class);
}
@Override
public Object processMessage(Message<?> message) {
Object[] args = buildArgs(message);
try {
return this.method.invoke(this.target, args);
}
catch (InvocationTargetException e) {
if (e.getTargetException() instanceof ClassCastException) {
logger.error("Could not invoke the method due to a class cast exception, if using a lambda in the DSL, "
+ "consider using an overloaded EIP method that takes a Class<?> argument to explicitly "
+ "specify the type. An example of when this often occurs is if the lambda is configured to "
+ "receive a Message<?> argument.", e.getCause());
}
throw new MessageHandlingException(message, e.getCause());
}
catch (Exception e) {
throw new MessageHandlingException(message, e);
}
}
private Object[] buildArgs(Message<?> message) {
Object[] args = new Object[this.parameterTypes.length];
for (int i = 0; i < this.parameterTypes.length; i++) {
Class<?> parameterType = this.parameterTypes[i];
if (Message.class.isAssignableFrom(parameterType)) {
args[i] = message;
}
else if (Map.class.isAssignableFrom(parameterType)) {
if (message.getPayload() instanceof Map && this.parameterTypes.length == 1) {
args[i] = message.getPayload();
}
else {
args[i] = message.getHeaders();
}
}
else {
if (this.payloadType != null &&
!ClassUtils.isAssignable(this.payloadType, message.getPayload().getClass())) {
if (Message.class.isAssignableFrom(this.payloadType)) {
args[i] = message;
}
else {
args[i] = this.messageConverter.fromMessage(message, this.payloadType);
}
}
else {
args[i] = message.getPayload();
}
}
}
return args;
}
}