You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2020/02/12 02:45:16 UTC

[camel] 06/18: Add hand-written property configurers for languages

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit ce995a75b31b30f5b81d153814b46c87fc1b5e92
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Mon Feb 10 21:49:20 2020 +0100

    Add hand-written property configurers for languages
---
 .../apache/camel/language/bean/BeanLanguage.java   | 20 ++++++++++++-
 .../apache/camel/jsonpath/JsonPathExpression.java  | 29 ++++++++++++++++++-
 .../camel/component/xquery/XQueryBuilder.java      | 19 +++++++++++--
 .../apache/camel/language/xpath/XPathBuilder.java  | 31 +++++++++++++++++++-
 .../camel/reifier/language/ExpressionReifier.java  | 13 ++++++++-
 .../support/ExpressionToPredicateAdapter.java      | 16 ++++++++++-
 .../xtokenizer/XMLTokenExpressionIterator.java     | 33 +++++++++++++++++++---
 .../language/xtokenizer/XMLTokenizeLanguage.java   |  2 +-
 8 files changed, 151 insertions(+), 12 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 a230806..93192a4 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
@@ -16,10 +16,13 @@
  */
 package org.apache.camel.language.bean;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
 import org.apache.camel.support.ExpressionToPredicateAdapter;
 import org.apache.camel.support.LanguageSupport;
+import org.apache.camel.support.component.PropertyConfigurerSupport;
 import org.apache.camel.util.StringHelper;
 
 /**
@@ -36,7 +39,7 @@ import org.apache.camel.util.StringHelper;
  * its classname or the bean itself.
  */
 @org.apache.camel.spi.annotations.Language("bean")
-public class BeanLanguage extends LanguageSupport {
+public class BeanLanguage extends LanguageSupport implements GeneratedPropertyConfigurer {
 
     private Object bean;
     private Class<?> beanType;
@@ -46,6 +49,21 @@ public class BeanLanguage extends LanguageSupport {
     public BeanLanguage() {
     }
 
+    @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 "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;
+            default: return false;
+        }
+    }
+
     public Object getBean() {
         return bean;
     }
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
index e6c85a1..49e6fa6 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
@@ -26,11 +26,16 @@ import org.apache.camel.Exchange;
 import org.apache.camel.ExpressionEvaluationException;
 import org.apache.camel.ExpressionIllegalSyntaxException;
 import org.apache.camel.jsonpath.easypredicate.EasyPredicateParser;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.PropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurerAware;
 import org.apache.camel.support.ExpressionAdapter;
+import org.apache.camel.support.component.PropertyConfigurerSupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class JsonPathExpression extends ExpressionAdapter implements AfterPropertiesConfigured {
+public class JsonPathExpression extends ExpressionAdapter implements AfterPropertiesConfigured, GeneratedPropertyConfigurer {
 
     private static final Logger LOG = LoggerFactory.getLogger(JsonPathExpression.class);
 
@@ -50,6 +55,28 @@ public class JsonPathExpression extends ExpressionAdapter implements AfterProper
         this.expression = expression;
     }
 
+    @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;
+            case "suppressexceptions":
+            case "suppressExceptions": setSuppressExceptions(PropertyConfigurerSupport.property(camelContext, Boolean.class, value)); return true;
+            case "allowsimple":
+            case "allowSimple": setAllowSimple(PropertyConfigurerSupport.property(camelContext, Boolean.class, value)); return true;
+            case "alloweasypredicate":
+            case "allowEasyPredicate": setAllowEasyPredicate(PropertyConfigurerSupport.property(camelContext, Boolean.class, value)); return true;
+            case "writeasstring":
+            case "writeAsString": setWriteAsString(PropertyConfigurerSupport.property(camelContext, Boolean.class, value)); return true;
+            case "headername":
+            case "headerName": setHeaderName(PropertyConfigurerSupport.property(camelContext, String.class, value)); return true;
+            default: return false;
+        }
+    }
+
     public boolean isPredicate() {
         return predicate;
     }
