use of java.lang.reflect.Field in project camel by apache.
the class CamelPostProcessorHelperTest method testBeanInject.
public void testBeanInject() throws Exception {
CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
MyBeanInjectBean bean = new MyBeanInjectBean();
Field field = bean.getClass().getField("foo");
BeanInject beanInject = field.getAnnotation(BeanInject.class);
Class<?> type = field.getType();
Object value = helper.getInjectionBeanValue(type, beanInject.value());
field.set(bean, value);
String out = bean.doSomething("World");
assertEquals("Hello World", out);
}
use of java.lang.reflect.Field in project camel by apache.
the class CamelPostProcessorHelperTest method testBeanInjectByType.
public void testBeanInjectByType() throws Exception {
CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
MyBeanInjectByTypeBean bean = new MyBeanInjectByTypeBean();
Field field = bean.getClass().getField("foo");
BeanInject beanInject = field.getAnnotation(BeanInject.class);
Class<?> type = field.getType();
Object value = helper.getInjectionBeanValue(type, beanInject.value());
field.set(bean, value);
String out = bean.doSomething("Camel");
assertEquals("Hello Camel", out);
}
use of java.lang.reflect.Field in project camel by apache.
the class CamelPostProcessorHelperTest method testEndpointInjectProducerTemplateFieldUrlUnknown.
public void testEndpointInjectProducerTemplateFieldUrlUnknown() throws Exception {
CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
MyEndpointInjectProducerTemplateUrlUnknown bean = new MyEndpointInjectProducerTemplateUrlUnknown();
Field field = bean.getClass().getField("producer");
EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
Class<?> type = field.getType();
String propertyName = "producer";
try {
helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
fail("Should throw exception");
} catch (ResolveEndpointFailedException e) {
assertEquals("Failed to resolve endpoint: xxx://foo due to: No component found with scheme: xxx", e.getMessage());
}
}
use of java.lang.reflect.Field in project camel by apache.
the class CamelPostProcessorHelperTest method testEndpointInjectBothUriAndRef.
public void testEndpointInjectBothUriAndRef() throws Exception {
CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
MyEndpointBothUriAndRef bean = new MyEndpointBothUriAndRef();
Field field = bean.getClass().getField("producer");
EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
Class<?> type = field.getType();
String propertyName = "producer";
try {
helper.getInjectionValue(type, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), propertyName, bean, "foo");
fail("Should throw exception");
} catch (IllegalArgumentException e) {
assertEquals("Both uri and name is provided, only either one is allowed: uri=seda:foo, ref=myEndpoint", e.getMessage());
}
}
use of java.lang.reflect.Field in project camel by apache.
the class AvroComponent method applyToConfiguration.
/**
* Applies endpoint parameters to configuration & resolves protocol and other required configuration properties.
*/
private void applyToConfiguration(AvroConfiguration config, URI endpointUri, Map<String, Object> parameters) throws Exception {
config.parseURI(endpointUri, parameters, this);
setProperties(config, parameters);
if (config.getProtocol() == null && config.getProtocolClassName() != null) {
Class<?> protocolClass = getCamelContext().getClassResolver().resolveClass(config.getProtocolClassName());
if (protocolClass != null) {
try {
Field f = protocolClass.getField("PROTOCOL");
if (f != null) {
Protocol protocol = (Protocol) f.get(null);
config.setProtocol(protocol);
}
} catch (NoSuchFieldException e) {
ReflectData reflectData = ReflectData.get();
config.setProtocol(reflectData.getProtocol(protocolClass));
config.setReflectionProtocol(true);
}
}
}
if (config.getProtocol() == null) {
throw new IllegalArgumentException("Avro configuration does not contain protocol");
}
if (config.getMessageName() != null && !config.getProtocol().getMessages().containsKey(config.getMessageName())) {
throw new IllegalArgumentException("Message " + config.getMessageName() + " is not defined in protocol");
}
if (config.isSingleParameter()) {
Map<String, Protocol.Message> messageMap = config.getProtocol().getMessages();
Iterable<Protocol.Message> messagesToCheck = config.getMessageName() == null ? messageMap.values() : Collections.singleton(messageMap.get(config.getMessageName()));
for (Protocol.Message message : messagesToCheck) {
if (message.getRequest().getFields().size() != 1) {
throw new IllegalArgumentException("Single parameter option can't be used with message " + message.getName() + " because it has " + message.getRequest().getFields().size() + " parameters defined");
}
}
}
}
Aggregations