You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2015/03/18 00:49:16 UTC

[4/6] incubator-tamaya git commit: Added Resource singleton. Added test ps.

Added Resource singleton.
Added test ps.


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

Branch: refs/heads/master
Commit: ded8115d83e7b51647c235ec17ce80f355443646
Parents: bc03d8c
Author: anatole <an...@apache.org>
Authored: Mon Mar 16 01:40:42 2015 +0100
Committer: anatole <an...@apache.org>
Committed: Mon Mar 16 01:40:42 2015 +0100

----------------------------------------------------------------------
 .../tamaya/resource/ResourceResolver.java       | 11 ----
 .../org/apache/tamaya/resource/Resources.java   | 44 ++++++++++++++
 .../PathBasedPropertySourceProvider.java        | 64 ++++++++++++++++++++
 3 files changed, 108 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ded8115d/modules/resources/src/main/java/org/apache/tamaya/resource/ResourceResolver.java
----------------------------------------------------------------------
diff --git a/modules/resources/src/main/java/org/apache/tamaya/resource/ResourceResolver.java b/modules/resources/src/main/java/org/apache/tamaya/resource/ResourceResolver.java
index a5c491c..c150c4d 100644
--- a/modules/resources/src/main/java/org/apache/tamaya/resource/ResourceResolver.java
+++ b/modules/resources/src/main/java/org/apache/tamaya/resource/ResourceResolver.java
@@ -107,15 +107,4 @@ public interface ResourceResolver {
      */
     Collection<URL> getResources(ClassLoader classLoader, Collection<String> expressions);
 
-    /**
-     * Access the current ResourceResolver.
-     * @return the current ResourceResolver instance, never null.
-     * @throws ConfigException, if no ResourceResolver is available (should not happen).
-     */
-    public static ResourceResolver getInstance(){
-        return ServiceContext.getInstance().getService(ResourceResolver.class).orElseThrow(
-                () -> new ConfigException("ResourceResolver not available.")
-        );
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ded8115d/modules/resources/src/main/java/org/apache/tamaya/resource/Resources.java
----------------------------------------------------------------------
diff --git a/modules/resources/src/main/java/org/apache/tamaya/resource/Resources.java b/modules/resources/src/main/java/org/apache/tamaya/resource/Resources.java
new file mode 100644
index 0000000..b62a832
--- /dev/null
+++ b/modules/resources/src/main/java/org/apache/tamaya/resource/Resources.java
@@ -0,0 +1,44 @@
+/*
+ * 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.resource;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.spi.ServiceContext;
+
+/**
+ * Singleton Accessor for accessing the current {@link org.apache.tamaya.resource.ResourceResolver} instance.
+ */
+public final class Resources {
+
+    /**
+     * Singleton constructor.
+     */
+    private Resources(){}
+
+    /**
+     * Access the current ResourceResolver.
+     * @return the current ResourceResolver instance, never null.
+     * @throws org.apache.tamaya.ConfigException, if no ResourceResolver is available (should not happen).
+     */
+    public static ResourceResolver getResourceResolver(){
+        return ServiceContext.getInstance().getService(ResourceResolver.class).orElseThrow(
+                () -> new ConfigException("ResourceResolver not available.")
+        );
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ded8115d/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java b/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java
new file mode 100644
index 0000000..423a115
--- /dev/null
+++ b/modules/resources/src/test/java/org/apache/tamaya/resource/internal/PathBasedPropertySourceProvider.java
@@ -0,0 +1,64 @@
+package org.apache.tamaya.resource.internal;
+
+import org.apache.tamaya.resource.Resources;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertySourceProvider;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Created by Anatole on 03.03.2015.
+ */
+public class PathBasedPropertySourceProvider implements PropertySourceProvider{
+
+    @Override
+    public Collection<PropertySource> getPropertySources() {
+        List<PropertySource> propertySources = new ArrayList<>();
+        Collection<URL> resources = Resources.getResourceResolver().getResources("META-INF/cfg/**/*.properties");
+        for(URL url:resources){
+            Properties props = new Properties();
+            try(InputStream is = url.openStream()){
+                props.load(is);
+                propertySources.add(new PropertiesBasedPropertySource(url.toString(), props));
+            }
+            catch(Exception e){
+                e.printStackTrace();
+            }
+        }
+        return propertySources;
+    }
+
+
+    private final static class PropertiesBasedPropertySource implements PropertySource{
+
+        private String name;
+        private Map<String,String> properties = new HashMap<>();
+
+        public PropertiesBasedPropertySource(String name, Properties props) {
+            this.name = name;
+            props.forEach((k,v) -> this.properties.put(k.toString(), v.toString()));
+        }
+
+        @Override
+        public String getName() {
+            return name;
+        }
+
+        @Override
+        public String get(String key) {
+            return properties.get(key);
+        }
+
+        @Override
+        public Map<String, String> getProperties() {
+            return properties;
+        }
+    }
+}