Project

General

Profile

Download (10.3 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.taxeditor.remoting.source;
11

    
12
import java.io.IOException;
13
import java.net.Socket;
14
import java.util.ArrayList;
15
import java.util.Collections;
16
import java.util.Comparator;
17
import java.util.List;
18

    
19
import org.apache.http.HttpEntity;
20
import org.apache.http.HttpResponse;
21
import org.apache.http.client.ClientProtocolException;
22
import org.apache.http.client.HttpClient;
23
import org.apache.http.client.ResponseHandler;
24
import org.apache.http.client.methods.HttpGet;
25
import org.apache.http.impl.client.DefaultHttpClient;
26
import org.apache.http.util.EntityUtils;
27
import org.apache.log4j.Logger;
28
import org.json.JSONArray;
29
import org.json.JSONException;
30
import org.json.JSONObject;
31

    
32
import eu.etaxonomy.cdm.config.CdmSourceException;
33
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
34
import eu.etaxonomy.cdm.database.ICdmDataSource;
35
import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
36

    
37
/**
38
 * @author cmathew
39
 * @date 20 Jan 2015
40
 *
41
 */
42
public class CdmServerInfo {
43
    public static final Logger logger = Logger.getLogger(CdmServerInfo.class);
44

    
45
    private final static String CDMSERVER_PREFIX = "cdmserver";
46
    private final static String NAME_PRODUCTION = "edit-production";
47
    private final static String SERVER_PRODUCTION = "dev.e-taxonomy.eu";
48

    
49
    private final static String NAME_INTEGRATION = "edit-integration";
50
    private final static String SERVER_INTEGRATION = "int.e-taxonomy.eu";
51

    
52
    private final static String NAME_TEST = "edit-test";
53
    private final static String SERVER_TEST = "test.e-taxonomy.eu";
54

    
55
    public final static String SERVER_LOCALHOST = "localhost";
56
    private final static String NAME_LOCALHOST = "localhost";
57
    public final static String NAME_LOCALHOST_MGD = "localhost mgd.";
58

    
59
    private final static String NAME_LOCALHOST_DEV = "localhost-dev";
60
    private final static String NAME_INSTANCE_LOCALHOST_DEV = "local-dev";
61
    private final static String SERVER_LOCALHOST_DEV = "localhost";
62
    private final static int PORT_LOCALHOST_DEV = 8080;
63
    private final static String BASEPATH_LOCALHOST_DEV = "";
64

    
65
    private final String name;
66
    private final String server;
67
    private final int port;
68
    private final List<CdmInstanceInfo> instances;
69

    
70
    private static List<CdmServerInfo> cdmServerInfoList;
71

    
72
    public CdmServerInfo(String name, String server, int port) {
73
        this.name = name;
74
        this.server = server;
75
        this.port = port;
76
        instances = new ArrayList<CdmInstanceInfo>();
77
    }
78

    
79

    
80
    public CdmInstanceInfo addInstance(String name, String basePath) {
81
        CdmInstanceInfo cii = new CdmInstanceInfo(name, basePath);
82
        instances.add(cii);
83
        return cii;
84

    
85
    }
86

    
87
    public boolean isLocalhost() {
88
        return name.startsWith(SERVER_LOCALHOST);
89
    }
90

    
91
    public boolean isLocalhostMgd() {
92
        return NAME_LOCALHOST_MGD.equals(name);
93
    }
94

    
95
    public void refreshInstances() throws CDMServerException {
96
        instances.clear();
97
        if(isLocalhostMgd()) {
98
            addInstancesFromDataSourcesConfig();
99
        } else {
100
            addInstancesViaHttp();
101
        }
102
        Collections.sort(instances, new Comparator<CdmInstanceInfo>() {
103
            @Override
104
            public int compare(CdmInstanceInfo cii1, CdmInstanceInfo cii2)
105
            {
106
                return cii1.getName().toString().compareTo(cii2.getName().toString());
107
            }
108
        });
109
    }
110

    
111
    public void addInstancesViaHttp() throws CDMServerException {
112
        String url = "http://" + server + ":" + String.valueOf(port) + "/" + CDMSERVER_PREFIX + "/instances.jsp";
113

    
114
        HttpClient client = new DefaultHttpClient();
115
        HttpGet httpGet = new HttpGet(url);
116

    
117
        logger.info("Executing request " + httpGet.getRequestLine());
118

    
119
        // Create a custom response handler
120
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
121

    
122
            @Override
123
            public String handleResponse(
124
                    final HttpResponse response) throws ClientProtocolException, IOException {
125
                int status = response.getStatusLine().getStatusCode();
126
                if (status >= 200 && status < 300) {
127
                    HttpEntity entity = response.getEntity();
128
                    return entity != null ? EntityUtils.toString(entity) : null;
129
                } else {
130
                    throw new ClientProtocolException("Unexpected response status: " + status);
131
                }
132
            }
133

    
134
        };
135
        String responseBody = null;
136
        try {
137
            responseBody = client.execute(httpGet, responseHandler);
138
        } catch (ClientProtocolException e) {
139
            throw new CDMServerException(e);
140
        } catch (IOException e) {
141
            throw new CDMServerException(e);
142
        }
143

    
144

    
145
        if(responseBody != null) {
146
            try {
147
                JSONArray array = new JSONArray(responseBody);
148
                for(int i=0;i<array.length();i++) {
149
                    JSONObject instance = (JSONObject)array.get(i);
150
                    if(instance != null) {
151
                        JSONObject conf = (JSONObject)instance.get("configuration");
152
                        if(conf != null) {
153
                            String instanceName = conf.getString("instanceName");
154
                            // we need to remove the first (char) forward slash from
155
                            // the base path
156
                            String basePath = conf.getString("basePath").substring(1);
157
                            addInstance(instanceName, basePath);
158
                            logger.info("Added instance with name : " + instanceName + ", basePath : " + basePath);
159
                        }
160
                    }
161
                }
162
            } catch (JSONException e) {
163
                throw new CDMServerException(e);
164
            }
165
        }
166
    }
167

    
168
    public void addInstancesFromDataSourcesConfig() {
169

    
170
        for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
171
            logger.warn("Adding local instance " + dataSource.getName());
172
            addInstance(dataSource.getName(), dataSource.getName());
173
        }
174

    
175
    }
