Subversion Repositories SmartDukaan

Rev

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

Rev 1021 Rev 3258
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.io.BufferedReader;
-
 
8
import java.io.FileNotFoundException;
-
 
9
import java.io.FileReader;
-
 
10
import java.net.URL;
-
 
11
 
-
 
12
import org.apache.thrift.TException;
7
import org.apache.thrift.TException;
13
import org.apache.thrift.protocol.TBinaryProtocol;
8
import org.apache.thrift.protocol.TBinaryProtocol;
14
import org.apache.thrift.protocol.TProtocol;
9
import org.apache.thrift.protocol.TProtocol;
15
import org.apache.thrift.transport.TFramedTransport;
10
import org.apache.thrift.transport.TFramedTransport;
16
import org.apache.thrift.transport.TSocket;
11
import org.apache.thrift.transport.TSocket;
17
import org.apache.thrift.transport.TTransport;
12
import org.apache.thrift.transport.TTransport;
18
import org.apache.thrift.transport.TTransportException;
13
import org.apache.thrift.transport.TTransportException;
19
 
14
 
20
/**
15
/**
21
 * The text in file is like 
-
 
22
 * config://localhost:9999
-
 
23
 * @author ashish
16
 * @author ashish
24
 *
17
 *
25
 */
18
 */
26
 
19
 
27
public class ConfigClient {
20
public class ConfigClient {
Line 41... Line 34...
41
		}
34
		}
42
	}
35
	}
43
 
36
 
44
	private int PORT_NUMBER = 9999;
37
	private int PORT_NUMBER = 9999;
45
	private String HOST = "localhost";
38
	private String HOST = "localhost";
46
	private final String CONFIG_URL_FILE="/tmp/config.properties";
-
 
47
	private Client client = null;
39
	private Client client = null;
48
	
40
	
49
	TSocket tsocket = null;
41
	TSocket tsocket = null;
50
	TTransport transport = null;
42
	TTransport transport = null;
51
	TProtocol protocol = null;
43
	TProtocol protocol = null;
Line 53... Line 45...
53
	public static ConfigClient getClient(){
45
	public static ConfigClient getClient(){
54
		return configClient;
46
		return configClient;
55
	}
47
	}
56
	
48
	
57
	private ConfigClient()throws ConfigException{
49
	private ConfigClient()throws ConfigException{
58
		try {
-
 
59
			URL url = getConfigServerUrl();
-
 
60
			HOST = url.getHost();
-
 
61
			PORT_NUMBER = url.getPort();
-
 
62
		} catch (Exception e) {
-
 
63
			Logger.log("Falling back to default options for host and port number ", this);
-
 
64
			
-
 
65
		}
-
 
66
		if(!initClient(HOST, PORT_NUMBER)){
50
		if(!initClient(HOST, PORT_NUMBER)){
67
			throw new ConfigException(-1, "unable to intialize the config client");
51
			throw new ConfigException(-1, "unable to intialize the config client");
68
		}
52
		}
69
	}
53
	}
70
	
54
	
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
	
-
 
91
	private boolean initClient(String host, int port){
55
	private boolean initClient(String host, int port){
92
		tsocket = new TSocket(HOST, PORT_NUMBER);
56
		tsocket = new TSocket(HOST, PORT_NUMBER);
93
		transport = new TFramedTransport(tsocket);
57
		transport = new TFramedTransport(tsocket);
94
		protocol = new TBinaryProtocol(transport);
58
		protocol = new TBinaryProtocol(transport);
95
		client = new Client(protocol);
59
		client = new Client(protocol);