You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/02/02 11:33:43 UTC
(camel) 11/13: CAMEL-20378: Languages should be thread-safe and be configured only via properties array, all in the same way.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch lang2
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 882ec8a7da0a2f472a8dc09f4ff665189b98c2ad
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Feb 2 12:10:58 2024 +0100
CAMEL-20378: Languages should be thread-safe and be configured only via properties array, all in the same way.
---
.../apache/camel/language/bean/BeanLanguage.java | 194 +++++++--------------
.../ROOT/pages/camel-4x-upgrade-guide-4_4.adoc | 3 +-
2 files changed, 61 insertions(+), 136 deletions(-)
diff --git a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanLanguage.java b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanLanguage.java
index 2a3d3f5fb47..e34cef7caf4 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanLanguage.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanLanguage.java
@@ -64,56 +64,11 @@ public class BeanLanguage extends TypedLanguageSupport implements ScriptingLangu
private volatile ParameterMappingStrategy parameterMappingStrategy;
private volatile Language simple;
- private Object bean;
- private Class<?> beanType;
- private String ref;
- private String method;
- private BeanScope scope = BeanScope.Singleton;
private boolean validate = true;
public BeanLanguage() {
}
- public Object getBean() {
- return bean;
- }
-
- public void setBean(Object bean) {
- this.bean = bean;
- }
-
- public Class<?> getBeanType() {
- return beanType;
- }
-
- public void setBeanType(Class<?> beanType) {
- this.beanType = beanType;
- }
-
- public String getRef() {
- return ref;
- }
-
- public void setRef(String ref) {
- this.ref = ref;
- }
-
- public String getMethod() {
- return method;
- }
-
- public void setMethod(String method) {
- this.method = method;
- }
-
- public BeanScope getScope() {
- return scope;
- }
-
- public void setScope(BeanScope scope) {
- this.scope = scope;
- }
-
public boolean isValidate() {
return validate;
}
@@ -128,22 +83,6 @@ public class BeanLanguage extends TypedLanguageSupport implements ScriptingLangu
throw new IllegalStateException("Can only configure our own instance !");
}
switch (ignoreCase ? name.toLowerCase() : name) {
- case "bean":
- setBean(PropertyConfigurerSupport.property(camelContext, Object.class, value));
- return true;
- case "beantype":
- case "beanType":
- setBeanType(PropertyConfigurerSupport.property(camelContext, Class.class, value));
- return true;
- case "ref":
- setRef(PropertyConfigurerSupport.property(camelContext, String.class, value));
- return true;
- case "method":
- setMethod(PropertyConfigurerSupport.property(camelContext, String.class, value));
- return true;
- case "scope":
- setScope(PropertyConfigurerSupport.property(camelContext, BeanScope.class, value));
- return true;
case "validate":
setValidate(PropertyConfigurerSupport.property(camelContext, Boolean.class, value));
return true;
@@ -161,6 +100,11 @@ public class BeanLanguage extends TypedLanguageSupport implements ScriptingLangu
return ExpressionToPredicateAdapter.toPredicate(createExpression(expression));
}
+ @Override
+ public Expression createExpression(String expression) {
+ return createExpression(expression, null);
+ }
+
@Override
public Predicate createPredicate(String expression, Object[] properties) {
return ExpressionToPredicateAdapter.toPredicate(createExpression(expression, properties));
@@ -170,42 +114,36 @@ public class BeanLanguage extends TypedLanguageSupport implements ScriptingLangu
public Expression createExpression(String expression, Object[] properties) {
BeanExpression answer = null;
- String method = (String) properties[1];
- Object bean = properties[0];
+ String method = property(String.class, properties, 1, null);
+ Object bean = property(Object.class, properties, 0, null);
if (bean != null) {
answer = new BeanExpression(bean, method);
}
if (answer == null) {
- Class<?> beanType = (Class<?>) properties[2];
+ Class<?> beanType = property(Class.class, properties, 2, null);
if (beanType != null) {
answer = new BeanExpression(beanType, method);
}
}
if (answer == null) {
- String ref = (String) properties[3];
+ String ref = property(String.class, properties, 3, null);
if (ref != null) {
answer = new BeanExpression(ref, method);
}
}
if (answer == null) {
- throw new IllegalArgumentException("Bean language requires bean, beanType, or ref argument");
+ answer = createBeanExpression(expression);
}
- if (properties.length >= 5) {
- Object scope = properties[4];
- if (scope instanceof BeanScope) {
- answer.setScope((BeanScope) scope);
- } else if (scope != null) {
- answer.setScope(BeanScope.valueOf(scope.toString()));
- }
+ if (answer == null) {
+ throw new IllegalArgumentException("Bean language requires bean, beanType, or ref argument");
}
- if (properties.length >= 6) {
- Object validate = properties[5];
- if (validate != null) {
- answer.setValidate(Boolean.parseBoolean(validate.toString()));
- } else {
- answer.setValidate(this.isValidate());
- }
+ Object scope = property(Object.class, properties, 4, null);
+ if (scope instanceof BeanScope) {
+ answer.setScope((BeanScope) scope);
+ } else if (scope != null) {
+ answer.setScope(BeanScope.valueOf(scope.toString()));
}
+ answer.setValidate(property(boolean.class, properties, 5, isValidate()));
answer.setResultType(property(Class.class, properties, 6, getResultType()));
answer.setBeanComponent(beanComponent);
answer.setParameterMappingStrategy(parameterMappingStrategy);
@@ -214,70 +152,53 @@ public class BeanLanguage extends TypedLanguageSupport implements ScriptingLangu
return answer;
}
- @Override
- public Expression createExpression(String expression) {
+ protected BeanExpression createBeanExpression(String expression) {
BeanExpression answer;
- String beanScope = null;
- // favour using the configured options
- if (bean != null) {
- answer = new BeanExpression(bean, method);
- } else if (beanType != null) {
- answer = new BeanExpression(beanType, method);
- } else if (ref != null) {
- answer = new BeanExpression(ref, method);
+ // we support different syntax for bean function
+ String beanName = expression;
+ String method = null;
+ String beanScope = null;
+ if (expression.contains("?method=") || expression.contains("?scope=")) {
+ beanName = StringHelper.before(expression, "?");
+ String query = StringHelper.after(expression, "?");
+ try {
+ Map<String, Object> map = URISupport.parseQuery(query);
+ method = (String) map.get("method");
+ beanScope = (String) map.get("scope");
+ } catch (URISyntaxException e) {
+ throw RuntimeCamelException.wrapRuntimeException(e);
+ }
} else {
- // we support different syntax for bean function
- String beanName = expression;
- String method = null;
- if (expression.contains("?method=") || expression.contains("?scope=")) {
- beanName = StringHelper.before(expression, "?");
- String query = StringHelper.after(expression, "?");
- try {
- Map<String, Object> map = URISupport.parseQuery(query);
- method = (String) map.get("method");
- beanScope = (String) map.get("scope");
- } catch (URISyntaxException e) {
- throw RuntimeCamelException.wrapRuntimeException(e);
- }
+ //first check case :: because of my.own.Bean::method
+ int doubleColonIndex = expression.indexOf("::");
+ //need to check that not inside params
+ int beginOfParameterDeclaration = expression.indexOf('(');
+ if (doubleColonIndex > 0 && (!expression.contains("(") || doubleColonIndex < beginOfParameterDeclaration)) {
+ beanName = expression.substring(0, doubleColonIndex);
+ method = expression.substring(doubleColonIndex + 2);
} else {
- //first check case :: because of my.own.Bean::method
- int doubleColonIndex = expression.indexOf("::");
- //need to check that not inside params
- int beginOfParameterDeclaration = expression.indexOf('(');
- if (doubleColonIndex > 0 && (!expression.contains("(") || doubleColonIndex < beginOfParameterDeclaration)) {
- beanName = expression.substring(0, doubleColonIndex);
- method = expression.substring(doubleColonIndex + 2);
- } else {
- int idx = expression.indexOf('.');
- if (idx > 0) {
- beanName = expression.substring(0, idx);
- method = expression.substring(idx + 1);
- }
+ int idx = expression.indexOf('.');
+ if (idx > 0) {
+ beanName = expression.substring(0, idx);
+ method = expression.substring(idx + 1);
}
}
+ }
- if (beanName.startsWith("type:")) {
- try {
- Class<?> clazz = getCamelContext().getClassResolver().resolveMandatoryClass(beanName.substring(5));
- answer = new BeanExpression(clazz, method);
- } catch (ClassNotFoundException e) {
- throw RuntimeCamelException.wrapRuntimeException(e);
- }
- } else {
- answer = new BeanExpression(beanName, method);
+ if (beanName.startsWith("type:")) {
+ try {
+ Class<?> clazz = getCamelContext().getClassResolver().resolveMandatoryClass(beanName.substring(5));
+ answer = new BeanExpression(clazz, method);
+ } catch (ClassNotFoundException e) {
+ throw RuntimeCamelException.wrapRuntimeException(e);
}
+ } else {
+ answer = new BeanExpression(beanName, method);
}
-
if (beanScope != null) {
- answer.setScope(getCamelContext().getTypeConverter().tryConvertTo(BeanScope.class, beanScope));
- } else {
- answer.setScope(scope);
+ answer.setScope(BeanScope.valueOf(beanScope));
}
- answer.setBeanComponent(beanComponent);
- answer.setParameterMappingStrategy(parameterMappingStrategy);
- answer.setSimple(simple);
- answer.init(getCamelContext());
return answer;
}
@@ -354,12 +275,17 @@ public class BeanLanguage extends TypedLanguageSupport implements ScriptingLangu
}
@Override
- public void start() {
+ public void init() {
beanComponent = getCamelContext().getComponent("bean", BeanComponent.class);
parameterMappingStrategy = ParameterMappingStrategyHelper.createParameterMappingStrategy(getCamelContext());
simple = getCamelContext().resolveLanguage("simple");
}
+ @Override
+ public void start() {
+ // noop
+ }
+
@Override
public void stop() {
// noop
diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_4.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_4.adoc
index 127c4768f2d..dd07fcaa463 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_4.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_4.adoc
@@ -24,8 +24,7 @@ The `lookup` method in `org.apache.camel.component.properties.PropertiesLookup`
==== Languages
The way languages are created and configured by Camel has been refactored to be aligned and avoid a thread-safety issues
-when using Java DSL. All the setter/getter on the `Language` classes has been removed (such as `TokenizeLanguage`) as the options are configured in the DSL
-in a consistent and thread-safe manner.
+when using Java DSL. The setter/getter on the `Language` classes for options that are not general has been removed (such as in `TokenizeLanguage`).
==== WireTap EIP