Rev 3258 | 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.util.concurrent.ConcurrentHashMap;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;/*** @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 Client client = null;TSocket tsocket = null;TTransport transport = null;TProtocol protocol = null;private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<String, String>();public static ConfigClient getClient(){return configClient;}private ConfigClient()throws ConfigException{if(!initClient(HOST, PORT_NUMBER)){throw new ConfigException(-1, "unable to intialize the config client");}}private boolean initClient(String host, int port){try {if(transport != null && transport.isOpen()){transport.close();}} catch (Exception e) {// ignore close errors}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;}private synchronized void reconnect() {if(transport != null && transport.isOpen()){return;}Logger.log("Reconnecting to config server", this);initClient(HOST, PORT_NUMBER);}public synchronized String get(String property) throws ConfigException{String cached = cache.get(property);if(cached != null){return cached;}try {String value = client.getPropetry(property);if(value != null){cache.put(property, value);}return value;} catch (TException e) {Logger.log("Config get failed for " + property + ", reconnecting", this);reconnect();try {String value = client.getPropetry(property);if(value != null){cache.put(property, value);}return value;} catch (TException e2) {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);cache.put(property, value);} catch (TException e) {Logger.log("Config set failed for " + property + ", reconnecting", this);reconnect();try {client.loadProperty(property, value);cache.put(property, value);} catch (TException e2) {throw new ConfigException(-1, "Encountered exception while putting value "+ value+ " for property "+ property);}}}}