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 2021/11/12 08:36:01 UTC

[camel] branch camel-3.11.x updated: Revert "CAMEL-17187: camel-xpath - @XPath annotation should never pre-compile"

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

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


The following commit(s) were added to refs/heads/camel-3.11.x by this push:
     new 994758a  Revert "CAMEL-17187: camel-xpath - @XPath annotation should never pre-compile"
994758a is described below

commit 994758af4674e3879ca35c1e87799e14c47b7dc1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Nov 12 09:33:43 2021 +0100

    Revert "CAMEL-17187: camel-xpath - @XPath annotation should never pre-compile"
    
    This reverts commit 5699851398df04240d1157a567f62ed3e47f8bce.
---
 .../org/apache/camel/language/xpath/XPath.java     | 10 +++
 .../xpath/XPathAnnotationExpressionFactory.java    | 12 ++-
 .../BeanWithXPathInjectionPreCompileTest.java      | 97 ++++++++++++++++++++++
 3 files changed, 117 insertions(+), 2 deletions(-)

diff --git a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPath.java b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPath.java
index 718e07e..922259f 100644
--- a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPath.java
+++ b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPath.java
@@ -64,4 +64,14 @@ public @interface XPath {
      */
     boolean logNamespaces() default false;
 
+    /**
+     * Whether to enable pre-compiling the xpath expression during initialization phase. pre-compile is enabled by
+     * default.
+     *
+     * This can be used to turn off, for example in cases the compilation phase is desired at the starting phase, such
+     * as if the application is ahead of time compiled (for example with camel-quarkus) which would then load the xpath
+     * factory of the built operating system, and not a JVM runtime.
+     */
+    boolean preCompile() default true;
+
 }
diff --git a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathAnnotationExpressionFactory.java b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathAnnotationExpressionFactory.java
index ec76f1d..26aa250 100644
--- a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathAnnotationExpressionFactory.java
+++ b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathAnnotationExpressionFactory.java
@@ -42,8 +42,7 @@ public class XPathAnnotationExpressionFactory extends DefaultAnnotationExpressio
         }
 
         XPathBuilder builder = XPathBuilder.xpath(xpath, resultType);
-        // @XPath annotation cannot pre-compile so turn it off
-        builder.preCompile(false);
+        builder.preCompile(isPreCompile(annotation));
         builder.setLogNamespaces(isLogNamespaces(annotation));
         NamespacePrefix[] namespaces = getExpressionNameSpacePrefix(annotation);
         if (namespaces != null) {
@@ -96,4 +95,13 @@ public class XPathAnnotationExpressionFactory extends DefaultAnnotationExpressio
         return false;
     }
 
+    protected boolean isPreCompile(Annotation annotation) {
+        // in case @XPath is extended in a custom annotation then it may not have the method
+        try {
+            return (boolean) getAnnotationObjectValue(annotation, "preCompile");
+        } catch (Exception e) {
+            // Do Nothing
+        }
+        return false;
+    }
 }
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/BeanWithXPathInjectionPreCompileTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/BeanWithXPathInjectionPreCompileTest.java
new file mode 100644
index 0000000..00586f3
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/BeanWithXPathInjectionPreCompileTest.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.language.xpath.XPath;
+import org.apache.camel.spi.Registry;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class BeanWithXPathInjectionPreCompileTest extends ContextTestSupport {
+    private static final Logger LOG = LoggerFactory.getLogger(BeanRouteTest.class);
+    protected MyBean myBean = new MyBean();
+
+    @Test
+    public void testSendMessage() throws Exception {
+        String expectedBody = "<env:Envelope xmlns:env='http://www.w3.org/2003/05/soap-envelope'><env:Body>"
+                              + "<foo>bar</foo></env:Body></env:Envelope>";
+
+        template.sendBodyAndHeader("direct:in", expectedBody, "foo", "bar");
+
+        assertEquals(expectedBody, myBean.body, "bean body: " + myBean);
+        assertEquals("bar", myBean.foo, "bean foo: " + myBean);
+    }
+
+    @Test
+    public void testSendTwoMessages() throws Exception {
+        // 1st message
+        String expectedBody = "<env:Envelope xmlns:env='http://www.w3.org/2003/05/soap-envelope'><env:Body>"
+                              + "<foo>bar</foo></env:Body></env:Envelope>";
+
+        template.sendBodyAndHeader("direct:in", expectedBody, "foo", "bar");
+
+        assertEquals(expectedBody, myBean.body, "bean body: " + myBean);
+        assertEquals("bar", myBean.foo, "bean foo: " + myBean);
+
+        // 2nd message
+        String expectedBody2 = "<env:Envelope xmlns:env='http://www.w3.org/2003/05/soap-envelope'><env:Body>"
+                               + "<foo>baz</foo></env:Body></env:Envelope>";
+
+        template.sendBodyAndHeader("direct:in", expectedBody2, "foo", "baz");
+
+        assertEquals(expectedBody2, myBean.body, "bean body: " + myBean);
+        assertEquals("baz", myBean.foo, "bean foo: " + myBean);
+    }
+
+    @Override
+    protected Registry createRegistry() throws Exception {
+        Registry answer = super.createRegistry();
+
+        answer.bind("myBean", myBean);
+        return answer;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:in").bean("myBean");
+            }
+        };
+    }
+
+    public static class MyBean {
+        public String body;
+        public String foo;
+
+        @Override
+        public String toString() {
+            return "MyBean[foo: " + foo + " body: " + body + "]";
+        }
+
+        public void read(String body, @XPath(value = "/soap:Envelope/soap:Body/foo/text()", preCompile = false) String foo) {
+            this.foo = foo;
+            this.body = body;
+            LOG.info("read() method called on " + this);
+        }
+    }
+}