Utils/EtcdConnector/src/main/java/eu/mikroskeem/utils/etcdconnector/EtcdConnectorBase.java

170 lines
5.4 KiB
Java

package eu.mikroskeem.utils.etcdconnector;
import com.google.gson.Gson;
import mousio.etcd4j.EtcdClient;
import mousio.etcd4j.promises.EtcdResponsePromise;
import mousio.etcd4j.responses.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
abstract class EtcdConnectorBase implements EtcdConnector {
Logger logger;
EtcdClient etcdClient;
EtcdConnectorBase() throws IOException {}
/**
* Initializes EtcdClient object
*/
abstract void initClient(URI... urls);
/**
* Tests if Etcd connection is working
*
* @throws IOException if connection doesn't work
*/
void testEtcd() throws IOException {
/* Test client */
try {
logger.debug("Testing Etcd connection...");
EtcdVersionResponse ver = etcdClient.version();
logger.debug("Etcd server version: {}", ver.getServer());
} catch (NullPointerException e){
throw new IOException("Etcd connection test failed!");
}
logger.debug("Etcd is working!");
}
public void close() throws IOException {
if(this.etcdClient != null){
this.etcdClient.close();
}
}
@Nullable
public String getKey(@NotNull String path){
logger.debug("getKey: {}", path);
try {
EtcdKeysResponse response = etcdClient.get(path).send().get();
/* Parse object */
String rawResp = response.getNode().getValue();
if(rawResp != null){
return new Gson().fromJson(rawResp, String.class);
}
return null;
} catch(EtcdException e){
if(!e.isErrorCode(EtcdErrorCode.KeyNotFound)) {
e.printStackTrace();
}
} catch(IOException|EtcdAuthenticationException |TimeoutException e){
e.printStackTrace();
}
return null;
}
@Nullable
public String getKey(@NotNull String path, @NotNull String defaultValue){
logger.debug("getKey: {}, {}", path, defaultValue);
try {
EtcdKeysResponse response = etcdClient.get(path).send().get();
/* Parse object */
String rawResp = response.getNode().getValue();
if(rawResp != null){
return new Gson().fromJson(rawResp, String.class);
}
return null;
} catch(EtcdException e){
if(e.isErrorCode(EtcdErrorCode.KeyNotFound)){
/* Put default value to there then */
putKey(path, defaultValue);
return defaultValue;
} else e.printStackTrace();
} catch(IOException|EtcdAuthenticationException|TimeoutException e){
e.printStackTrace();
}
return null;
}
@Nullable
public List<String> getDir(@NotNull String path){
logger.debug("getDir: {}", path);
List<String> ret = new ArrayList<>();
try {
EtcdKeysResponse response = etcdClient.getDir(path).send().get();
response.getNode().getNodes().forEach(etcdNode -> ret.add((new File(etcdNode.getKey())).getName()));
return ret;
} catch(EtcdException e){
if(!(e.isErrorCode(EtcdErrorCode.KeyNotFound))){
e.printStackTrace();
}
return null;
} catch(IOException|EtcdAuthenticationException|TimeoutException e){
e.printStackTrace();
}
return null;
}
public boolean putKey(@NotNull String path, @NotNull String value){
logger.debug("putKey: {}, {}", path, value);
try {
/* Serialize value */
EtcdKeysResponse response = etcdClient.put(path, new Gson().toJson(value)).send().get();
response.getNode().getValue();
return true;
} catch(Exception e) {
e.printStackTrace();
}
return false;
}
public boolean putKey(@NotNull String path, @NotNull String value, @NotNull Integer ttl){
logger.debug("putKey: {}, {}, {}", path, value, ttl);
try {
EtcdKeysResponse response = etcdClient.put(path, new Gson().toJson(value)).ttl(ttl).send().get();
response.getNode().getValue();
return true;
} catch(Exception e) {
e.printStackTrace();
}
return false;
}
public boolean deleteKey(@NotNull String path){
logger.debug("deleteKey: {}", path);
try {
EtcdKeysResponse response = etcdClient.delete(path).send().get();
response.getNode().getValue();
return true;
} catch(EtcdException e){
if(!(e.isErrorCode(EtcdErrorCode.KeyNotFound))){
e.printStackTrace();
}
} catch(Exception e){
e.printStackTrace();
}
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();
}
}
}