diff --git a/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java b/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
index 19dc3fe..5e95d06 100644
--- a/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
+++ b/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
@@ -45,7 +45,6 @@ import org.w3c.dom.Node;
 import net.sf.saxon.Configuration;
 import net.sf.saxon.lib.ModuleURIResolver;
 import net.sf.saxon.om.AllElementsSpaceStrippingRule;
-import net.sf.saxon.om.DocumentInfo;
 import net.sf.saxon.om.IgnorableSpaceStrippingRule;
 import net.sf.saxon.om.Item;
 import net.sf.saxon.om.SequenceIterator;
@@ -62,6 +61,7 @@ import net.sf.saxon.value.Int64Value;
 import net.sf.saxon.value.IntegerValue;
 import net.sf.saxon.value.ObjectValue;
 import net.sf.saxon.value.StringValue;
+import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Message;
@@ -69,8 +69,10 @@ import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.RuntimeExpressionException;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
 import org.apache.camel.spi.NamespaceAware;
 import org.apache.camel.support.MessageHelper;
+import org.apache.camel.support.component.PropertyConfigurerSupport;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.xml.BytesSource;
@@ -83,7 +85,7 @@ import org.slf4j.LoggerFactory;
  * <p/>
  * The XQueryExpression, as you would expect, can be executed repeatedly, as often as you want, in the same or in different threads.
  */
-public abstract class XQueryBuilder implements Expression, Predicate, NamespaceAware, Processor {
+public abstract class XQueryBuilder implements Expression, Predicate, NamespaceAware, Processor, GeneratedPropertyConfigurer {
     private static final Logger LOG = LoggerFactory.getLogger(XQueryBuilder.class);
     private Configuration configuration;
     private Map<String, Object> configurationProperties = new HashMap<>();
@@ -101,6 +103,19 @@ public abstract class XQueryBuilder implements Expression, Predicate, NamespaceA
     private String headerName;
 
     @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;
+            case "headername":
+            case "headerName": setHeaderName(PropertyConfigurerSupport.property(camelContext, String.class, value)); return true;
+            default: return false;
+        }
+    }
+    @Override
     public String toString() {
         return "XQuery[" + expression + "]";
     }
diff --git a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathBuilder.java b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathBuilder.java
index 080bfaa..2d21048 100644
--- a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathBuilder.java
+++ b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathBuilder.java
@@ -57,6 +57,7 @@ import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.RuntimeExpressionException;
 import org.apache.camel.WrappedFile;
 import org.apache.camel.spi.ExpressionResultTypeAware;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.NamespaceAware;
 import org.apache.camel.support.DefaultExchange;
@@ -64,6 +65,7 @@ import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.support.MessageHelper;
 import org.apache.camel.support.builder.Namespaces;
 import org.apache.camel.support.builder.xml.XMLConverterHelper;
+import org.apache.camel.support.component.PropertyConfigurerSupport;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
@@ -96,7 +98,8 @@ import static org.apache.camel.support.builder.Namespaces.isMatchingNamespaceOrE
  *
  * @see XPathConstants#NODESET
  */
