Skip to content

Commit

Permalink
ADDED: will now check env vars 'ETCD_LISTEN_CLIENT_URLS' and 'ETCD_AD…
Browse files Browse the repository at this point in the history
…VERTISE_CLIENT_URLS' for a valid url should the client not define one.
  • Loading branch information
Christopher Dancy committed Jun 9, 2016
1 parent 46eb3fd commit 07f80ce
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/main/java/com/cdancy/etcd/rest/EtcdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package com.cdancy.etcd.rest;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.jclouds.ContextBuilder;

public class EtcdClient {
Expand All @@ -39,11 +43,15 @@ private void configureParameters() {

// query system for endPoint value
if (endPoint == null) {
if ((endPoint = retrivePropertyValue("EtcdApi.rest.endpoint")) == null) {
if ((endPoint = retrivePropertyValue("etcd.rest.endpoint")) == null) {
if ((endPoint = retrivePropertyValue("etcdRestEndpoint")) == null) {
if ((endPoint = retrivePropertyValue("ETCD_REST_ENDPOINT")) == null) {
endPoint = "http://127.0.0.1:2379";
System.out.println("Etcd REST endpoint was not found. Defaulting to: " + endPoint);
if ((endPoint = checkClient("ETCD_LISTEN_CLIENT_URLS")) == null) {
if ((endPoint = checkClient("ETCD_ADVERTISE_CLIENT_URLS")) == null) {
endPoint = "http://127.0.0.1:2379";
System.out.println("Etcd REST endpoint was not found. Defaulting to: " + endPoint);
}
}
}
}
}
Expand All @@ -62,11 +70,23 @@ private void configureParameters() {
}
}

private String retrivePropertyValue(String key) {
public String retrivePropertyValue(String key) {
String value = System.getProperty(key);
return value != null ? value : System.getenv(key);
}

public String checkClient(String key) {
String clientList = retrivePropertyValue(key);
if (clientList != null) {
for (String possibleClient : clientList.split(",")) {
if (EtcdClient.pingEtcdURL(possibleClient, 60000)) {
return possibleClient;
}
}
}
return null;
}

public String endPoint() {
return endPoint;
}
Expand Down Expand Up @@ -105,4 +125,23 @@ public EtcdClient build() {
return new EtcdClient(endPoint, credentials);
}
}

public static boolean pingEtcdURL(String url, int timeout) {
url = url.replaceFirst("^https", "http");
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(url + "/version").openConnection();
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
return (200 <= responseCode && responseCode <= 399);
} catch (IOException exception) {
return false;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}

0 comments on commit 07f80ce

Please sign in to comment.