Rev 48 | Rev 1021 | 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;import org.apache.thrift.transport.TTransportException;/*** 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){try {configClient = new ConfigClient();} catch (ConfigException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}private int PORT_NUMBER = 9999;private String HOST = "localhost";private final String CONFIG_URL_FILE="/tmp/config.properties";private Client client = null;TSocket tsocket = null;TTransport transport = null;TProtocol protocol = null;public static ConfigClient getClient(){return configClient;}private ConfigClient()throws ConfigException{try {URL url = getConfigServerUrl();HOST = url.getHost();PORT_NUMBER = url.getPort();} catch (Exception e) {Logger.log("Falling back to default options for host and port number ", this);}if(!initClient(HOST, PORT_NUMBER)){throw new ConfigException(-1, "unable to intialize the config client");}}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 boolean initClient(String host, int port){tsocket = new TSocket(HOST, PORT_NUMBER);transport = new TFramedTransport(tsocket);protocol = new TBinaryProtocol(transport);client = new Client(protocol);try {transport.open();} catch (TTransportException e) {Logger.log("unable to open transport for client library"+ e , this);return false;}Logger.log("Config Client initialized for host "+ host +" port "+ port, this);return true;}public synchronized 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 synchronized 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);}}}