Refactor whole project

Turn EtcdConnector into interface
This commit is contained in:
Mark Vainomaa 2016-09-11 22:22:01 +03:00
parent 8c42f0dcb9
commit ba3fd05932
5 changed files with 119 additions and 73 deletions

View File

@ -9,7 +9,7 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>etcdconnector</artifactId> <artifactId>etcdconnector</artifactId>
<version>1.1-SNAPSHOT</version> <version>1.2-SNAPSHOT</version>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@ -1,34 +1,80 @@
package eu.mikroskeem.utils.etcdconnector; package eu.mikroskeem.utils.etcdconnector;
import mousio.client.retry.RetryOnce; import org.jetbrains.annotations.NotNull;
import mousio.etcd4j.EtcdClient; import org.jetbrains.annotations.Nullable;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.util.List;
import java.util.function.Function;
public interface EtcdConnector {
public class EtcdConnector extends EtcdConnectorBase {
/** /**
* Sets up EtcdConnector against * Closes Etcd4j client
* Etcd server with no encryption
* *
* @param etcdUrls url(s) where to connect to * @throws IOException Thrown by Etcd4j
*/ */
public EtcdConnector(URI... etcdUrls) throws IOException { void close() throws IOException;
super();
/* Set up logger */ /**
logger = LoggerFactory.getLogger("EtcdConnector"); * Gets etcd key value
*
* @param path Key path
* @return Key value
*/
@Nullable String getKey(@NotNull String path);
/* Initialize client */ /**
initClient(etcdUrls); * Gets etcd key value and sets default,
* if key isn't defined
*
* @param path Key path
* @param defaultValue Value to set if key is nonexistent
* @return Key value
*/
@Nullable String getKey(@NotNull String path, @NotNull String defaultValue);
/* Test client */ /**
testEtcd(); * Gets etcd directory contents and returns it as
} * List of String
*
* @param path directory path
* @return directory contents
*/
@Nullable List<String> getDir(@NotNull String path);
@Override void initClient(URI... urls){ /**
etcdClient = new EtcdClient(urls); * Sets etcd key value
etcdClient.setRetryHandler(new RetryOnce(5000)); *
} * @param path key path
} * @param value key value
* @return whether write succeeded or not
*/
boolean putKey(@NotNull String path, @NotNull String value);
/**
* Sets etcd key value with TTL
*
* @param path key path
* @param value key value
* @param ttl key ttl (seconds)
* @return whether write succeeded or not
*/
boolean putKey(@NotNull String path, @NotNull String value, @NotNull Integer ttl);
/**
* Deletes etcd key
*
* @param path key path
* @return whether delete succeeded or not
*/
boolean deleteKey(@NotNull String path);
/**
* Waits for key change
*
* @param path Key path
* @param callback Callback to run
*/
void waitKey(@NotNull String path, @NotNull Function<Object,Void> callback);
}

View File

@ -3,11 +3,11 @@ package eu.mikroskeem.utils.etcdconnector;
import com.google.gson.Gson; import com.google.gson.Gson;
import mousio.etcd4j.EtcdClient; import mousio.etcd4j.EtcdClient;
import mousio.etcd4j.promises.EtcdResponsePromise;
import mousio.etcd4j.responses.*; import mousio.etcd4j.responses.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -15,14 +15,14 @@ import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.function.Function;
abstract class EtcdConnectorBase { abstract class EtcdConnectorBase implements EtcdConnector {
Logger logger; Logger logger;
EtcdClient etcdClient; EtcdClient etcdClient;
EtcdConnectorBase() throws IOException {} EtcdConnectorBase() throws IOException {}
/** /**
* Initializes EtcdClient object * Initializes EtcdClient object
*/ */
@ -45,23 +45,12 @@ abstract class EtcdConnectorBase {
logger.debug("Etcd is working!"); logger.debug("Etcd is working!");
} }
/**
* Closes Etcd4j client
*
* @throws IOException Thrown by Etcd4j
*/
public void close() throws IOException { public void close() throws IOException {
if(this.etcdClient != null){ if(this.etcdClient != null){
this.etcdClient.close(); this.etcdClient.close();
} }
} }
/**
* Gets etcd key value
*
* @param path Key path
* @return Key value
*/
@Nullable @Nullable
public String getKey(@NotNull String path){ public String getKey(@NotNull String path){
logger.debug("getKey: {}", path); logger.debug("getKey: {}", path);
@ -83,14 +72,6 @@ abstract class EtcdConnectorBase {
return null; return null;
} }
/**
* Gets etcd key value and sets default,
* if key isn't defined
*
* @param path Key path
* @param defaultValue Value to set if key is nonexistent
* @return Key value
*/
@Nullable @Nullable
public String getKey(@NotNull String path, @NotNull String defaultValue){ public String getKey(@NotNull String path, @NotNull String defaultValue){
logger.debug("getKey: {}, {}", path, defaultValue); logger.debug("getKey: {}, {}", path, defaultValue);
@ -114,13 +95,6 @@ abstract class EtcdConnectorBase {
return null; return null;
} }
/**
* Gets etcd directory contents and returns it as
* List of String
*
* @param path directory path
* @return directory contents
*/
@Nullable @Nullable
public List<String> getDir(@NotNull String path){ public List<String> getDir(@NotNull String path){
logger.debug("getDir: {}", path); logger.debug("getDir: {}", path);
@ -140,13 +114,6 @@ abstract class EtcdConnectorBase {
return null; return null;
} }
/**
* Sets etcd key value
*
* @param path key path
* @param value key value
* @return whether write succeeded or not
*/
public boolean putKey(@NotNull String path, @NotNull String value){ public boolean putKey(@NotNull String path, @NotNull String value){
logger.debug("putKey: {}, {}", path, value); logger.debug("putKey: {}, {}", path, value);
try { try {
@ -160,14 +127,6 @@ abstract class EtcdConnectorBase {
return false; return false;
} }
/**
* Sets etcd key value with TTL
*
* @param path key path
* @param value key value
* @param ttl key ttl (seconds)
* @return whether write succeeded or not
*/
public boolean putKey(@NotNull String path, @NotNull String value, @NotNull Integer ttl){ public boolean putKey(@NotNull String path, @NotNull String value, @NotNull Integer ttl){
logger.debug("putKey: {}, {}, {}", path, value, ttl); logger.debug("putKey: {}, {}, {}", path, value, ttl);
try { try {
@ -180,11 +139,6 @@ abstract class EtcdConnectorBase {
return false; return false;
} }
/**
* Deletes etcd key
* @param path key path
* @return whether delete succeeded or not
*/
public boolean deleteKey(@NotNull String path){ public boolean deleteKey(@NotNull String path){
logger.debug("deleteKey: {}", path); logger.debug("deleteKey: {}", path);
try { try {
@ -200,4 +154,16 @@ abstract class EtcdConnectorBase {
} }
return false; return false;
} }
public void waitKey(@NotNull String path, @NotNull Function<Object,Void> callback){
logger.debug("waitKey: {}", path);
try {
EtcdResponsePromise promise = etcdClient.get(path).waitForChange().send();
promise.addListener(p -> {
callback.apply(p.getNow());
});
} catch (IOException e){
e.printStackTrace();
}
}
} }

View File

@ -0,0 +1,34 @@
package eu.mikroskeem.utils.etcdconnector;
import mousio.client.retry.RetryOnce;
import mousio.etcd4j.EtcdClient;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URI;
public class EtcdHTTPConnector extends EtcdConnectorBase implements EtcdConnector {
/**
* Sets up EtcdHTTPConnector against
* Etcd server with no encryption
*
* @param etcdUrls url(s) where to connect to
*/
public EtcdHTTPConnector(URI... etcdUrls) throws IOException {
super();
/* Set up logger */
logger = LoggerFactory.getLogger("EtcdConnector");
/* Initialize client */
initClient(etcdUrls);
/* Test client */
testEtcd();
}
@Override void initClient(URI... urls){
etcdClient = new EtcdClient(urls);
etcdClient.setRetryHandler(new RetryOnce(5000));
}
}

View File

@ -10,7 +10,7 @@ import javax.net.ssl.SSLException;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
public class EtcdSSLConnector extends EtcdConnectorBase { public class EtcdSSLConnector extends EtcdConnectorBase implements EtcdConnector {
public EtcdSSLConnector(URI... etcdUrls) throws IOException { public EtcdSSLConnector(URI... etcdUrls) throws IOException {
super(); super();