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

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

commit 70f16b82295de1eb6880f10ac4daacbe64e2ba57
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Oct 1 18:33:31 2020 +0200

    CAMEL-15605: Languages should be singleton for better performance.
---
 .../apache/camel/language/xpath/XPathBuilder.java  | 47 +---------------
 .../apache/camel/language/xpath/XPathLanguage.java | 57 ++++++++++++++++----
 .../reifier/language/XPathExpressionReifier.java   | 62 +++++++++++++++++-----
 3 files changed, 97 insertions(+), 69 deletions(-)

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 a2f4531..42e2ee3 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,7 +57,6 @@ 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;
@@ -65,7 +64,6 @@ 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 +94,7 @@ import static 
org.apache.camel.support.builder.Namespaces.isMatchingNamespaceOrE
  */
 public class XPathBuilder extends ServiceSupport
         implements CamelContextAware, Expression, Predicate,
-        NamespaceAware, ExpressionResultTypeAware, GeneratedPropertyConfigurer 
{
+        NamespaceAware, ExpressionResultTypeAware {
     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";
@@ -171,49 +169,6 @@ public class XPathBuilder extends ServiceSupport
     }
 
     @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/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathLanguage.java
 
b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathLanguage.java
index f035027..fd90f94 100644
--- 
a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathLanguage.java
+++ 
b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathLanguage.java
@@ -16,7 +16,8 @@
  */
 package org.apache.camel.language.xpath;
 
-import javax.xml.namespace.QName;
+import java.util.Map;
+
 import javax.xml.xpath.XPathFactory;
 
 import org.apache.camel.Expression;
@@ -29,7 +30,8 @@ import org.apache.camel.support.LanguageSupport;
  */
 @Language("xpath")
 public class XPathLanguage extends LanguageSupport {
-    private QName resultType;
+    private Class<?> resultType;
+    private Class<?> documentType;
     private XPathFactory xpathFactory;
     private Boolean useSaxon;
     private String objectModelUri;
@@ -55,14 +57,52 @@ public class XPathLanguage extends LanguageSupport {
         return builder;
     }
 
-    public QName getResultType() {
+    @Override
+    public Predicate createPredicate(Map<String, Object> properties) {
+        return (Predicate) createExpression(properties);
+    }
+
+    @Override
+    public Expression createExpression(Map<String, Object> properties) {
+        String expression = (String) properties.get("expression");
+        expression = loadResource(expression);
+
+        Class<?> clazz = property(Class.class, properties, "documentType", 
null);
+        if (clazz != null) {
+            setDocumentType(clazz);
+        }
+        clazz = property(Class.class, properties, "resultType", null);
+        if (clazz != null) {
+            setResultType(clazz);
+        }
+        setUseSaxon(property(Boolean.class, properties, "useSaxon", null));
+        setObjectModelUri(property(String.class, properties, "objectModelUri", 
null));
+        setThreadSafety(property(Boolean.class, properties, "threadSafety", 
null));
+        setLogNamespaces(property(Boolean.class, properties, "logNamespaces", 
null));
+        setHeaderName(property(String.class, properties, "headerName", null));
+        setXpathFactory(property(XPathFactory.class, properties, 
"xpathFactory", null));
+
+        XPathBuilder builder = XPathBuilder.xpath(expression);
+        configureBuilder(builder);
+        return builder;
+    }
+
+    public Class<?> getResultType() {
         return resultType;
     }
 
-    public void setResultType(QName resultType) {
+    public void setResultType(Class<?> resultType) {
         this.resultType = resultType;
     }
 
+    public Class<?> getDocumentType() {
+        return documentType;
+    }
+
+    public void setDocumentType(Class<?> documentType) {
+        this.documentType = documentType;
+    }
+
     public XPathFactory getXpathFactory() {
         return xpathFactory;
     }
@@ -120,7 +160,7 @@ public class XPathLanguage extends LanguageSupport {
             builder.setThreadSafety(threadSafety);
         }
         if (resultType != null) {
-            builder.setResultQName(resultType);
+            builder.setResultType(resultType);
         }
         if (logNamespaces != null) {
             builder.setLogNamespaces(logNamespaces);
@@ -128,6 +168,9 @@ public class XPathLanguage extends LanguageSupport {
         if (headerName != null) {
             builder.setHeaderName(headerName);
         }
+        if (documentType != null) {
+            builder.setDocumentType(documentType);
+        }
 
         if (isUseSaxon()) {
             builder.enableSaxon();
@@ -141,8 +184,4 @@ public class XPathLanguage extends LanguageSupport {
         }
     }
 
-    @Override
-    public boolean isSingleton() {
-        return false;
-    }
 }
diff --git 
a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/language/XPathExpressionReifier.java
 
b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/language/XPathExpressionReifier.java
index a34af0f..c49f1f0 100644
--- 
a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/language/XPathExpressionReifier.java
+++ 
b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/language/XPathExpressionReifier.java
@@ -19,33 +19,42 @@ package org.apache.camel.reifier.language;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.xml.xpath.XPathFactory;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.model.language.ExpressionDefinition;
 import org.apache.camel.model.language.XPathExpression;
+import org.apache.camel.spi.Language;
 import org.apache.camel.spi.NamespaceAware;
+import org.apache.camel.support.CamelContextHelper;
 
 public class XPathExpressionReifier extends ExpressionReifier<XPathExpression> 
{
 
-    // TODO: Update me
-
     public XPathExpressionReifier(CamelContext camelContext, 
ExpressionDefinition definition) {
         super(camelContext, (XPathExpression) definition);
     }
 
     @Override
-    protected void configureExpression(Expression expression) {
-        bindProperties(expression);
-        configureNamespaceAware(expression);
-        super.configureExpression(expression);
+    protected Expression createExpression(Language language, String exp) {
+        return language.createExpression(createProperties(exp));
+    }
+
+    @Override
+    protected Predicate createPredicate(Language language, String exp) {
+        return language.createPredicate(createProperties(exp));
     }
 
     @Override
     protected void configurePredicate(Predicate predicate) {
-        bindProperties(predicate);
         configureNamespaceAware(predicate);
-        super.configurePredicate(predicate);
+    }
+
+    @Override
+    protected void configureExpression(Expression expression) {
+        configureNamespaceAware(expression);
     }
 
     protected void configureNamespaceAware(Object builder) {
@@ -55,17 +64,42 @@ public class XPathExpressionReifier extends 
ExpressionReifier<XPathExpression> {
         }
     }
 
-    protected void bindProperties(Object target) {
-        Map<String, Object> properties = new HashMap<>();
-        properties.put("documentType", or(definition.getDocumentType(), 
definition.getDocumentTypeName()));
-        properties.put("resultType", or(definition.getResultType(), 
definition.getResultTypeName()));
+    protected Map<String, Object> createProperties(String expression) {
+        Map<String, Object> properties = new HashMap<>(9);
+        properties.put("expression", expression);
+        properties.put("documentType", definition.getDocumentType());
+        properties.put("resultType", definition.getResultType());
         properties.put("useSaxon", definition.getSaxon());
-        properties.put("xPathFactory", or(definition.getXPathFactory(), 
asRef(definition.getFactoryRef())));
+        properties.put("xPathFactory", definition.getXPathFactory());
         properties.put("objectModelUri", definition.getObjectModel());
         properties.put("threadSafety", definition.getThreadSafety());
         properties.put("logNamespaces", definition.getLogNamespaces());
         properties.put("headerName", definition.getHeaderName());
-        setProperties(target, properties);
+        return properties;
+    }
+
+    @Override
+    protected void configureLanguage(Language language) {
+        if (definition.getResultType() == null && 
definition.getResultTypeName() != null) {
+            try {
+                Class<?> clazz = 
camelContext.getClassResolver().resolveMandatoryClass(definition.getResultTypeName());
+                definition.setResultType(clazz);
+            } catch (ClassNotFoundException e) {
+                throw RuntimeCamelException.wrapRuntimeException(e);
+            }
+        }
+        if (definition.getDocumentType() == null && 
definition.getDocumentTypeName() != null) {
+            try {
+                Class<?> clazz = 
camelContext.getClassResolver().resolveMandatoryClass(definition.getDocumentTypeName());
+                definition.setDocumentType(clazz);
+            } catch (ClassNotFoundException e) {
+                throw RuntimeCamelException.wrapRuntimeException(e);
+            }
+        }
+        if (definition.getXPathFactory() == null && definition.getFactoryRef() 
!= null) {
+            definition.setXPathFactory(
+                    CamelContextHelper.mandatoryLookupAndConvert(camelContext, 
definition.getFactoryRef(), XPathFactory.class));
+        }
     }
 
 }

Reply via email to