| 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 java.io.BufferedReader;
|
|
|
8 |
import java.io.FileNotFoundException;
|
|
|
9 |
import java.io.FileReader;
|
|
|
10 |
import java.net.URL;
|
|
|
11 |
|
|
|
12 |
import org.apache.thrift.TException;
|
|
|
13 |
import org.apache.thrift.protocol.TBinaryProtocol;
|
|
|
14 |
import org.apache.thrift.protocol.TProtocol;
|
|
|
15 |
import org.apache.thrift.transport.TFramedTransport;
|
|
|
16 |
import org.apache.thrift.transport.TSocket;
|
|
|
17 |
import org.apache.thrift.transport.TTransport;
|
| 68 |
ashish |
18 |
import org.apache.thrift.transport.TTransportException;
|
| 48 |
ashish |
19 |
|
|
|
20 |
/**
|
|
|
21 |
* The text in file is like
|
|
|
22 |
* config://localhost:9999
|
|
|
23 |
* @author ashish
|
|
|
24 |
*
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
public class ConfigClient {
|
|
|
28 |
|
|
|
29 |
private static ConfigClient configClient = null;
|
|
|
30 |
|
|
|
31 |
static{
|
|
|
32 |
synchronized (ConfigClient.class) {
|
|
|
33 |
if(configClient == null){
|
| 68 |
ashish |
34 |
try {
|
|
|
35 |
configClient = new ConfigClient();
|
|
|
36 |
} catch (ConfigException e) {
|
|
|
37 |
// TODO Auto-generated catch block
|
|
|
38 |
e.printStackTrace();
|
|
|
39 |
}
|
| 48 |
ashish |
40 |
}
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
private int PORT_NUMBER = 9999;
|
|
|
45 |
private String HOST = "localhost";
|
|
|
46 |
private final String CONFIG_URL_FILE="/tmp/config.properties";
|
|
|
47 |
private Client client = null;
|
|
|
48 |
|
| 68 |
ashish |
49 |
TSocket tsocket = null;
|
|
|
50 |
TTransport transport = null;
|
|
|
51 |
TProtocol protocol = null;
|
| 48 |
ashish |
52 |
|
|
|
53 |
public static ConfigClient getClient(){
|
|
|
54 |
return configClient;
|
|
|
55 |
}
|
|
|
56 |
|
| 68 |
ashish |
57 |
private ConfigClient()throws ConfigException{
|
| 48 |
ashish |
58 |
try {
|
|
|
59 |
URL url = getConfigServerUrl();
|
| 68 |
ashish |
60 |
HOST = url.getHost();
|
|
|
61 |
PORT_NUMBER = url.getPort();
|
| 48 |
ashish |
62 |
} catch (Exception e) {
|
|
|
63 |
Logger.log("Falling back to default options for host and port number ", this);
|
| 68 |
ashish |
64 |
|
| 48 |
ashish |
65 |
}
|
| 68 |
ashish |
66 |
if(!initClient(HOST, PORT_NUMBER)){
|
|
|
67 |
throw new ConfigException(-1, "unable to intialize the config client");
|
|
|
68 |
}
|
| 48 |
ashish |
69 |
}
|
|
|
70 |
|
|
|
71 |
private URL getConfigServerUrl() throws Exception{
|
|
|
72 |
|
|
|
73 |
BufferedReader in = null;
|
|
|
74 |
|
|
|
75 |
try {
|
|
|
76 |
in = new BufferedReader(new FileReader(CONFIG_URL_FILE));
|
|
|
77 |
} catch (FileNotFoundException e) {
|
|
|
78 |
Logger.log("Error reading config file "+ CONFIG_URL_FILE +". Please check if file exists", this);
|
|
|
79 |
e.printStackTrace();
|
|
|
80 |
throw e;
|
|
|
81 |
}
|
|
|
82 |
//We read only first line
|
|
|
83 |
String urlString = in.readLine();
|
|
|
84 |
in.close();
|
|
|
85 |
|
|
|
86 |
URL url = new URL(urlString);
|
|
|
87 |
return url;
|
|
|
88 |
|
|
|
89 |
}
|
|
|
90 |
|
| 68 |
ashish |
91 |
private boolean initClient(String host, int port){
|
|
|
92 |
tsocket = new TSocket(HOST, PORT_NUMBER);
|
|
|
93 |
transport = new TFramedTransport(tsocket);
|
|
|
94 |
protocol = new TBinaryProtocol(transport);
|
| 48 |
ashish |
95 |
client = new Client(protocol);
|
| 68 |
ashish |
96 |
try {
|
|
|
97 |
transport.open();
|
|
|
98 |
} catch (TTransportException e) {
|
|
|
99 |
Logger.log("unable to open transport for client library"+ e , this);
|
|
|
100 |
return false;
|
|
|
101 |
}
|
| 48 |
ashish |
102 |
Logger.log("Config Client initialized for host "+ host +" port "+ port, this);
|
| 68 |
ashish |
103 |
return true;
|
| 48 |
ashish |
104 |
}
|
|
|
105 |
|
| 68 |
ashish |
106 |
public synchronized String get(String property) throws ConfigException{
|
| 48 |
ashish |
107 |
try {
|
| 68 |
ashish |
108 |
|
| 48 |
ashish |
109 |
return client.getPropetry(property);
|
|
|
110 |
} catch (TException e) {
|
|
|
111 |
e.printStackTrace();
|
|
|
112 |
throw new ConfigException(-1, "Encountered exception while obtaining value for "+ property);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
| 68 |
ashish |
116 |
public synchronized void set(String property, String value) throws ConfigException{
|
| 48 |
ashish |
117 |
try {
|
|
|
118 |
client.loadProperty(property, value);
|
|
|
119 |
} catch (TException e) {
|
|
|
120 |
e.printStackTrace();
|
|
|
121 |
throw new ConfigException(-1, "Encountered exception while putting value "+ value+ "for property "+ property);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
}
|