Initial commit
This commit is contained in:
76
EtcdConnector/pom.xml
Normal file
76
EtcdConnector/pom.xml
Normal file
@@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>utils</artifactId>
|
||||
<groupId>eu.mikroskeem</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>etcdconnector</artifactId>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.4.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/maven/**</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>io.netty</pattern>
|
||||
<shadedPattern>eu.mikroskeem.utils.etcdconnector.deps.io.netty</shadedPattern>
|
||||
<excludes>
|
||||
<exclude>io.netty.*</exclude>
|
||||
</excludes>
|
||||
</relocation>
|
||||
</relocations>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations-java5</artifactId>
|
||||
<version>RELEASE</version>
|
||||
</dependency>
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.21</version>
|
||||
</dependency>
|
||||
<!-- Etcd -->
|
||||
<dependency>
|
||||
<groupId>org.mousio</groupId>
|
||||
<artifactId>etcd4j</artifactId>
|
||||
<version>2.12.0</version>
|
||||
</dependency>
|
||||
<!-- Serialization -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,30 @@
|
||||
package eu.mikroskeem.utils.etcdconnector;
|
||||
|
||||
import mousio.client.retry.RetryOnce;
|
||||
import mousio.etcd4j.EtcdClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
public class EtcdConnector extends EtcdConnectorBase {
|
||||
/**
|
||||
* Sets up EtcdConnector against
|
||||
* Etcd server with no encryption
|
||||
*
|
||||
* @param etcdUrls url(s) where to connect to
|
||||
*/
|
||||
public EtcdConnector(URI... etcdUrls) throws IOException {
|
||||
super();
|
||||
|
||||
/* Initialize client */
|
||||
initClient(etcdUrls);
|
||||
|
||||
/* Test client */
|
||||
testEtcd();
|
||||
}
|
||||
|
||||
@Override void initClient(URI... urls){
|
||||
etcdClient = new EtcdClient(urls);
|
||||
etcdClient.setRetryHandler(new RetryOnce(5000));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package eu.mikroskeem.utils.etcdconnector;
|
||||
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import mousio.etcd4j.EtcdClient;
|
||||
import mousio.etcd4j.responses.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
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;
|
||||
|
||||
abstract class EtcdConnectorBase {
|
||||
final Logger logger;
|
||||
EtcdClient etcdClient;
|
||||
|
||||
EtcdConnectorBase() throws IOException {
|
||||
/* Set up logger */
|
||||
logger = LoggerFactory.getLogger("EtcdConnector");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes Etcd4j client
|
||||
*
|
||||
* @throws IOException Thrown by Etcd4j
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if(this.etcdClient != null){
|
||||
this.etcdClient.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets etcd key value
|
||||
*
|
||||
* @param path Key path
|
||||
* @return Key value
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets etcd directory contents and returns it as
|
||||
* List of String
|
||||
*
|
||||
* @param path directory path
|
||||
* @return directory contents
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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){
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 int 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes etcd key
|
||||
* @param path key path
|
||||
* @return whether delete succeeded or not
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user