-public class XPathBuilder extends ServiceSupport implements CamelContextAware, Expression, Predicate, NamespaceAware, ExpressionResultTypeAware {
+public class XPathBuilder extends ServiceSupport implements CamelContextAware, Expression, Predicate,
+        NamespaceAware, ExpressionResultTypeAware, GeneratedPropertyConfigurer {
     private static final Logger LOG = LoggerFactory.getLogger(XPathBuilder.class);
     private static final String SAXON_OBJECT_MODEL_URI = "http://saxon.sf.net/jaxp/xpath/om";
     private static final String SAXON_FACTORY_CLASS_NAME = "net.sf.saxon.xpath.XPathFactoryImpl";
@@ -168,6 +171,32 @@ public class XPathBuilder extends ServiceSupport implements CamelContextAware, E
     }
 
     @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 "documenttype":
+            case "documentType": setDocumentType(PropertyConfigurerSupport.property(camelContext, Class.class, value)); return true;
+            case "resulttype":
+            case "resultType": setResultType(PropertyConfigurerSupport.property(camelContext, Class.class, value)); return true;
+            case "usesaxon":
+            case "useSaxon": setUseSaxon(PropertyConfigurerSupport.property(camelContext, Boolean.class, value)); return true;
+            case "xpathfactory":
+            case "xPathFactory": setXPathFactory(PropertyConfigurerSupport.property(camelContext, XPathFactory.class, value)); return true;
+            case "objectmodeluri":
+            case "objectModelUri": setObjectModelUri(PropertyConfigurerSupport.property(camelContext, String.class, value)); return true;
+            case "threadsafety":
+            case "threadSafety": setThreadSafety(PropertyConfigurerSupport.property(camelContext, Boolean.class, value)); return true;
+            case "lognamespaces":
+            case "logNamespaces": setLogNamespaces(PropertyConfigurerSupport.property(camelContext, Boolean.class, value)); return true;
+            case "headername":
+            case "headerName": setHeaderName(PropertyConfigurerSupport.property(camelContext, String.class, value)); return true;
+            default: return false;
+        }
+    }
+
+    @Override
     public String toString() {
         return "XPath: " + text;
     }
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/language/ExpressionReifier.java b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/language/ExpressionReifier.java
index 3c905a2..3b63184 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/language/ExpressionReifier.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/language/ExpressionReifier.java
@@ -47,6 +47,8 @@ import org.apache.camel.model.language.XPathExpression;
 import org.apache.camel.model.language.XQueryExpression;
 import org.apache.camel.reifier.AbstractReifier;
 import org.apache.camel.spi.Language;
+import org.apache.camel.spi.PropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurerAware;
 import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.ExpressionToPredicateAdapter;
 import org.apache.camel.support.PropertyBindingSupport;
