This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new f80ec9f CAMEL-17149: camel-main - Binding beans should allow to refer to other beans in the constructor parameters f80ec9f is described below commit f80ec9f616aa21b00b8b61cffdc264cde69f3ca9 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Oct 30 10:56:36 2021 +0200 CAMEL-17149: camel-main - Binding beans should allow to refer to other beans in the constructor parameters --- .../java/org/apache/camel/main/MyAddress.java | 36 ++++++++++++++++ .../camel/main/MainBeansConstructorTest.java | 49 ++++++++++++++++++++++ .../test/java/org/apache/camel/main/MyOrder.java | 36 ++++++++++++++++ .../camel/support/PropertyBindingSupport.java | 15 ++++++- 4 files changed, 135 insertions(+), 1 deletion(-) diff --git a/core/camel-main/src/generated/java/org/apache/camel/main/MyAddress.java b/core/camel-main/src/generated/java/org/apache/camel/main/MyAddress.java new file mode 100644 index 0000000..e0f59d8 --- /dev/null +++ b/core/camel-main/src/generated/java/org/apache/camel/main/MyAddress.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.main; + +public class MyAddress { + + private int zip; + private String street; + + public MyAddress(int zip, String street) { + this.zip = zip; + this.street = street; + } + + public int getZip() { + return zip; + } + + public String getStreet() { + return street; + } +} diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainBeansConstructorTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainBeansConstructorTest.java new file mode 100644 index 0000000..bdadb3f --- /dev/null +++ b/core/camel-main/src/test/java/org/apache/camel/main/MainBeansConstructorTest.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.main; + +import org.apache.camel.CamelContext; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class MainBeansConstructorTest { + + @Test + public void testBindBeans() throws Exception { + Main main = new Main(); + // create by class + main.addProperty("camel.beans.address", "#class:org.apache.camel.main.MyAddress(90210, 'Somestreet 123')"); + main.addProperty("camel.beans.order", "#class:org.apache.camel.main.MyOrder('Acme', #bean:address)"); + // start + main.start(); + + CamelContext camelContext = main.getCamelContext(); + assertNotNull(camelContext); + + MyOrder order = camelContext.getRegistry().lookupByNameAndType("order", MyOrder.class); + assertNotNull(order); + + assertEquals("Acme", order.getCompany()); + assertEquals(90210, order.getAddress().getZip()); + assertEquals("Somestreet 123", order.getAddress().getStreet()); + + main.stop(); + } + +} diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MyOrder.java b/core/camel-main/src/test/java/org/apache/camel/main/MyOrder.java new file mode 100644 index 0000000..376d08b --- /dev/null +++ b/core/camel-main/src/test/java/org/apache/camel/main/MyOrder.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.main; + +public class MyOrder { + + private String company; + private MyAddress address; + + public MyOrder(String company, MyAddress address) { + this.company = company; + this.address = address; + } + + public String getCompany() { + return company; + } + + public MyAddress getAddress() { + return address; + } +} diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java index ca65f08..ee379b9 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java @@ -1200,7 +1200,20 @@ public final class PropertyBindingSupport { for (int i = 0; i < found.getParameterCount(); i++) { Class<?> paramType = found.getParameterTypes()[i]; Object param = params[i]; - Object val = camelContext.getTypeConverter().convertTo(paramType, param); + Object val = null; + // special as we may refer to other #bean or #type in the parameter + if (param instanceof String) { + String str = param.toString(); + if (str.startsWith("#")) { + Object bean = resolveBean(camelContext, param); + if (bean != null) { + val = bean; + } + } + } + if (val == null) { + val = camelContext.getTypeConverter().convertTo(paramType, param); + } // unquote text if (val instanceof String) { val = StringHelper.removeLeadingAndEndingQuotes((String) val);