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/05/09 01:13:25 UTC

[2/6] incubator-tamaya git commit: Added remote sandbox module.

Added remote sandbox module.


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

Branch: refs/heads/master
Commit: f961007036f46e9e371ff68bcc3c5f8ad4ea9f9b
Parents: 96c2da7
Author: anatole <an...@apache.org>
Authored: Sat May 9 00:14:50 2015 +0200
Committer: anatole <an...@apache.org>
Committed: Sat May 9 00:14:50 2015 +0200

----------------------------------------------------------------------
 sandbox/pom.xml                                 |  1 +
 sandbox/remote/pom.xml                          | 55 ++++++++++++
 .../tamaya/remote/BaseConfigServerServlet.java  | 51 +++++++++++
 .../tamaya/remote/BaseRemotePropertySource.java | 92 ++++++++++++++++++++
 4 files changed, 199 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/f9610070/sandbox/pom.xml
----------------------------------------------------------------------
diff --git a/sandbox/pom.xml b/sandbox/pom.xml
index 8efd9e4..0513f07 100644
--- a/sandbox/pom.xml
+++ b/sandbox/pom.xml
@@ -42,6 +42,7 @@ under the License.
         <module>functions</module>
         <module>sysprops</module>
         <module>environment</module>
+        <module>remote</module>
     </modules>
 
 </project>

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/f9610070/sandbox/remote/pom.xml
----------------------------------------------------------------------
diff --git a/sandbox/remote/pom.xml b/sandbox/remote/pom.xml
new file mode 100644
index 0000000..f4b7100
--- /dev/null
+++ b/sandbox/remote/pom.xml
@@ -0,0 +1,55 @@
+<!-- 
+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-sandbox</artifactId>
+        <version>0.1-incubating-SNAPSHOT</version>
+        <relativePath>..</relativePath>
+    </parent>
+
+    <artifactId>tamaya-remote</artifactId>
+    <name>Apache Tamaya Remote PropertySource</name>
+    <packaging>jar</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.tamaya</groupId>
+            <artifactId>tamaya-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+            <version>3.1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>tamaya-json</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+    </dependencies>
+</project>

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/f9610070/sandbox/remote/src/main/java/org/apache/tamaya/remote/BaseConfigServerServlet.java
----------------------------------------------------------------------
diff --git a/sandbox/remote/src/main/java/org/apache/tamaya/remote/BaseConfigServerServlet.java b/sandbox/remote/src/main/java/org/apache/tamaya/remote/BaseConfigServerServlet.java
new file mode 100644
index 0000000..e931b4e
--- /dev/null
+++ b/sandbox/remote/src/main/java/org/apache/tamaya/remote/BaseConfigServerServlet.java
@@ -0,0 +1,51 @@
+/*
+ * 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.remote;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Simple servlet base class that delivers a configuration with the given parameter key/value keys.
+ */
+public abstract class BaseConfigServerServlet extends HttpServlet {
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        Map<String,String> params = new HashMap<>();
+        req.getParameterMap().forEach((k,v) -> params.put(k, v[0]));
+        String config = getFormattedConfiguration(params);
+        if(config!=null){
+            // write config to response
+            resp.getWriter().print(config);
+        }
+        else{
+            resp.sendError(404, "No such config: " + params.toString());
+        }
+
+    }
+
+    protected abstract String getFormattedConfiguration(Map<String, String> params);
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/f9610070/sandbox/remote/src/main/java/org/apache/tamaya/remote/BaseRemotePropertySource.java
----------------------------------------------------------------------
diff --git a/sandbox/remote/src/main/java/org/apache/tamaya/remote/BaseRemotePropertySource.java b/sandbox/remote/src/main/java/org/apache/tamaya/remote/BaseRemotePropertySource.java
new file mode 100644
index 0000000..7cd4572
--- /dev/null
+++ b/sandbox/remote/src/main/java/org/apache/tamaya/remote/BaseRemotePropertySource.java
@@ -0,0 +1,92 @@
+/*
+ * 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.remote;
+
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.format.ConfigurationFormat;
+import org.apache.tamaya.json.JSONFormat;
+import org.apache.tamaya.spi.PropertySource;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Abstract base class for implementing a PropertySource that reads configuration data from a remote resource. It uses
+ * by default the JSON format as defined by the JSON module.
+ */
+public abstract class BaseRemotePropertySource implements PropertySource{
+
+    private static final ConfigurationFormat DEFAULT_FORMAT = new JSONFormat();
+
+    private Map<String,String> properties = new HashMap<>();
+
+    protected BaseRemotePropertySource(){
+        reload();
+    }
+
+    @Override
+    public String getName() {
+        return getClass().getSimpleName();
+    }
+
+    @Override
+    public Map<String, String> getProperties() {
+        return properties;
+    }
+
+    /**
+     * Reloads the remote configuration. If reloads fails to whatever reasons the already loaded configuration will
+     * stay untouched.
+     */
+    public void reload(){
+        Map newProperties;
+        ConfigurationFormat format = getConfigurationFormat();
+        for(URL url:getAccessURLs()) {
+            try(InputStream is = url.openStream()) {
+                ConfigurationData data = format.readConfiguration(url.toExternalForm(), is);
+                if(data!=null){
+                    newProperties = data.getDefaultSection();
+                    if(newProperties!=null){
+                        this.properties = newProperties;
+                        Logger.getLogger(getClass().getName()).info("Reloaded remote config from: " + url);
+                    }
+                }
+            }
+            catch(Exception e){
+                Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to load config from url: " + url, e);
+            }
+        }
+    }
+
+    protected abstract Collection<URL> getAccessURLs();
+
+    protected ConfigurationFormat getConfigurationFormat(){
+        return DEFAULT_FORMAT;
+    }
+
+    protected Map<String,String> mapConfigurationData(ConfigurationData data){
+        return data.getDefaultSection();
+    }
+
+}