tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [3/6] incubator-tamaya git commit: Reorganized format module. Redesigned format interface.
Date Sun, 11 Jan 2015 00:43:50 GMT
Reorganized format module.
Redesigned format interface.


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

Branch: refs/heads/master
Commit: e8cc5666d583f795f6bb8190c97995fe90c60e55
Parents: ae66299
Author: anatole <anatole@apache.org>
Authored: Thu Jan 8 04:13:49 2015 +0100
Committer: anatole <anatole@apache.org>
Committed: Sun Jan 11 01:40:58 2015 +0100

----------------------------------------------------------------------
 modules/formats/pom.xml                         |   5 +
 ...hBasedMultiFormatPropertySourceProvider.java | 136 +++++++++++++++++++
 .../BaseSimpleFormatPropertySourceProvider.java | 103 ++++++++++++++
 .../tamaya/format/ConfigurationFormat.java      |  34 ++++-
 4 files changed, 275 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e8cc5666/modules/formats/pom.xml
----------------------------------------------------------------------
diff --git a/modules/formats/pom.xml b/modules/formats/pom.xml
index 5c98c76..65b40f4 100644
--- a/modules/formats/pom.xml
+++ b/modules/formats/pom.xml
@@ -36,5 +36,10 @@ under the License.
             <artifactId>tamaya-api</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.tamaya.ext</groupId>
