tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [6/9] incubator-tamaya-sandbox git commit: TAMAYA-239: Improved programmatic API for consul/etcd.
Date Sun, 26 Feb 2017 23:05:27 GMT
TAMAYA-239: Improved programmatic API for consul/etcd.


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

Branch: refs/heads/master
Commit: 63b77be45ae2cfd0ace9f784f41903dcad51f32b
Parents: 7251f41
Author: anatole <anatole@apache.org>
Authored: Thu Feb 23 01:11:34 2017 +0100
Committer: anatole <anatole@apache.org>
Committed: Mon Feb 27 00:05:00 2017 +0100

----------------------------------------------------------------------
 .../tamaya/consul/ConsulBackendConfig.java      | 74 ++++++++++++++++++
 .../apache/tamaya/consul/ConsulBackends.java    | 59 --------------
 .../tamaya/consul/ConsulPropertySource.java     | 58 ++++++++++++--
 .../apache/tamaya/etcd/EtcdBackendConfig.java   | 80 +++++++++++++++++++
 .../org/apache/tamaya/etcd/EtcdBackends.java    | 65 ----------------
 .../apache/tamaya/etcd/EtcdPropertySource.java  | 82 ++++++++++++--------
 6 files changed, 254 insertions(+), 164 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/63b77be4/consul/src/main/java/org/apache/tamaya/consul/ConsulBackendConfig.java
