You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/11/10 10:29:55 UTC
[camel-k-runtime] 01/04: Leveragte MP Config to load application
and user properties
This is an automated email from the ASF dual-hosted git repository.
lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
commit b9719f275aa8502e487f0ccb2c30e03a8a012130
Author: Luca Burgazzoli <lb...@gmail.com>
AuthorDate: Tue Nov 10 09:41:20 2020 +0100
Leveragte MP Config to load application and user properties
---
.../camel/k/listener/PropertiesConfigurer.java | 8 --
.../apache/camel/k/support/PropertiesSupport.java | 103 ---------------------
.../org/apache/camel/k/support/RuntimeSupport.java | 84 +++++++++++++++++
.../k/quarkus/ApplicationConfigSourceProvider.java | 21 ++---
.../org/apache/camel/k/quarkus/it/Application.java | 24 +----
.../org/apache/camel/k/quarkus/it/RuntimeTest.java | 23 +----
.../src/test/resources/conf.d/003/flat-property | 1 +
7 files changed, 97 insertions(+), 167 deletions(-)
diff --git a/camel-k-core/support/src/main/java/org/apache/camel/k/listener/PropertiesConfigurer.java b/camel-k-core/support/src/main/java/org/apache/camel/k/listener/PropertiesConfigurer.java
index 5baeb4a..2dadca5 100644
--- a/camel-k-core/support/src/main/java/org/apache/camel/k/listener/PropertiesConfigurer.java
+++ b/camel-k-core/support/src/main/java/org/apache/camel/k/listener/PropertiesConfigurer.java
@@ -20,7 +20,6 @@ import org.apache.camel.Ordered;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.support.Constants;
import org.apache.camel.k.support.KubernetesPropertiesFunction;
-import org.apache.camel.k.support.PropertiesSupport;
public class PropertiesConfigurer extends AbstractPhaseListener {
public PropertiesConfigurer() {
@@ -34,13 +33,6 @@ public class PropertiesConfigurer extends AbstractPhaseListener {
@Override
protected void accept(Runtime runtime) {
- runtime.setInitialProperties(
- PropertiesSupport.loadApplicationProperties()
- );
- runtime.setPropertiesLocations(
- PropertiesSupport.resolveUserPropertiesLocations()
- );
-
//
// Register properties functions to resolve k8s secrets or config maps like:
//
diff --git a/camel-k-core/support/src/main/java/org/apache/camel/k/support/PropertiesSupport.java b/camel-k-core/support/src/main/java/org/apache/camel/k/support/PropertiesSupport.java
index b88d64d..43a93e4 100644
--- a/camel-k-core/support/src/main/java/org/apache/camel/k/support/PropertiesSupport.java
+++ b/camel-k-core/support/src/main/java/org/apache/camel/k/support/PropertiesSupport.java
@@ -16,22 +16,9 @@
*/
package org.apache.camel.k.support;
-import java.io.IOException;
-import java.io.Reader;
-import java.nio.file.FileVisitResult;
-import java.nio.file.FileVisitor;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.nio.file.SimpleFileVisitor;
-import java.nio.file.attribute.BasicFileAttributes;
-import java.util.Collection;
import java.util.HashMap;
-import java.util.LinkedHashSet;
import java.util.Map;
-import java.util.Objects;
import java.util.Properties;
-import java.util.Set;
import java.util.function.Predicate;
import org.apache.camel.CamelContext;
@@ -41,7 +28,6 @@ import org.apache.camel.spi.PropertiesComponent;
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.service.ServiceHelper;
-import org.apache.camel.util.ObjectHelper;
public final class PropertiesSupport {
private PropertiesSupport() {
@@ -103,93 +89,4 @@ public final class PropertiesSupport {
return target;
}
- public static String resolveApplicationPropertiesLocation() {
- return System.getProperty(Constants.PROPERTY_CAMEL_K_CONF, System.getenv(Constants.ENV_CAMEL_K_CONF));
- }
-
- public static Properties loadApplicationProperties() {
- final String conf = resolveApplicationPropertiesLocation();
- final Properties properties = new Properties();
-
- if (ObjectHelper.isEmpty(conf)) {
- return properties;
- }
-
- try {
- Path confPath = Paths.get(conf);
-
- if (Files.exists(confPath)) {
- try (Reader reader = Files.newBufferedReader(confPath)) {
- properties.load(reader);
- }
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
- return properties;
- }
-
- public static String resolveUserPropertiesLocation() {
- return System.getProperty(Constants.PROPERTY_CAMEL_K_CONF_D, System.getenv(Constants.ENV_CAMEL_K_CONF_D));
- }
-
- public static Properties loadUserProperties() {
- final Properties properties = new Properties();
-
- try {
- for (String location: resolveUserPropertiesLocations()) {
- try (Reader reader = Files.newBufferedReader(Paths.get(location))) {
- properties.load(reader);
- }
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
- return properties;
- }
-
- public static Properties loadProperties() {
- final Properties app = loadApplicationProperties();
- final Properties usr = loadUserProperties();
-
- app.putAll(usr);
-
- return app;
- }
-
- public static Collection<String> resolveUserPropertiesLocations() {
- final String conf = resolveUserPropertiesLocation();
- final Set<String> locations = new LinkedHashSet<>();
-
- // Additional locations
- if (ObjectHelper.isNotEmpty(conf)) {
- Path root = Paths.get(conf);
- FileVisitor<Path> visitor = new SimpleFileVisitor<>() {
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- Objects.requireNonNull(file);
- Objects.requireNonNull(attrs);
-
- final String path = file.toFile().getAbsolutePath();
- if (path.endsWith(".properties")) {
- locations.add(path);
- }
-
- return FileVisitResult.CONTINUE;
- }
- };
-
- if (Files.exists(root)) {
- try {
- Files.walkFileTree(root, visitor);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- return locations;
- }
}
diff --git a/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java b/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
index b211307..16df12d 100644
--- a/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
+++ b/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
@@ -16,8 +16,19 @@
*/
package org.apache.camel.k.support;
+import java.io.IOException;
import java.io.InputStream;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -294,4 +305,77 @@ public final class RuntimeSupport {
return Objects.requireNonNull(version, "Could not determine Camel K Runtime version");
}
+ // *********************************
+ //
+ // Properties
+ //
+ // *********************************
+
+ public static Map<String, String> loadApplicationProperties() {
+ final String conf = System.getProperty(Constants.PROPERTY_CAMEL_K_CONF, System.getenv(Constants.ENV_CAMEL_K_CONF));
+ final Map<String, String> properties = new HashMap<>();
+
+ if (ObjectHelper.isEmpty(conf)) {
+ return properties;
+ }
+
+ try {
+ Path confPath = Paths.get(conf);
+
+ if (Files.exists(confPath)) {
+ try (Reader reader = Files.newBufferedReader(confPath)) {
+ Properties p = new Properties();
+ p.load(reader);
+ p.forEach((key, value) -> properties.put(String.valueOf(key), String.valueOf(value)));
+ }
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ return properties;
+ }
+
+ public static Map<String, String> loadUserProperties() {
+ final String conf = System.getProperty(Constants.PROPERTY_CAMEL_K_CONF_D, System.getenv(Constants.ENV_CAMEL_K_CONF_D));
+ final Map<String, String> properties = new HashMap<>();
+
+ if (ObjectHelper.isEmpty(conf)) {
+ return properties;
+ }
+
+ FileVisitor<Path> visitor = new SimpleFileVisitor<>() {
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ Objects.requireNonNull(file);
+ Objects.requireNonNull(attrs);
+
+ if (file.toFile().getAbsolutePath().endsWith(".properties")) {
+ try (Reader reader = Files.newBufferedReader(file)) {
+ Properties p = new Properties();
+ p.load(reader);
+ p.forEach((key, value) -> properties.put(String.valueOf(key), String.valueOf(value)));
+ }
+ } else {
+ properties.put(
+ file.getFileName().toString(),
+ Files.readString(file, StandardCharsets.UTF_8));
+ }
+
+ return FileVisitResult.CONTINUE;
+ }
+ };
+
+ Path root = Paths.get(conf);
+
+ if (Files.exists(root)) {
+ try {
+ Files.walkFileTree(root, visitor);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ return properties;
+ }
}
diff --git a/camel-k-runtime/runtime/src/main/java/org/apache/camel/k/quarkus/ApplicationConfigSourceProvider.java b/camel-k-runtime/runtime/src/main/java/org/apache/camel/k/quarkus/ApplicationConfigSourceProvider.java
index cef2910..77fffb2 100644
--- a/camel-k-runtime/runtime/src/main/java/org/apache/camel/k/quarkus/ApplicationConfigSourceProvider.java
+++ b/camel-k-runtime/runtime/src/main/java/org/apache/camel/k/quarkus/ApplicationConfigSourceProvider.java
@@ -16,28 +16,23 @@
*/
package org.apache.camel.k.quarkus;
-import java.util.Collections;
-import java.util.Properties;
+import java.util.List;
+import java.util.Map;
import io.smallrye.config.PropertiesConfigSource;
-import org.apache.camel.k.support.PropertiesSupport;
+import org.apache.camel.k.support.RuntimeSupport;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
public class ApplicationConfigSourceProvider implements ConfigSourceProvider {
@Override
public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) {
- final Properties applicationProperties = PropertiesSupport.loadProperties();
- final Properties quarkusProperties = new Properties();
+ final Map<String, String> appProperties = RuntimeSupport.loadApplicationProperties();
+ final Map<String, String> usrProperties = RuntimeSupport.loadUserProperties();
- for (String name : applicationProperties.stringPropertyNames()) {
- if (name.startsWith("quarkus.")) {
- quarkusProperties.put(name, applicationProperties.get(name));
- }
- }
-
- return Collections.singletonList(
- new PropertiesConfigSource(quarkusProperties, "camel-k")
+ return List.of(
+ new PropertiesConfigSource(appProperties, "camel-k-app", ConfigSource.DEFAULT_ORDINAL),
+ new PropertiesConfigSource(usrProperties, "camel-k-usr", ConfigSource.DEFAULT_ORDINAL + 1)
);
}
}
diff --git a/itests/camel-k-itests-runtime/src/main/java/org/apache/camel/k/quarkus/it/Application.java b/itests/camel-k-itests-runtime/src/main/java/org/apache/camel/k/quarkus/it/Application.java
index 92391ac..cf4c855 100644
--- a/itests/camel-k-itests-runtime/src/main/java/org/apache/camel/k/quarkus/it/Application.java
+++ b/itests/camel-k-itests-runtime/src/main/java/org/apache/camel/k/quarkus/it/Application.java
@@ -57,13 +57,6 @@ public class Application {
.add(
"routes-collector",
instance(CamelMain.class).map(BaseMainSupport::getRoutesCollector).map(Object::getClass).map(Class::getName).orElse(""))
- .add(
- "properties-locations",
- Json.createArrayBuilder(instance(CamelContext.class)
- .map(CamelContext::getPropertiesComponent)
- .map(PropertiesComponent.class::cast)
- .map(PropertiesComponent::getLocations)
- .orElseGet(Collections::emptyList)))
.build();
}
@@ -88,27 +81,16 @@ public class Application {
.flatMap(pc -> pc.resolveProperty(name)).orElse("");
}
- @GET
- @Path("/initial-property/{name}")
- @Produces(MediaType.TEXT_PLAIN)
- public String initialProperty(@PathParam("name") String name) {
- return (String)instance(CamelContext.class)
- .map(CamelContext::getPropertiesComponent)
- .map(PropertiesComponent.class::cast)
- .map(pc -> pc.getInitialProperties().get(name))
- .orElse("");
- }
-
@SuppressWarnings("unchecked")
@GET
- @Path("/initial-properties")
+ @Path("/properties")
@Produces(MediaType.APPLICATION_JSON)
- public JsonObject initialProperties() {
+ public JsonObject properties() {
return Json.createObjectBuilder(
instance(CamelContext.class)
.map(CamelContext::getPropertiesComponent)
.map(PropertiesComponent.class::cast)
- .map(PropertiesComponent::getInitialProperties)
+ .map(PropertiesComponent::loadProperties)
.map(Map.class::cast)
.orElseGet(Collections::emptyMap)
).build();
diff --git a/itests/camel-k-itests-runtime/src/test/java/org/apache/camel/k/quarkus/it/RuntimeTest.java b/itests/camel-k-itests-runtime/src/test/java/org/apache/camel/k/quarkus/it/RuntimeTest.java
index 3d7f41a..19fea58 100644
--- a/itests/camel-k-itests-runtime/src/test/java/org/apache/camel/k/quarkus/it/RuntimeTest.java
+++ b/itests/camel-k-itests-runtime/src/test/java/org/apache/camel/k/quarkus/it/RuntimeTest.java
@@ -16,14 +16,11 @@
*/
package org.apache.camel.k.quarkus.it;
-import java.util.Map;
-
import javax.ws.rs.core.MediaType;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.JsonPath;
import org.apache.camel.k.quarkus.Application;
-import org.apache.camel.k.support.PropertiesSupport;
import org.apache.camel.quarkus.core.FastCamelContext;
import org.junit.jupiter.api.Test;
@@ -50,25 +47,6 @@ public class RuntimeTest {
.isEqualTo(Application.Runtime.class.getName());
assertThat(p.getString("routes-collector"))
.isEqualTo(Application.NoRoutesCollector.class.getName());
- assertThat(p.getList("properties-locations", String.class))
- .contains("file:" + System.getProperty("camel.k.conf.d", System.getenv("CAMEL_K_CONF_D")) + "/001/conf.properties")
- .contains("file:" + System.getProperty("camel.k.conf.d", System.getenv("CAMEL_K_CONF_D")) + "/002/conf.properties");
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void initialProperties() {
- Map<String, String> initialProperties = given()
- .accept(MediaType.APPLICATION_JSON)
- .get("/test/initial-properties")
- .then()
- .statusCode(200)
- .extract()
- .body().jsonPath().getMap(".", String.class, String.class);
-
- assertThat(initialProperties).containsExactlyEntriesOf((Map)PropertiesSupport.loadApplicationProperties());
- assertThat(initialProperties).containsEntry("root.key", "root.value");
- assertThat(initialProperties).containsEntry("a.key", "a.root");
}
@Test
@@ -78,5 +56,6 @@ public class RuntimeTest {
given().get("/test/property/001.key").then().statusCode(200).body(is("001.value"));
given().get("/test/property/002.key").then().statusCode(200).body(is("002.value"));
given().get("/test/property/a.key").then().statusCode(200).body(is("a.002"));
+ given().get("/test/property/flat-property").then().statusCode(200).body(is("flat-value"));
}
}
\ No newline at end of file
diff --git a/itests/camel-k-itests-runtime/src/test/resources/conf.d/003/flat-property b/itests/camel-k-itests-runtime/src/test/resources/conf.d/003/flat-property
new file mode 100644
index 0000000..313b57d
--- /dev/null
+++ b/itests/camel-k-itests-runtime/src/test/resources/conf.d/003/flat-property
@@ -0,0 +1 @@
+flat-value
\ No newline at end of file