+            <artifactId>tamaya-resources</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 </project>

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e8cc5666/modules/formats/src/main/java/org/apache/tamaya/format/BasePathBasedMultiFormatPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/BasePathBasedMultiFormatPropertySourceProvider.java
b/modules/formats/src/main/java/org/apache/tamaya/format/BasePathBasedMultiFormatPropertySourceProvider.java
new file mode 100644
index 0000000..992878d
--- /dev/null
+++ b/modules/formats/src/main/java/org/apache/tamaya/format/BasePathBasedMultiFormatPropertySourceProvider.java
@@ -0,0 +1,136 @@
+/*
+ * 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.format;
+
+import org.apache.tamaya.resource.ResourceResolver;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+import org.apache.tamaya.spi.ServiceContext;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Implementation of a {@link PropertySourceProvider} that reads configuration from some
given resource paths
+ * and using the given formats. The resource path are resolved using the current
+ * {@link org.apache.tamaya.resource.ResourceResolver} active. For each resource found the
configuration formats
+ * passed get a chance to read the resource, if they succeed the result is taken as the providers
PropertySources
+ * to be exposed.
+ */
+public abstract class BasePathBasedMultiFormatPropertySourceProvider implements PropertySourceProvider
{
+    /**
+     * The logger used.
+     */
+    private static final Logger LOG = Logger.getLogger(BasePathBasedMultiFormatPropertySourceProvider.class.getName());
+    /**
+     * The config formats supported for the given location/resource paths.
+     */
+    private List<ConfigurationFormat> configFormats = new ArrayList<>();
+    /**
+     * The paths to be evaluated.
+     */
+    private List<String> paths = new ArrayList<>();
+    /**
+     * The ClassLoader to use.
+     */
+    private Optional<ClassLoader> classLoader;
+
+    /**
+     * Creates a new instance.
+     *
+     * @param formats    the formats to be used, not null, not empty.
+     * @param paths      the paths to be resolved, not null, not empty.
+     */
+    public BasePathBasedMultiFormatPropertySourceProvider(
+            List<ConfigurationFormat> formats,
+            String... paths) {
+        this.configFormats.addAll(Objects.requireNonNull(formats));
+        this.paths.addAll(Arrays.asList(Objects.requireNonNull(paths)));
+    }
+
+    /**
+     * Creates a new instance.
+     *
+     * @param formats    the formats to be used, not null, not empty.
+     * @param paths      the paths to be resolved, not null, not empty.
+     */
+    public BasePathBasedMultiFormatPropertySourceProvider(
+            ClassLoader classLoader,
+            List<ConfigurationFormat> formats, String... paths) {
+        this.classLoader = Optional.ofNullable(classLoader);
+        this.configFormats.addAll(Objects.requireNonNull(formats));
+        this.paths.addAll(Arrays.asList(Objects.requireNonNull(paths)));
+    }
+
+    /**
+     * Method to create a {@link org.apache.tamaya.spi.PropertySource} based on the given
entries read.
+     * @param entryTypeName the entry type of the entries read, not null.
+     * @param entries the entries read by the {@link org.apache.tamaya.format.ConfigurationFormat}
+     * @param formatUsed the format instance used to read the entries.
+     * @return the {@link org.apache.tamaya.spi.PropertySource} instance ready to be registered.
+     * @see org.apache.tamaya.format.ConfigurationFormat#getEntryTypes()
+     */
+    protected abstract PropertySource getPropertySource(String entryTypeName, Map<String,String>
entries,
+                                     ConfigurationFormat formatUsed);
+
+    /**
+     * This method does dynamically resolve the paths using the current ClassLoader set.
If no ClassLoader was
+     * explcitly set during creation the current Thread context ClassLoader is used. If none
of the supported
+     * formats is able to parse a resource a WARNING log is written.
+     *
+     * @return the PropertySources successfully read
+     */
+    @Override
+    public Collection<PropertySource> getPropertySources() {
+        List<PropertySource> propertySources = new ArrayList<>();
+        paths.forEach((path) -> {
+            for (URL res : ServiceContext.getInstance().getService(ResourceResolver.class).get().getResources(
+                    this.classLoader.orElse(Thread.currentThread().getContextClassLoader()),
+                    path)) {
+                try {
+                    for (ConfigurationFormat format : configFormats) {
+                        Map<String, Map<String, String>> entries = format.readConfiguration(res);
+                        for(Map.Entry<String, Map<String, String>> en:entries.entrySet())
{
+                            PropertySource ps = getPropertySource(en.getKey(), en.getValue(),
format);
+                            if(ps!=null) {
+                                propertySources.add(ps);
+                            }
+                            else{
+                                LOG.info(() -> "Config Entries read ignored by PropertySourceFactory:
format="+format+
+                                        ", entryType="+en.getKey());
+                            }
+                        }
+                    }
+                } catch (Exception e) {
+                    LOG.log(Level.WARNING, "Failed to add resource based config: " + res,
e);
+                }
+            }
+        });
+        return propertySources;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e8cc5666/modules/formats/src/main/java/org/apache/tamaya/format/BaseSimpleFormatPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/BaseSimpleFormatPropertySourceProvider.java
b/modules/formats/src/main/java/org/apache/tamaya/format/BaseSimpleFormatPropertySourceProvider.java
new file mode 100644
index 0000000..1fe08c2
--- /dev/null
+++ b/modules/formats/src/main/java/org/apache/tamaya/format/BaseSimpleFormatPropertySourceProvider.java
@@ -0,0 +1,103 @@
+/*
+ * 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.format;
+
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Implementation of a {@link org.apache.tamaya.spi.PropertySourceProvider} that reads configuration
from
+ * a given resource and in a given format.
+ */
+public abstract class BaseSimpleFormatPropertySourceProvider implements PropertySourceProvider
{
+    /**
+     * The logger used.
+     */
+    private static final Logger LOG = Logger.getLogger(BaseSimpleFormatPropertySourceProvider.class.getName());
+    /**
+     * The config formats supported, not null.
+     */
+    private ConfigurationFormat configFormat;
+    /**
+     * The resource to be read, not null.
+     */
+    private URL resource;
+
+    /**
+     * Creates a new instance.
+     *
+     * @param format   the formas to be used, not null.
+     * @param resource the resource to be read, not null.
+     */
+    public BaseSimpleFormatPropertySourceProvider(
+            ConfigurationFormat format,
+            URL resource) {
+        this.configFormat = Objects.requireNonNull(format);
+        this.resource = Objects.requireNonNull(resource);
+    }
+
+    /**
+     * Method to create a {@link org.apache.tamaya.spi.PropertySource} based on the given
entries read.
+     *
+     * @param entryTypeName the entry type of the entries read, not null.
+     * @param entries       the entries read by the {@link ConfigurationFormat}
+     * @param formatUsed    the format instance used to read the entries.
+     * @return the {@link org.apache.tamaya.spi.PropertySource} instance ready to be registered.
+     * @see ConfigurationFormat#getEntryTypes()
+     */
+    protected abstract PropertySource getPropertySource(String entryTypeName, Map<String,
String> entries,
+                                                        ConfigurationFormat formatUsed);
+
+    /**
+     * This method does dynamically resolve the paths using the current ClassLoader set.
If no ClassLoader was
+     * explcitly set during creation the current Thread context ClassLoader is used. If none
of the supported
+     * formats is able to parse a resource a WARNING log is written.
+     *
+     * @return the PropertySources successfully read
+     */
+    @Override
+    public Collection<PropertySource> getPropertySources() {
+        List<PropertySource> propertySources = new ArrayList<>();
+        try {
+            Map<String, Map<String, String>> entries = configFormat.readConfiguration(resource);
+            for (Map.Entry<String, Map<String, String>> en : entries.entrySet())
{
+                PropertySource ps = getPropertySource(en.getKey(), en.getValue(), configFormat);
+                if (ps != null) {
+                    propertySources.add(ps);
+                } else {
+                    LOG.info(() -> "Config Entries read ignored by PropertySourceFactory:
format=" + configFormat +
+                            ", entryType=" + en.getKey());
+                }
+            }
+        } catch (Exception e) {
+            LOG.log(Level.WARNING, "Failed to add resource based config: " + resource, e);
+        }
+        return propertySources;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/e8cc5666/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java
----------------------------------------------------------------------
diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java
b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java
index 924fa7c..970d0c5 100644
--- a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java
+++ b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java
@@ -23,6 +23,8 @@ import org.apache.tamaya.spi.PropertySource;
 import java.io.IOException;
 import java.net.URL;
 import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * Implementations current this class encapsulate the mechanism how to read a
@@ -32,10 +34,35 @@ import java.util.Collection;
  * of different priorities. In this cases, each ordinal type found must be returned as a
separate
  * {@link PropertySource} instance.
  */
-@FunctionalInterface
 public interface ConfigurationFormat {
 
     /**
+     * The default entry type returned if a format implementation does not support any explicit
entry types.
+     */
+    public static final String DEFAULT_ENTRY_TYPE = "default";
+
+    /**
+     * Access the different entry types a format supports. Entries of the same entry type
hereby share the same
+     * configuration priority. The reason for this concept is that a configuration format
can produce different
+     * types of properties, e.g. default properties, named properties, overriding ones as
illustrated below:
+     * <pre>
+     *     [defaults]
+     *     a.b.c=alphabet
+     *     foo.bar=any
+     *
+     *     [staged:development]
+     *     a.b.myEntry=1234
+     *
+     *     [management-overrides]
+     *     a.b.d=Alphabet
+     * </pre>
+     * If just using ordinary property files, of course, only one entry type is returned,
called 'default'.
+     * #see DEFAULT_ENTRY_TYPE
+     * @return the set of supported entry types, never null and never empty.
+     */
+    public Set<String> getEntryTypes();
+
+    /**
      * Reads a list {@link org.apache.tamaya.spi.PropertySource} instances from a resource,
using this format.
      * If the configuration format only contains entries of one ordinal type, normally only
one single
      * instance of PropertySource is returned. Nevertheless custom formats may contain different
sections or parts,
@@ -45,9 +72,10 @@ public interface ConfigurationFormat {
      *
      * @param url the url to read the configuration data from (could be a file, a remote
location, a classpath
      *            resource or something else.
-     * @return the corresponding {@link org.apache.tamaya.spi.PropertySource} instances,
never {@code null}.
+     * @return the corresponding {@link java.util.Map} instances of properties read, never
{@code null}. Each
+     * {@link java.util.Map} instance hereby is provided using a type key.
      */
-    Collection<PropertySource> readConfiguration(URL url)
+    Map<String, Map<String,String>> readConfiguration(URL url)
             throws IOException;
 
 }


Mime
View raw message