TAMAYA-136: Adding PropertyValue for PropertySource SPI. All changes and test fixes in all modules/examples relevant for release.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/3cb7f8de Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/3cb7f8de Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/3cb7f8de Branch: refs/heads/old_master Commit: 3cb7f8de5bd875ae38dd5dedd3bf591bbce2de19 Parents: 89223dc Author: anatole <anat...@apache.org> Authored: Tue Feb 2 17:15:53 2016 +0100 Committer: Oliver B. Fischer <ple...@apache.org> Committed: Fri Sep 30 21:29:37 2016 +0200 ---------------------------------------------------------------------- pom.xml | 77 +++++++++++++ .../tamaya/filter/ConfigurationFilter.java | 112 +++++++++++++++++++ .../tamaya/filter/DefaultMetadataFilter.java | 42 +++++++ .../tamaya/filter/ProgrammableFilter.java | 111 ++++++++++++++++++ .../tamaya/filter/RegexPropertyFilter.java | 56 ++++++++++ .../org.apache.tamaya.spi.PropertyFilter | 20 ++++ 6 files changed, 418 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/3cb7f8de/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2480950 --- /dev/null +++ b/pom.xml @@ -0,0 +1,77 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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 current 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-extensions</artifactId> + <version>0.2-incubating-SNAPSHOT</version> + </parent> + + <artifactId>tamaya-filter</artifactId> + <name>Apache Tamaya Modules - Adaptive Configuration Filtering</name> + <packaging>bundle</packaging> + + <properties> + <jdkVersion>1.7</jdkVersion> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-api</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-core</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Export-Package> + org.apache.tamaya.filter, + </Export-Package> + <!--<Private-Package>--> + <!--org.apache.tamaya.events.internal--> + <!--</Private-Package>--> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/3cb7f8de/src/main/java/org/apache/tamaya/filter/ConfigurationFilter.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/filter/ConfigurationFilter.java b/src/main/java/org/apache/tamaya/filter/ConfigurationFilter.java new file mode 100644 index 0000000..99ab27f --- /dev/null +++ b/src/main/java/org/apache/tamaya/filter/ConfigurationFilter.java @@ -0,0 +1,112 @@ +/* + * 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.filter; + +import org.apache.tamaya.spi.FilterContext; +import org.apache.tamaya.spi.PropertyFilter; + + +/** + * Hereby + * <ul> + * <li><b>Single</b> filters are applied only when values are explicitly accessed. This is useful, e.g. for + * filtering passwords into clear text variants. Nevertheless metadata keys hidden on map level must be + * accessible (=not filtered) when accessed as single values.</li> + * <li><b>Map</b> filters are applied when values are filtered as part of a full properties access. + * Often filtering in these cases is more commonly applied, e.g. you dont want to show up all kind of metadata. + * </li> + * For both variants individual filter rules can be applied here. All filters configured are managed on a + * thread-local level, so this class is typically used to temporarely filter out some values. Do not forget to + * restore its state, when not using a thread anymore (especially important in multi-threaded environments), not + * doing so will create nasty side effects of configuration not being visisble depending on the thread + * active. + * </ul> + */ +public final class ConfigurationFilter implements PropertyFilter{ + + static final ThreadLocal<Boolean> THREADED_METADATA_FILTERED = new ThreadLocal<Boolean>(){ + @Override + protected Boolean initialValue() { + return Boolean.TRUE; + } + }; + + private static final ThreadLocal<ProgrammableFilter> THREADED_MAP_FILTERS = new ThreadLocal<ProgrammableFilter>(){ + @Override + protected ProgrammableFilter initialValue() { + return new ProgrammableFilter(); + } + }; + + private static final ThreadLocal<ProgrammableFilter> THREADED_SINGLE_FILTERS = new ThreadLocal<ProgrammableFilter>(){ + @Override + protected ProgrammableFilter initialValue() { + return new ProgrammableFilter(); + } + }; + + /** + * Seactivates metadata filtering also on global map access for this thread. + * @see #clearFilters() + * @param active true,to enable metadata filtering (default). + */ + public static void setMetadataFilter(boolean active){ + THREADED_METADATA_FILTERED.set(active); + } + + /** + * Access the filtering configuration that is used for filtering single property values accessed. + * @return the filtering config, never null. + */ + public static ProgrammableFilter getSingleFilters(){ + return THREADED_SINGLE_FILTERS.get(); + } + + /** + * Access the filtering configuration that is used for filtering configuration properties accessed as full + * map. + * @return the filtering config, never null. + */ + public static ProgrammableFilter getMapFilters(){ + return THREADED_MAP_FILTERS.get(); + } + + /** + * Removes all programmable filters active on the current thread. + */ + public static void clearFilters(){ + THREADED_MAP_FILTERS.get().clearFilters(); + THREADED_SINGLE_FILTERS.get().clearFilters(); + THREADED_METADATA_FILTERED.set(true); + } + + @Override + public String filterProperty(String valueToBeFiltered, FilterContext context) { + if(!context.isSinglePropertyScoped()){ + for(PropertyFilter pred: THREADED_MAP_FILTERS.get().getFilters()){ + valueToBeFiltered = pred.filterProperty(valueToBeFiltered, context); + } + }else{ + for(PropertyFilter pred: THREADED_SINGLE_FILTERS.get().getFilters()){ + valueToBeFiltered = pred.filterProperty(valueToBeFiltered, context); + } + } + return valueToBeFiltered; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/3cb7f8de/src/main/java/org/apache/tamaya/filter/DefaultMetadataFilter.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/filter/DefaultMetadataFilter.java b/src/main/java/org/apache/tamaya/filter/DefaultMetadataFilter.java new file mode 100644 index 0000000..389d9fa --- /dev/null +++ b/src/main/java/org/apache/tamaya/filter/DefaultMetadataFilter.java @@ -0,0 +1,42 @@ +/* + * 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.filter; + +import org.apache.tamaya.spi.FilterContext; +import org.apache.tamaya.spi.PropertyFilter; + +/** + * Default property filter that hides metadta entries starting with an '_', similar ti {@code etcd}. + */ +public final class DefaultMetadataFilter implements PropertyFilter{ + @Override + public String filterProperty(String valueToBeFiltered, FilterContext context) { + if(context.isSinglePropertyScoped()){ + // When accessing keys explicitly, do not hide anything. + return valueToBeFiltered; + } + if(ConfigurationFilter.THREADED_METADATA_FILTERED.get()) { + if (context.getKey().startsWith("_")) { + // Hide metadata entries. + return null; + } + } + return valueToBeFiltered; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/3cb7f8de/src/main/java/org/apache/tamaya/filter/ProgrammableFilter.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/filter/ProgrammableFilter.java b/src/main/java/org/apache/tamaya/filter/ProgrammableFilter.java new file mode 100644 index 0000000..8589faa --- /dev/null +++ b/src/main/java/org/apache/tamaya/filter/ProgrammableFilter.java @@ -0,0 +1,111 @@ +/* + * 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.filter; + +import org.apache.tamaya.spi.FilterContext; +import org.apache.tamaya.spi.PropertyFilter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * A set of property filter and accessor methods. + */ +public final class ProgrammableFilter implements PropertyFilter{ + /** The filters. */ + private List<PropertyFilter> filters = new ArrayList<>(); + + /** + * Add a filter. + * @param filter the filter. + */ + public void addFilter(PropertyFilter filter){ + filters.add(filter); + } + + /** + * Adds a filter at given position. + * @param pos the position. + * @param filter the filter. + */ + public void addFilter(int pos, PropertyFilter filter){ + filters.add(pos, filter); + } + + /** + * Removes a filter at a given position. + * @param pos the position. + * @return the filter removed, or null. + */ + public PropertyFilter removeFilter(int pos){ + return filters.remove(pos); + } + + /** + * Clears all filters. + */ + public void clearFilters(){ + filters.clear(); + } + + /** + * Set the filters. + * @param filters the filters to be applied. + */ + public void setFilters(PropertyFilter... filters){ + setFilters(Arrays.asList(filters)); + } + + /** + * Set the filters. + * @param filters the filters to be applied. + */ + public void setFilters(Collection<PropertyFilter> filters) { + filters.clear(); + filters.addAll(filters); + } + + /** + * Get all filters. + * @return all filters. + */ + public List<PropertyFilter> getFilters(){ + return Collections.unmodifiableList(filters); + } + + @Override + public String filterProperty(String valueToBeFiltered, FilterContext context) { + for(PropertyFilter filter:filters){ + valueToBeFiltered = filter.filterProperty(valueToBeFiltered, context); + } + return valueToBeFiltered; + } + + @Override + public String toString() { + return "FilterConfig{" + + "filters=" + filters + + '}'; + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/3cb7f8de/src/main/java/org/apache/tamaya/filter/RegexPropertyFilter.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tamaya/filter/RegexPropertyFilter.java b/src/main/java/org/apache/tamaya/filter/RegexPropertyFilter.java new file mode 100644 index 0000000..616f2cf --- /dev/null +++ b/src/main/java/org/apache/tamaya/filter/RegexPropertyFilter.java @@ -0,0 +1,56 @@ +/* + * 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.filter; + +import org.apache.tamaya.spi.FilterContext; +import org.apache.tamaya.spi.PropertyFilter; + +import java.util.Objects; + +/** + * Predicate filtering using a regex expression operating on the key. + */ +public final class RegexPropertyFilter implements PropertyFilter{ + /** The expression used to filter. */ + private String expression; + + /** + * Creates a new regex filter expression. + * @param expression the reged expression, not null. + */ + public RegexPropertyFilter(String expression){ + this.expression = Objects.requireNonNull(expression); + } + + @Override + public String filterProperty(String valueToBeFiltered, FilterContext context) { + if(context.getKey().matches(expression)){ + return null; + } + return valueToBeFiltered; + } + + @Override + public String toString() { + return "RegexPredicate{" + + "expression='" + expression + '\'' + + '}'; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/3cb7f8de/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter ---------------------------------------------------------------------- diff --git a/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter b/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter new file mode 100644 index 0000000..630c222 --- /dev/null +++ b/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyFilter @@ -0,0 +1,20 @@ +# +# 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 current 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. +# +org.apache.tamaya.filter.ConfigurationFilter +org.apache.tamaya.filter.DefaultMetadataFilter \ No newline at end of file