| 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 |
|
|
|
7 |
import org.apache.thrift.TException;
|
|
|
8 |
import org.apache.thrift.protocol.TBinaryProtocol;
|
|
|
9 |
import org.apache.thrift.protocol.TProtocol;
|
|
|
10 |
import org.apache.thrift.transport.TFramedTransport;
|
|
|
11 |
import org.apache.thrift.transport.TSocket;
|
|
|
12 |
import org.apache.thrift.transport.TTransport;
|
| 68 |
ashish |
13 |
import org.apache.thrift.transport.TTransportException;
|
| 48 |
ashish |
14 |
|
|
|
15 |
/**
|
|
|
16 |
* @author ashish
|
|
|
17 |
*
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
public class ConfigClient {
|
|
|
21 |
|
|
|
22 |
private static ConfigClient configClient = null;
|
|
|
23 |
|
|
|
24 |
static{
|
|
|
25 |
synchronized (ConfigClient.class) {
|
|
|
26 |
if(configClient == null){
|
| 68 |
ashish |
27 |
try {
|
|
|
28 |
configClient = new ConfigClient();
|
|
|
29 |
} catch (ConfigException e) {
|
|
|
30 |
// TODO Auto-generated catch block
|
|
|
31 |
e.printStackTrace();
|
|
|
32 |
}
|
| 48 |
ashish |
33 |
}
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
private int PORT_NUMBER = 9999;
|
|
|
38 |
private String HOST = "localhost";
|
|
|
39 |
private Client client = null;
|
|
|
40 |
|
| 68 |
ashish |
41 |
TSocket tsocket = null;
|
|
|
42 |
TTransport transport = null;
|
|
|
43 |
TProtocol protocol = null;
|
| 48 |
ashish |
44 |
|
|
|
45 |
public static ConfigClient getClient(){
|
|
|
46 |
return configClient;
|
|
|
47 |
}
|
|
|
48 |
|
| 68 |
ashish |
49 |
private ConfigClient()throws ConfigException{
|
|
|
50 |
if(!initClient(HOST, PORT_NUMBER)){
|
|
|
51 |
throw new ConfigException(-1, "unable to intialize the config client");
|
|
|
52 |
}
|
| 48 |
ashish |
53 |
}
|
|
|
54 |
|
| 68 |
ashish |
55 |
private boolean initClient(String host, int port){
|
|
|
56 |
tsocket = new TSocket(HOST, PORT_NUMBER);
|
|
|
57 |
transport = new TFramedTransport(tsocket);
|
|
|
58 |
protocol = new TBinaryProtocol(transport);
|
| 48 |
ashish |
59 |
client = new Client(protocol);
|
| 68 |
ashish |
60 |
try {
|
|
|
61 |
transport.open();
|
|
|
62 |
} catch (TTransportException e) {
|
|
|
63 |
Logger.log("unable to open transport for client library"+ e , this);
|
|
|
64 |
return false;
|
|
|
65 |
}
|
| 48 |
ashish |
66 |
Logger.log("Config Client initialized for host "+ host +" port "+ port, this);
|
| 68 |
ashish |
67 |
return true;
|
| 48 |
ashish |
68 |
}
|
|
|
69 |
|
| 68 |
ashish |
70 |
public synchronized String get(String property) throws ConfigException{
|
| 48 |
ashish |
71 |
try {
|
| 68 |
ashish |
72 |
|
| 48 |
ashish |
73 |
return client.getPropetry(property);
|
|
|
74 |
} catch (TException e) {
|
|
|
75 |
e.printStackTrace();
|
|
|
76 |
throw new ConfigException(-1, "Encountered exception while obtaining value for "+ property);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
| 68 |
ashish |
80 |
public synchronized void set(String property, String value) throws ConfigException{
|
| 48 |
ashish |
81 |
try {
|
|
|
82 |
client.loadProperty(property, value);
|
|
|
83 |
} catch (TException e) {
|
|
|
84 |
e.printStackTrace();
|
|
|
85 |
throw new ConfigException(-1, "Encountered exception while putting value "+ value+ "for property "+ property);
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
}
|