package net.helipilot50.stocktrade.server;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import net.citrusleaf.CitrusleafClient;
public class DBClient {
private static CitrusleafClient client;
private static String host = "127.0.0.1";
private static int port = 3000;
private static final String CONFIG_FILE = "StockTrade.conf";
public static final String NAME_SPACE = "test";
static {
try {
readConfig();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
client = new CitrusleafClient(host, port);
}
private DBClient(){
}
public static CitrusleafClient getInstance(){
return client;
}
public static void readConfig() throws FileNotFoundException{
String hostString = new Scanner(new File(CONFIG_FILE)).useDelimiter("\\Z").next();
parseAndSet(hostString);
}
public static void writeConfig() throws FileNotFoundException{
PrintWriter out = new PrintWriter(CONFIG_FILE);
out.println(host + ":" + port);
out.close();
}
public static void setHostString(String hostString){
parseAndSet(hostString);
try {
writeConfig();
if (client != null){
client.close();
client = null;
client = new CitrusleafClient(host, port);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static String getHostString(){
return host + ":" + port;
}
private static void parseAndSet(String hostString){
String parts[] = hostString.split(":");
host = parts[0];
port = Integer.parseInt(parts[1]);
}
}