Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added options to select resources for inclusion in native executable #868 #960

Merged
merged 1 commit into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -208,6 +209,35 @@ List<NativeImageResourceBuildItem> camelRuntimeCatalog(

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());
});
}
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -180,6 +186,35 @@ public static class ServiceRegistryConfig {
public Optional<List<String>> includePatterns;
}

@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 {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +39,7 @@
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
Expand Down Expand Up @@ -152,4 +155,16 @@ public String catalog(@PathParam("type") String type, @PathParam("name") String
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------
MATCH exclude-patterns BUT NOT include-patterns
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------
MATCH include-patterns AND exclude-patterns
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------
MATCH include-patterns BUT NOT exclude-patterns
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------
DO NOT MATCH exclude-patterns NOR include-patterns
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -53,7 +53,8 @@ public String applyMustacheTemplateFromClassPathResource(String message) {
@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);
}

Expand All @@ -63,24 +64,25 @@ public String applyMustacheTemplateFromHeader(String message) {
@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")
@GET
@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")
@GET
@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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

quarkus.camel.resources.include-patterns = template/*.mustache

This file was deleted.