Subversion Repositories SmartDukaan

Rev

Rev 3258 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
48 ashish 1
package in.shop2020.thrift.clients.config;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.config.Configuration.Client;
5
import in.shop2020.utils.Logger;
6
 
35764 amit 7
import java.util.concurrent.ConcurrentHashMap;
8
 
48 ashish 9
import org.apache.thrift.TException;
10
import org.apache.thrift.protocol.TBinaryProtocol;
11
import org.apache.thrift.protocol.TProtocol;
12
import org.apache.thrift.transport.TFramedTransport;
13
import org.apache.thrift.transport.TSocket;
14
import org.apache.thrift.transport.TTransport;
68 ashish 15
import org.apache.thrift.transport.TTransportException;
48 ashish 16
 
17
/**
18
 * @author ashish
19
 *
20
 */
21
 
22
public class ConfigClient {
35764 amit 23
 
48 ashish 24
	private static ConfigClient configClient = null;
35764 amit 25
 
48 ashish 26
	static{
27
		synchronized (ConfigClient.class) {
28
			if(configClient == null){
68 ashish 29
				try {
30
					configClient = new ConfigClient();
31
				} catch (ConfigException e) {
32
					// TODO Auto-generated catch block
33
					e.printStackTrace();
34
				}
48 ashish 35
			}
36
		}
37
	}
38
 
39
	private int PORT_NUMBER = 9999;
40
	private String HOST = "localhost";
41
	private Client client = null;
35764 amit 42
 
68 ashish 43
	TSocket tsocket = null;
44
	TTransport transport = null;
45
	TProtocol protocol = null;
35764 amit 46
 
47
	private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<String, String>();
48
 
48 ashish 49
	public static ConfigClient getClient(){
50
		return configClient;
51
	}
35764 amit 52
 
68 ashish 53
	private ConfigClient()throws ConfigException{
54
		if(!initClient(HOST, PORT_NUMBER)){
55
			throw new ConfigException(-1, "unable to intialize the config client");
56
		}
48 ashish 57
	}
35764 amit 58
 
68 ashish 59
	private boolean initClient(String host, int port){
35764 amit 60
		try {
61
			if(transport != null && transport.isOpen()){
62
				transport.close();
63
			}
64
		} catch (Exception e) {
65
			// ignore close errors
66
		}
68 ashish 67
		tsocket = new TSocket(HOST, PORT_NUMBER);
68
		transport = new TFramedTransport(tsocket);
69
		protocol = new TBinaryProtocol(transport);
48 ashish 70
		client = new Client(protocol);
68 ashish 71
		try {
72
			transport.open();
73
		} catch (TTransportException e) {
74
			Logger.log("unable to open transport for client library"+ e , this);
75
			return false;
76
		}
48 ashish 77
		Logger.log("Config Client initialized for host "+ host +" port "+ port, this);
68 ashish 78
		return true;
48 ashish 79
	}
35764 amit 80
 
81
	private synchronized void reconnect() {
82
		if(transport != null && transport.isOpen()){
83
			return;
84
		}
85
		Logger.log("Reconnecting to config server", this);
86
		initClient(HOST, PORT_NUMBER);
87
	}
88
 
68 ashish 89
	public synchronized String get(String property) throws ConfigException{
35764 amit 90
		String cached = cache.get(property);
91
		if(cached != null){
92
			return cached;
93
		}
48 ashish 94
		try {
35764 amit 95
			String value = client.getPropetry(property);
96
			if(value != null){
97
				cache.put(property, value);
98
			}
99
			return value;
48 ashish 100
		} catch (TException e) {
35764 amit 101
			Logger.log("Config get failed for " + property + ", reconnecting", this);
102
			reconnect();
103
			try {
104
				String value = client.getPropetry(property);
105
				if(value != null){
106
					cache.put(property, value);
107
				}
108
				return value;
109
			} catch (TException e2) {
110
				throw new ConfigException(-1, "Encountered exception while obtaining value for "+ property);
111
			}
48 ashish 112
		}
113
	}
35764 amit 114
 
68 ashish 115
	public synchronized void set(String property, String value) throws ConfigException{
48 ashish 116
		try {
117
			client.loadProperty(property, value);
35764 amit 118
			cache.put(property, value);
48 ashish 119
		} catch (TException e) {
35764 amit 120
			Logger.log("Config set failed for " + property + ", reconnecting", this);
121
			reconnect();
122
			try {
123
				client.loadProperty(property, value);
124
				cache.put(property, value);
125
			} catch (TException e2) {
126
				throw new ConfigException(-1, "Encountered exception while putting value "+ value+ " for property "+ property);
127
			}
48 ashish 128
		}
129
	}
130
}