blob: d47783457d5155aca372ac612a0e9aae2c4d9bb7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package com.orbekk.same;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.googlecode.jsonrpc4j.ProxyUtil;
import com.orbekk.paxos.PaxosService;
import java.net.MalformedURLException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* TODO: Implement sequencing of requests. This should be done by
* synchronizing on some object on a per-URL basis. For instance,
* we could extend JsonRpcHttpClient.
*/
public class ConnectionManagerImpl implements ConnectionManager {
private int connectionTimeout;
private int readTimeout;
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* @param connectionTimout Timeout for establishing connection in milliseconds.
* @param readTimeout Timeout for waiting for answer in milliseconds.
*/
public ConnectionManagerImpl(int connectionTimout, int readTimeout) {
this.connectionTimeout = connectionTimout;
this.readTimeout = readTimeout;
}
private <T>T getClassProxy(String url, Class<T> clazz) {
T service = null;
try {
JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(url));
client.setConnectionTimeoutMillis(connectionTimeout);
client.setReadTimeoutMillis(readTimeout);
service = ProxyUtil.createProxy(
this.getClass().getClassLoader(),
clazz,
client);
} catch (MalformedURLException e) {
logger.warn("Unable to create client for {}, {}", url, e);
}
return service;
}
@Override
public ClientService getClient(String url) {
return getClassProxy(url, ClientService.class);
}
@Override
public MasterService getMaster(String url) {
return getClassProxy(url, MasterService.class);
}
@Override
public PaxosService getPaxos(String url) {
return getClassProxy(url, PaxosService.class);
}
}
|