@@ -186,7 +188,16 @@ public class ExpressionReifier<T extends ExpressionDefinition> extends AbstractR
 
     protected void setProperties(Object target, Map<String, Object> properties) {
         properties.entrySet().removeIf(e -> e.getValue() == null);
-        PropertyBindingSupport.build().bind(camelContext, target, properties);
+
+        PropertyConfigurer configurer = null;
+        if (target instanceof PropertyConfigurerAware) {
+            configurer = ((PropertyConfigurerAware) target).getPropertyConfigurer(target);
+        } else if (target instanceof PropertyConfigurer) {
+            configurer = (PropertyConfigurer) target;
+        }
+        PropertyBindingSupport.build()
+                .withConfigurer(configurer)
+                .bind(camelContext, target, properties);
     }
 
     protected Object or(Object a, Object b) {
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ExpressionToPredicateAdapter.java b/core/camel-support/src/main/java/org/apache/camel/support/ExpressionToPredicateAdapter.java
index 027a971..7ef3bca 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/ExpressionToPredicateAdapter.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/ExpressionToPredicateAdapter.java
@@ -21,12 +21,15 @@ import org.apache.camel.CamelContextAware;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurerAware;
 import org.apache.camel.util.ObjectHelper;
 
 /**
  * To adapt {@link org.apache.camel.Expression} as a {@link Predicate}
  */
-public final class ExpressionToPredicateAdapter implements Predicate, CamelContextAware {
+public final class ExpressionToPredicateAdapter implements Predicate, CamelContextAware, PropertyConfigurerAware {
     private final Expression expression;
 
     public ExpressionToPredicateAdapter(Expression expression) {
@@ -70,4 +73,15 @@ public final class ExpressionToPredicateAdapter implements Predicate, CamelConte
             return null;
         }
     }
+
+    @Override
+    public PropertyConfigurer getPropertyConfigurer(Object instance) {
+        if (expression instanceof PropertyConfigurer) {
+            return (PropertyConfigurer) expression;
+        } else if (expression instanceof PropertyConfigurerAware) {
+            return ((PropertyConfigurerAware) expression).getPropertyConfigurer(expression);
+        } else {
+            return null;
+        }
+    }
 }
\ No newline at end of file
diff --git a/core/camel-xml-jaxp/src/main/java/org/apache/camel/language/xtokenizer/XMLTokenExpressionIterator.java b/core/camel-xml-jaxp/src/main/java/org/apache/camel/language/xtokenizer/XMLTokenExpressionIterator.java
index a33b89c..36c830b 100644
--- a/core/camel-xml-jaxp/src/main/java/org/apache/camel/language/xtokenizer/XMLTokenExpressionIterator.java
+++ b/core/camel-xml-jaxp/src/main/java/org/apache/camel/language/xtokenizer/XMLTokenExpressionIterator.java
@@ -39,12 +39,15 @@ import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.converter.jaxp.StaxConverter;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
 import org.apache.camel.spi.NamespaceAware;
 import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.support.ExpressionAdapter;
+import org.apache.camel.support.component.PropertyConfigurerSupport;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
@@ -54,18 +57,18 @@ import org.slf4j.LoggerFactory;
 /**
  * An {@link org.apache.camel.language.xtokenizer.XMLTokenizeLanguage} based iterator.
  */
-public class XMLTokenExpressionIterator extends ExpressionAdapter implements NamespaceAware {
-    protected final String headerName;
+public class XMLTokenExpressionIterator extends ExpressionAdapter implements NamespaceAware, GeneratedPropertyConfigurer {
     protected final String path;
     protected char mode;
     protected int group;
+    protected String headerName;
     protected Map<String, String> nsmap;
 
     public XMLTokenExpressionIterator(String path, char mode) {
-        this(null, path, mode, 1);
+        this(path, mode, 1, null);
     }
 
-    public XMLTokenExpressionIterator(String headerName, String path, char mode, int group) {
+    public XMLTokenExpressionIterator(String path, char mode, int group, String headerName) {
         StringHelper.notEmpty(path, "path");
         this.headerName = headerName;
         this.path = path;
@@ -74,6 +77,20 @@ public class XMLTokenExpressionIterator extends ExpressionAdapter implements Nam
     }
 
     @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 "headername":
+            case "headerName": setHeaderName(PropertyConfigurerSupport.property(camelContext, String.class, value)); return true;
+            case "mode": setMode(PropertyConfigurerSupport.property(camelContext, String.class, value)); return true;
+            case "group": setGroup(PropertyConfigurerSupport.property(camelContext, Integer.class, value)); return true;
+            default: return false;
+        }
+    }
+
+    @Override
     public void setNamespaces(Map<String, String> nsmap) {
         this.nsmap = nsmap;
     }
@@ -99,6 +116,14 @@ public class XMLTokenExpressionIterator extends ExpressionAdapter implements Nam
         this.group = group;
     }
 
+    public String getHeaderName() {
+        return headerName;
+    }
+
+    public void setHeaderName(String headerName) {
+        this.headerName = headerName;
+    }
+
     protected Iterator<?> createIterator(InputStream in, String charset) throws XMLStreamException, UnsupportedEncodingException {
         return createIterator(new InputStreamReader(in, charset));
     }
diff --git a/core/camel-xml-jaxp/src/main/java/org/apache/camel/language/xtokenizer/XMLTokenizeLanguage.java b/core/camel-xml-jaxp/src/main/java/org/apache/camel/language/xtokenizer/XMLTokenizeLanguage.java
index b57c952..e873eb2 100644
--- a/core/camel-xml-jaxp/src/main/java/org/apache/camel/language/xtokenizer/XMLTokenizeLanguage.java
+++ b/core/camel-xml-jaxp/src/main/java/org/apache/camel/language/xtokenizer/XMLTokenizeLanguage.java
@@ -80,7 +80,7 @@ public class XMLTokenizeLanguage extends LanguageSupport {
     public Expression createExpression(String expression) {
         String path = expression != null ? expression : this.path;
         ObjectHelper.notNull(path, "path");
-        XMLTokenExpressionIterator expr = new XMLTokenExpressionIterator(headerName, path, mode, group);
+        XMLTokenExpressionIterator expr = new XMLTokenExpressionIterator(path, mode, group, headerName);
         if (namespaces != null) {
             expr.setNamespaces(namespaces.getNamespaces());
         }