| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.spring; |
| 19 |
|
|
| 20 |
|
import org.aopalliance.intercept.MethodInvocation; |
| 21 |
|
import org.apache.camel.CamelContext; |
| 22 |
|
import org.apache.camel.Consumer; |
| 23 |
|
import org.apache.camel.Endpoint; |
| 24 |
|
import org.apache.camel.EndpointInject; |
| 25 |
|
import org.apache.camel.Exchange; |
| 26 |
|
import org.apache.camel.MessageDriven; |
| 27 |
|
import org.apache.camel.Processor; |
| 28 |
|
import org.apache.camel.Producer; |
| 29 |
|
import org.apache.camel.RuntimeCamelException; |
| 30 |
|
import org.apache.camel.CamelTemplate; |
| 31 |
|
import org.apache.camel.spring.util.BeanInfo; |
| 32 |
|
import org.apache.camel.spring.util.DefaultMethodInvocationStrategy; |
| 33 |
|
import org.apache.camel.spring.util.MethodInvocationStrategy; |
| 34 |
|
import org.apache.camel.spring.util.ReflectionUtils; |
| 35 |
|
import org.apache.camel.util.ObjectHelper; |
| 36 |
|
import static org.apache.camel.util.ObjectHelper.isNotNullOrBlank; |
| 37 |
|
import org.apache.commons.logging.Log; |
| 38 |
|
import org.apache.commons.logging.LogFactory; |
| 39 |
|
import org.springframework.beans.BeansException; |
| 40 |
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 41 |
|
import org.springframework.beans.factory.config.BeanPostProcessor; |
| 42 |
|
import org.springframework.context.ApplicationContext; |
| 43 |
|
import org.springframework.context.ApplicationContextAware; |
| 44 |
|
|
| 45 |
|
import java.lang.reflect.Field; |
| 46 |
|
import java.lang.reflect.Method; |
| 47 |
|
import java.util.ArrayList; |
| 48 |
|
import java.util.List; |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
1 |
public class CamelBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { |
| 57 |
1 |
private static final transient Log log = LogFactory.getLog(CamelBeanPostProcessor.class); |
| 58 |
|
|
| 59 |
|
private CamelContext camelContext; |
| 60 |
|
private ApplicationContext applicationContext; |
| 61 |
4 |
private MethodInvocationStrategy invocationStrategy = new DefaultMethodInvocationStrategy(); |
| 62 |
|
|
| 63 |
|
|
| 64 |
4 |
public CamelBeanPostProcessor() { |
| 65 |
4 |
} |
| 66 |
|
|
| 67 |
|
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 68 |
14 |
injectFields(bean); |
| 69 |
14 |
injectMethods(bean); |
| 70 |
14 |
return bean; |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 74 |
15 |
return bean; |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 81 |
4 |
this.applicationContext = applicationContext; |
| 82 |
4 |
} |
| 83 |
|
|
| 84 |
|
public MethodInvocationStrategy getInvocationStrategy() { |
| 85 |
0 |
return invocationStrategy; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
public void setInvocationStrategy(MethodInvocationStrategy invocationStrategy) { |
| 89 |
0 |
this.invocationStrategy = invocationStrategy; |
| 90 |
0 |
} |
| 91 |
|
|
| 92 |
|
public CamelContext getCamelContext() { |
| 93 |
7 |
return camelContext; |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
public void setCamelContext(CamelContext camelContext) { |
| 97 |
4 |
this.camelContext = camelContext; |
| 98 |
4 |
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
protected void injectFields(final Object bean) { |
| 109 |
14 |
ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() { |
| 110 |
14 |
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { |
| 111 |
52 |
EndpointInject annotation = field.getAnnotation(EndpointInject.class); |
| 112 |
52 |
if (annotation != null) { |
| 113 |
8 |
ReflectionUtils.setField(field, bean, getEndpointInjectionValue(annotation, field.getType())); |
| 114 |
|
} |
| 115 |
52 |
} |
| 116 |
|
}); |
| 117 |
14 |
} |
| 118 |
|
|
| 119 |
|
protected void injectMethods(final Object bean) { |
| 120 |
14 |
ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() { |
| 121 |
|
@SuppressWarnings("unchecked") |
| 122 |
14 |
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { |
| 123 |
288 |
setterInjection(method, bean); |
| 124 |
288 |
consumerInjection(method, bean); |
| 125 |
288 |
} |
| 126 |
|
}); |
| 127 |
14 |
} |
| 128 |
|
|
| 129 |
|
protected void setterInjection(Method method, Object bean) { |
| 130 |
288 |
EndpointInject annoation = method.getAnnotation(EndpointInject.class); |
| 131 |
288 |
if (annoation != null) { |
| 132 |
3 |
Class<?>[] parameterTypes = method.getParameterTypes(); |
| 133 |
3 |
if (parameterTypes != null) { |
| 134 |
3 |
if (parameterTypes.length != 1) { |
| 135 |
0 |
log.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: " + method); |
| 136 |
0 |
} |
| 137 |
|
else { |
| 138 |
3 |
Object value = getEndpointInjectionValue(annoation, parameterTypes[0]); |
| 139 |
3 |
ObjectHelper.invokeMethod(method, bean, value); |
| 140 |
|
} |
| 141 |
|
} |
| 142 |
|
} |
| 143 |
288 |
} |
| 144 |
|
|
| 145 |
|
protected void consumerInjection(final Object bean) { |
| 146 |
0 |
ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() { |
| 147 |
|
@SuppressWarnings("unchecked") |
| 148 |
0 |
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { |
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
0 |
} |
| 176 |
|
}); |
| 177 |
0 |
} |
| 178 |
|
|
| 179 |
|
protected void consumerInjection(Method method, Object bean) { |
| 180 |
288 |
MessageDriven annotation = method.getAnnotation(MessageDriven.class); |
| 181 |
288 |
if (annotation != null) { |
| 182 |
1 |
log.info("Creating a consumer for: " + annotation); |
| 183 |
|
|
| 184 |
|
|
| 185 |
1 |
Endpoint endpoint = getEndpointInjection(annotation.uri(), annotation.name()); |
| 186 |
1 |
if (endpoint != null) { |
| 187 |
|
try { |
| 188 |
1 |
Processor processor = createConsumerProcessor(bean, method, endpoint); |
| 189 |
1 |
log.info("Created processor: " + processor); |
| 190 |
1 |
Consumer consumer = endpoint.createConsumer(processor); |
| 191 |
1 |
consumer.start(); |
| 192 |
1 |
addConsumer(consumer); |
| 193 |
|
} |
| 194 |
0 |
catch (Exception e) { |
| 195 |
0 |
log.warn(e); |
| 196 |
0 |
throw new RuntimeCamelException(e); |
| 197 |
1 |
} |
| 198 |
|
} |
| 199 |
|
} |
| 200 |
288 |
} |
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
|
| 205 |
|
protected Processor createConsumerProcessor(final Object pojo, final Method method, final Endpoint endpoint) { |
| 206 |
1 |
final BeanInfo beanInfo = new BeanInfo(pojo.getClass(), invocationStrategy); |
| 207 |
|
|
| 208 |
1 |
return new Processor() { |
| 209 |
|
@Override |
| 210 |
|
public String toString() { |
| 211 |
1 |
return "Processor on " + endpoint; |
| 212 |
|
} |
| 213 |
|
|
| 214 |
1 |
public void process(Exchange exchange) throws Exception { |
| 215 |
1 |
if (log.isDebugEnabled()) { |
| 216 |
0 |
log.debug(">>>> invoking method for: " + exchange); |
| 217 |
|
} |
| 218 |
1 |
MethodInvocation invocation = beanInfo.createInvocation(method, pojo, exchange); |
| 219 |
1 |
if (invocation == null) { |
| 220 |
0 |
throw new IllegalStateException("No method invocation could be created"); |
| 221 |
|
} |
| 222 |
|
try { |
| 223 |
1 |
invocation.proceed(); |
| 224 |
|
} |
| 225 |
0 |
catch (Exception e) { |
| 226 |
0 |
throw e; |
| 227 |
|
} |
| 228 |
0 |
catch (Throwable throwable) { |
| 229 |
0 |
throw new Exception(throwable); |
| 230 |
1 |
} |
| 231 |
1 |
} |
| 232 |
|
}; |
| 233 |
|
} |
| 234 |
|
|
| 235 |
|
protected void addConsumer(Consumer consumer) { |
| 236 |
1 |
log.debug("Adding consumer: " + consumer); |
| 237 |
|
|
| 238 |
1 |
} |
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
|
|
| 243 |
|
protected Object getEndpointInjectionValue(EndpointInject annotation, Class<?> type) { |
| 244 |
11 |
Endpoint endpoint = getEndpointInjection(annotation.uri(), annotation.name()); |
| 245 |
11 |
if (endpoint != null) { |
| 246 |
11 |
if (type.isInstance(endpoint)) { |
| 247 |
2 |
return endpoint; |
| 248 |
|
} |
| 249 |
9 |
else if (type.isAssignableFrom(Producer.class)) { |
| 250 |
|
try { |
| 251 |
2 |
return endpoint.createProducer(); |
| 252 |
|
} |
| 253 |
0 |
catch (Exception e) { |
| 254 |
0 |
throw new RuntimeCamelException(e); |
| 255 |
|
} |
| 256 |
|
} |
| 257 |
7 |
else if (type.isAssignableFrom(CamelTemplate.class)) { |
| 258 |
7 |
return new CamelTemplate(getCamelContext(), endpoint); |
| 259 |
|
} |
| 260 |
|
} |
| 261 |
0 |
return null; |
| 262 |
|
} |
| 263 |
|
|
| 264 |
|
protected Endpoint getEndpointInjection(String uri, String name) { |
| 265 |
12 |
Endpoint endpoint = null; |
| 266 |
12 |
if (isNotNullOrBlank(uri)) { |
| 267 |
11 |
endpoint = camelContext.getEndpoint(uri); |
| 268 |
11 |
} |
| 269 |
|
else { |
| 270 |
1 |
if (isNotNullOrBlank(name)) { |
| 271 |
1 |
endpoint = (Endpoint) applicationContext.getBean(name); |
| 272 |
1 |
if (endpoint == null) { |
| 273 |
0 |
throw new NoSuchBeanDefinitionException(name); |
| 274 |
|
} |
| 275 |
|
} |
| 276 |
|
else { |
| 277 |
0 |
log.warn("No uri or name specified on @EndpointInject annotation!"); |
| 278 |
|
} |
| 279 |
|
} |
| 280 |
12 |
return endpoint; |
| 281 |
|
} |
| 282 |
|
} |