Search in sources :

Example 6 with NoTypeConversionAvailableException

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;
    }
}
Also used : NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) Method(java.lang.reflect.Method) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) URISyntaxException(java.net.URISyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with NoTypeConversionAvailableException

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);
}
Also used : CamelContext(org.apache.camel.CamelContext) TypeConverter(org.apache.camel.TypeConverter) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException)

Example 8 with NoTypeConversionAvailableException

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);
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) TypeConversionException(org.apache.camel.TypeConversionException)

Example 9 with 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
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) Timestamp(java.sql.Timestamp)

Example 10 with NoTypeConversionAvailableException

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
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException)

Aggregations

NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)23 Exchange (org.apache.camel.Exchange)7 Map (java.util.Map)4 CamelExecutionException (org.apache.camel.CamelExecutionException)4 DefaultExchange (org.apache.camel.impl.DefaultExchange)4 Method (java.lang.reflect.Method)3 TypeConverter (org.apache.camel.TypeConverter)3 InputStream (java.io.InputStream)2 Serializable (java.io.Serializable)2 URI (java.net.URI)2 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 BytesMessage (javax.jms.BytesMessage)2 JMSException (javax.jms.JMSException)2 MapMessage (javax.jms.MapMessage)2 MessageFormatException (javax.jms.MessageFormatException)2 ObjectMessage (javax.jms.ObjectMessage)2 TextMessage (javax.jms.TextMessage)2 CamelContext (org.apache.camel.CamelContext)2