----------------------------------------------------------------------
diff --git a/consul/src/main/java/org/apache/tamaya/consul/ConsulBackendConfig.java b/consul/src/main/java/org/apache/tamaya/consul/ConsulBackendConfig.java
new file mode 100644
index 0000000..958a843
--- /dev/null
+++ b/consul/src/main/java/org/apache/tamaya/consul/ConsulBackendConfig.java
@@ -0,0 +1,74 @@
+/*
+ * 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.consul;
+
+import com.google.common.net.HostAndPort;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Singleton that reads and stores the current consul setup, especially the possible host:ports
to be used.
+ */
+public final class ConsulBackendConfig {
+
+    private static final Logger LOG = Logger.getLogger(ConsulBackendConfig.class.getName());
+    private static List<HostAndPort> consulBackends = new ArrayList<>();
+
+    static{
+        String serverURLs = System.getProperty("tamaya.consul.urls");
+        if(serverURLs==null){
+            serverURLs = System.getenv("tamaya.consul.urls");
+        }
+        if(serverURLs==null){
+            serverURLs = "127.0.0.1:8300";
+        }
+        for(String url:serverURLs.split("\\,")) {
+            try{
+                consulBackends.add(HostAndPort.fromString(url.trim()));
+                LOG.info("Using consul endoint: " + url);
+            } catch(Exception e){
+                LOG.log(Level.SEVERE, "Error initializing consul accessor for URL: " + url,
e);
+            }
+        }
+    }
+
+    private ConsulBackendConfig(){}
+
+    private static boolean isConsulDisabled() {
+        String value = System.getProperty("tamaya.consul.disable");
+        if(value==null){
+            value = System.getenv("tamaya.consul.disable");
+        }
+        if(value==null){
+            return false;
+        }
+        return value.isEmpty() || Boolean.parseBoolean(value);
+    }
+
+    public static List<HostAndPort> getConsulBackends(){
+        if(isConsulDisabled()){
+            return Collections.emptyList();
+        }
+        return consulBackends;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/63b77be4/consul/src/main/java/org/apache/tamaya/consul/ConsulBackends.java
----------------------------------------------------------------------
diff --git a/consul/src/main/java/org/apache/tamaya/consul/ConsulBackends.java b/consul/src/main/java/org/apache/tamaya/consul/ConsulBackends.java
deleted file mode 100644
index 4eab141..0000000
--- a/consul/src/main/java/org/apache/tamaya/consul/ConsulBackends.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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.consul;
-
-import com.google.common.net.HostAndPort;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-/**
- * Singleton that reads and stores the current consul setup, especially the possible host:ports
to be used.
- */
-public final class ConsulBackends {
-
-    private static final Logger LOG = Logger.getLogger(ConsulBackends.class.getName());
-    private static List<HostAndPort> consulBackends = new ArrayList<>();
-
-    static{
-        String serverURLs = System.getProperty("tamaya.consul.urls");
-        if(serverURLs==null){
-            serverURLs = System.getenv("tamaya.consul.urls");
-        }
-        if(serverURLs==null){
-            serverURLs = "127.0.0.1:8300";
-        }
-        for(String url:serverURLs.split("\\,")) {
-            try{
-                consulBackends.add(HostAndPort.fromString(url.trim()));
-                LOG.info("Using consul endoint: " + url);
-            } catch(Exception e){
-                LOG.log(Level.SEVERE, "Error initializing consul accessor for URL: " + url,
e);
-            }
-        }
-    }
-
-    private ConsulBackends(){}
-
-    public static List<HostAndPort> getConsulBackends(){
-        return consulBackends;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/63b77be4/consul/src/main/java/org/apache/tamaya/consul/ConsulPropertySource.java
----------------------------------------------------------------------
diff --git a/consul/src/main/java/org/apache/tamaya/consul/ConsulPropertySource.java b/consul/src/main/java/org/apache/tamaya/consul/ConsulPropertySource.java
index 74b60eb..7c0b017 100644
--- a/consul/src/main/java/org/apache/tamaya/consul/ConsulPropertySource.java
+++ b/consul/src/main/java/org/apache/tamaya/consul/ConsulPropertySource.java
@@ -29,9 +29,7 @@ import org.apache.tamaya.spi.PropertyValue;
 import org.apache.tamaya.spi.PropertyValueBuilder;
 import org.apache.tamaya.spisupport.BasePropertySource;
 
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -44,10 +42,46 @@ public class ConsulPropertySource extends BasePropertySource
 implements MutablePropertySource{
     private static final Logger LOG = Logger.getLogger(ConsulPropertySource.class.getName());
 
-    private String prefix = System.getProperty("tamaya.consul.prefix", "");
+    private String prefix = "";
 
+    private List<HostAndPort> consulBackends;
+
+
+    public ConsulPropertySource(String prefix, Collection<String> backends){
+        this.prefix = prefix==null?"":prefix;
+        consulBackends = new ArrayList<>();
+        for(String s:backends){
+            consulBackends.add(HostAndPort.fromString(s));
+        }
+    }
+
+    public ConsulPropertySource(Collection<String> backends){
+        consulBackends = new ArrayList<>();
+        for(String s:backends){
+            consulBackends.add(HostAndPort.fromString(s));
+        }
+    }
+
+    public ConsulPropertySource(){
+        prefix = System.getProperty("tamaya.consul.prefix", "");
+    }
+
+    public ConsulPropertySource(String... backends){
+        consulBackends = new ArrayList<>();
+        for (String s : backends) {
+            consulBackends.add(HostAndPort.fromString(s));
+        }
+    }
+
+    public String getPrefix() {
+        return prefix;
+    }
+
+    public ConsulPropertySource setPrefix(String prefix) {
+        this.prefix = prefix;
+        return this;
+    }
 
-    @Override
     public int getOrdinal() {
         PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL);
         if(configuredOrdinal!=null){
@@ -98,7 +132,7 @@ implements MutablePropertySource{
                 reqKey = reqKey.substring(0,reqKey.length()-".source".length());
             }
         }
-        for(HostAndPort hostAndPort: ConsulBackends.getConsulBackends()){
+        for(HostAndPort hostAndPort: getConsulBackends()){
             try{
                 Consul consul = Consul.builder().withHostAndPort(hostAndPort).build();
                 KeyValueClient kvClient = consul.keyValueClient();
@@ -124,7 +158,7 @@ implements MutablePropertySource{
 
     @Override
     public Map<String, String> getProperties() {
-//        for(HostAndPort hostAndPort: ConsulBackends.getConsulBackends()){
+//        for(HostAndPort hostAndPort: getConsulBackends()){
 //            try{
 //                Consul consul = Consul.builder().withHostAndPort(hostAndPort).build();
 //                KeyValueClient kvClient = consul.keyValueClient();
@@ -168,7 +202,7 @@ implements MutablePropertySource{
 
     @Override
     public void applyChange(ConfigChangeRequest configChange) {
-        for(HostAndPort hostAndPort: ConsulBackends.getConsulBackends()){
+        for(HostAndPort hostAndPort: ConsulBackendConfig.getConsulBackends()){
             try{
                 Consul consul = Consul.builder().withHostAndPort(hostAndPort).build();
                 KeyValueClient kvClient = consul.keyValueClient();
@@ -195,4 +229,12 @@ implements MutablePropertySource{
             }
         }
     }
+
+    private List<HostAndPort> getConsulBackends(){
+        if(consulBackends==null){
+            consulBackends = ConsulBackendConfig.getConsulBackends();
+            LOG.info("Using consul backends: " + consulBackends);
+        }
+        return consulBackends;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/63b77be4/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackendConfig.java
----------------------------------------------------------------------
diff --git a/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackendConfig.java b/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackendConfig.java
new file mode 100644
index 0000000..f760b21
--- /dev/null
+++ b/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackendConfig.java
@@ -0,0 +1,80 @@
+/*
+ * 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.etcd;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Singleton that reads and stores the current etcd setup, especially the possible URLs to
be used.
+ */
+public final class EtcdBackendConfig {
+
+    private static final Logger LOG = Logger.getLogger(EtcdBackendConfig.class.getName());
+    private static List<EtcdAccessor> etcdBackends = new ArrayList<>();
+
+    static{
+        int timeout = 2;
+        String val = System.getProperty("tamaya.etcd.timeout");
+        if(val == null){
+            val = System.getenv("tamaya.etcd.timeout");
+        }
+        if(val!=null){
+            timeout = Integer.parseInt(val);
+        }
+        String serverURLs = System.getProperty("tamaya.etcd.server.urls");
+        if(serverURLs==null){
+            serverURLs = System.getenv("tamaya.etcd.server.urls");
+        }
+        if(serverURLs==null){
+            serverURLs = "http://127.0.0.1:4001";
+        }
+        for(String url:serverURLs.split("\\,")) {
+            try{
+                etcdBackends.add(new EtcdAccessor(url.trim(), timeout));
+                LOG.info("Using etcd endoint: " + url);
+            } catch(Exception e){
+                LOG.log(Level.SEVERE, "Error initializing etcd accessor for URL: " + url,
e);
+            }
+        }
+    }
+
+    private EtcdBackendConfig(){}
+
+    private static boolean isEtcdDisabled() {
+        String value = System.getProperty("tamaya.etcd.disable");
+        if(value==null){
+            value = System.getenv("tamaya.etcd.disable");
+        }
+        if(value==null){
+            return false;
+        }
+        return value.isEmpty() || Boolean.parseBoolean(value);
+    }
+
+    public static List<EtcdAccessor> getEtcdBackends(){
+        if(isEtcdDisabled()){
+            return Collections.emptyList();
+        }
+        return etcdBackends;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/63b77be4/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackends.java
----------------------------------------------------------------------
diff --git a/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackends.java b/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackends.java
deleted file mode 100644
index a0c0703..0000000
--- a/etcd/src/main/java/org/apache/tamaya/etcd/EtcdBackends.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * 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.etcd;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-/**
- * Singleton that reads and stores the current etcd setup, especially the possible URLs to
be used.
- */
-public final class EtcdBackends {
-
-    private static final Logger LOG = Logger.getLogger(EtcdBackends.class.getName());
-    private static List<EtcdAccessor> etcdBackends = new ArrayList<>();
-
-    static{
-        int timeout = 2;
-        String val = System.getProperty("tamaya.etcd.timeout");
-        if(val == null){
-            val = System.getenv("tamaya.etcd.timeout");
-        }
-        if(val!=null){
-            timeout = Integer.parseInt(val);
-        }
-        String serverURLs = System.getProperty("tamaya.etcd.server.urls");
-        if(serverURLs==null){
-            serverURLs = System.getenv("tamaya.etcd.server.urls");
-        }
-        if(serverURLs==null){
-            serverURLs = "http://127.0.0.1:4001";
-        }
-        for(String url:serverURLs.split("\\,")) {
-            try{
-                etcdBackends.add(new EtcdAccessor(url.trim(), timeout));
-                LOG.info("Using etcd endoint: " + url);
-            } catch(Exception e){
-                LOG.log(Level.SEVERE, "Error initializing etcd accessor for URL: " + url,
e);
-            }
-        }
-    }
-
-    private EtcdBackends(){}
-
-    public static List<EtcdAccessor> getEtcdBackends(){
-        return etcdBackends;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/63b77be4/etcd/src/main/java/org/apache/tamaya/etcd/EtcdPropertySource.java
----------------------------------------------------------------------
diff --git a/etcd/src/main/java/org/apache/tamaya/etcd/EtcdPropertySource.java b/etcd/src/main/java/org/apache/tamaya/etcd/EtcdPropertySource.java
index 0daa4bf..326097f 100644
--- a/etcd/src/main/java/org/apache/tamaya/etcd/EtcdPropertySource.java
+++ b/etcd/src/main/java/org/apache/tamaya/etcd/EtcdPropertySource.java
@@ -24,8 +24,11 @@ import org.apache.tamaya.spi.PropertyValue;
 import org.apache.tamaya.spi.PropertyValueBuilder;
 import org.apache.tamaya.spisupport.BasePropertySource;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -42,25 +45,41 @@ public class EtcdPropertySource extends BasePropertySource
 
     private String prefix = System.getProperty("tamaya.etcd.prefix", "");
 
-    private final boolean disabled = evaluateDisabled();
+    private List<EtcdAccessor> etcdBackends;
 
-    public EtcdPropertySource(){
-        super("etcd", 1000);
+    public EtcdPropertySource(String prefix, Collection<String> backends){
+        this.prefix = prefix==null?"":prefix;
+        etcdBackends = new ArrayList<>();
+        for(String s:backends){
+            etcdBackends.add(new EtcdAccessor(s));
+        }
     }
 
-    public EtcdPropertySource(int defaultOrdinal){
-        super("etcd", defaultOrdinal);
+    public EtcdPropertySource(Collection<String> backends){
+        etcdBackends = new ArrayList<>();
+        for(String s:backends){
+            etcdBackends.add(new EtcdAccessor(s));
+        }
     }
 
-    private boolean evaluateDisabled() {
-        String value = System.getProperty("tamaya.etcdprops.disable");
-        if(value==null){
-            value = System.getenv("tamaya.etcdprops.disable");
-        }
-        if(value==null){
-            return false;
+    public EtcdPropertySource(){
+        prefix = System.getProperty("tamaya.etcd.prefix", "");
+    }
+
+    public EtcdPropertySource(String... backends){
+        etcdBackends = new ArrayList<>();
+        for (String s : backends) {
+            etcdBackends.add(new EtcdAccessor(s));
         }
-        return value.isEmpty() || Boolean.parseBoolean(value);
+    }
+
+    public String getPrefix() {
+        return prefix;
+    }
+
+    public EtcdPropertySource setPrefix(String prefix) {
+        this.prefix = prefix;
+        return this;
     }
 
     @Override
@@ -79,9 +98,6 @@ public class EtcdPropertySource extends BasePropertySource
 
     @Override
     public PropertyValue get(String key) {
-        if(disabled){
-            return null;
-        }
         // check prefix, if key does not start with it, it is not part of our name space
         // if so, the prefix part must be removedProperties, so etcd can resolve without
it
         if(!key.startsWith(prefix)){
@@ -105,7 +121,7 @@ public class EtcdPropertySource extends BasePropertySource
                 reqKey = reqKey.substring(0,reqKey.length()-".source".length());
             }
         }
-        for(EtcdAccessor accessor: EtcdBackends.getEtcdBackends()){
+        for(EtcdAccessor accessor: EtcdBackendConfig.getEtcdBackends()){
             try{
                 props = accessor.get(reqKey);
                 if(!props.containsKey("_ERROR")) {
@@ -123,21 +139,16 @@ public class EtcdPropertySource extends BasePropertySource
 
     @Override
     public Map<String, String> getProperties() {
-        if(disabled){
-            return Collections.emptyMap();
-        }
-        if(!EtcdBackends.getEtcdBackends().isEmpty()){
-            for(EtcdAccessor accessor: EtcdBackends.getEtcdBackends()){
-                try{
-                    Map<String, String> props = accessor.getProperties("");
-                    if(!props.containsKey("_ERROR")) {
-                        return mapPrefix(props);
-                    } else{
-                        LOG.log(Level.FINE, "etcd error on " + accessor.getUrl() + ": " +
props.get("_ERROR"));
-                    }
-                } catch(Exception e){
-                    LOG.log(Level.FINE, "etcd access failed on " + accessor.getUrl() + ",
trying next...", e);
+        for(EtcdAccessor accessor: getEtcdBackends()){
+            try{
+                Map<String, String> props = accessor.getProperties("");
+                if(!props.containsKey("_ERROR")) {
+                    return mapPrefix(props);
+                } else{
+                    LOG.log(Level.FINE, "etcd error on " + accessor.getUrl() + ": " + props.get("_ERROR"));
                 }
+            } catch(Exception e){
+                LOG.log(Level.FINE, "etcd access failed on " + accessor.getUrl() + ", trying
next...", e);
             }
         }
         return Collections.emptyMap();
@@ -165,7 +176,7 @@ public class EtcdPropertySource extends BasePropertySource
 
     @Override
     public void applyChange(ConfigChangeRequest configChange) {
-        for(EtcdAccessor accessor: EtcdBackends.getEtcdBackends()){
+        for(EtcdAccessor accessor: EtcdBackendConfig.getEtcdBackends()){
             try{
                 for(String k: configChange.getRemovedProperties()){
                     Map<String,String> res = accessor.delete(k);
@@ -201,4 +212,11 @@ public class EtcdPropertySource extends BasePropertySource
         }
     }
 
+    private List<EtcdAccessor> getEtcdBackends(){
+        if(etcdBackends==null){
+            etcdBackends = EtcdBackendConfig.getEtcdBackends();
+            LOG.info("Using etcd backends: " + etcdBackends);
+        }
+        return etcdBackends;
+    }
 }


Mime
View raw message