Skip to content

Commit

Permalink
Lang5 (#12986)
Browse files Browse the repository at this point in the history
CAMEL-20378: Languages should be thread-safe and be configured only via properties array, all in the same way.
  • Loading branch information
davsclaus authored Feb 4, 2024
1 parent 8e8e7c5 commit 78126c7
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ public boolean configure(CamelContext camelContext, Object target, String name,
case "validate":
setValidate(PropertyConfigurerSupport.property(camelContext, Boolean.class, value));
return true;
case "resultType":
case "resulttype":
setResultType(PropertyConfigurerSupport.property(camelContext, Class.class, value));
return true;
default:
return false;
}
Expand All @@ -114,19 +110,19 @@ public Predicate createPredicate(String expression, Object[] properties) {
public Expression createExpression(String expression, Object[] properties) {
BeanExpression answer = null;

String method = property(String.class, properties, 1, null);
Object bean = property(Object.class, properties, 0, null);
Object bean = property(Object.class, properties, 1, null);
String method = property(String.class, properties, 2, null);
if (bean != null) {
answer = new BeanExpression(bean, method);
}
if (answer == null) {
Class<?> beanType = property(Class.class, properties, 2, null);
Class<?> beanType = property(Class.class, properties, 3, null);
if (beanType != null) {
answer = new BeanExpression(beanType, method);
}
}
if (answer == null) {
String ref = property(String.class, properties, 3, null);
String ref = property(String.class, properties, 4, null);
if (ref != null) {
answer = new BeanExpression(ref, method);
}
Expand All @@ -137,14 +133,14 @@ public Expression createExpression(String expression, Object[] properties) {
if (answer == null) {
throw new IllegalArgumentException("Bean language requires bean, beanType, or ref argument");
}
Object scope = property(Object.class, properties, 4, null);
Object scope = property(Object.class, properties, 5, 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.setValidate(property(boolean.class, properties, 6, isValidate()));
answer.setResultType(property(Class.class, properties, 0, null));
answer.setBeanComponent(beanComponent);
answer.setParameterMappingStrategy(parameterMappingStrategy);
answer.setSimple(simple);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,16 @@
import com.datasonnet.document.MediaType;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;
import org.apache.camel.CamelContext;
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.spi.annotations.Language;
import org.apache.camel.support.LRUCacheFactory;
import org.apache.camel.support.TypedLanguageSupport;
import org.apache.camel.support.component.PropertyConfigurerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Language("datasonnet")
public class DatasonnetLanguage extends TypedLanguageSupport implements PropertyConfigurer {
public class DatasonnetLanguage extends TypedLanguageSupport {
private static final Logger LOG = LoggerFactory.getLogger(DatasonnetLanguage.class);

private static final Map<String, String> CLASSPATH_IMPORTS = new HashMap<>();
Expand Down Expand Up @@ -84,7 +81,7 @@ public Expression createExpression(String expression, Object[] properties) {
expression = loadResource(expression);

DatasonnetExpression answer = new DatasonnetExpression(expression);
answer.setResultType(property(Class.class, properties, 0, getResultType()));
answer.setResultType(property(Class.class, properties, 0, null));
String mediaType = property(String.class, properties, 1, null);
if (mediaType != null) {
answer.setBodyMediaType(MediaType.valueOf(mediaType));
Expand All @@ -111,20 +108,4 @@ public Map<String, String> getClasspathImports() {
return CLASSPATH_IMPORTS;
}

@Override
public boolean configure(CamelContext camelContext, Object target, String name, Object value, boolean ignoreCase) {
if (target != this) {
throw new IllegalStateException("Can only configure our own instance !");
}

switch (ignoreCase ? name.toLowerCase() : name) {
case "resultType":
case "resulttype":
setResultType(PropertyConfigurerSupport.property(camelContext, Class.class, value));
return true;
default:
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public Expression createExpression(
if (resultType.equals(Object.class)) {
resultType = expressionReturnType;
}
params[1] = resultType;
params[0] = resultType;
if (annotation instanceof Java) {
Java joorAnnotation = (Java) annotation;
params[0] = joorAnnotation.preCompile();
params[1] = joorAnnotation.preCompile();
params[2] = joorAnnotation.singleQuotes();
}
String expression = getExpressionFromAnnotation(annotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public Predicate createPredicate(String expression, Object[] properties) {
public Expression createExpression(String expression, Object[] properties) {
JoorExpression answer = new JoorExpression(expression);
answer.setCompiler(compiler);
answer.setPreCompile(property(boolean.class, properties, 0, preCompile));
answer.setResultType(property(Class.class, properties, 1, getResultType()));
answer.setResultType(property(Class.class, properties, 0, null));
answer.setPreCompile(property(boolean.class, properties, 1, preCompile));
answer.setSingleQuotes(property(boolean.class, properties, 2, singleQuotes));
if (getCamelContext() != null) {
answer.init(getCamelContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public Expression createExpression(
if (resultType.equals(Object.class)) {
resultType = expressionReturnType;
}
params[1] = resultType;
params[0] = resultType;
if (annotation instanceof Joor) {
Joor joorAnnotation = (Joor) annotation;
params[0] = joorAnnotation.preCompile();
params[1] = joorAnnotation.preCompile();
params[2] = joorAnnotation.singleQuotes();
}
String expression = getExpressionFromAnnotation(annotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void stop() {
@Override
public Expression createExpression(Expression source, String expression, Object[] properties) {
JqExpression answer = new JqExpression(Scope.newChildScope(rootScope), expression);
answer.setResultType(property(Class.class, properties, 0, getResultType()));
answer.setResultType(property(Class.class, properties, 0, null));
answer.setSource(source);
if (getCamelContext() != null) {
answer.init(getCamelContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected ExpressionAdapter doCreateJsonPathExpression(
JsonPathExpression answer = new JsonPathExpression(expression);
answer.setSource(source);
answer.setPredicate(predicate);
answer.setResultType(property(Class.class, properties, 0, getResultType()));
answer.setResultType(property(Class.class, properties, 0, null));
answer.setSuppressExceptions(property(boolean.class, properties, 4, isSuppressExceptions()));
answer.setAllowSimple(property(boolean.class, properties, 5, isAllowSimple()));
answer.setAllowEasyPredicate(property(boolean.class, properties, 6, isAllowEasyPredicate()));
Expand Down Expand Up @@ -151,10 +151,6 @@ public boolean configure(CamelContext camelContext, Object target, String name,
}

switch (ignoreCase ? name.toLowerCase() : name) {
case "resulttype":
case "resultType":
setResultType(PropertyConfigurerSupport.property(camelContext, Class.class, value));
return true;
case "suppressexceptions":
case "suppressExceptions":
setSuppressExceptions(PropertyConfigurerSupport.property(camelContext, boolean.class, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.EndpointHelper;
import org.apache.camel.support.ResourceHelper;
import org.apache.camel.support.TypedLanguageSupport;
import org.apache.camel.util.IOHelper;

/**
Expand All @@ -48,7 +47,8 @@
* defined as well.
*/
@UriEndpoint(firstVersion = "2.5.0", scheme = "language", title = "Language", syntax = "language:languageName:resourceUri",
remote = false, producerOnly = true, category = { Category.CORE, Category.SCRIPT }, headersClass = LanguageConstants.class)
remote = false, producerOnly = true, category = { Category.CORE, Category.SCRIPT },
headersClass = LanguageConstants.class)
public class LanguageEndpoint extends ResourceEndpoint {
private Language language;
private Expression expression;
Expand All @@ -73,6 +73,7 @@ public class LanguageEndpoint extends ResourceEndpoint {
private boolean contentCache;
@UriParam
private String resultType;
private volatile Class<?> resultTypeClass;

public LanguageEndpoint() {
// enable cache by default
Expand All @@ -93,16 +94,15 @@ protected void doInit() throws Exception {
if (language == null && languageName != null) {
language = getCamelContext().resolveLanguage(languageName);
}
if (language instanceof TypedLanguageSupport && resultType != null) {
Class<?> clazz = getCamelContext().getClassResolver().resolveMandatoryClass(resultType);
((TypedLanguageSupport) language).setResultType(clazz);
if (resultTypeClass == null && resultType != null) {
resultTypeClass = getCamelContext().getClassResolver().resolveMandatoryClass(resultType);
}
if (cacheScript && expression == null && script != null) {
boolean external = script.startsWith("file:") || script.startsWith("http:");
if (!external) {
// we can pre optimize this as the script can be loaded from classpath or registry etc
script = resolveScript(script);
expression = language.createExpression(script);
expression = language.createExpression(script, new Object[] { resultTypeClass });
}
}
if (expression != null) {
Expand All @@ -114,7 +114,7 @@ protected void doInit() throws Exception {
public Producer createProducer() throws Exception {
if (cacheScript && expression == null && script != null) {
script = resolveScript(script);
expression = language.createExpression(script);
expression = language.createExpression(script, new Object[] { resultTypeClass });
expression.init(getCamelContext());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public void process(Exchange exchange) throws Exception {
// if we have a text based script then use and evaluate it
if (script != null) {
// create the expression from the script
exp = getEndpoint().getLanguage().createExpression(script);
Class<?> type = resultType != Object.class ? resultType : null;
exp = getEndpoint().getLanguage().createExpression(script, new Object[] { type });
exp.init(getEndpoint().getCamelContext());
// expression was resolved from resource
getEndpoint().setContentResolvedFromResource(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Expression createExpression(Expression source, String expression, Object[
protected void configureBuilder(XQueryBuilder builder, Object[] properties, Expression source) {
builder.setSource(source);

Class<?> clazz = property(Class.class, properties, 0, getResultType());
Class<?> clazz = property(Class.class, properties, 0, null);
if (clazz != null) {
builder.setResultType(clazz);
}
Expand All @@ -68,10 +68,6 @@ public boolean configure(CamelContext camelContext, Object target, String name,
throw new IllegalStateException("Can only configure our own instance !");
}
switch (ignoreCase ? name.toLowerCase() : name) {
case "resulttype":
case "resultType":
setResultType(PropertyConfigurerSupport.property(camelContext, Class.class, value));
return true;
case "configuration":
case "Configuration":
setConfiguration(PropertyConfigurerSupport.property(camelContext, Configuration.class, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ public boolean configure(CamelContext camelContext, Object target, String name,
}

switch (ignoreCase ? name.toLowerCase() : name) {
case "resulttype":
case "resultType":
setResultType(PropertyConfigurerSupport.property(camelContext, Class.class, value));
return true;
case "module":
setModule(PropertyConfigurerSupport.property(camelContext, String.class, value));
return true;
Expand All @@ -79,7 +75,7 @@ public Predicate createPredicate(String expression, Object[] properties) {
@Override
public Expression createExpression(String expression, Object[] properties) {
WasmExpression answer = new WasmExpression(expression);
answer.setResultType(property(Class.class, properties, 0, getResultType()));
answer.setResultType(property(Class.class, properties, 0, null));
answer.setModule(property(String.class, properties, 1, getModule()));
if (getCamelContext() != null) {
answer.init(getCamelContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ public void setPreCompile(Boolean preCompile) {
protected void configureBuilder(XPathBuilder builder, Object[] properties, Expression source) {
builder.setSource(source);

Class<?> clazz = property(Class.class, properties, 4, documentType);
Class<?> clazz = property(Class.class, properties, 0, null);
if (clazz != null) {
builder.setResultType(clazz);
}
clazz = property(Class.class, properties, 4, documentType);
if (clazz != null) {
builder.setDocumentType(clazz);
}
QName qname = property(QName.class, properties, 5, resultQName);
if (qname != null) {
builder.setResultQName(qname);
}
clazz = property(Class.class, properties, 0, getResultType());
if (clazz != null) {
builder.setResultType(clazz);
}
Boolean bool = property(Boolean.class, properties, 6, saxon);
if (bool != null) {
builder.setUseSaxon(bool);
Expand Down Expand Up @@ -185,10 +185,6 @@ public boolean configure(CamelContext camelContext, Object target, String name,
}
switch (ignoreCase ? name.toLowerCase() : name) {
case "resulttype":
case "resultType":
setResultType(PropertyConfigurerSupport.property(camelContext, Class.class, value));
return true;
case "resultqname":
case "resultQName":
setResultQName(PropertyConfigurerSupport.property(camelContext, QName.class, value));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ private LanguageValidationResult doValidateLanguage(
}
return answer;
} catch (NoSuchMethodException e) {
// ignore
// ignore
}
if (predicate) {
instance.getClass().getMethod("createPredicate", String.class).invoke(instance, text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,11 @@ public static Object bean(Exchange exchange, Language bean, String ref, String m
}
}

Object[] properties = new Object[5];
properties[2] = type;
properties[3] = ref;
properties[1] = method;
properties[4] = scope;
Object[] properties = new Object[7];
properties[3] = type;
properties[4] = ref;
properties[2] = method;
properties[5] = scope;
Expression exp = bean.createExpression(null, properties);
exp.init(exchange.getContext());
return exp.evaluate(exchange, Object.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Predicate createPredicate(String expression) {

@Override
public Expression createExpression(String expression, Object[] properties) {
Class<?> resultType = property(Class.class, properties, 0, getResultType());
Class<?> resultType = property(Class.class, properties, 0, null);
if (Boolean.class == resultType || boolean.class == resultType) {
// we want it compiled as a predicate
return (Expression) createPredicate(expression);
Expand Down
Loading

0 comments on commit 78126c7

Please sign in to comment.