Repository: incubator-tamaya Updated Branches: refs/heads/master ced3463f9 -> 574332226
TAMAYA-42,43,44: Added Javadoc. Added getProperties() method to Configuration. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/57433222 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/57433222 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/57433222 Branch: refs/heads/master Commit: 574332226b9a53c188c95de43668a114476a22de Parents: ced3463 Author: anatole <anat...@apache.org> Authored: Sat Jan 3 17:12:19 2015 +0100 Committer: anatole <anat...@apache.org> Committed: Sat Jan 3 17:12:19 2015 +0100 ---------------------------------------------------------------------- .../java/org/apache/tamaya/Configuration.java | 7 +++ .../core/internal/DefaultConfiguration.java | 57 +++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/57433222/api/src/main/java/org/apache/tamaya/Configuration.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/tamaya/Configuration.java b/api/src/main/java/org/apache/tamaya/Configuration.java index e9d47a5..2786af7 100644 --- a/api/src/main/java/org/apache/tamaya/Configuration.java +++ b/api/src/main/java/org/apache/tamaya/Configuration.java @@ -52,6 +52,13 @@ public interface Configuration { */ Optional<String> get(String key); + /** + * Access all current known Configuration properties as a full {@code Map<String,String>}. + * Be aware that entries from non scannable parts of the registered {@link org.apache.tamaya.spi.PropertySource} + * instances may not be contained in the result, but nevertheless be accessible calling one of the + * {@code get(...)} methods. + */ + Map<String,String> getProperties(); /** * Get the property keys as type T. This will implicitly require a corresponding {@link http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/57433222/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 e6c0c44..0a39b7d 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 @@ -25,14 +25,34 @@ import org.apache.tamaya.spi.PropertyConverter; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.ServiceContext; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; /** - * Implementation of the Configuration API. + * Implementation of the Configuration API. This class uses the current {@link ConfigurationContext} to evaluate the + * chain of {@link org.apache.tamaya.spi.PropertySource} and {@link org.apache.tamaya.spi.PropertyFilter} + * instance to evaluate the current Configuration. */ public class DefaultConfiguration implements Configuration { + private static final Logger LOG = Logger.getLogger(DefaultConfiguration.class.getName()); + + /** + * This method evaluates the given configuration key. Hereby if goes down the chain or PropertySource instances + * provided by the current {@link org.apache.tamaya.spi.ConfigurationContext}. The first non-null-value returned + * is taken as an intermediate value. Finally the value is filtered through the + * {@link org.apache.tamaya.spi.PropertyFilter} instances installed, before it is returned as the final result of + * this method. + * + * @param key the property's key, not null. + * @return the optional configuration value, never null. + */ @Override public Optional<String> get(String key) { List<PropertySource> propertySources = ServiceContext.getInstance().getService(ConfigurationContext.class).get().getPropertySources(); @@ -46,6 +66,38 @@ public class DefaultConfiguration implements Configuration { } @Override + public Map<String, String> getProperties() { + List<PropertySource> propertySources = new ArrayList<>( + ServiceContext.getInstance().getService(ConfigurationContext.class).get().getPropertySources()); + Collections.reverse(propertySources); + Map<String, String> result = new HashMap<>(); + for (PropertySource propertySource : propertySources) { + try { + int origSize = result.size(); + Map<String, String> otherMap = propertySource.getProperties(); + LOG.log(Level.FINEST, null, () -> "Overriding with properties from " + propertySource.getName()); + result.putAll(otherMap); + 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); + } + } + return result; + } + + /** + * Accesses the current String value for the given key (see {@link #get(String)}) and tries to convert it + * using the {@link org.apache.tamaya.spi.PropertyConverter} instances provided by the current + * {@link org.apache.tamaya.spi.ConfigurationContext}. + * + * @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 + * @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()) { @@ -57,7 +109,8 @@ public class DefaultConfiguration implements Configuration { return Optional.of(t); } } catch (Exception e) { - // TODO LOG + LOG.log(Level.FINEST, e, () -> "PropertyConverter: " + converter + + " failed to convert value: " + value.get()); } } throw new ConfigException("Unparseable config value for type: " + type.getName() + ": " + key);