tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [2/8] incubator-tamaya git commit: TAMAY-68: Added ConfigurationContextBuilder and all related stuff needed.
Date Fri, 27 Feb 2015 02:40:47 GMT
TAMAY-68: Added ConfigurationContextBuilder and all related stuff needed.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/e21acb28
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/e21acb28
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/e21acb28

Branch: refs/heads/master
Commit: e21acb28bd87c5bf4f5a3c27f2c4502beb2c2055
Parents: 3a5d22c
Author: anatole <anatole@apache.org>
Authored: Fri Feb 27 02:17:54 2015 +0100
Committer: anatole <anatole@apache.org>
Committed: Fri Feb 27 03:40:33 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/tamaya/Configuration.java   |  31 ++-
 .../apache/tamaya/ConfigurationProvider.java    |  43 +++-
 .../apache/tamaya/spi/ConfigurationContext.java |  33 ++--
 .../tamaya/spi/ConfigurationContextBuilder.java | 194 +++++++++++++++++++
 .../tamaya/spi/ConfigurationProviderSpi.java    |  32 +++
 .../org/apache/tamaya/spi/PropertySource.java   |   4 +-
 6 files changed, 314 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e21acb28/java8/api/src/main/java/org/apache/tamaya/Configuration.java
----------------------------------------------------------------------
diff --git a/java8/api/src/main/java/org/apache/tamaya/Configuration.java b/java8/api/src/main/java/org/apache/tamaya/Configuration.java
index da476ea..afaced9 100644
--- a/java8/api/src/main/java/org/apache/tamaya/Configuration.java
+++ b/java8/api/src/main/java/org/apache/tamaya/Configuration.java
@@ -18,12 +18,15 @@
  */
 package org.apache.tamaya;
 
+import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.OptionalDouble;
 import java.util.OptionalInt;
 import java.util.OptionalLong;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 
 /**
@@ -79,10 +82,30 @@ public interface 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.
-     * @return the property value, never null..
-     * @throws ConfigException if the keys could not be converted to the required target
type.
+     * @return the property value, or null, if the underlying value is null as well.
+     * @throws ConfigException if the value could not be converted to the required target
type by any of the
+     *                         registered converters.
      */
