You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2020/03/23 20:06:00 UTC

[camel-quarkus] branch master updated: Added config options to select resources for inclusion in native executable #868

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b0600f9  Added config options to select resources for inclusion in native executable #868
b0600f9 is described below

commit b0600f9f7a637b1a17167f86f7208fcc98739012
Author: aldettinger <al...@gmail.com>
AuthorDate: Mon Mar 23 17:37:07 2020 +0100

    Added config options to select resources for inclusion in native executable #868
---
 .../core/deployment/NativeImageProcessor.java      | 30 ++++++++++++++++++
 .../org/apache/camel/quarkus/core/CamelConfig.java | 35 +++++++++++++++++++++
 .../apache/camel/quarkus/core/CoreResource.java    | 15 +++++++++
 .../core/src/main/resources/application.properties |  2 ++
 .../resources/exclude-pattern-folder/excluded.txt} |  2 +-
 .../resources/include-pattern-folder/excluded.txt} |  2 +-
 .../resources/include-pattern-folder/included.txt} |  2 +-
 .../main/resources/no-pattern-folder/excluded.txt} |  2 +-
 .../java/org/apache/camel/quarkus/core/CoreIT.java | 36 ++++++++++++++++++++++
 .../component/mustache/it/MustacheResource.java    | 14 +++++----
 .../src/main/resources/application.properties      |  3 +-
 .../src/main/resources/resources-config.json       |  7 -----
 .../main/resources/{ => template}/another.mustache |  0
 .../main/resources/{ => template}/child.mustache   |  0
 .../resources/{ => template}/included.mustache     |  0
 .../resources/{ => template}/includer.mustache     |  0
 .../main/resources/{ => template}/parent.mustache  |  0
 .../main/resources/{ => template}/simple.mustache  |  0
 18 files changed, 132 insertions(+), 18 deletions(-)

diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/NativeImageProcessor.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/NativeImageProcessor.java
index 5f4b528..7c556e4 100644
--- a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/NativeImageProcessor.java
+++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/NativeImageProcessor.java
@@ -23,6 +23,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import io.quarkus.deployment.ApplicationArchive;
 import io.quarkus.deployment.annotations.BuildProducer;
@@ -208,6 +209,35 @@ class NativeImageProcessor {
 
             return resources;
         }
