Repository: incubator-tamaya Updated Branches: refs/heads/configjsr c509a8f15 -> 1478e6130
Reduced filter API. Signed-off-by: Anatole Tresch <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/1478e613 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/1478e613 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/1478e613 Branch: refs/heads/configjsr Commit: 1478e61309bdb3853cf63386a3d3cf33d95b6b1f Parents: c509a8f Author: Anatole Tresch <[email protected]> Authored: Tue Jan 2 23:27:09 2018 +0100 Committer: Anatole Tresch <[email protected]> Committed: Tue Jan 2 23:27:09 2018 +0100 ---------------------------------------------------------------------- .../org/apache/tamaya/base/DefaultConfig.java | 10 +-- .../base/configsource/ConfigSourceManager.java | 12 +-- .../tamaya/base/filter/FilterContext.java | 26 +----- .../tamaya/base/filter/FilterManager.java | 35 +++----- .../tamaya/base/filter/RegexPropertyFilter.java | 84 ++++++++++++++++++++ .../spi/ConfigValueCombinationPolicy.java | 5 +- .../main/java/org/apache/tamaya/spi/Filter.java | 3 +- .../tamaya/base/DefaultConfigBuilderTest.java | 8 +- .../tamaya/filter/FilterComparatorTest.java | 4 +- .../apache/tamaya/core/testdata/TestFilter.java | 8 +- .../core/testdata/TestRemovingFilter.java | 10 +-- 11 files changed, 128 insertions(+), 77 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java ---------------------------------------------------------------------- diff --git a/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java b/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java index 7607199..665a605 100644 --- a/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java +++ b/code/base/src/main/java/org/apache/tamaya/base/DefaultConfig.java @@ -85,13 +85,13 @@ public class DefaultConfig implements Config, ConfigContextSupplier { Objects.requireNonNull(key, "Key must not be null."); Objects.requireNonNull(targetType, "Target type must not be null."); - ConfigValue value = configSourceManager.evaluteRawValue(key); - if(value==null || value.getValue()==null){ + String value = configSourceManager.evaluteRawValue(key); + if(value==null){ return null; } - value = filterManager.filterValue(value, this); + value = filterManager.filterValue(key, value, this); if(value!=null){ - return (T)converterManager.convertValue(value.getValue(), targetType); + return (T)converterManager.convertValue(value, targetType); } return null; } @@ -113,7 +113,7 @@ public class DefaultConfig implements Config, ConfigContextSupplier { */ @Override public Set<String> getPropertyNames() { - Map<String, ConfigValue> filtered = configSourceManager.evaluateRawValues(); + Map<String, String> filtered = configSourceManager.evaluateRawValues(); return filtered.keySet(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/base/src/main/java/org/apache/tamaya/base/configsource/ConfigSourceManager.java ---------------------------------------------------------------------- diff --git a/code/base/src/main/java/org/apache/tamaya/base/configsource/ConfigSourceManager.java b/code/base/src/main/java/org/apache/tamaya/base/configsource/ConfigSourceManager.java index 7a09bf5..eff7e55 100644 --- a/code/base/src/main/java/org/apache/tamaya/base/configsource/ConfigSourceManager.java +++ b/code/base/src/main/java/org/apache/tamaya/base/configsource/ConfigSourceManager.java @@ -306,9 +306,9 @@ public class ConfigSourceManager { * @param key the key, not null. * @return the value, before filtering is applied. */ - public ConfigValue evaluteRawValue(String key) { + public String evaluteRawValue(String key) { List<ConfigSource> configSources = getSources(); - ConfigValue unfilteredValue = null; + String unfilteredValue = null; ConfigValueCombinationPolicy combinationPolicy = getConfigValueCombinationPolicy(); for (ConfigSource propertySource : configSources) { unfilteredValue = combinationPolicy.collect(unfilteredValue, key, propertySource); @@ -316,17 +316,17 @@ public class ConfigSourceManager { return unfilteredValue; } - public Map<String, ConfigValue> evaluateRawValues() { + public Map<String, String> evaluateRawValues() { List<ConfigSource> configSources = getSources(); ConfigValueCombinationPolicy combinationPolicy = getConfigValueCombinationPolicy(); - Map<String, ConfigValue> result = new HashMap<>(); + Map<String, String> result = new HashMap<>(); for (ConfigSource propertySource : configSources) { for (Map.Entry<String,String> propEntry: propertySource.getProperties().entrySet()) { - ConfigValue unfilteredValue = result.get(propEntry.getKey()); + String unfilteredValue = result.get(propEntry.getKey()); unfilteredValue = combinationPolicy. collect(unfilteredValue, propEntry.getKey(), propertySource); if(unfilteredValue!=null){ - result.put(unfilteredValue.getKey(), unfilteredValue); + result.put(propEntry.getKey(), unfilteredValue); } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/base/src/main/java/org/apache/tamaya/base/filter/FilterContext.java ---------------------------------------------------------------------- diff --git a/code/base/src/main/java/org/apache/tamaya/base/filter/FilterContext.java b/code/base/src/main/java/org/apache/tamaya/base/filter/FilterContext.java index 6769445..cdbcba5 100644 --- a/code/base/src/main/java/org/apache/tamaya/base/filter/FilterContext.java +++ b/code/base/src/main/java/org/apache/tamaya/base/filter/FilterContext.java @@ -18,7 +18,6 @@ */ package org.apache.tamaya.base.filter; -import org.apache.tamaya.spi.ConfigValue; import org.apache.tamaya.spi.Filter; import javax.config.Config; @@ -26,14 +25,13 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; + /** * A filter context containing all the required values for implementing filtering. * * @see Filter */ public final class FilterContext { - /** The key. */ - private final ConfigValue property; private Map<String,String> configEntries = new HashMap<>(); @@ -56,18 +54,15 @@ public final class FilterContext { * Creates a new FilterContext, for filtering of a multi value access * using {@link Config#getPropertyNames()} . * - * @param value the value under evaluation, not {@code null}. * @param configEntries the raw configuration data available in the * current evaluation context, not {@code null}. * @param config the current config, not {@code null}. */ - public FilterContext(ConfigValue value, Map<String,String> configEntries, Config config) { - Objects.requireNonNull(value, "Value must not be null."); + public FilterContext(Map<String,String> configEntries, Config config) { Objects.requireNonNull(configEntries, "Initial configuration entries must be not null."); Objects.requireNonNull(config, "config must be not null."); this.singlePropertyScoped = false; - this.property = Objects.requireNonNull(value); this.configEntries.putAll(configEntries); this.config = config; } @@ -75,12 +70,10 @@ public final class FilterContext { /** * Creates a new FilterContext, for filtering of a single value access * using {@link Config#getPropertyNames()}. - * @param value the value under evaluation, not {@code null}. * @param config the current config, not {@code null}. */ - public FilterContext(ConfigValue value, Config config) { + public FilterContext(Config config) { this.config = config; - this.property = Objects.requireNonNull(value, "Value must not be null."); this.singlePropertyScoped = true; } @@ -93,17 +86,6 @@ public final class FilterContext { } /** - * Get the property value under evaluation. This information is very useful to evaluate additional metadata needed to determine/ - * control further aspects of the conversion. - * - * @return the key. This may be null in case where a default value has to be converted and no unique underlying - * key/value configuration is present. - */ - public ConfigValue getProperty() { - return property; - } - - /** * Method that determines if filtering is done for a single property accessed, or as part of call to * {@code getProperties()}. * @return true, if its scoped to a single property accessed. @@ -138,7 +120,7 @@ public final class FilterContext { @Override public String toString() { - return "FilterContext{property='" + property + "', configEntries=" + configEntries.keySet() + '}'; + return "FilterContext{configEntries=" + configEntries.keySet() + '}'; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/base/src/main/java/org/apache/tamaya/base/filter/FilterManager.java ---------------------------------------------------------------------- diff --git a/code/base/src/main/java/org/apache/tamaya/base/filter/FilterManager.java b/code/base/src/main/java/org/apache/tamaya/base/filter/FilterManager.java index 32c8f3f..361f47c 100644 --- a/code/base/src/main/java/org/apache/tamaya/base/filter/FilterManager.java +++ b/code/base/src/main/java/org/apache/tamaya/base/filter/FilterManager.java @@ -173,22 +173,12 @@ public class FilterManager { /** * Filters a single value. * @param value the raw value, not {@code null}. - * @return the filtered value, including {@code null}. - */ - public ConfigValue filterValue(ConfigValue value) { - FilterContext filterContext = new FilterContext(value, null); - return filterValue(filterContext); - } - - /** - * Filters a single value. - * @param value the raw value, not {@code null}. * @param config the config * @return the filtered value, including {@code null}. */ - public ConfigValue filterValue(ConfigValue value, Config config) { - FilterContext filterContext = new FilterContext(value, config); - return filterValue(filterContext); + public String filterValue(String key, String value, Config config) { + FilterContext filterContext = new FilterContext(config); + return filterValue(key, value, filterContext); } /** @@ -197,14 +187,14 @@ public class FilterManager { * @param config the config * @return the filtered value, inclusing null. */ - public Map<String, String> applyFilters(Map<String, String> rawProperties, Config config) { - Map<String, String> result = new HashMap<>(); + public String applyFilters(String value, Map<String, String> rawProperties, Config config) { + String result = value; // Apply filters to values, prevent values filtered to null! for (Map.Entry<String, String> entry : rawProperties.entrySet()) { - FilterContext filterContext = new FilterContext(ConfigValue.of(entry.getKey(), rawProperties), config); - ConfigValue filtered = filterValue(filterContext); - if(filtered!=null){ - result.putAll(filtered.asMap()); + FilterContext filterContext = new FilterContext(rawProperties, config); + String filtered = filterValue(entry.getKey(), result, filterContext); + if(result!=null){ + result = filtered; } } return result; @@ -215,13 +205,12 @@ public class FilterManager { * @param context the filter context, not {@code null}. * @return the filtered value. */ - private ConfigValue filterValue(FilterContext context) { - ConfigValue inputValue = context.getProperty(); - ConfigValue filteredValue = inputValue; + private String filterValue(String key, String inputValue, FilterContext context) { + String filteredValue = inputValue; for (int i = 0; i < MAX_FILTER_LOOPS; i++) { int changes = 0; for (Filter filter :filters) { - filteredValue = filter.filterProperty(inputValue); + filteredValue = filter.filterProperty(key, inputValue); if (filteredValue != null && !filteredValue.equals(inputValue)) { changes++; LOG.finest("Filter - " + inputValue + " -> " + filteredValue + " by " + filter); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/base/src/main/java/org/apache/tamaya/base/filter/RegexPropertyFilter.java ---------------------------------------------------------------------- diff --git a/code/base/src/main/java/org/apache/tamaya/base/filter/RegexPropertyFilter.java b/code/base/src/main/java/org/apache/tamaya/base/filter/RegexPropertyFilter.java new file mode 100644 index 0000000..3e46803 --- /dev/null +++ b/code/base/src/main/java/org/apache/tamaya/base/filter/RegexPropertyFilter.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.base.filter; + +import org.apache.tamaya.spi.ConfigValue; +import org.apache.tamaya.spi.Filter; + +import java.util.Arrays; +import java.util.List; + +/** + * Predicate filtering using a regex expression operating on the key. It allows either + * to define the target keys to be selected (includes), or to be excluded (excludes). + */ +public final class RegexPropertyFilter implements Filter{ + /** The expression used to include entries that match. */ + private List<String> includes; + /** The expression used to exclude entries that match. */ + private List<String> excludes; + + /** + * Sets the regex expression to be applied on the key to filter the corresponding entry + * if matching. + * @param expressions the regular expression for inclusion, not null. + */ + public void setIncludes(String... expressions){ + this.includes = Arrays.asList(expressions); + } + + /** + * Sets the regex expression to be applied on the key to remove the corresponding entries + * if matching. + * @param expressions the regular expressions for exclusion, not null. + */ + public void setExcludes(String... expressions){ + this.excludes= Arrays.asList(expressions); + } + + @Override + public String filterProperty(String key, String valueToBeFiltered) { + FilterContext context = FilterContext.getContext(); + if(includes!=null){ + for(String expression:includes){ + if(context!=null && key.matches(expression)){ + return valueToBeFiltered; + } + } + return null; + } + if(excludes!=null){ + for(String expression:excludes){ + if(context!=null && key.matches(expression)){ + return null; + } + } + } + return valueToBeFiltered; + } + + @Override + public String toString() { + return "RegexPropertyFilter{" + + "includes='" + includes + '\'' + + "excludes='" + excludes + '\'' + + '}'; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/base/src/main/java/org/apache/tamaya/spi/ConfigValueCombinationPolicy.java ---------------------------------------------------------------------- diff --git a/code/base/src/main/java/org/apache/tamaya/spi/ConfigValueCombinationPolicy.java b/code/base/src/main/java/org/apache/tamaya/spi/ConfigValueCombinationPolicy.java index b94dd9d..f380bf8 100644 --- a/code/base/src/main/java/org/apache/tamaya/spi/ConfigValueCombinationPolicy.java +++ b/code/base/src/main/java/org/apache/tamaya/spi/ConfigValueCombinationPolicy.java @@ -38,8 +38,7 @@ public interface ConfigValueCombinationPolicy { ConfigValueCombinationPolicy DEFAULT_OVERRIDING_POLICY = (currentValue, key, propertySource) -> { String value = propertySource.getValue(key); String meta = propertySource.getValue(key+"[meta]"); - return value!=null? ConfigValue.builder(key, value).setMetaEntry(meta) - .build():currentValue; + return value!=null? value:currentValue; }; /** @@ -67,6 +66,6 @@ public interface ConfigValueCombinationPolicy { * {@code currentValue} should be returned in almost all cases. * @return the value to be used for future evaluation, including metadata entries. */ - ConfigValue collect(ConfigValue currentValue, String key, ConfigSource propertySource); + String collect(String currentValue, String key, ConfigSource propertySource); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/base/src/main/java/org/apache/tamaya/spi/Filter.java ---------------------------------------------------------------------- diff --git a/code/base/src/main/java/org/apache/tamaya/spi/Filter.java b/code/base/src/main/java/org/apache/tamaya/spi/Filter.java index 9fdad86..2cafbdb 100644 --- a/code/base/src/main/java/org/apache/tamaya/spi/Filter.java +++ b/code/base/src/main/java/org/apache/tamaya/spi/Filter.java @@ -40,11 +40,12 @@ public interface Filter { * <li>reentrant</li> * <li>thread-safe</li> * </ul> + * @param key the key, not null. * @param value the value to be filtered, which also can be {@code null} if removed by another filter. * @return the filtered value, or {@code null} if the value should be removed alltogether. * @see ConfigValue * @see ConfigValueBuilder */ - ConfigValue filterProperty(ConfigValue value); + String filterProperty(String key, String value); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java ---------------------------------------------------------------------- diff --git a/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java b/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java index 84c1e70..006bd2d 100644 --- a/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java +++ b/code/base/src/test/java/org/apache/tamaya/base/DefaultConfigBuilderTest.java @@ -76,8 +76,8 @@ public class DefaultConfigBuilderTest { @Test public void addPropertyFilters_Array() throws Exception { - Filter filter1 = (value) -> value; - Filter filter2 = (value) -> value; + Filter filter1 = (key,value) -> value; + Filter filter2 = (key,value) -> value; DefaultConfigBuilder b = new DefaultConfigBuilder(); b.withFilters(filter1, filter2); ConfigContext ctx = b.getConfigContext(); @@ -92,8 +92,8 @@ public class DefaultConfigBuilderTest { @Test public void removePropertyFilters_Array() throws Exception { - Filter filter1 = (value) -> value; - Filter filter2 = (value) -> value; + Filter filter1 = (key,value) -> value; + Filter filter2 = (key,value) -> value; TamayaConfigBuilder b = new DefaultConfigBuilder() .withFilters(filter1, filter2); ConfigContext ctx = b.getConfigContext(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/base/src/test/java/org/apache/tamaya/filter/FilterComparatorTest.java ---------------------------------------------------------------------- diff --git a/code/base/src/test/java/org/apache/tamaya/filter/FilterComparatorTest.java b/code/base/src/test/java/org/apache/tamaya/filter/FilterComparatorTest.java index f43cfa1..56afd47 100644 --- a/code/base/src/test/java/org/apache/tamaya/filter/FilterComparatorTest.java +++ b/code/base/src/test/java/org/apache/tamaya/filter/FilterComparatorTest.java @@ -61,14 +61,14 @@ public class FilterComparatorTest { @Priority(1) private static class FilterA implements Filter { - public ConfigValue filterProperty(ConfigValue value) { + public String filterProperty(String key, String value) { throw new RuntimeException("Not implemented or look at me!"); } } @Priority(2) private static class FilterB implements Filter { - public ConfigValue filterProperty(ConfigValue value) { + public String filterProperty(String key, String value) { throw new RuntimeException("Not implemented or look at me!"); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/core/src/test/java/org/apache/tamaya/core/testdata/TestFilter.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestFilter.java b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestFilter.java index 9ffe868..e0f93bf 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestFilter.java +++ b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestFilter.java @@ -30,12 +30,10 @@ import javax.annotation.Priority; @Priority(100) public class TestFilter implements Filter { @Override - public ConfigValue filterProperty(ConfigValue valueToBeFiltered) { + public String filterProperty(String key, String valueToBeFiltered) { FilterContext context = FilterContext.getContext(); - if("name4".equals(valueToBeFiltered.getKey())){ - return valueToBeFiltered.toBuilder() - .setValue(valueToBeFiltered.getValue() + "(filtered)") - .build(); + if("name4".equals(key)){ + return valueToBeFiltered + "(filtered)"; } return valueToBeFiltered; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1478e613/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingFilter.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingFilter.java b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingFilter.java index 4bead15..795ec37 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingFilter.java +++ b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingFilter.java @@ -31,16 +31,14 @@ import javax.config.ConfigProvider; @Priority(200) public class TestRemovingFilter implements Filter { @Override - public ConfigValue filterProperty(ConfigValue valueToBeFiltered) { + public String filterProperty(String key, String valueToBeFiltered) { FilterContext context = FilterContext.getContext(); - if("name5".equals(valueToBeFiltered.getKey())){ + if("name5".equals(key)){ return null; } - else if("name3".equals(valueToBeFiltered.getKey())){ - return valueToBeFiltered.toBuilder().setValue( - "Mapped to name: " + ConfigProvider.getConfig().getValue("name", String.class)) - .build(); + else if("name3".equals(key)){ + return "Mapped to name: " + ConfigProvider.getConfig().getValue("name", String.class); } return valueToBeFiltered; }
