use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.
the class IntrospectionSupport method setProperty.
/**
* This method supports two modes to set a property:
*
* 1. Setting a property that has already been resolved, this is the case when {@code context} and {@code refName} are
* NULL and {@code value} is non-NULL.
*
* 2. Setting a property that has not yet been resolved, the property will be resolved based on the suitable methods
* found matching the property name on the {@code target} bean. For this mode to be triggered the parameters
* {@code context} and {@code refName} must NOT be NULL, and {@code value} MUST be NULL.
*
*/
public static boolean setProperty(CamelContext context, TypeConverter typeConverter, Object target, String name, Object value, String refName, boolean allowBuilderPattern) throws Exception {
Class<?> clazz = target.getClass();
Collection<Method> setters;
// we need to lookup the value from the registry
if (context != null && refName != null && value == null) {
setters = findSetterMethodsOrderedByParameterType(clazz, name, allowBuilderPattern);
} else {
// find candidates of setter methods as there can be overloaded setters
setters = findSetterMethods(clazz, name, value, allowBuilderPattern);
}
if (setters.isEmpty()) {
return false;
}
// loop and execute the best setter method
Exception typeConversionFailed = null;
for (Method setter : setters) {
Class<?> parameterType = setter.getParameterTypes()[0];
Object ref = value;
// try and lookup the reference based on the method
if (context != null && refName != null && ref == null) {
String s = StringHelper.replaceAll(refName, "#", "");
ref = CamelContextHelper.lookup(context, s);
if (ref == null) {
// try the next method if nothing was found
continue;
} else {
// setter method has not the correct type
// (must use ObjectHelper.isAssignableFrom which takes primitive types into account)
boolean assignable = isAssignableFrom(parameterType, ref.getClass());
if (!assignable) {
continue;
}
}
}
try {
try {
// If the type is null or it matches the needed type, just use the value directly
if (value == null || isAssignableFrom(parameterType, ref.getClass())) {
// we may want to set options on classes that has package view visibility, so override the accessible
setter.setAccessible(true);
setter.invoke(target, ref);
if (LOG.isDebugEnabled()) {
LOG.debug("Configured property: {} on bean: {} with value: {}", new Object[] { name, target, ref });
}
return true;
} else {
// We need to convert it
Object convertedValue = convert(typeConverter, parameterType, ref);
// we may want to set options on classes that has package view visibility, so override the accessible
setter.setAccessible(true);
setter.invoke(target, convertedValue);
if (LOG.isDebugEnabled()) {
LOG.debug("Configured property: {} on bean: {} with value: {}", new Object[] { name, target, ref });
}
return true;
}
} catch (InvocationTargetException e) {
// lets unwrap the exception
Throwable throwable = e.getCause();
if (throwable instanceof Exception) {
Exception exception = (Exception) throwable;
throw exception;
} else {
Error error = (Error) throwable;
throw error;
}
}
// ignore exceptions as there could be another setter method where we could type convert successfully
} catch (SecurityException e) {
typeConversionFailed = e;
} catch (NoTypeConversionAvailableException e) {
typeConversionFailed = e;
} catch (IllegalArgumentException e) {
typeConversionFailed = e;
}
if (LOG.isTraceEnabled()) {
LOG.trace("Setter \"{}\" with parameter type \"{}\" could not be used for type conversions of {}", new Object[] { setter, parameterType, ref });
}
}
if (typeConversionFailed != null && !isPropertyPlaceholder(context, value)) {
// this kind of exception as the caused by will hint this error
throw new IllegalArgumentException("Could not find a suitable setter for property: " + name + " as there isn't a setter method with same type: " + (value != null ? value.getClass().getCanonicalName() : "[null]") + " nor type conversion possible: " + typeConversionFailed.getMessage());
} else {
return false;
}
}
use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.
the class ExchangeHelper method convertToMandatoryType.
/**
* Converts the value to the given expected type or throws an exception
*
* @return the converted value
* @throws TypeConversionException is thrown if error during type conversion
* @throws NoTypeConversionAvailableException} if no type converters exists to convert to the given type
*/
public static <T> T convertToMandatoryType(Exchange exchange, Class<T> type, Object value) throws TypeConversionException, NoTypeConversionAvailableException {
CamelContext camelContext = exchange.getContext();
ObjectHelper.notNull(camelContext, "CamelContext of Exchange");
TypeConverter converter = camelContext.getTypeConverter();
if (converter != null) {
return converter.mandatoryConvertTo(type, exchange, value);
}
throw new NoTypeConversionAvailableException(value, type);
}
use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.
the class ConverterTest method testMandatoryConvertTo.
public void testMandatoryConvertTo() {
CamelContext camel = new DefaultCamelContext();
Exchange e = new DefaultExchange(camel);
try {
converter.mandatoryConvertTo(InputStream.class, e);
fail("Expect exception here");
} catch (Exception ex) {
assertTrue("Expect to get a NoTypeConversionAvailableException here", ex instanceof NoTypeConversionAvailableException);
}
}
use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.
the class StaticFallbackConverterTest method testStaticFallbackMandatoryFailed.
public void testStaticFallbackMandatoryFailed() throws Exception {
Exchange exchange = new DefaultExchange(context);
try {
context.getTypeConverter().mandatoryConvertTo(Date.class, exchange, new Timestamp(0));
fail("Should have thrown an exception");
} catch (NoTypeConversionAvailableException e) {
// expected
}
}
use of org.apache.camel.NoTypeConversionAvailableException in project camel by apache.
the class FutureConverterTest method testConvertMandatoryFutureWithExchangeFailed.
public void testConvertMandatoryFutureWithExchangeFailed() throws Exception {
Exchange exchange = new DefaultExchange(context);
Future<?> future = template.asyncRequestBody("direct:foo", "Hello World");
try {
context.getTypeConverter().mandatoryConvertTo(Timestamp.class, exchange, future);
fail("Should have thrown an exception");
} catch (NoTypeConversionAvailableException e) {
// expected
}
}
Aggregations