CamelPropertiesHelper { 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) { 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) { if (stringValue != null) { if (stringValue.startsWith("#")) { stringValue = stringValue.substring(1); } stringValue = "#bean:" + stringValue; hit = PropertyBindingSupport.build().withIgnoreCase(true).bind(context, target, name, stringValue); } } if (hit) { 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; } private  CamelPropertiesHelper(); @SuppressWarnings({"unchecked", "deprecation"}) static void copyProperties(CamelContext camelContext, Object source, Object target); static boolean setCamelProperties(CamelContext context, Object target, Map<String, Object> properties, boolean failIfNotSet);  }
@Test public void testSetCamelProperties() throws Exception { MyClass target = new MyClass(); Map<String, Object> map = new HashMap<>(); map.put("id", "123"); map.put("name", "Donald Duck"); map.put("option", "myCoolOption"); CamelPropertiesHelper.setCamelProperties(camelContext, target, map, true); Assert.assertEquals("Should configure all options", 0, map.size()); Assert.assertEquals(123, target.getId()); Assert.assertEquals("Donald Duck", target.getName()); Assert.assertSame(context.getBean("myCoolOption"), target.getOption()); }
@Test public void testSetCamelPropertiesAutowired() throws Exception { MyClass target = new MyClass(); Map<String, Object> map = new HashMap<>(); map.put("id", "123"); map.put("name", "Donald Duck"); map.put("option", "myCoolOption"); map.put("camelContext", "#autowired"); CamelPropertiesHelper.setCamelProperties(camelContext, target, map, true); Assert.assertEquals("Should configure all options", 0, map.size()); Assert.assertEquals(123, target.getId()); Assert.assertEquals("Donald Duck", target.getName()); Assert.assertSame(context.getBean("myCoolOption"), target.getOption()); Assert.assertSame(camelContext, target.getCamelContext()); }
@Test public void testSetCamelPropertiesType() throws Exception { MyClass target = new MyClass(); Map<String, Object> map = new HashMap<>(); map.put("id", "123"); map.put("name", "Donald Duck"); map.put("option", "myCoolOption"); map.put("camelContext", "#type:org.apache.camel.CamelContext"); CamelPropertiesHelper.setCamelProperties(camelContext, target, map, true); Assert.assertEquals("Should configure all options", 0, map.size()); Assert.assertEquals(123, target.getId()); Assert.assertEquals("Donald Duck", target.getName()); Assert.assertSame(context.getBean("myCoolOption"), target.getOption()); Assert.assertSame(camelContext, target.getCamelContext()); }
@Test public void testSetCamelPropertiesClass() throws Exception { MyClass target = new MyClass(); Map<String, Object> map = new LinkedHashMap<>(); map.put("id", "123"); map.put("name", "Donald Duck"); map.put("option", "myCoolOption"); map.put("camelContext", "#type:org.apache.camel.CamelContext"); map.put("myFooClass", "#class:org.apache.camel.spring.boot.util.MyFooClass"); map.put("myFooClass.name", "Goofy"); CamelPropertiesHelper.setCamelProperties(camelContext, target, map, true); Assert.assertEquals("Should configure all options", 0, map.size()); Assert.assertEquals(123, target.getId()); Assert.assertEquals("Donald Duck", target.getName()); Assert.assertSame(context.getBean("myCoolOption"), target.getOption()); Assert.assertSame(camelContext, target.getCamelContext()); MyFooClass myFooClass = target.getMyFooClass(); Assert.assertNotNull(myFooClass); Assert.assertSame(camelContext, myFooClass.getCamelContext()); Assert.assertEquals("Goofy", myFooClass.getName()); }
@Test public void testSetCamelPropertiesReference() throws Exception { MyClass target = new MyClass(); Map<String, Object> map = new HashMap<>(); map.put("id", "123"); map.put("name", "Donald Duck"); map.put("option", "#myCoolOption"); CamelPropertiesHelper.setCamelProperties(camelContext, target, map, true); Assert.assertEquals("Should configure all options", 0, map.size()); Assert.assertEquals(123, target.getId()); Assert.assertEquals("Donald Duck", target.getName()); Assert.assertSame(context.getBean("myCoolOption"), target.getOption()); }
@Test public void testSetCamelPropertiesUnknownOption() throws Exception { MyClass target = new MyClass(); Map<String, Object> map = new LinkedHashMap<>(); map.put("id", "123"); map.put("name", "Donald Duck"); map.put("option", "#myCoolOption"); map.put("unknown", "foo"); try { CamelPropertiesHelper.setCamelProperties(camelContext, target, map, true); Assert.fail("Should have thrown exception"); } catch (IllegalArgumentException e) { Assert.assertTrue(e.getMessage().startsWith("Cannot configure option [unknown] with value [foo]")); } Assert.assertEquals("Should configure the three first options", 1, map.size()); Assert.assertEquals(123, target.getId()); Assert.assertEquals("Donald Duck", target.getName()); Assert.assertSame(context.getBean("myCoolOption"), target.getOption()); }
@Test public void testSetCamelPropertiesUnknownOptionIgnore() throws Exception { MyClass target = new MyClass(); Map<String, Object> map = new LinkedHashMap<>(); map.put("id", "123"); map.put("name", "Donald Duck"); map.put("option", "#myCoolOption"); map.put("unknown", "foo"); CamelPropertiesHelper.setCamelProperties(camelContext, target, map, false); Assert.assertEquals("Should configure the three first options", 1, map.size()); Assert.assertEquals(123, target.getId()); Assert.assertEquals("Donald Duck", target.getName()); Assert.assertSame(context.getBean("myCoolOption"), target.getOption()); }