176

    
177
    public String toString(String instanceName, int port) {
178
        return server + ":" + String.valueOf(port) + "/" + instanceName;
179
    }
180

    
181
    public CdmInstanceInfo getInstanceFromName(String instanceName) {
182
        if(instanceName == null) {
183
            return null;
184
        }
185

    
186
        for(CdmInstanceInfo instance : instances) {
187
            if(instance.getName() != null && instance.getName().equals(instanceName)) {
188
                return instance;
189
            }
190
        }
191
        return null;
192
    }
193

    
194
    public CdmRemoteSource getCdmRemoteSource(CdmInstanceInfo instance, int port) {
195
        if(instance != null) {
196
            return CdmRemoteSource.NewInstance(name,
197
                    server,
198
                    port,
199
                    instance.getBasePath(),
200
                    null);
201
        }
202
        return null;
203
    }
204

    
205
    public boolean pingServer(){
206
        try {
207
            Socket s = new Socket(server, port);
208
            logger.info("[CDM-Server] Available @ " + server + ":" + port );
209
            return true;
210
        } catch (IOException ioe) {
211

    
212
        }
213
        return false;
214
    }
215

    
216
    public boolean pingInstance(CdmInstanceInfo instance, int port) throws CDMServerException  {
217

    
218
        ICdmRemoteSource crs = getCdmRemoteSource(instance, port);
219
        try {
220
            if(crs != null && crs.checkConnection()) {
221
                logger.info("[CDM-Server] Running @ " + server + ":" + port + " for instance " + instance);
222
                return true;
223
            }
224
        } catch (CdmSourceException e) {
225
            throw new CDMServerException(e);
226
        }
227

    
228
        return false;
229
    }
230

    
231
    public static List<CdmServerInfo> getCdmServers() {
232
        if(cdmServerInfoList == null) {
233
            cdmServerInfoList = new ArrayList<CdmServerInfo>();
234
            cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80));
235
            //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
236
            cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
237
            cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
238
            cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST_MGD, SERVER_LOCALHOST,8080));
239
        }
240
        return cdmServerInfoList;
241
    }
242

    
243
    public String getName() {
244
        return name;
245
    }
246

    
247
    public String getServer() {
248
        return server;
249
    }
250

    
251

    
252
    public int getPort() {
253
        return port;
254
    }
255

    
256

    
257
    public List<CdmInstanceInfo> getInstances() throws CDMServerException {
258
        if(instances.isEmpty()) {
259
            refreshInstances();
260
        }
261
        return instances;
262
    }
263

    
264
    public static CdmRemoteSource getDevServerRemoteSource() {
265
        String value = System.getProperty("cdm.server.dev.port");
266
        boolean available = false;
267
        CdmInstanceInfo devInstance = null;
268
        if(value != null && !value.isEmpty()) {
269
            int devPort = Integer.valueOf(value);
270
            CdmServerInfo devCii = new CdmServerInfo(NAME_LOCALHOST_DEV, SERVER_LOCALHOST_DEV, devPort);
271
            try {
272
                devInstance = devCii.addInstance(NAME_INSTANCE_LOCALHOST_DEV, BASEPATH_LOCALHOST_DEV);
273
                available = devCii.pingInstance(devInstance, devPort);
274
                if(available) {
275
                    return devCii.getCdmRemoteSource(devInstance, devPort);
276
                }
277
            } catch (Exception e) {
278

    
279
            }
280
        }
281
        return null;
282
    }
283

    
284
    public class CdmInstanceInfo {
285
        private final String name;
286

    
287
        /**
288
         * The full path of the instance including the the prefix (if any).
289
         * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
290
         * For a managed local server this would simply be "remoting"
291
         */
292
        private final String basePath;
293

    
294

    
295
        public CdmInstanceInfo(String name, String basePath) {
296
            this.name = name;
297
            this.basePath = basePath;
298
        }
299

    
300

    
301
        public String getName() {
302
            return name;
303
        }
304

    
305
        public String getBasePath() {
306
            return basePath;
307
        }
308
    }
309
}
(5-5/6)