-    <T> T get(String key, TypeLiteral<T> type);
+    default <T> T get(String key, TypeLiteral<T> type) {
+        List<PropertyConverter<T>> converters = ConfigurationProvider.getConfigurationContext().getPropertyConverters(
+                type);
+        String value = get(key);
+        if (value == null) {
+            return null;
+        }
+        for (PropertyConverter<T> converter : converters) {
+            try {
+                T item = converter.convert(value);
+                if (item != null) {
+                    return item;
+                }
+            } catch (Exception e) {
+                Logger.getLogger(getClass().getName()).log(Level.WARNING, e, () -> "PropertyConverter
was throwing " +
+                        "exception instead of returning null: " + converter.getClass().getName());
+            }
+        }
+        throw new ConfigException("Unconvertible value: " + value + ", target: " + type);
+    }
 
     /**
      * Access a property.
@@ -143,7 +166,7 @@ public interface Configuration {
      */
     default <T> T get(String key, PropertyConverter<T> converter) {
         String value = get(key);
-        if (value!=null) {
+        if (value != null) {
             return Objects.requireNonNull(converter).convert(value);
         }
         return null;

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e21acb28/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
----------------------------------------------------------------------
diff --git a/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java b/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
index 183786c..856841e 100644
--- a/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
+++ b/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
@@ -17,6 +17,7 @@
 package org.apache.tamaya;
 
 import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.spi.ConfigurationContextBuilder;
 import org.apache.tamaya.spi.ConfigurationProviderSpi;
 import org.apache.tamaya.spi.ServiceContextManager;
 
@@ -46,8 +47,48 @@ public final class ConfigurationProvider {
      *
      * @return the current ConfigurationContext, never null.
      */
-    public static ConfigurationContext getConfigurationContext(){
+    public static ConfigurationContext getConfigurationContext() {
         return PROVIDER_SPI.getConfigurationContext();
     }
 
+    /**
+     * This method allows to replace the current {@link org.apache.tamaya.spi.ConfigurationContext}
with a new
+     * instance. This can be used to update the context with a new one, e.g. because some
of the configuration
+     * data has changed and should be updated. It is the responsibility of the ConfigurationProvider
to trigger
+     * corresponding update events for the current {@link org.apache.tamaya.Configuration},
so observing
+     * listeners can do whatever is appropriate to react to any given configuration changes.
+     *
+     * @param context the new ConfigurationContext to be applied.
+     * @throws java.lang.UnsupportedOperationException if the current provider is read-only
and does not support
+     *                                                 applying a new ConfigurationContext.
+     */
+    public static void setConfigurationContext(ConfigurationContext context) {
+        PROVIDER_SPI.setConfigurationContext(context);
+    }
+
+    /**
+     * Method that allows to determine if a new {@link org.apache.tamaya.spi.ConfigurationContext}
can be applied
+     * programmatically.
+     * @see #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)
+     * @return true, if {@link #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)}
is supported
+     * by the current implementation.
+     */
+    public static boolean isConfigurationContextSettable() {
+        return PROVIDER_SPI.isConfigurationContextSettable();
+    }
+
+    /**
+     * Create a new {@link org.apache.tamaya.spi.ConfigurationContextBuilder} instance. This
method creates
+     * a new builder instance that is not related to any concrete {@link org.apache.tamaya.spi.ConfigurationContext}.
+     * You can use {@link #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)}
to change the
+     * current configuration context.
+     *
+     * @return a new, empty {@link org.apache.tamaya.spi.ConfigurationContextBuilder}, never
null.
+     * @see #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)
+     * @see org.apache.tamaya.spi.ConfigurationContext
+     */
+    public static ConfigurationContextBuilder getConfigurationContextBuilder() {
+        return PROVIDER_SPI.getConfigurationContextBuilder();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e21acb28/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java
----------------------------------------------------------------------
diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java
index b7cd843..2a344f1 100644
--- a/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java
+++ b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java
@@ -18,12 +18,14 @@
  */
 package org.apache.tamaya.spi;
 
-
 import org.apache.tamaya.PropertyConverter;
 import org.apache.tamaya.TypeLiteral;
 
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
 
 /**
  * Central SPI for programmatically dealing with the setup of the configuration system.
@@ -33,14 +35,6 @@ import java.util.Map;
 public interface ConfigurationContext {
 
     /**
-     * This method can be used for programmatically adding {@link org.apache.tamaya.spi.PropertySource}s.
-     * It is not needed for normal 'usage' by end users, but only for Extension Developers!
-     *
-     * @param propertySourcesToAdd the PropertySources to add
-     */
-    void addPropertySources(PropertySource... propertySourcesToAdd);
-
-    /**
      * This method returns the current list of registered PropertySources ordered via their
ordinal.
      * PropertySources with a lower ordinal come last. The PropertySource with the
      * highest ordinal comes first.
@@ -55,20 +49,21 @@ public interface ConfigurationContext {
     List<PropertySource> getPropertySources();
 
     /**
-     * This method can be used for programmatically adding {@link org.apache.tamaya.PropertyConverter}s.
-     * It is not needed for normal 'usage' by end users, but only for Extension Developers!
+     * This method returns a subset ot the currently registered PropertySources.
      *
-     * @param typeToConvert     the type which the converter is for
-     * @param propertyConverter the PropertyConverters to add for this type
+     * @param selector the selector query, not null.
+     * @return a collectino of selected PropertySources.
      */
-    <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T>
propertyConverter);
+    default Collection<PropertySource> getPropertySources(Predicate<PropertySource>
selector){
+        return getPropertySources().stream().filter(selector).collect(Collectors.toList());
+    }
 
     /**
      * <p>
      * This method returns the Map of registered PropertyConverters
      * per type.
      * The List for each type is ordered via their {@link javax.annotation.Priority} and
-     * class name. Refer also to {@link #getPropertyConverters()}.
+     * class name.
      * </p>
      * <p>
      * A simplified scenario could be like:
@@ -147,4 +142,12 @@ public interface ConfigurationContext {
      * @return the {@link org.apache.tamaya.spi.PropertyValueCombinationPolicy} used, never
null.
      */
     PropertyValueCombinationPolicy getPropertyValueCombinationPolicy();
+
+    /**
+     * Creates a new updates instance for changing the current ConfigurationContext.
+     * @throws java.lang.UnsupportedOperationException if this ConfigurationContext is not
updateable.
+     * @return a new ConfigurationContextUpdates instance.
+     */
+    ConfigurationContextBuilder toBuilder();
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e21acb28/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java
----------------------------------------------------------------------
diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java
b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java
new file mode 100644
index 0000000..d0ac2cf
--- /dev/null
+++ b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java
@@ -0,0 +1,194 @@
+/*
+ * 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.spi;
+
+import org.apache.tamaya.PropertyConverter;
+import org.apache.tamaya.TypeLiteral;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.function.Predicate;
+
+/**
+ * A builder for creating new or adapting instances of {@link org.apache.tamaya.spi.ConfigurationContext}.
+ * Builders can be obtained in exactly two ways:
+ * <ol>
+ *     <li>By accessing a preinitialized builder from an existing {@link org.apache.tamaya.spi.ConfigurationContext},
+ *     by calling {@link ConfigurationContext#toBuilder()}.</li>
+ *     <li>By accessing an empty builder instance from
+ *     {@link org.apache.tamaya.ConfigurationProvider#getConfigurationContextBuilder()}.</li>
+ * </ol>
+ * After all changes are applied to a builder a new {@link org.apache.tamaya.spi.ConfigurationContext}
instance can
+ * be created and can be applied by calling
+ * {@link org.apache.tamaya.ConfigurationProvider#setConfigurationContext(ConfigurationContext)}.
Since this method can
+ * throw an UnsupportedOperationException, you should check before if changing the current
ConfigurationContext
+ * programmatically is supported by calling
+ * {@link org.apache.tamaya.ConfigurationProvider#isConfigurationContextSettable()}.
+ */
+public interface ConfigurationContextBuilder {
+
+    /**
+     * Init this builder instance with the given {@link org.apache.tamaya.spi.ConfigurationContext}
instance. This
+     * method will replace any existing data in the current builder with the data contained
in the given
+     * {@link org.apache.tamaya.spi.ConfigurationContext}.
+     *
+     * @param context the {@link org.apache.tamaya.spi.ConfigurationContext} instance to
be used, not null.
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder setContext(ConfigurationContext context);
+
+    /**
+     * This method can be used for programmatically adding {@link org.apache.tamaya.spi.PropertySource}s.
+     * It is not needed for normal 'usage' by end users, but only for Extension Developers!
+     *
+     * @param propertySourcesToAdd the PropertySources to add
+     * @return this builder, for chaining, never null.
+     */
+    default ConfigurationContextBuilder addPropertySources(PropertySource... propertySourcesToAdd)
{
+        return addPropertySources(Arrays.asList(propertySourcesToAdd));
+    }
+
+
+    /**
+     * This method can be used for programmatically adding {@link org.apache.tamaya.spi.PropertySource}s.
+     * It is not needed for normal 'usage' by end users, but only for Extension Developers!
+     *
+     * @param propertySourcesToAdd the PropertySources to add
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder addPropertySources(Collection<PropertySource> propertySourcesToAdd);
+
+    /**
+     * This method can be used for programmatically adding {@link org.apache.tamaya.spi.PropertySource}s.
+     * It is not needed for normal 'usage' by end users, but only for Extension Developers!
+     *
+     * @param propertySourcesNames the PropertySource names of the sources to remove
+     * @return this builder, for chaining, never null.
+     */
+    default ConfigurationContextBuilder removePropertySources(String... propertySourcesNames)
{
+        return removePropertySources(Arrays.asList(propertySourcesNames));
+    }
+
+    /**
+     * This method can be used for programmatically adding {@link org.apache.tamaya.spi.PropertySource}s.
+     * It is not needed for normal 'usage' by end users, but only for Extension Developers!
+     *
+     * @param propertySourcesNames the PropertySource names of the sources to remove
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder removePropertySources(Collection<String> propertySourcesNames);
+
+    /**
+     * Adds the given PropertyFilter instances.
+     *
+     * @param filters the filters to add
+     * @return this builder, for chaining, never null.
+     */
+    default ConfigurationContextBuilder addPropertyFilters(PropertyFilter... filters) {
+        return addPropertyFilters(Arrays.asList(filters));
+    }
+
+    /**
+     * Adds the given PropertyFilter instances.
+     *
+     * @param filters the filters to add
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder addPropertyFilters(Collection<PropertyFilter> filters);
+
+    /**
+     * Removes the given PropertyFilter instances.
+     *
+     * @param filters the filters to remove
+     * @return this builder, for chaining, never null.
+     */
+    default ConfigurationContextBuilder removePropertyFilters(PropertyFilter... filters)
{
+        return removePropertyFilters(Arrays.asList(filters));
+    }
+
+    /**
+     * Removes the given PropertyFilter instances.
+     *
+     * @param selector the selector query, not null.
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder removePropertyFilters(Predicate<PropertyFilter> selector);
+
+    /**
+     * Removes the given PropertyFilter instances.
+     *
+     * @param filters the filters to remove
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder removePropertyFilters(Collection<PropertyFilter> filters);
+
+    /**
+     * Remove the property sources selected by the given selector predicate.
+     *
+     * @param selector the selector query, not null.
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder removePropertySources(Predicate<PropertySource> selector);
+
+    /**
+     * This method can be used for programmatically adding {@link org.apache.tamaya.PropertyConverter}s.
+     * It is not needed for normal 'usage' by end users, but only for Extension Developers!
+     *
+     * @param typeToConvert     the type which the converter is for
+     * @param propertyConverter the PropertyConverters to add for this type
+     * @return this builder, for chaining, never null.
+     */
+    <T> ConfigurationContextBuilder addPropertyConverter(TypeLiteral<T> typeToConvert,
+                                                         PropertyConverter<T> propertyConverter);
+
+    /**
+     * Removes the given PropertyConverter instances.
+     *
+     * @param typeToConvert the type which the converter is for
+     * @param converters    the converters to remove
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert,
+                                                         PropertyConverter<?>... converters);
+
+    /**
+     * Removes the given PropertyConverter instances.
+     *
+     * @param typeToConvert the type which the converter is for
+     * @param converters    the converters to remove
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert,
+                                                         Collection<PropertyConverter<?>>
converters);
+
+    /**
+     * Sets the {@link org.apache.tamaya.spi.PropertyValueCombinationPolicy} used to evaluate
the final
+     * property values.
+     *
+     * @param policy the {@link org.apache.tamaya.spi.PropertyValueCombinationPolicy} used,
not null
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy
policy);
+
+    /**
+     * Builds a {@link org.apache.tamaya.spi.ConfigurationContext} based on the data set.
+     */
+    ConfigurationContext build();
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e21acb28/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java
----------------------------------------------------------------------
diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java
index 577163b..b2cc381 100644
--- a/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java
+++ b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java
@@ -42,4 +42,36 @@ public interface ConfigurationProviderSpi {
      */
     ConfigurationContext getConfigurationContext();
 
+    /**
+     * This method allows to replace the current {@link org.apache.tamaya.spi.ConfigurationContext}
with a new
+     * instance. This can be used to update the context with a new one, e.g. because some
of the configuration
+     * data has changed and must be updated. It is the responsibility of the ConfigurationProvider
to trigger
+     * corresponding update event for the current {@link org.apache.tamaya.spi.ConfigurationContext}
or
+     * {@link org.apache.tamaya.Configuration}.
+     *
+     * @param context the new ConfigurationContext to be applied.
+     * @throws java.lang.UnsupportedOperationException if the current provider is read-only.
+     */
+    default void setConfigurationContext(ConfigurationContext context){
+        throw new UnsupportedOperationException("By default a ConfigurationContext cannot
not be switched later.");
+    }
+
+    /**
+     * Method that allows to determine if a new {@link org.apache.tamaya.spi.ConfigurationContext}
can be applied
+     * programmatically.
+     * @see #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)
+     * @return true, if {@link #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)}
is supported
+     * by the current implementation.
+     */
+    default boolean isConfigurationContextSettable(){
+        return false;
+    }
+
+    /**
+     * Creates a new {@link org.apache.tamaya.spi.ConfigurationContextBuilder} instance.
+     *
+     * @return a new {@link org.apache.tamaya.spi.ConfigurationContextBuilder}, never null.
+     */
+    ConfigurationContextBuilder getConfigurationContextBuilder();
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e21acb28/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java
----------------------------------------------------------------------
diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java b/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java
index b8622a9..20e8b88 100644
--- a/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java
+++ b/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java
@@ -90,9 +90,7 @@ public interface PropertySource {
      *
      * @return the property source's name, never null.
      */
-    default String getName() {
-        return getClass().getName();
-    }
+    String getName();
 
     /**
      * Access a property.


Mime
View raw message