Rev 68 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.thrift.clients.config;import in.shop2020.config.ConfigException;import in.shop2020.config.Configuration.Client;import in.shop2020.utils.Logger;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.net.URL;import org.apache.thrift.TException;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TProtocol;import org.apache.thrift.transport.TFramedTransport;import org.apache.thrift.transport.TSocket;import org.apache.thrift.transport.TTransport;/*** The text in file is like* config://localhost:9999* @author ashish**/public class ConfigClient {private static ConfigClient configClient = null;static{synchronized (ConfigClient.class) {if(configClient == null){configClient = new ConfigClient();}}}private int PORT_NUMBER = 9999;private String HOST = "localhost";private final String CONFIG_URL_FILE="/tmp/config.properties";private Client client = null;public static ConfigClient getClient(){return configClient;}private ConfigClient(){try {URL url = getConfigServerUrl();initClient(url.getHost(), url.getPort());} catch (Exception e) {Logger.log("Falling back to default options for host and port number ", this);initClient(HOST, PORT_NUMBER);}}private URL getConfigServerUrl() throws Exception{BufferedReader in = null;try {in = new BufferedReader(new FileReader(CONFIG_URL_FILE));} catch (FileNotFoundException e) {Logger.log("Error reading config file "+ CONFIG_URL_FILE +". Please check if file exists", this);e.printStackTrace();throw e;}//We read only first lineString urlString = in.readLine();in.close();URL url = new URL(urlString);return url;}private void initClient(String host, int port){final TSocket tsocket = new TSocket(host, port);final TTransport transport = new TFramedTransport(tsocket);final TProtocol protocol = new TBinaryProtocol(transport);client = new Client(protocol);Logger.log("Config Client initialized for host "+ host +" port "+ port, this);}public String get(String property) throws ConfigException{try {return client.getPropetry(property);} catch (TException e) {e.printStackTrace();throw new ConfigException(-1, "Encountered exception while obtaining value for "+ property);}}public void set(String property, String value) throws ConfigException{try {client.loadProperty(property, value);} catch (TException e) {e.printStackTrace();throw new ConfigException(-1, "Encountered exception while putting value "+ value+ "for property "+ property);}}}