Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | 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
 
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;
18
 
19
/**
20
 * The text in file is like 
21
 * config://localhost:9999
22
 * @author ashish
23
 *
24
 */
25
 
26
public class ConfigClient {
27
 
28
	private static ConfigClient configClient = null;
29
 
30
	static{
31
		synchronized (ConfigClient.class) {
32
			if(configClient == null){
33
				configClient = new ConfigClient();
34
			}
35
		}
36
	}
37
 
38
	private int PORT_NUMBER = 9999;
39
	private String HOST = "localhost";
40
	private final String CONFIG_URL_FILE="/tmp/config.properties";
41
	private Client client = null;
42
 
43
 
44
	public static ConfigClient getClient(){
45
		return configClient;
46
	}
47
 
48
	private ConfigClient(){
49
		try {
50
			URL url = getConfigServerUrl();
51
			initClient(url.getHost(), url.getPort());
52
		} catch (Exception e) {
53
			Logger.log("Falling back to default options for host and port number ", this);
54
			initClient(HOST, PORT_NUMBER);
55
		}
56
	}
57
 
58
	private URL getConfigServerUrl() throws Exception{
59
 
60
		BufferedReader in = null;
61
 
62
		try {
63
			in = new BufferedReader(new FileReader(CONFIG_URL_FILE));
64
		} catch (FileNotFoundException e) {
65
			Logger.log("Error reading config file "+ CONFIG_URL_FILE +". Please check if file exists", this);
66
			e.printStackTrace();
67
			throw e;
68
		}
69
		//We read only first line
70
		String urlString = in.readLine();
71
		in.close();
72
 
73
		URL url = new URL(urlString);
74
		return url;
75
 
76
	}
77
 
78
	private void initClient(String host, int port){
79
		final TSocket tsocket = new TSocket(host, port);
80
		final TTransport transport = new TFramedTransport(tsocket);
81
		final TProtocol protocol = new TBinaryProtocol(transport);
82
		client = new Client(protocol);
83
		Logger.log("Config Client initialized for host "+ host +" port "+ port, this);
84
	}
85
 
86
	public String get(String property) throws ConfigException{
87
		try {
88
			return client.getPropetry(property);
89
		} catch (TException e) {
90
			e.printStackTrace();
91
			throw new ConfigException(-1, "Encountered exception while obtaining value for "+ property);
92
		}
93
	}
94
 
95
	public void set(String property, String value) throws ConfigException{
96
		try {
97
			client.loadProperty(property, value);
98
		} catch (TException e) {
99
			e.printStackTrace();
100
			throw new ConfigException(-1, "Encountered exception while putting value "+ value+ "for property "+ property);
101
		}
102
	}
103
}