+
+        @BuildStep
+        void embedSelectResourcesInNativeExecutable(CamelConfig config, ApplicationArchivesBuildItem archives,
+                BuildProducer<NativeImageResourceBuildItem> resources) {
+
+            if (!config.resources.includePatterns.isPresent()) {
+                LOGGER.debug("Not scanning resources for native inclusion as include-patterns is not set");
+                return;
+            }
+
+            PathFilter.Builder builder = new PathFilter.Builder();
+            LOGGER.debug("Scanning resources for native inclusion from include-patterns {}",
+                    config.resources.includePatterns.get());
+            builder.include(config.resources.includePatterns);
+            builder.exclude(config.resources.excludePatterns);
+            PathFilter pathFilter = builder.build();
+
+            for (ApplicationArchive archive : archives.getAllApplicationArchives()) {
+                LOGGER.debug("Scanning resources for native inclusion from archive at {}", archive.getArchiveLocation());
+
+                final Path rootPath = archive.getArchiveRoot();
+                Stream<Path> resourceStream = CamelSupport.safeWalk(rootPath).filter(path -> Files.isRegularFile(path));
+                resourceStream.map(filePath -> rootPath.relativize(filePath)).filter(pathFilter.asPathPredicate())
+                        .forEach(filteredPath -> {
+                            resources.produce(new NativeImageResourceBuildItem(filteredPath.toString()));
+                            LOGGER.debug("Embedding resource in native executable: {}", filteredPath.toString());
+                        });
+            }
+        }
     }
 
     /*
diff --git a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelConfig.java b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelConfig.java
index 1b2c75c..b5d8319 100644
--- a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelConfig.java
+++ b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelConfig.java
@@ -44,6 +44,12 @@ public class CamelConfig {
     @ConfigItem
     public RuntimeCatalogConfig runtimeCatalog;
 
+    /**
+     * Build time configuration options for resources inclusion in the native executable.
+     */
+    @ConfigItem
+    public ResourcesConfig resources;
+
     @ConfigGroup
     public static class MainConfig {
         /**
@@ -181,6 +187,35 @@ public class CamelConfig {
     }
 
     @ConfigGroup
+    public static class ResourcesConfig {
+
+        /**
+         * A comma separated list of Ant-path style patterns to match resources
+         * that should be <strong>excluded</strong> from the native executable. By default,
+         * resources not selected by quarkus itself are ignored. Then, inclusion
+         * of additional resources could be triggered with
+         * <code>includePatterns</code>. When the inclusion patterns is too
+         * large, eviction of previously selected resources could be triggered
+         * with <code>excludePatterns</code>.
+         */
+        @ConfigItem
+        public Optional<List<String>> excludePatterns;
+
+        /**
+         * A comma separated list of Ant-path style patterns to match resources
+         * that should be <strong>included</strong> in the native executable. By default,
+         * resources not selected by quarkus itself are ignored. Then, inclusion
+         * of additional resources could be triggered with
+         * <code>includePatterns</code>. When the inclusion patterns is too
+         * large, eviction of previously selected resources could be triggered
+         * with <code>excludePatterns</code>.
+         */
+        @ConfigItem
+        public Optional<List<String>> includePatterns;
+
+    }
+
+    @ConfigGroup
     public static class RuntimeCatalogConfig {
         /**
          * Used to control the resolution of components catalog info.
diff --git a/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java b/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java
index b4e4f49..9e58def 100644
--- a/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java
+++ b/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java
@@ -17,6 +17,8 @@
 package org.apache.camel.quarkus.core;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
@@ -37,6 +39,7 @@ import org.apache.camel.model.ModelCamelContext;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.support.LRUCacheFactory;
 import org.apache.camel.support.processor.DefaultExchangeFormatter;
+import org.apache.commons.io.IOUtils;
 
 @Path("/test")
 @ApplicationScoped
@@ -152,4 +155,16 @@ public class CoreResource {
     public String lruCacheFactory() {
         return LRUCacheFactory.getInstance().getClass().getName();
     }
+
+    @Path("/resources/{name : (.+)?}")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String getResource(@PathParam("name") String name) throws IOException {
+        try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(name)) {
+            if (is == null) {
+                return null;
+            }
+            return IOUtils.toString(is, StandardCharsets.UTF_8);
+        }
+    }
 }
diff --git a/integration-tests/core/src/main/resources/application.properties b/integration-tests/core/src/main/resources/application.properties
index 0314477..f59e097 100644
--- a/integration-tests/core/src/main/resources/application.properties
+++ b/integration-tests/core/src/main/resources/application.properties
@@ -25,6 +25,8 @@ quarkus.log.category."org.apache.camel.quarkus.core".level = DEBUG
 #
 quarkus.camel.main.enabled = false
 quarkus.camel.runtime-catalog.languages = false
+quarkus.camel.resources.include-patterns = include-pattern-folder/*
+quarkus.camel.resources.exclude-patterns = exclude-pattern-folder/*,include-pattern-folder/excluded.txt
 
 #
 # Camel
diff --git a/integration-tests/mustache/src/main/resources/application.properties b/integration-tests/core/src/main/resources/exclude-pattern-folder/excluded.txt
similarity index 91%
copy from integration-tests/mustache/src/main/resources/application.properties
copy to integration-tests/core/src/main/resources/exclude-pattern-folder/excluded.txt
index c333816..fe18b7e 100644
--- a/integration-tests/mustache/src/main/resources/application.properties
+++ b/integration-tests/core/src/main/resources/exclude-pattern-folder/excluded.txt
@@ -14,4 +14,4 @@
 ## See the License for the specific language governing permissions and
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
-quarkus.native.additional-build-args =-H:ResourceConfigurationFiles=resources-config.json
\ No newline at end of file
+MATCH exclude-patterns BUT NOT include-patterns
\ No newline at end of file
diff --git a/integration-tests/mustache/src/main/resources/application.properties b/integration-tests/core/src/main/resources/include-pattern-folder/excluded.txt
similarity index 91%
copy from integration-tests/mustache/src/main/resources/application.properties
copy to integration-tests/core/src/main/resources/include-pattern-folder/excluded.txt
index c333816..56dd2a5 100644
--- a/integration-tests/mustache/src/main/resources/application.properties
+++ b/integration-tests/core/src/main/resources/include-pattern-folder/excluded.txt
@@ -14,4 +14,4 @@
 ## See the License for the specific language governing permissions and
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
-quarkus.native.additional-build-args =-H:ResourceConfigurationFiles=resources-config.json
\ No newline at end of file
+MATCH include-patterns AND exclude-patterns
\ No newline at end of file
diff --git a/integration-tests/mustache/src/main/resources/application.properties b/integration-tests/core/src/main/resources/include-pattern-folder/included.txt
similarity index 91%
copy from integration-tests/mustache/src/main/resources/application.properties
copy to integration-tests/core/src/main/resources/include-pattern-folder/included.txt
index c333816..ab48d12 100644
--- a/integration-tests/mustache/src/main/resources/application.properties
+++ b/integration-tests/core/src/main/resources/include-pattern-folder/included.txt
@@ -14,4 +14,4 @@
 ## See the License for the specific language governing permissions and
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
-quarkus.native.additional-build-args =-H:ResourceConfigurationFiles=resources-config.json
\ No newline at end of file
+MATCH include-patterns BUT NOT exclude-patterns
\ No newline at end of file
diff --git a/integration-tests/mustache/src/main/resources/application.properties b/integration-tests/core/src/main/resources/no-pattern-folder/excluded.txt
similarity index 91%
copy from integration-tests/mustache/src/main/resources/application.properties
copy to integration-tests/core/src/main/resources/no-pattern-folder/excluded.txt
index c333816..d1dea1b 100644
--- a/integration-tests/mustache/src/main/resources/application.properties
+++ b/integration-tests/core/src/main/resources/no-pattern-folder/excluded.txt
@@ -14,4 +14,4 @@
 ## See the License for the specific language governing permissions and
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
-quarkus.native.additional-build-args =-H:ResourceConfigurationFiles=resources-config.json
\ No newline at end of file
+DO NOT MATCH exclude-patterns NOR include-patterns
\ No newline at end of file
diff --git a/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CoreIT.java b/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CoreIT.java
index 4f577aa..2996e5b 100644
--- a/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CoreIT.java
+++ b/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CoreIT.java
@@ -17,7 +17,43 @@
 package org.apache.camel.quarkus.core;
 
 import io.quarkus.test.junit.NativeImageTest;
+import io.restassured.RestAssured;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 @NativeImageTest
 public class CoreIT extends CoreTest {
+
+    @Test
+    public void nonExistentResourceCouldNotBeLoadedFromNativeExecutable() {
+        RestAssured.when().get("/test/resources/not-exist.txt").then().assertThat().statusCode(204);
+    }
+
+    @Test
+    public void resourceMatchingExcludedPatternOnlyCouldNotBeLoadedFromNativeExecutable() {
+        RestAssured.when().get("/test/resources/exclude-pattern-folder/excluded.txt").then().assertThat()
+                .statusCode(204);
+    }
+
+    @Test
+    public void resourceMatchingIncludeAndExcludedPatternCouldNotBeLoadedFromNativeExecutable() {
+        RestAssured.when().get("/test/resources/include-pattern-folder/excluded.txt").then().assertThat()
+                .statusCode(204);
+    }
+
+    @Test
+    public void resourceMatchingIncludePatternOnlyCouldBeLoadedFromNativeExecutable() {
+        String response = RestAssured.when().get("/test/resources/include-pattern-folder/included.txt").then()
+                .assertThat().statusCode(200).extract().asString();
+        assertNotNull(response);
+        assertTrue(response.endsWith("MATCH include-patterns BUT NOT exclude-patterns"), response);
+    }
+
+    @Test
+    public void resourceMatchingNoPatternCouldNotBeLoadedFromNativeExecutable() {
+        RestAssured.when().get("/test/resources/no-pattern-folder/excluded.properties.txt").then().assertThat()
+                .statusCode(204);
+    }
 }
diff --git a/integration-tests/mustache/src/main/java/org/apache/camel/quarkus/component/mustache/it/MustacheResource.java b/integration-tests/mustache/src/main/java/org/apache/camel/quarkus/component/mustache/it/MustacheResource.java
index 15ebce2..d827943 100644
--- a/integration-tests/mustache/src/main/java/org/apache/camel/quarkus/component/mustache/it/MustacheResource.java
+++ b/integration-tests/mustache/src/main/java/org/apache/camel/quarkus/component/mustache/it/MustacheResource.java
@@ -44,7 +44,7 @@ public class MustacheResource {
     @Produces(MediaType.TEXT_PLAIN)
     public String applyMustacheTemplateFromClassPathResource(String message) {
         LOG.infof("Calling applyMustacheTemplateFromClassPathResource with %s", message);
-        return template.requestBodyAndHeader("mustache://simple.mustache", message, "header", "value", String.class);
+        return template.requestBodyAndHeader("mustache://template/simple.mustache", message, "header", "value", String.class);
     }
 
     @Path("/applyMustacheTemplateFromHeader")
@@ -53,7 +53,8 @@ public class MustacheResource {
     @Produces(MediaType.TEXT_PLAIN)
     public String applyMustacheTemplateFromHeader(String message) {
         LOG.infof("Calling applyMustacheTemplateFromHeader with %s", message);
-        return template.requestBodyAndHeader("mustache://simple.mustache", message, MustacheConstants.MUSTACHE_TEMPLATE,
+        return template.requestBodyAndHeader("mustache://template/simple.mustache", message,
+                MustacheConstants.MUSTACHE_TEMPLATE,
                 "Body='{{body}}'", String.class);
     }
 
@@ -63,8 +64,9 @@ public class MustacheResource {
     @Produces(MediaType.TEXT_PLAIN)
     public String applyMustacheTemplateUriFromHeader(String message) {
         LOG.infof("Calling applyMustacheTemplateUriFromHeader with %s", message);
-        return template.requestBodyAndHeader("mustache://simple.mustache", message, MustacheConstants.MUSTACHE_RESOURCE_URI,
-                "/another.mustache", String.class);
+        return template.requestBodyAndHeader("mustache://template/simple.mustache", message,
+                MustacheConstants.MUSTACHE_RESOURCE_URI,
+                "/template/another.mustache", String.class);
     }
 
     @Path("/applyMustacheTemplateWithInheritance")
@@ -72,7 +74,7 @@ public class MustacheResource {
     @Produces(MediaType.TEXT_PLAIN)
     public String applyMustacheTemplateWithInheritance() {
         LOG.infof("Calling applyMustacheTemplateWithInheritance");
-        return template.requestBody("mustache://child.mustache", null, String.class);
+        return template.requestBody("mustache://template/child.mustache", null, String.class);
     }
 
     @Path("/applyMustacheTemplateWithPartials")
@@ -80,7 +82,7 @@ public class MustacheResource {
     @Produces(MediaType.TEXT_PLAIN)
     public String applyMustacheTemplateWithPartials() {
         LOG.infof("Calling applyMustacheTemplateWithPartials");
-        return template.requestBody("mustache://includer.mustache", null, String.class);
+        return template.requestBody("mustache://template/includer.mustache", null, String.class);
     }
 
     @Path("/applyMustacheTemplateFromRegistry")
diff --git a/integration-tests/mustache/src/main/resources/application.properties b/integration-tests/mustache/src/main/resources/application.properties
index c333816..90c68fd 100644
--- a/integration-tests/mustache/src/main/resources/application.properties
+++ b/integration-tests/mustache/src/main/resources/application.properties
@@ -14,4 +14,5 @@
 ## See the License for the specific language governing permissions and
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
-quarkus.native.additional-build-args =-H:ResourceConfigurationFiles=resources-config.json
\ No newline at end of file
+
+quarkus.camel.resources.include-patterns = template/*.mustache
\ No newline at end of file
diff --git a/integration-tests/mustache/src/main/resources/resources-config.json b/integration-tests/mustache/src/main/resources/resources-config.json
deleted file mode 100644
index 8307b4f..0000000
--- a/integration-tests/mustache/src/main/resources/resources-config.json
+++ /dev/null
@@ -1,7 +0,0 @@
-{
-  "resources": [
-    {
-      "pattern": ".*\\.mustache"
-    }
-  ]
-}
\ No newline at end of file
diff --git a/integration-tests/mustache/src/main/resources/another.mustache b/integration-tests/mustache/src/main/resources/template/another.mustache
similarity index 100%
rename from integration-tests/mustache/src/main/resources/another.mustache
rename to integration-tests/mustache/src/main/resources/template/another.mustache
diff --git a/integration-tests/mustache/src/main/resources/child.mustache b/integration-tests/mustache/src/main/resources/template/child.mustache
similarity index 100%
rename from integration-tests/mustache/src/main/resources/child.mustache
rename to integration-tests/mustache/src/main/resources/template/child.mustache
diff --git a/integration-tests/mustache/src/main/resources/included.mustache b/integration-tests/mustache/src/main/resources/template/included.mustache
similarity index 100%
rename from integration-tests/mustache/src/main/resources/included.mustache
rename to integration-tests/mustache/src/main/resources/template/included.mustache
diff --git a/integration-tests/mustache/src/main/resources/includer.mustache b/integration-tests/mustache/src/main/resources/template/includer.mustache
similarity index 100%
rename from integration-tests/mustache/src/main/resources/includer.mustache
rename to integration-tests/mustache/src/main/resources/template/includer.mustache
diff --git a/integration-tests/mustache/src/main/resources/parent.mustache b/integration-tests/mustache/src/main/resources/template/parent.mustache
similarity index 100%
rename from integration-tests/mustache/src/main/resources/parent.mustache
rename to integration-tests/mustache/src/main/resources/template/parent.mustache
diff --git a/integration-tests/mustache/src/main/resources/simple.mustache b/integration-tests/mustache/src/main/resources/template/simple.mustache
similarity index 100%
rename from integration-tests/mustache/src/main/resources/simple.mustache
rename to integration-tests/mustache/src/main/resources/template/simple.mustache