This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch bean in repository https://gitbox.apache.org/repos/asf/camel.git
commit 14244e532272d0b5269c155ff842e2dcf7970732 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Apr 24 18:36:43 2019 +0200 CAMEL-13449: camel3 - Move bean component out of camel-core --- core/camel-core/src/main/docs/bean-language.adoc | 2 +- .../apache/camel/language/bean/BeanExpression.java | 74 ++++++++++++++-- .../camel/model/language/MethodCallExpression.java | 99 +++++----------------- .../util/DumpModelAsXmlRouteExpressionTest.java | 2 +- .../util/DumpModelAsXmlRoutePredicateTest.java | 2 +- .../bean/springboot/BeanLanguageConfiguration.java | 4 - 6 files changed, 89 insertions(+), 94 deletions(-) diff --git a/core/camel-core/src/main/docs/bean-language.adoc b/core/camel-core/src/main/docs/bean-language.adoc index ecab01d..a9f37a9 100644 --- a/core/camel-core/src/main/docs/bean-language.adoc +++ b/core/camel-core/src/main/docs/bean-language.adoc @@ -34,7 +34,7 @@ The Bean method language supports 4 options, which are listed below. | ref | | String | Reference to bean to lookup in the registry | method | | String | Name of method to call | beanType | | String | Class name of the bean to use -| trim | true | Boolean | Whether to trim the value to remove leading and trailing whitespaces and line breaks +| trim | true | Boolean | |=== // language options: END diff --git a/core/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java b/core/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java index 115261f..ef10407 100644 --- a/core/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java +++ b/core/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java @@ -19,29 +19,35 @@ package org.apache.camel.language.bean; import java.util.List; import java.util.Map; +import org.apache.camel.AfterPropertiesConfigured; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.ExchangePattern; import org.apache.camel.Expression; import org.apache.camel.ExpressionIllegalSyntaxException; import org.apache.camel.Predicate; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.bean.BeanExpressionProcessor; import org.apache.camel.component.bean.BeanHolder; +import org.apache.camel.component.bean.BeanInfo; import org.apache.camel.component.bean.ConstantBeanHolder; import org.apache.camel.component.bean.ConstantTypeBeanHolder; +import org.apache.camel.component.bean.MethodNotFoundException; import org.apache.camel.component.bean.RegistryBean; import org.apache.camel.spi.Language; +import org.apache.camel.support.CamelContextHelper; import org.apache.camel.support.ExchangeHelper; import org.apache.camel.support.LanguageSupport; import org.apache.camel.util.KeyValueHolder; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.OgnlHelper; import org.apache.camel.util.StringHelper; +import static org.apache.camel.util.ObjectHelper.hasDefaultPublicNoArgConstructor; /** * Evaluates an expression using a bean method invocation */ -public class BeanExpression implements Expression, Predicate { +public class BeanExpression implements Expression, Predicate, AfterPropertiesConfigured { private final Object bean; private final String beanName; private final Class<?> type; @@ -69,14 +75,6 @@ public class BeanExpression implements Expression, Predicate { this.beanName = null; } - public BeanExpression(BeanHolder beanHolder, String method) { - this.beanHolder = beanHolder; - this.method = method; - this.bean = null; - this.beanName = null; - this.type = null; - } - @Override public String toString() { StringBuilder sb = new StringBuilder("BeanExpression["); @@ -147,6 +145,64 @@ public class BeanExpression implements Expression, Predicate { return ObjectHelper.evaluateValuePredicate(value); } + @Override + public void afterPropertiesConfigured(CamelContext camelContext) { + Object target = bean; + if (bean == null && type == null && beanName != null) { + target = CamelContextHelper.mandatoryLookup(camelContext, beanName); + } + validateHasMethod(camelContext, target, type, method); + } + + /** + * Validates the given bean has the method. + * <p/> + * This implementation will skip trying to validate OGNL method name expressions. + * + * @param context camel context + * @param bean the bean instance + * @param type the bean type + * @param method the method, can be <tt>null</tt> if no method name provided + * @throws org.apache.camel.RuntimeCamelException is thrown if bean does not have the method + */ + protected void validateHasMethod(CamelContext context, Object bean, Class<?> type, String method) { + if (method == null) { + return; + } + + if (bean == null && type == null) { + throw new IllegalArgumentException("Either bean or type should be provided on " + this); + } + + if (bean == null && hasDefaultPublicNoArgConstructor(type)) { + bean = context.getInjector().newInstance(type); + } + + // do not try to validate ognl methods + if (OgnlHelper.isValidOgnlExpression(method)) { + return; + } + + // if invalid OGNL then fail + if (OgnlHelper.isInvalidValidOgnlExpression(method)) { + ExpressionIllegalSyntaxException cause = new ExpressionIllegalSyntaxException(method); + throw RuntimeCamelException.wrapRuntimeCamelException(new MethodNotFoundException(bean != null ? bean : type, method, cause)); + } + + if (bean != null) { + BeanInfo info = new BeanInfo(context, bean.getClass()); + if (!info.hasMethod(method)) { + throw RuntimeCamelException.wrapRuntimeCamelException(new MethodNotFoundException(null, bean, method)); + } + } else { + BeanInfo info = new BeanInfo(context, type); + // must be a static method as we do not have a bean instance to invoke + if (!info.hasStaticMethod(method)) { + throw RuntimeCamelException.wrapRuntimeCamelException(new MethodNotFoundException(null, type, method, true)); + } + } + } + /** * Optimize to create the bean holder once, so we can reuse it for further * evaluation, which is faster. diff --git a/core/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java b/core/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java index e6ed19b..66f64f5 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java @@ -24,19 +24,11 @@ import javax.xml.bind.annotation.XmlTransient; import org.apache.camel.CamelContext; import org.apache.camel.Expression; -import org.apache.camel.ExpressionIllegalSyntaxException; import org.apache.camel.Predicate; import org.apache.camel.RuntimeCamelException; -import org.apache.camel.component.bean.BeanHolder; -import org.apache.camel.component.bean.BeanInfo; -import org.apache.camel.component.bean.ConstantBeanHolder; -import org.apache.camel.component.bean.ConstantStaticTypeBeanHolder; -import org.apache.camel.component.bean.MethodNotFoundException; -import org.apache.camel.component.bean.RegistryBean; import org.apache.camel.language.bean.BeanExpression; import org.apache.camel.spi.Metadata; import org.apache.camel.util.ObjectHelper; -import org.apache.camel.util.OgnlHelper; /** * To use a Java bean (aka method call) in Camel expressions or predicates. @@ -64,7 +56,13 @@ public class MethodCallExpression extends ExpressionDefinition { } public MethodCallExpression(String beanName, String method) { - super(beanName); + super((String)null); + if (beanName != null && beanName.startsWith("ref:")) { + beanName = beanName.substring(4); + } else if (beanName != null && beanName.startsWith("bean:")) { + beanName = beanName.substring(5); + } + this.ref = beanName; this.method = method; } @@ -93,6 +91,7 @@ public class MethodCallExpression extends ExpressionDefinition { return "bean"; } + @Deprecated public String getRef() { return ref; } @@ -100,6 +99,7 @@ public class MethodCallExpression extends ExpressionDefinition { /** * Reference to bean to lookup in the registry */ + @Deprecated public void setRef(String ref) { this.ref = ref; } @@ -152,8 +152,6 @@ public class MethodCallExpression extends ExpressionDefinition { @Override public Expression createExpression(CamelContext camelContext) { - // TODO: need to use setProperty ... to copy over the options - // and move this logic to BeanLanguage Expression answer; if (beanType == null && beanTypeName != null) { @@ -164,31 +162,19 @@ public class MethodCallExpression extends ExpressionDefinition { } } - BeanHolder holder; - if (beanType != null) { - // create a bean if there is a default public no-arg constructor - if (ObjectHelper.hasDefaultPublicNoArgConstructor(beanType)) { - instance = camelContext.getInjector().newInstance(beanType); - holder = new ConstantBeanHolder(instance, camelContext); - } else { - holder = new ConstantStaticTypeBeanHolder(beanType, camelContext); - } - } else if (instance != null) { - holder = new ConstantBeanHolder(instance, camelContext); + // TODO: Must use setProperty, so we need to have properties for these on BeanExpression + + if (instance != null) { + answer = new BeanExpression(instance, method); + } else if (beanType != null) { + answer = new BeanExpression(beanType, method); + } else if (ref != null) { + answer = new BeanExpression(ref, method); } else { - String ref = beanName(); - // if its a ref then check that the ref exists - BeanHolder regHolder = new RegistryBean(camelContext, ref); - // get the bean which will check that it exists - instance = regHolder.getBean(); - holder = new ConstantBeanHolder(instance, camelContext); + answer = new BeanExpression(getExpression(), method); } - // create answer using the holder - answer = new BeanExpression(holder, getMethod()); - - // and do sanity check that if a method name was given, that it exists - validateHasMethod(camelContext, instance, beanType, getMethod()); + configureExpression(camelContext, answer); return answer; } @@ -197,51 +183,7 @@ public class MethodCallExpression extends ExpressionDefinition { return (Predicate) createExpression(camelContext); } - /** - * Validates the given bean has the method. - * <p/> - * This implementation will skip trying to validate OGNL method name expressions. - * - * @param context camel context - * @param bean the bean instance - * @param type the bean type - * @param method the method, can be <tt>null</tt> if no method name provided - * @throws org.apache.camel.RuntimeCamelException is thrown if bean does not have the method - */ - protected void validateHasMethod(CamelContext context, Object bean, Class<?> type, String method) { - if (method == null) { - return; - } - - if (bean == null && type == null) { - throw new IllegalArgumentException("Either bean or type should be provided on " + this); - } - - // do not try to validate ognl methods - if (OgnlHelper.isValidOgnlExpression(method)) { - return; - } - - // if invalid OGNL then fail - if (OgnlHelper.isInvalidValidOgnlExpression(method)) { - ExpressionIllegalSyntaxException cause = new ExpressionIllegalSyntaxException(method); - throw RuntimeCamelException.wrapRuntimeCamelException(new MethodNotFoundException(bean != null ? bean : type, method, cause)); - } - - if (bean != null) { - BeanInfo info = new BeanInfo(context, bean.getClass()); - if (!info.hasMethod(method)) { - throw RuntimeCamelException.wrapRuntimeCamelException(new MethodNotFoundException(null, bean, method)); - } - } else { - BeanInfo info = new BeanInfo(context, type); - // must be a static method as we do not have a bean instance to invoke - if (!info.hasStaticMethod(method)) { - throw RuntimeCamelException.wrapRuntimeCamelException(new MethodNotFoundException(null, type, method, true)); - } - } - } - + @Deprecated protected String beanName() { if (ref != null) { return ref; @@ -256,4 +198,5 @@ public class MethodCallExpression extends ExpressionDefinition { boolean isRef = ref != null; return "bean[" + (isRef ? "ref:" : "") + beanName() + (method != null ? " method:" + method : "") + "]"; } + } \ No newline at end of file diff --git a/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlRouteExpressionTest.java b/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlRouteExpressionTest.java index f980c03..dfc5b69 100644 --- a/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlRouteExpressionTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlRouteExpressionTest.java @@ -69,7 +69,7 @@ public class DumpModelAsXmlRouteExpressionTest extends ContextTestSupport { log.info(xml); assertTrue(xml.contains("<setHeader headerName=\"foo\"")); - assertTrue(xml.contains("<method>myCoolBean</method>")); + assertTrue(xml.contains("<method ref=\"myCoolBean\"/>")); } @Override diff --git a/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlRoutePredicateTest.java b/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlRoutePredicateTest.java index 6e85222..73ac72f 100644 --- a/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlRoutePredicateTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlRoutePredicateTest.java @@ -68,7 +68,7 @@ public class DumpModelAsXmlRoutePredicateTest extends ContextTestSupport { assertNotNull(xml); log.info(xml); - assertTrue(xml.contains("<method>myCoolBean</method>")); + assertTrue(xml.contains("<method ref=\"myCoolBean\"/>")); } @Override diff --git a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/language/bean/springboot/BeanLanguageConfiguration.java b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/language/bean/springboot/BeanLanguageConfiguration.java index 8e28050..1c0f763 100644 --- a/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/language/bean/springboot/BeanLanguageConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-core-starter/src/main/java/org/apache/camel/language/bean/springboot/BeanLanguageConfiguration.java @@ -36,10 +36,6 @@ public class BeanLanguageConfiguration * enabled by default. */ private Boolean enabled; - /** - * Whether to trim the value to remove leading and trailing whitespaces and - * line breaks - */ private Boolean trim = true; public Boolean getTrim() {