Skip to content

Commit

Permalink
Configure NativeImageResourceBuildItem for camel route classpath reso…
Browse files Browse the repository at this point in the history
…urces

Fixes #1812
  • Loading branch information
jamesnetherton authored and ppalaga committed Sep 23, 2020
1 parent a591261 commit 9b5f04b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import org.apache.camel.impl.engine.AbstractCamelContext;
import org.apache.camel.quarkus.core.deployment.spi.CamelServiceBuildItem;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.ClassInfo;

public final class CamelSupport {
Expand Down Expand Up @@ -118,4 +119,10 @@ public static String getCamelVersion() {

return Objects.requireNonNull(version, "Could not determine Camel version");
}

public static <T> T getOptionalConfigValue(String property, Class<T> type, T defaultValue) {
return ConfigProvider.getConfig()
.getOptionalValue(property, type)
.orElse(defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import org.eclipse.microprofile.config.ConfigProvider;
import org.apache.camel.quarkus.core.deployment.util.CamelSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -53,10 +53,7 @@ List<HotDeploymentWatchedFileBuildItem> xmlRests() {
}

private static List<HotDeploymentWatchedFileBuildItem> locations(String property) {
String[] locations = ConfigProvider.getConfig()
.getOptionalValue(property, String[].class)
.orElse(EMPTY_STRING_ARRAY);

String[] locations = CamelSupport.getOptionalConfigValue(property, String[].class, EMPTY_STRING_ARRAY);
List<HotDeploymentWatchedFileBuildItem> items = Stream.of(locations)
.filter(location -> location.startsWith(FILE_PREFIX))
.map(location -> location.substring(FILE_PREFIX.length()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@
*/
package org.apache.camel.quarkus.main.deployment;

import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import org.apache.camel.quarkus.core.deployment.util.CamelSupport;
import org.apache.camel.quarkus.support.common.CamelCapabilities;
import org.apache.camel.support.ResourceHelper;
import org.apache.camel.util.AntPathMatcher;
import org.jboss.logging.Logger;

public class CamelMainNativeImageProcessor {

private static final Logger LOG = Logger.getLogger(CamelMainNativeImageProcessor.class);

@BuildStep
ReflectiveClassBuildItem reflectiveCLasses() {
// TODO: The classes below are needed to fix https://github.com/apache/camel-quarkus/issues/1005
Expand All @@ -33,4 +44,33 @@ ReflectiveClassBuildItem reflectiveCLasses() {
org.apache.camel.spi.RestConfiguration.class,
org.apache.camel.quarkus.main.CamelMainApplication.class);
}

@BuildStep
void camelNativeImageResources(Capabilities capabilities, BuildProducer<NativeImageResourceBuildItem> nativeResource) {
if (capabilities.isCapabilityPresent(CamelCapabilities.XML)) {
String prefix = "camel.main.";
String[] properties = new String[] { "xml-rests", "xml-routes", "xml-route-templates" };
for (String property : properties) {
embedCamelResource(prefix + property, nativeResource);
}
}
}

private void embedCamelResource(String propertyName, BuildProducer<NativeImageResourceBuildItem> nativeResource) {
for (String path : CamelSupport.getOptionalConfigValue(propertyName, String[].class, new String[0])) {
String scheme = ResourceHelper.getScheme(path);

// Null scheme is equivalent to classpath scheme
if (scheme == null || scheme.equals("classpath:")) {
if (AntPathMatcher.INSTANCE.isPattern(path)) {
// Classpath directory traversal via wildcard paths does not work on GraalVM. The exact path to the resource has to be looked up
// https://github.com/oracle/graal/issues/1108
LOG.warnf("%s classpath wildcards does not work in native mode. Resources matching %s will not be loaded.",
propertyName, path);
} else {
nativeResource.produce(new NativeImageResourceBuildItem(path.replace("classpath:", "")));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#
quarkus.ssl.native=true

# include xml routes in native image
quarkus.native.additional-build-args = -H:IncludeResources=routes/.*-routes.xml

#
# Camel
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
# Quarkus
#

# include xml routes in native image
quarkus.native.additional-build-args = -H:IncludeResources=routes/my-routes.xml

#
# Camel
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
# Quarkus
#

# include xml routes in native image
quarkus.native.additional-build-args = -H:IncludeResources=routes/my-routes.xml

#
# Camel
#
Expand Down

0 comments on commit 9b5f04b

Please sign in to comment.