You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2015/01/05 21:02:33 UTC
[1/3] incubator-tamaya git commit: TAMAYA-42: - Added direct
reference from Configuration to the ConfigurationContext. - Added repeatable
filter resolutions (currently by default max. 10 cycles are performed).
Repository: incubator-tamaya
Updated Branches:
refs/heads/master 853047a72 -> b98765721
TAMAYA-42:
- Added direct reference from Configuration to the ConfigurationContext.
- Added repeatable filter resolutions (currently by default max. 10 cycles are performed).
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/b9331a7d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/b9331a7d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/b9331a7d
Branch: refs/heads/master
Commit: b9331a7de84f901cbc8d795e393bef6ae69a4048
Parents: 909ec16
Author: anatole <an...@apache.org>
Authored: Mon Jan 5 20:56:48 2015 +0100
Committer: anatole <an...@apache.org>
Committed: Mon Jan 5 20:56:48 2015 +0100
----------------------------------------------------------------------
.../core/internal/DefaultConfiguration.java | 106 +++++++++++++++----
.../apache/tamaya/core/ConfigurationTest.java | 2 +-
2 files changed, 89 insertions(+), 19 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9331a7d/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java b/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
index 3769e27..47828a3 100644
--- a/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
+++ b/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
@@ -32,6 +32,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@@ -42,8 +43,15 @@ import java.util.stream.Collectors;
* instance to evaluate the current Configuration.
*/
public class DefaultConfiguration implements Configuration {
-
+ /** The logger. */
private static final Logger LOG = Logger.getLogger(DefaultConfiguration.class.getName());
+ /** The maximal number of filter cycles performed before aborting. */
+ private static final int MAX_FILTER_LOOPS = 10;
+
+ /**
+ * The current {@link org.apache.tamaya.spi.ConfigurationContext} of the current instance.
+ */
+ private ConfigurationContext configurationContext = ServiceContext.getInstance().getService(ConfigurationContext.class).get();
/**
* This method evaluates the given configuration key. Hereby if goes down the chain or PropertySource instances
@@ -57,7 +65,7 @@ public class DefaultConfiguration implements Configuration {
*/
@Override
public Optional<String> get(String key) {
- List<PropertySource> propertySources = ServiceContext.getInstance().getService(ConfigurationContext.class).get().getPropertySources();
+ List<PropertySource> propertySources = configurationContext.getPropertySources();
String unfilteredValue = null;
for (PropertySource propertySource : propertySources) {
Optional<String> value = propertySource.get(key);
@@ -66,19 +74,51 @@ public class DefaultConfiguration implements Configuration {
break;
}
}
+ return Optional.ofNullable(applyFilter(key, unfilteredValue));
+ }
+
+ private String applyFilter(String key, String unfilteredValue) {
// Apply filters to values, prevent values filtered to null!
- for(PropertyFilter filter:
- ServiceContext.getInstance().getService(ConfigurationContext.class).get().getPropertyFilters()){
- unfilteredValue = filter.filterProperty(key, unfilteredValue,
- (String k) -> key.equals(k)?null:get(k).orElse(null));
+ for(int i=0; i<MAX_FILTER_LOOPS;i++) {
+ boolean changed = false;
+ // Apply filters to values, prevent values filtered to null!
+ for (PropertyFilter filter : configurationContext.getPropertyFilters()) {
+ String newValue = filter.filterProperty(key, unfilteredValue,
+ (String k) -> key.equals(k) ? null : get(k).orElse(null));
+ if (newValue != null && !newValue.equals(unfilteredValue)) {
+ changed = true;
+ if(LOG.isLoggable(Level.FINEST)) {
+ LOG.finest("Filter - " + key + ": " + unfilteredValue + " -> " + newValue + " by " + filter);
+ }
+ } else if (unfilteredValue != null && !unfilteredValue.equals(newValue)) {
+ changed = true;
+ if(LOG.isLoggable(Level.FINEST)) {
+ LOG.finest("Filter - " + key + ": " + unfilteredValue + " -> " + newValue + " by " + filter);
+ }
+ }
+ unfilteredValue = newValue;
+ }
+ if (!changed) {
+ LOG.finest(() -> "Finishing filter loop, no changes detected.");
+ break;
+ }
+ else{
+ if(i==(MAX_FILTER_LOOPS-1)) {
+ if(LOG.isLoggable(Level.WARNING)) {
+ LOG.warning("Maximal filter loop count reached, aborting filter evaluation after cycles: " + i);
+ }
+ }
+ else{
+ LOG.finest(() -> "Repeating filter loop, changes detected.");
+ }
+ }
}
- return Optional.ofNullable(unfilteredValue);
+ return unfilteredValue;
}
@Override
public Map<String, String> getProperties() {
- List<PropertySource> propertySources = new ArrayList<>(
- ServiceContext.getInstance().getService(ConfigurationContext.class).get().getPropertySources());
+ List<PropertySource> propertySources = new ArrayList<>(configurationContext.getPropertySources());
Collections.reverse(propertySources);
Map<String, String> result = new HashMap<>();
for (PropertySource propertySource : propertySources) {
@@ -90,17 +130,48 @@ public class DefaultConfiguration implements Configuration {
LOG.log(Level.FINEST, null, () -> "Handled properties from " + propertySource.getName() + "(new: " +
(result.size() - origSize) + ", overrides: " + origSize + ", total: " + result.size());
} catch (Exception e) {
- LOG.log(Level.SEVERE, "Error adding properties from PropertySource: " + propertySource +", ignoring PropertySource.", e);
+ LOG.log(Level.SEVERE, "Error adding properties from PropertySource: " + propertySource + ", ignoring PropertySource.", e);
}
}
+ return applyFilters(result);
+ }
+
+ private Map<String, String> applyFilters(Map<String, String> result) {
// Apply filters to values, prevent values filtered to null!
- for(PropertyFilter filter:
- ServiceContext.getInstance().getService(ConfigurationContext.class).get().getPropertyFilters()){
- result.replaceAll((k,v) -> filter.filterProperty(k, v,
- (String k2) -> k2.equals(k)?null:get(k2).orElse(null)));
+ for(int i=0; i<MAX_FILTER_LOOPS;i++) {
+ AtomicInteger changes = new AtomicInteger();
+ for (PropertyFilter filter : configurationContext.getPropertyFilters()) {
+ result.replaceAll((k, v) -> {
+ String newValue = filter.filterProperty(k, v,
+ (String k2) -> k2.equals(k) ? v : get(k2).orElse(null));
+ if (newValue != null && !newValue.equals(v)) {
+ changes.incrementAndGet();
+ LOG.finest(() -> "Filter - " + k + ": " + v + " -> " + newValue + " by " + filter);
+ } else if (v != null && !v.equals(newValue)) {
+ changes.incrementAndGet();
+ LOG.finest(() -> "Filter - " + k + ": " + v + " -> " + newValue + " by " + filter);
+ }
+ return newValue;
+ });
+ }
+ if(changes.get()==0){
+ LOG.finest(() -> "Finishing filter loop, no changes detected.");
+ break;
+ }
+ else{
+ if(i==(MAX_FILTER_LOOPS-1)) {
+ if(LOG.isLoggable(Level.WARNING)){
+ LOG.warning("Maximal filter loop count reached, aborting filter evaluation after cycles: " + i);
+ }
+ }
+ else{
+ LOG.finest(() -> "Repeating filter loop, changes detected: " + changes.get());
+ }
+ changes.set(0);
+ }
}
// Remove null values
- return result.entrySet().parallelStream().filter((e) -> e.getValue()!=null).collect(
+ return result.entrySet().parallelStream().filter((e) -> e.getValue() != null).collect(
Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()));
}
@@ -112,15 +183,14 @@ public class DefaultConfiguration implements Configuration {
* @param key the property's absolute, or relative path, e.g. @code
* a/b/c/d.myProperty}.
* @param type The target type required, not null.
- * @param <T> the value type
+ * @param <T> the value type
* @return the converted value, never null.
*/
@Override
public <T> Optional<T> get(String key, Class<T> type) {
Optional<String> value = get(key);
if (value.isPresent()) {
- List<PropertyConverter<T>> converters = ServiceContext.getInstance().getService(ConfigurationContext.class)
- .get().getPropertyConverters(type);
+ List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type);
for (PropertyConverter<T> converter : converters) {
try {
T t = converter.convert(value.get());
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9331a7d/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java b/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java
index 2e93558..5a6fc70 100644
--- a/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java
+++ b/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java
@@ -40,7 +40,7 @@ public class ConfigurationTest {
assertEquals("Robin", Configuration.current().get("name").get());
assertEquals("Sabine", Configuration.current().get("name2").get()); // from default
assertEquals("Mapped to name: Robin", Configuration.current().get("name3").get()); // oderridden default, mapped by filter to name property
- assertEquals("Sereina(filtered)", Configuration.current().get("name4").get()); // final only
+ assertEquals("Sereina(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)", Configuration.current().get("name4").get()); // final only
assertNull(Configuration.current().get("name5").orElse(null)); // final only, but removed from filter
System.out.println("name : " + Configuration.current().get("name").get());
[3/3] incubator-tamaya git commit: TAMAYA-42: - Added direct
reference from Configuration to the ConfigurationContext (merged). - Added
repeatable filter resolutions (currently by default max. 10 cycles are
performed).
Posted by an...@apache.org.
TAMAYA-42:
- Added direct reference from Configuration to the ConfigurationContext (merged).
- Added repeatable filter resolutions (currently by default max. 10 cycles are performed).
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/b9876572
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/b9876572
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/b9876572
Branch: refs/heads/master
Commit: b98765721948c57dfa01c4597ac512938ede2724
Parents: a830f38
Author: anatole <an...@apache.org>
Authored: Mon Jan 5 21:02:23 2015 +0100
Committer: anatole <an...@apache.org>
Committed: Mon Jan 5 21:02:23 2015 +0100
----------------------------------------------------------------------
.../core/internal/DefaultConfiguration.java | 22 +++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9876572/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java b/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
index 47828a3..d6e25ca 100644
--- a/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
+++ b/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
@@ -77,6 +77,12 @@ public class DefaultConfiguration implements Configuration {
return Optional.ofNullable(applyFilter(key, unfilteredValue));
}
+ /**
+ * Apply filters to a single property value.
+ * @param key the key, used for logging, not null.
+ * @param unfilteredValue the unfiltered property value.
+ * @return the filtered value, or null.
+ */
private String applyFilter(String key, String unfilteredValue) {
// Apply filters to values, prevent values filtered to null!
for(int i=0; i<MAX_FILTER_LOOPS;i++) {
@@ -116,6 +122,11 @@ public class DefaultConfiguration implements Configuration {
return unfilteredValue;
}
+ /**
+ * Get the current properties, composed by the loaded {@link org.apache.tamaya.spi.PropertySource} and filtered
+ * by registered {@link org.apache.tamaya.spi.PropertyFilter}.
+ * @return the final properties.
+ */
@Override
public Map<String, String> getProperties() {
List<PropertySource> propertySources = new ArrayList<>(configurationContext.getPropertySources());
@@ -136,12 +147,17 @@ public class DefaultConfiguration implements Configuration {
return applyFilters(result);
}
- private Map<String, String> applyFilters(Map<String, String> result) {
+ /**
+ * Filter a full configuration property map.
+ * @param inputMap the unfiltered map
+ * @return the filtered map.
+ */
+ private Map<String, String> applyFilters(Map<String, String> inputMap) {
// Apply filters to values, prevent values filtered to null!
for(int i=0; i<MAX_FILTER_LOOPS;i++) {
AtomicInteger changes = new AtomicInteger();
for (PropertyFilter filter : configurationContext.getPropertyFilters()) {
- result.replaceAll((k, v) -> {
+ inputMap.replaceAll((k, v) -> {
String newValue = filter.filterProperty(k, v,
(String k2) -> k2.equals(k) ? v : get(k2).orElse(null));
if (newValue != null && !newValue.equals(v)) {
@@ -171,7 +187,7 @@ public class DefaultConfiguration implements Configuration {
}
}
// Remove null values
- return result.entrySet().parallelStream().filter((e) -> e.getValue() != null).collect(
+ return inputMap.entrySet().parallelStream().filter((e) -> e.getValue() != null).collect(
Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()));
}
[2/3] incubator-tamaya git commit: Merge remote-tracking branch
'origin/master'
Posted by an...@apache.org.
Merge remote-tracking branch 'origin/master'
Conflicts:
core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/a830f38f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/a830f38f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/a830f38f
Branch: refs/heads/master
Commit: a830f38f722ec29e51efea8bcefe85449b519a56
Parents: b9331a7 853047a
Author: anatole <an...@apache.org>
Authored: Mon Jan 5 20:58:07 2015 +0100
Committer: anatole <an...@apache.org>
Committed: Mon Jan 5 20:58:07 2015 +0100
----------------------------------------------------------------------
.../internal/DefaultConfigurationContext.java | 100 ++++++++++---------
1 file changed, 54 insertions(+), 46 deletions(-)
----------------------------------------------------------------------