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

Leveragte MP Config to load application and user properties #546

Merged
merged 4 commits into from
Nov 10, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions camel-k-core/api/src/main/java/org/apache/camel/k/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
*/
package org.apache.camel.k;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import org.apache.camel.CamelContext;
import org.apache.camel.Ordered;
Expand All @@ -33,7 +30,14 @@
import static org.apache.camel.util.CollectionHelper.mapOf;

public interface Runtime extends HasCamelContext, AutoCloseable {

/**
* Returns the camel context adapting it to the specialized type.
*
* @see HasCamelContext#getCamelContext()
* @see CamelContext#adapt(Class)
*
* @return the camel context.
*/
default <T extends CamelContext> T getCamelContext(Class<T> type) {
return getCamelContext().adapt(type);
}
Expand All @@ -45,40 +49,50 @@ default Registry getRegistry() {
return getCamelContext().getRegistry();
}

default void setInitialProperties(Properties properties) {
getCamelContext().getPropertiesComponent().setInitialProperties(properties);
}

default void setInitialProperties(Map<String, String> properties) {
Properties p = new Properties();
p.putAll(properties);

setInitialProperties(p);
}

default void setInitialProperties(String key, String value, String... keyVals) {
setInitialProperties(
mapOf(HashMap::new, key, value, keyVals)
);
}

/**
* Sets a special list of properties that take precedence and will use first, if a property exist.
*
* @see org.apache.camel.spi.PropertiesComponent#setOverrideProperties(Properties)
* @param properties the properties to set
*/
default void setProperties(Properties properties) {
getCamelContext().getPropertiesComponent().setOverrideProperties(properties);
}

/**
* Sets a special list of properties that take precedence and will use first, if a property exist.
*
* @see org.apache.camel.spi.PropertiesComponent#setOverrideProperties(Properties)
* @param properties the properties to set
*/
default void setProperties(Map<String, String> properties) {
Properties p = new Properties();
p.putAll(properties);

setProperties(p);
}

default void setProperties(String key, String value, String... keyVals) {
/**
* Sets a special list of properties that take precedence and will use first, if a property exist.
*
* @see org.apache.camel.spi.PropertiesComponent#setOverrideProperties(Properties)
* @param key the mapping's key
* @param value the mapping's value
* @param entries containing the keys and values from which the map is populated
*
*/
default void setProperties(String key, String value, String... entries) {
setProperties(
mapOf(HashMap::new, key, value, keyVals)
mapOf(HashMap::new, key, value, entries)
);
}

/**
* Sets a special list of properties that take precedence and will use first, if a property exist.
*
* @see org.apache.camel.spi.PropertiesComponent#setOverrideProperties(Properties)
* @param builder the builder which will create the routes
*/
default void addRoutes(RoutesBuilder builder) {
try {
getCamelContext().addRoutes(builder);
Expand All @@ -87,20 +101,6 @@ default void addRoutes(RoutesBuilder builder) {
}
}

default void setPropertiesLocations(Collection<String> locations) {
getCamelContext().getPropertiesComponent().setLocation(
locations.stream()
.map(location -> location.startsWith("file:") ? location : "file:" + location)
.distinct()
.sorted()
.collect(Collectors.joining(","))
);
}

default void setPropertiesLocations(String... locations) {
setPropertiesLocations(Arrays.asList(locations));
}

/**
* Lifecycle method used to stops the entire integration.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public interface SourceLoader {
* @param runtime the runtime.
* @param source the source to load.
* @return the RoutesBuilder.
* @throws Exception
*/
RoutesBuilder load(Runtime runtime, Source source);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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() {
Expand All @@ -34,13 +33,6 @@ public int getOrder() {

@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:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.camel.k.support;

import java.util.Collection;
import java.util.Map;
import java.util.Properties;

Expand All @@ -42,21 +41,6 @@ public Registry getRegistry() {
return runtime.getRegistry();
}

@Override
public void setInitialProperties(Properties properties) {
runtime.setInitialProperties(properties);
}

@Override
public void setInitialProperties(Map<String, String> properties) {
runtime.setInitialProperties(properties);
}

@Override
public void setInitialProperties(String key, String value, String... keyVals) {
runtime.setInitialProperties(key, value, keyVals);
}

@Override
public void setProperties(Properties properties) {
runtime.setProperties(properties);
Expand All @@ -77,16 +61,6 @@ public void addRoutes(RoutesBuilder builder) {
runtime.addRoutes(builder);
}

@Override
public void setPropertiesLocations(Collection<String> locations) {
runtime.setPropertiesLocations(locations);
}

@Override
public void setPropertiesLocations(String... locations) {
runtime.setPropertiesLocations(locations);
}

@Override
public void stop() throws Exception {
runtime.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,7 +28,6 @@
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() {
Expand Down Expand Up @@ -103,93 +89,4 @@ public static <T> T bindProperties(CamelContext context, T target, Predicate<Str
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;
}
}
Loading