Search in sources :

Example 1 with PropertyBindingException

use of org.apache.camel.PropertyBindingException in project camel-spring-boot by apache.

the class SqlEndpointMisconfigureDataSourceTest method testFail.

@Test
public void testFail() {
    RouteBuilder rb = new RouteBuilder() {

        @Override
        public void configure() {
            from("direct:start").to("sql:foo?dataSource=myDataSource").to("mock:result");
        }
    };
    FailedToCreateRouteException e = assertThrows(FailedToCreateRouteException.class, () -> context.addRoutes(rb), "Should throw exception");
    PropertyBindingException pbe = (PropertyBindingException) e.getCause().getCause();
    assertEquals("dataSource", pbe.getPropertyName());
    assertEquals("myDataSource", pbe.getValue());
}
Also used : FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) PropertyBindingException(org.apache.camel.PropertyBindingException) RouteBuilder(org.apache.camel.builder.RouteBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) CamelSpringBootTest(org.apache.camel.test.spring.junit5.CamelSpringBootTest)

Example 2 with PropertyBindingException

use of org.apache.camel.PropertyBindingException in project camel-spring-boot by apache.

the class CamelPropertiesHelper method setCamelProperties.

/**
 * Sets the properties on the target bean.
 * <p/>
 * This method uses {@link PropertyBindingSupport} and therefore offers its capabilities such as:
 * <ul>
 *     <li>property placeholders - Keys and values using Camels property placeholder will be resolved</li>
 *     <li>nested - Properties can be nested using the dot syntax (OGNL and builder pattern using with as prefix), eg foo.bar=123</li>
 *     <li>map</li> - Properties can lookup in Map's using map syntax, eg foo[bar] where foo is the name of the property that is a Map instance, and bar is the name of the key.</li>
 *     <li>list</li> - Properties can refer or add to in List's using list syntax, eg foo[0] where foo is the name of the property that is a
 *                     List instance, and 0 is the index. To refer to the last element, then use last as key.</li>
 * </ul>
 * This implementation sets the properties using the following algorithm in the given order:
 * <ul>
 *     <li>reference by bean id - Values can refer to other beans in the registry by prefixing with with # or #bean: eg #myBean or #bean:myBean</li>
 *     <li>reference by type - Values can refer to singleton beans by their type in the registry by prefixing with #type: syntax, eg #type:com.foo.MyClassType</li>
 *     <li>autowire by type - Values can refer to singleton beans by auto wiring by setting the value to #autowired</li>
 *     <li>reference new class - Values can refer to creating new beans by their class name by prefixing with #class, eg #class:com.foo.MyClassType</li>
 *     <li>value as lookup - The value is used as-is (eg like #value) to lookup in the Registry if there is a bean then its set on the target</li>
 * </ul>
 * When an option has been set on the target bean, then its removed from the given properties map. If all the options has been set, then the map will be empty.
 * The implementation ignores case for the property keys.
 *
 * @param context        the CamelContext
 * @param target         the target bean
 * @param properties     the properties
 * @param failIfNotSet   whether to fail if an option either does not exists on the target bean or if the option cannot be due no suitable setter methods with the given type
 * @return <tt>true</tt> if at least one option was configured
 * @throws IllegalArgumentException is thrown if an option cannot be configured on the bean because there is no suitable setter method and failOnNoSet is true.
 */
public static boolean setCamelProperties(CamelContext context, Object target, Map<String, Object> properties, boolean failIfNotSet) {
    ObjectHelper.notNull(context, "context");
    ObjectHelper.notNull(target, "target");
    ObjectHelper.notNull(properties, "properties");
    boolean rc = false;
    PropertyConfigurer configurer = null;
    if (target instanceof Component) {
        // the component needs to be initialized to have the configurer ready
        ServiceHelper.initService(target);
        configurer = ((Component) target).getComponentPropertyConfigurer();
    }
    Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, Object> entry = it.next();
        String name = entry.getKey();
        Object value = entry.getValue();
        String stringValue = value != null ? value.toString() : null;
        boolean hit = false;
        try {
            hit = PropertyBindingSupport.build().withConfigurer(configurer).withIgnoreCase(true).bind(context, target, name, value);
        } catch (PropertyBindingException e) {
            // then maybe the value refers to a spring bean in the registry so try this
            if (stringValue != null) {
                if (stringValue.startsWith("#")) {
                    stringValue = stringValue.substring(1);
                }
                // use #bean: to lookup
                stringValue = "#bean:" + stringValue;
                hit = PropertyBindingSupport.build().withIgnoreCase(true).bind(context, target, name, stringValue);
            }
        }
        if (hit) {
            // must remove as its a valid option and we could configure it
            it.remove();
            rc = true;
        } else if (failIfNotSet) {
            throw new IllegalArgumentException("Cannot configure option [" + name + "] with value [" + stringValue + "] as the bean class [" + ObjectHelper.classCanonicalName(target) + "] has no suitable setter method, or not possible to lookup a bean with the id [" + stringValue + "] in Spring Boot registry");
        }
    }
    return rc;
}
Also used : PropertyBindingException(org.apache.camel.PropertyBindingException) PropertyConfigurer(org.apache.camel.spi.PropertyConfigurer) Component(org.apache.camel.Component) Map(java.util.Map)

Aggregations

PropertyBindingException (org.apache.camel.PropertyBindingException)2 Map (java.util.Map)1 Component (org.apache.camel.Component)1 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 PropertyConfigurer (org.apache.camel.spi.PropertyConfigurer)1 CamelSpringBootTest (org.apache.camel.test.spring.junit5.CamelSpringBootTest)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1