Search in sources :

Example 1 with PropertyConfigurer

use of org.apache.camel.spi.PropertyConfigurer in project camel-k-runtime by apache.

the class PropertiesSupport method bindProperties.

public static <T> T bindProperties(CamelContext context, T target, Predicate<String> filter, String prefix, boolean stripPrefix) {
    final PropertiesComponent component = context.getPropertiesComponent();
    final Properties propertiesWithPrefix = component.loadProperties(filter);
    final Map<String, Object> properties = new HashMap<>();
    propertiesWithPrefix.stringPropertyNames().forEach(name -> properties.put(stripPrefix ? name.substring(prefix.length()) : name, propertiesWithPrefix.getProperty(name)));
    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();
    }
    if (configurer == null) {
        String name = target.getClass().getName();
        if (target instanceof ExtendedCamelContext) {
            // special for camel context itself as we have an extended configurer
            name = ExtendedCamelContext.class.getName();
        }
        // see if there is a configurer for it
        configurer = context.adapt(ExtendedCamelContext.class).getConfigurerResolver().resolvePropertyConfigurer(name, context);
    }
    PropertyBindingSupport.build().withIgnoreCase(true).withCamelContext(context).withTarget(target).withProperties(properties).withRemoveParameters(true).withOptionPrefix(stripPrefix ? null : prefix).withConfigurer(configurer).bind();
    return target;
}
Also used : HashMap(java.util.HashMap) ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) PropertyConfigurer(org.apache.camel.spi.PropertyConfigurer) Properties(java.util.Properties) PropertiesComponent(org.apache.camel.spi.PropertiesComponent) Component(org.apache.camel.Component) PropertiesComponent(org.apache.camel.spi.PropertiesComponent)

Example 2 with PropertyConfigurer

use of org.apache.camel.spi.PropertyConfigurer 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

Component (org.apache.camel.Component)2 PropertyConfigurer (org.apache.camel.spi.PropertyConfigurer)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ExtendedCamelContext (org.apache.camel.ExtendedCamelContext)1 PropertyBindingException (org.apache.camel.PropertyBindingException)1 PropertiesComponent (org.apache.camel.spi.PropertiesComponent)1