Skip to content

Commit

Permalink
Allow to configure default properties values apache#250
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Mar 4, 2020
1 parent 170fc99 commit faf9549
Show file tree
Hide file tree
Showing 27 changed files with 371 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,9 @@ class LoaderTest extends Specification {
void addRoutes(RoutesBuilder builder) {
this.builders.add(builder)
}

@Override
void setPropertiesLocations(Collection<String> locations) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.k.loader.java;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

Expand Down Expand Up @@ -166,6 +167,10 @@ public void addRoutes(RoutesBuilder builder) {
public void addConfiguration(Object configuration) {
this.configurations.add(configuration);
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.k.loader.js;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

Expand Down Expand Up @@ -86,5 +87,9 @@ public CamelContext getCamelContext() {
public void addRoutes(RoutesBuilder builder) {
this.builders.add(builder);
}

@Override
public void setPropertiesLocations(Collection<String> locations) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,8 @@ class LoaderTest {
override fun addRoutes(builder: RoutesBuilder) {
this.builders.add(builder)
}

override fun setPropertiesLocations(locations: MutableCollection<String>?) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.k.loader.xml;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

Expand Down Expand Up @@ -84,6 +85,10 @@ public void addRoutes(RoutesBuilder builder) {
this.builders.add(builder);
}

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

public SourceLoader load(Source source) throws Exception {
SourceLoader loader = RuntimeSupport.loaderFor(camelContext, source);
SourceLoader.Result result = loader.load(this, source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.k.main;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -59,12 +60,6 @@ public ApplicationRuntime() {
this.main.configure().setXmlRests("false");
this.main.setRoutesCollector(new NoRoutesCollector());
this.main.addMainListener(new MainListenerAdapter());

this.main.setPropertyPlaceholderLocations(
PropertiesSupport.resolvePropertiesLocations().stream()
.map(location -> "file:" + location)
.collect(Collectors.joining(","))
);
}

@Override
Expand All @@ -91,11 +86,27 @@ public void addConfiguration(Object configuration) {
this.main.addConfiguration(configuration);
}

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

@Override
public void setProperties(Properties properties) {
this.main.setOverrideProperties(properties);
}

@Override
public void setPropertiesLocations(Collection<String> locations) {
this.main.setPropertyPlaceholderLocations(
locations.stream()
.map(location -> location.startsWith("file:") ? location : "file:" + location)
.distinct()
.sorted()
.collect(Collectors.joining(","))
);
}

public void addListeners(Iterable<Runtime.Listener> listeners) {
listeners.forEach(this::addListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

import org.apache.camel.CamelContext;
import org.apache.camel.component.seda.SedaComponent;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.k.Constants;
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.listener.ContextConfigurer;
Expand All @@ -33,21 +36,42 @@ public class PropertiesTest {

@Test
public void testLoadProperties() throws Exception {
Properties properties = PropertiesSupport.loadProperties("src/test/resources/conf.properties", "src/test/resources/conf.d");
System.setProperty(Constants.PROPERTY_CAMEL_K_CONF, "src/test/resources/conf.properties");
System.setProperty(Constants.PROPERTY_CAMEL_K_CONF_D, "src/test/resources/conf.d");

ApplicationRuntime runtime = new ApplicationRuntime();
runtime.setProperties(properties);
runtime.addListener(new ContextConfigurer());
runtime.addListener(Runtime.Phase.Started, r -> {
CamelContext context = r.getCamelContext();
assertThat(context.resolvePropertyPlaceholders("{{root.key}}")).isEqualTo("root.value");
assertThat(context.resolvePropertyPlaceholders("{{001.key}}")).isEqualTo("001.value");
assertThat(context.resolvePropertyPlaceholders("{{002.key}}")).isEqualTo("002.value");
assertThat(context.resolvePropertyPlaceholders("{{a.key}}")).isEqualTo("a.002");
runtime.stop();
});
try {
ApplicationRuntime runtime = new ApplicationRuntime();
runtime.setInitialProperties(PropertiesSupport.loadApplicationProperties());
runtime.setPropertiesLocations(PropertiesSupport.resolveUserPropertiesLocations());
runtime.addListener(new ContextConfigurer());
runtime.addListener(Runtime.Phase.Started, r -> {
final CamelContext context = r.getCamelContext();
final PropertiesComponent pc = (PropertiesComponent)context.getPropertiesComponent();

assertThat(pc.getInitialProperties()).containsExactlyEntriesOf(PropertiesSupport.loadApplicationProperties());
assertThat(pc.getInitialProperties().getProperty("root.key")).isEqualTo("root.value");
assertThat(pc.getInitialProperties().getProperty("a.key")).isEqualTo("a.root");

assertThat(pc.getLocations()).hasSameElementsAs(
PropertiesSupport.resolveUserPropertiesLocations().stream()
.map(location -> "file:" + location)
.distinct()
.sorted()
.collect(Collectors.toList())
);

assertThat(pc.resolveProperty("root.key")).get().isEqualTo("root.value");
assertThat(pc.resolveProperty("001.key")).get().isEqualTo("001.value");
assertThat(pc.resolveProperty("002.key")).get().isEqualTo("002.value");
assertThat(pc.resolveProperty("a.key")).get().isEqualTo("a.002");
runtime.stop();
});

runtime.run();
runtime.run();
} finally {
System.getProperties().remove(Constants.PROPERTY_CAMEL_K_CONF);
System.getProperties().remove(Constants.PROPERTY_CAMEL_K_CONF_D);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package org.apache.camel.k.core.quarkus;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
Expand Down Expand Up @@ -108,6 +111,27 @@ public void addRoutes(RoutesBuilder builder) {
public void addConfiguration(Object configuration) {
main.addConfiguration(configuration);
}

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

@Override
public void setProperties(Properties properties) {
main.setOverrideProperties(properties);
}

@Override
public void setPropertiesLocations(Collection<String> locations) {
main.setPropertyPlaceholderLocations(
locations.stream()
.map(location -> location.startsWith("file:") ? location : "file:" + location)
.distinct()
.sorted()
.collect(Collectors.joining(","))
);
}
};
}
}

This file was deleted.

This file was deleted.

47 changes: 45 additions & 2 deletions camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
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;
Expand All @@ -37,6 +39,23 @@ 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)
);
}

default void setProperties(Properties properties) {
getCamelContext().getPropertiesComponent().setOverrideProperties(properties);
}
Expand Down Expand Up @@ -66,6 +85,12 @@ default void addConfiguration(Object configuration) {
throw new UnsupportedOperationException();
}

void setPropertiesLocations(Collection<String> locations);

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

/**
* Lifecycle method used to stops the entire integration.
*/
Expand Down Expand Up @@ -100,7 +125,16 @@ default int getOrder() {
* @return the runtime
*/
static Runtime on(CamelContext camelContext) {
return () -> camelContext;
return new Runtime() {
@Override
public void setPropertiesLocations(Collection<String> locations) {
}

@Override
public CamelContext getCamelContext() {
return camelContext;
}
};
}

/**
Expand All @@ -110,6 +144,15 @@ static Runtime on(CamelContext camelContext) {
* @return the runtime
*/
static Runtime on(HasCamelContext provider) {
return () -> provider.getCamelContext();
return new Runtime() {
@Override
public void setPropertiesLocations(Collection<String> locations) {
}

@Override
public CamelContext getCamelContext() {
return provider.getCamelContext();
}
};
}
}
Loading

0 comments on commit faf9549

Please sign in to comment.