Subversion Repositories SmartDukaan

Rev

Rev 1021 | 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 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 block
                                        e.printStackTrace();
                                }
                        }
                }
        }

        private int PORT_NUMBER = 9999;
        private String HOST = "localhost";
        private Client client = null;
        
        TSocket tsocket = null;
        TTransport transport = null;
        TProtocol protocol = null;
        
        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){
                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);
                }
        }
}