summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java
blob: 0a1e54fe0e8ee77ff7f22fe70d8364abd1e5693a (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
package com.orbekk.same;

import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.googlecode.jsonrpc4j.ProxyUtil;
import java.net.MalformedURLException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConnectionManagerImpl implements ConnectionManager {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public ConnectionManagerImpl() {
    }

    private <T>T getClassProxy(String url, Class<T> clazz) {
        T service = null;
        try {
            JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(url));
            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);
    }
}