Subversion Repositories SmartDukaan

Rev

Rev 3258 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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