Repository: incubator-tamaya Updated Branches: refs/heads/master b59c1ae26 -> 8ad767b48
TAMAYA-252: Unified PropertyValue builder API. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/8ad767b4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/8ad767b4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/8ad767b4 Branch: refs/heads/master Commit: 8ad767b484f2e9bb6247b14ca53c8395b773ebe0 Parents: b2dddeb Author: anatole <anat...@apache.org> Authored: Mon Mar 6 00:28:58 2017 +0100 Committer: anatole <anat...@apache.org> Committed: Mon Mar 6 00:29:34 2017 +0100 ---------------------------------------------------------------------- .../org/apache/tamaya/spi/FilterContext.java | 47 ++++--- .../apache/tamaya/spi/FilterContextTest.java | 71 +++++++++-- .../core/internal/DefaultConfiguration.java | 2 +- .../tamaya/core/internal/PropertyFiltering.java | 123 ++++++++----------- .../core/internal/PropertySourceComparator.java | 4 +- .../core/testdata/TestPropertyFilter.java | 2 +- .../testdata/TestRemovingPropertyFilter.java | 4 +- 7 files changed, 152 insertions(+), 101 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8ad767b4/code/api/src/main/java/org/apache/tamaya/spi/FilterContext.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/spi/FilterContext.java b/code/api/src/main/java/org/apache/tamaya/spi/FilterContext.java index d5c7850..e3f4465 100644 --- a/code/api/src/main/java/org/apache/tamaya/spi/FilterContext.java +++ b/code/api/src/main/java/org/apache/tamaya/spi/FilterContext.java @@ -18,6 +18,8 @@ */ package org.apache.tamaya.spi; +import org.apache.tamaya.Configuration; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -30,7 +32,9 @@ import java.util.Objects; */ public class FilterContext { /** The key. */ - private final String key; + private final PropertyValue value; + /** tHE CURRENT CONTEXT. */ + private final ConfigurationContext context; @Experimental private Map<String, PropertyValue> configEntries = new HashMap(); @Experimental @@ -38,35 +42,50 @@ public class FilterContext { /** - * Creates a new FilterContext. - * @param key the key under evaluation, not null. + * Creates a new FilterContext, for filtering of a multi value access + * using {@link Configuration#getProperties()}. + * @param value the value under evaluation, not null. * @param configEntries the raw configuration data available in the current evaluation context, not null. + * @param context the current context, not null. */ - public FilterContext(String key, Map<String,PropertyValue> configEntries) { + public FilterContext(PropertyValue value, Map<String,PropertyValue> configEntries, ConfigurationContext context) { this.singlePropertyScoped = false; - this.key = Objects.requireNonNull(key); + this.value = Objects.requireNonNull(value); + this.context = Objects.requireNonNull(context); this.configEntries.putAll(configEntries); this.configEntries = Collections.unmodifiableMap(this.configEntries); } - public FilterContext(String key, PropertyValue value) { + /** + * Creates a new FilterContext, for filtering of a single value access + * using {@link Configuration#getProperties()}. + * @param value the value under evaluation, not null. + * @param context the current context, not null. + */ + public FilterContext(PropertyValue value, ConfigurationContext context) { this.singlePropertyScoped = true; - this.key = Objects.requireNonNull(key); - if(value!=null) { - this.configEntries.put(value.getKey(), value); - } + this.context = Objects.requireNonNull(context); + this.value = Objects.requireNonNull(value); this.configEntries = Collections.unmodifiableMap(this.configEntries); } /** - * Get the key accessed. This information is very useful to evaluate additional metadata needed to determine/ + * Get the current context. + * @return the current context, not null. + */ + public ConfigurationContext getContext(){ + return context; + } + + /** + * 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 String getKey() { - return key; + public PropertyValue getProperty() { + return value; } /** @@ -106,7 +125,7 @@ public class FilterContext { @Override public String toString() { - return "FilterContext{key='" + key + "', configEntries=" + configEntries.keySet() + '}'; + return "FilterContext{value='" + value + "', configEntries=" + configEntries.keySet() + '}'; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8ad767b4/code/api/src/test/java/org/apache/tamaya/spi/FilterContextTest.java ---------------------------------------------------------------------- diff --git a/code/api/src/test/java/org/apache/tamaya/spi/FilterContextTest.java b/code/api/src/test/java/org/apache/tamaya/spi/FilterContextTest.java index d8d9380..3d0e1f0 100644 --- a/code/api/src/test/java/org/apache/tamaya/spi/FilterContextTest.java +++ b/code/api/src/test/java/org/apache/tamaya/spi/FilterContextTest.java @@ -18,9 +18,11 @@ */ package org.apache.tamaya.spi; +import org.apache.tamaya.TypeLiteral; import org.junit.Test; import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.junit.Assert.*; @@ -31,17 +33,18 @@ import static org.junit.Assert.*; public class FilterContextTest { @Test public void getKey() throws Exception { - FilterContext ctx = new FilterContext("getKey", - new HashMap<String,PropertyValue>()); - assertEquals("getKey", ctx.getKey()); + PropertyValue val = PropertyValue.of("getKey", "v", ""); + FilterContext ctx = new FilterContext(val, + new HashMap<String,PropertyValue>(), new TestConfigContext()); + assertEquals(val, ctx.getProperty()); } @Test public void isSinglePropertyScoped() throws Exception { - FilterContext ctx = new FilterContext("isSinglePropertyScoped", - new HashMap<String,PropertyValue>()); + PropertyValue val = PropertyValue.of("isSinglePropertyScoped", "v", ""); + FilterContext ctx = new FilterContext(val, new HashMap<String,PropertyValue>(), new TestConfigContext()); assertEquals(false, ctx.isSinglePropertyScoped()); - ctx = new FilterContext("isSinglePropertyScoped", PropertyValue.of("isSinglePropertyScoped", "val", "test")); + ctx = new FilterContext(val, new TestConfigContext()); assertEquals(true, ctx.isSinglePropertyScoped()); } @@ -51,7 +54,8 @@ public class FilterContextTest { for(int i=0;i<10;i++) { config.put("key-"+i, PropertyValue.of("key-"+i, "value-"+i, "test")); } - FilterContext ctx = new FilterContext("getMetaEntries", config); + PropertyValue val = PropertyValue.of("getConfigEntries", "v", ""); + FilterContext ctx = new FilterContext(val, config, new TestConfigContext()); assertEquals(config, ctx.getConfigEntries()); assertTrue(config != ctx.getConfigEntries()); } @@ -62,13 +66,62 @@ public class FilterContextTest { for(int i=0;i<2;i++) { config.put("key-"+i, PropertyValue.of("key-"+i, "value-"+i, "test")); } - FilterContext ctx = new FilterContext("testToString", config); + PropertyValue val = PropertyValue.of("testToString", "val", "mySource"); + FilterContext ctx = new FilterContext(val, config, new TestConfigContext()); String toString = ctx.toString(); assertNotNull(toString); - assertTrue(toString.contains("FilterContext{key='testToString', configEntries=[")); + assertTrue(toString.contains("FilterContext{value='PropertyValue{key='testToString', value='val', source='val'}', configEntries=[")); assertTrue(toString.contains("key-0")); assertTrue(toString.contains("key-1")); assertTrue(toString.endsWith("}")); } + private static class TestConfigContext implements ConfigurationContext{ + + @Override + public void addPropertySources(PropertySource... propertySources) { + + } + + @Override + public List<PropertySource> getPropertySources() { + return null; + } + + @Override + public PropertySource getPropertySource(String name) { + return null; + } + + @Override + public <T> void addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T> propertyConverter) { + + } + + @Override + public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { + return null; + } + + @Override + public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> type) { + return null; + } + + @Override + public List<PropertyFilter> getPropertyFilters() { + return null; + } + + @Override + public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy() { + return null; + } + + @Override + public ConfigurationContextBuilder toBuilder() { + return null; + } + } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8ad767b4/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java index 8ad7489..0b25b00 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java @@ -82,7 +82,7 @@ public class DefaultConfiguration implements Configuration { if(value==null || value.getValue()==null){ return null; } - value = PropertyFiltering.applyFilter(key, value, configurationContext); + value = PropertyFiltering.applyFilter(value, configurationContext); if(value!=null){ return value.getValue(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8ad767b4/code/core/src/main/java/org/apache/tamaya/core/internal/PropertyFiltering.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/PropertyFiltering.java b/code/core/src/main/java/org/apache/tamaya/core/internal/PropertyFiltering.java index 932d38c..79cca09 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/PropertyFiltering.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/PropertyFiltering.java @@ -24,10 +24,8 @@ import org.apache.tamaya.spi.PropertyFilter; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; -import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -52,95 +50,76 @@ public final class PropertyFiltering{ */ private PropertyFiltering(){} - public static PropertyValue applyFilter(String key, PropertyValue value, ConfigurationContext configurationContext) { + /** + * Filters a single value. + * @param value the raw value, not null. + * @param context the context + * @return the filtered value, inclusing null. + */ + public static PropertyValue applyFilter(PropertyValue value, ConfigurationContext context) { + FilterContext filterContext = new FilterContext(value, context); + return filterValue(filterContext); + } + + /** + * Filters all properties. + * @param rawProperties the unfiltered properties, not null. + * @param context the context + * @return the filtered value, inclusing null. + */ + public static Map<String, PropertyValue> applyFilters(Map<String, PropertyValue> rawProperties, ConfigurationContext context) { + Map<String, PropertyValue> result = new HashMap<>(); // Apply filters to values, prevent values filtered to null! - for (int i = 0; i < MAX_FILTER_LOOPS; i++) { - boolean changed = false; - // Apply filters to values, prevent values filtered to null! - FilterContext filterContext = new FilterContext(key, value); - for (PropertyFilter filter : configurationContext.getPropertyFilters()) { - PropertyValue newValue = filter.filterProperty(value, filterContext); - if (newValue != null && !newValue.equals(value)) { - changed = true; - if (LOG.isLoggable(Level.FINEST)) { - LOG.finest("Filter - " + value + " -> " + newValue + " by " + filter); - } - } else if (value != null && !value.equals(newValue)) { - changed = true; - if (LOG.isLoggable(Level.FINEST)) { - LOG.finest("Filter - " + value + " -> " + newValue + " by " + filter); - } - } - value = 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."); - } + for (Map.Entry<String, PropertyValue> entry : rawProperties.entrySet()) { + FilterContext filterContext = new FilterContext(entry.getValue(), rawProperties, context); + PropertyValue filtered = filterValue(filterContext); + if(filtered!=null){ + result.put(filtered.getKey(), filtered); } } - return value; + return result; } - public static Map<String, PropertyValue> applyFilters(Map<String, PropertyValue> inputMap, ConfigurationContext configurationContext) { - Map<String, PropertyValue> resultMap = new HashMap<>(inputMap); - // Apply filters to values, prevent values filtered to null! + /** + * Basic filter logic. + * @param context the filter context, not null. + * @return the filtered value. + */ + private static PropertyValue filterValue(FilterContext context) { + PropertyValue inputValue = context.getProperty(); + PropertyValue filteredValue = inputValue; + for (int i = 0; i < MAX_FILTER_LOOPS; i++) { - AtomicInteger changes = new AtomicInteger(); - for (Map.Entry<String, PropertyValue> entry : inputMap.entrySet()) { - FilterContext filterContext = new FilterContext(entry.getKey(), inputMap); - for (PropertyFilter filter : configurationContext.getPropertyFilters()) { - final String k = entry.getKey(); - final PropertyValue v = entry.getValue(); - PropertyValue newValue = filter.filterProperty(v, filterContext); - 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); - } - // Remove null values - if (null != newValue) { - resultMap.put(k, newValue); - } - else{ - resultMap.remove(k); - } + int changes = 0; + for (PropertyFilter filter : context.getContext().getPropertyFilters()) { + filteredValue = filter.filterProperty(inputValue, context); + if (filteredValue != null && !filteredValue.equals(inputValue)) { + changes++; + LOG.finest("Filter - " + inputValue + " -> " + filteredValue + " by " + filter); + } + if(filteredValue==null){ + LOG.finest("Filter removed entry - " + inputValue + ": " + filter); + break; + }else{ + inputValue = filteredValue; } } - if (changes.get() == 0) { + if (changes == 0) { LOG.finest("Finishing filter loop, no changes detected."); break; + } else if (filteredValue == null) { + 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()); + LOG.finest("Repeating filter loop, changes detected: " + changes); } - changes.set(0); - } - } - return resultMap; - } - - private static Map<String, String> filterMetadata(Map<String, String> inputMap) { - Map<String,String> result = new HashMap<>(); - for(Map.Entry<String,String> en:inputMap.entrySet()){ - if(en.getKey().startsWith("_")){ - result.put(en.getKey(), en.getValue()); } } - return Collections.unmodifiableMap(result); + return filteredValue; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8ad767b4/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java b/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java index 7e9c503..35e147f 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/PropertySourceComparator.java @@ -88,10 +88,10 @@ public class PropertySourceComparator implements Comparator<PropertySource>, Ser // PropertyValue ordinalValue = propertySource.get(PropertySource.TAMAYA_ORDINAL); // if(ordinalValue!=null){ // try{ -// return Integer.parseInt(ordinalValue.getValue().trim()); +// return Integer.parseInt(ordinalValue.getProperty().trim()); // }catch(Exception e){ // LOG.finest("Failed to parse ordinal from " + PropertySource.TAMAYA_ORDINAL + -// " in " + propertySource.getName()+": "+ordinalValue.getValue()); +// " in " + propertySource.getName()+": "+ordinalValue.getProperty()); // } // } // try { http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8ad767b4/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java index f0ae4d3..96f80a6 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java +++ b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilter.java @@ -31,7 +31,7 @@ import javax.annotation.Priority; public class TestPropertyFilter implements PropertyFilter{ @Override public PropertyValue filterProperty(PropertyValue valueToBeFiltered, FilterContext context) { - if("name4".equals(context.getKey())){ + if("name4".equals(context.getProperty().getKey())){ return valueToBeFiltered.toBuilder() .setValue(valueToBeFiltered.getValue() + "(filtered)") .build(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8ad767b4/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java index e984962..488ea0b 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java +++ b/code/core/src/test/java/org/apache/tamaya/core/testdata/TestRemovingPropertyFilter.java @@ -32,10 +32,10 @@ import javax.annotation.Priority; public class TestRemovingPropertyFilter implements PropertyFilter{ @Override public PropertyValue filterProperty(PropertyValue valueToBeFiltered, FilterContext context) { - if("name5".equals(context.getKey())){ + if("name5".equals(context.getProperty().getKey())){ return null; } - else if("name3".equals(context.getKey())){ + else if("name3".equals(context.getProperty().getKey())){ return valueToBeFiltered.toBuilder().setValue( "Mapped to name: " + ConfigurationProvider.getConfiguration().get("name")) .build();