3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.remoting
.source
;
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
;
19 import org
.apache
.commons
.lang
.StringUtils
;
20 import org
.apache
.http
.HttpEntity
;
21 import org
.apache
.http
.HttpResponse
;
22 import org
.apache
.http
.client
.ClientProtocolException
;
23 import org
.apache
.http
.client
.HttpClient
;
24 import org
.apache
.http
.client
.ResponseHandler
;
25 import org
.apache
.http
.client
.methods
.HttpGet
;
26 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
27 import org
.apache
.http
.params
.HttpConnectionParams
;
28 import org
.apache
.http
.params
.HttpParams
;
29 import org
.apache
.http
.util
.EntityUtils
;
30 import org
.apache
.log4j
.Logger
;
31 import org
.json
.JSONArray
;
32 import org
.json
.JSONException
;
33 import org
.json
.JSONObject
;
35 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
36 import eu
.etaxonomy
.cdm
.config
.CdmSourceException
;
37 import eu
.etaxonomy
.cdm
.database
.CdmPersistentDataSource
;
38 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
39 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
40 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerException
;
47 public class CdmServerInfo
{
48 public static final Logger logger
= Logger
.getLogger(CdmServerInfo
.class);
50 private final static String CDMSERVER_PREFIX
= "cdmserver";
51 private final static String NAME_PRODUCTION
= "edit-production";
52 private final static String SERVER_PRODUCTION
= "dev.e-taxonomy.eu";
54 private final static String NAME_INTEGRATION
= "edit-integration";
55 private final static String SERVER_INTEGRATION
= "int.e-taxonomy.eu";
57 private final static String NAME_DEMO_1
= "edit-WS I";
58 private final static String SERVER_DEMO_1
= "160.45.63.230";
60 private final static String NAME_DEMO_2
= "edit-WS II";
61 private final static String SERVER_DEMO_2
= "160.45.63.231";
63 private final static String NAME_TEST
= "edit-test";
64 private final static String SERVER_TEST
= "test.e-taxonomy.eu";
66 public final static String SERVER_LOCALHOST
= "localhost";
67 private final static String NAME_LOCALHOST
= "localhost";
68 public final static String NAME_LOCALHOST_MGD
= "localhost mgd.";
70 private final static String NAME_LOCALHOST_DEV
= "localhost-dev";
71 private final static String NAME_INSTANCE_LOCALHOST_DEV
= "local-dev";
72 private final static String SERVER_LOCALHOST_DEV
= "localhost";
73 private final static int PORT_LOCALHOST_DEV
= 8080;
74 private final static String BASEPATH_LOCALHOST_DEV
= "";
76 private final String name
;
77 private final String server
;
78 private final int port
;
79 private final List
<CdmInstanceInfo
> instances
;
81 private static List
<CdmServerInfo
> cdmServerInfoList
;
83 private String cdmlibServicesVersion
= "";
84 private String cdmlibServicesLastModified
= "";
87 public CdmServerInfo(String name
, String server
, int port
) {
91 instances
= new ArrayList
<CdmInstanceInfo
>();
96 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
97 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, basePath
);
103 public boolean isLocalhost() {
104 return name
.startsWith(SERVER_LOCALHOST
);
107 public boolean isLocalhostMgd() {
108 return NAME_LOCALHOST_MGD
.equals(name
);
111 public String
getCdmlibServicesVersion() {
112 return cdmlibServicesVersion
;
115 public String
getCdmlibLastModified() {
116 return cdmlibServicesLastModified
;
119 public void refreshInstances() throws CDMServerException
{
121 if(isLocalhostMgd()) {
122 addInstancesFromDataSourcesConfig();
124 addInstancesViaHttp();
126 Collections
.sort(instances
, new Comparator
<CdmInstanceInfo
>() {
128 public int compare(CdmInstanceInfo cii1
, CdmInstanceInfo cii2
)
130 return cii1
.getName().toString().compareTo(cii2
.getName().toString());
135 public void updateInfo() throws CDMServerException
{
136 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/info.jsp";
137 String responseBody
= getResponse(url
);
138 if(responseBody
!= null) {
140 JSONObject info
= new JSONObject(responseBody
);
141 cdmlibServicesVersion
= info
.getString("cdmlibServicesVersion");
142 cdmlibServicesLastModified
= info
.getString("cdmlibServicesLastModified");
143 } catch (JSONException e
) {
144 throw new CDMServerException(e
);
149 public void addInstancesViaHttp() throws CDMServerException
{
151 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/instances.jsp";
152 String responseBody
= getResponse(url
);
153 if(responseBody
!= null) {
155 JSONArray array
= new JSONArray(responseBody
);
156 for(int i
=0;i
<array
.length();i
++) {
157 JSONObject instance
= (JSONObject
)array
.get(i
);
158 if(instance
!= null) {
159 JSONObject conf
= (JSONObject
)instance
.get("configuration");
161 String instanceName
= conf
.getString("instanceName");
162 // we need to remove the first (char) forward slash from
164 String basePath
= conf
.getString("basePath").substring(1);
165 addInstance(instanceName
, basePath
);
166 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
170 } catch (JSONException e
) {
171 throw new CDMServerException(e
);
176 private String
getResponse(String url
) throws CDMServerException
{
177 HttpClient client
= new DefaultHttpClient();
178 HttpParams params
= client
.getParams();
180 HttpConnectionParams
.setConnectionTimeout(params
, 5000);
181 HttpConnectionParams
.setSoTimeout(params
, 5000);
183 HttpGet httpGet
= new HttpGet(url
);
185 logger
.info("Executing request " + httpGet
.getRequestLine());
187 // Create a custom response handler
188 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
191 public String
handleResponse(
192 final HttpResponse response
) throws ClientProtocolException
, IOException
{
193 int status
= response
.getStatusLine().getStatusCode();
194 if (status
>= 200 && status
< 300) {
195 HttpEntity entity
= response
.getEntity();
196 return entity
!= null ? EntityUtils
.toString(entity
) : null;
198 throw new ClientProtocolException("Unexpected response status: " + status
);
203 String responseBody
= null;
205 responseBody
= client
.execute(httpGet
, responseHandler
);
206 } catch (ClientProtocolException e
) {
207 throw new CDMServerException(e
);
208 } catch (IOException e
) {
209 throw new CDMServerException(e
);
214 public void addInstancesFromDataSourcesConfig() {
216 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
217 logger
.warn("Adding local instance " + dataSource
.getName());
218 addInstance(dataSource
.getName(), dataSource
.getName());
223 public String
toString(String instanceName
, int port
) {
224 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
227 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
228 if(instanceName
== null) {
232 for(CdmInstanceInfo instance
: instances
) {
233 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
240 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
241 if(instance
!= null) {
242 return CdmRemoteSource
.NewInstance(name
,
245 instance
.getBasePath(),
251 public boolean pingServer() {
253 Socket s
= new Socket(server
, port
);
254 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
257 } catch (IOException ioe
) {
259 } catch (CDMServerException e
) {
265 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
267 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
269 if(crs
!= null && crs
.checkConnection()) {
270 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
273 } catch (CdmSourceException e
) {
274 throw new CDMServerException(e
);
280 public int compareDbSchemaVersion(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
282 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
283 String dbSchemaVersion
;
285 dbSchemaVersion
= crs
.getDbSchemaVersion();
286 } catch (CdmSourceException e
) {
287 throw new CDMServerException(e
);
291 if(dbSchemaVersion
!= null) {
292 return CdmMetaData
.compareVersion(dbSchemaVersion
, CdmMetaData
.getDbSchemaVersion(), 3, null);
294 throw new CDMServerException("Cannot determine editor db. schema version");
298 public int compareCdmlibServicesVersion() throws CdmSourceException
{
300 String serverVersion
= cdmlibServicesVersion
;
301 String editorVersion
= CdmApplicationState
.getCdmlibVersion();
303 if(StringUtils
.isBlank(serverVersion
) || StringUtils
.isBlank(editorVersion
)) {
304 throw new CdmSourceException("cdmlib-services server or editor version is empty");
307 String
[] serverVersionSplit
= serverVersion
.split("\\.");
308 String
[] editorVersionSplit
= editorVersion
.split("\\.");
310 if(serverVersionSplit
.length
< 3 || editorVersionSplit
.length
< 3 || serverVersionSplit
.length
> 4 || editorVersionSplit
.length
> 4) {
311 throw new CdmSourceException("cdmlib-services server or editor version is invalid");
314 Integer serverVersionPart
;
315 Integer editorVersionPart
;
317 for(int i
=0 ; i
<3 ; i
++) {
318 serverVersionPart
= Integer
.valueOf(serverVersionSplit
[i
]);
319 editorVersionPart
= Integer
.valueOf(editorVersionSplit
[i
]);
321 int partCompare
= serverVersionPart
.compareTo(editorVersionPart
);
322 if (partCompare
!= 0){
326 // at this point major, minor and patch versions are matching
328 if(StringUtils
.isBlank(cdmlibServicesLastModified
) || StringUtils
.isBlank(CdmApplicationState
.getCdmlibLastModified())) {
329 throw new CdmSourceException("cdmlib-services server or editor version is empty");
332 String cdmServerIgnoreVersion
= System
.getProperty("cdm.server.version.lm.ignore");
333 if(StringUtils
.isBlank(cdmServerIgnoreVersion
) || !cdmServerIgnoreVersion
.equals("true")) {
334 Long serverLastModified
= Long
.valueOf(cdmlibServicesLastModified
);
335 Long editorLastModified
= Long
.valueOf(CdmApplicationState
.getCdmlibLastModified());
336 return serverLastModified
.compareTo(editorLastModified
);
343 public static List
<CdmServerInfo
> getCdmServers() {
344 if(cdmServerInfoList
== null) {
345 cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
346 //cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80));
347 //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
348 cdmServerInfoList
.add(new CdmServerInfo(NAME_DEMO_1
, SERVER_DEMO_1
, 80));
349 cdmServerInfoList
.add(new CdmServerInfo(NAME_DEMO_2
, SERVER_DEMO_2
, 80));
350 cdmServerInfoList
.add(new CdmServerInfo(NAME_TEST
, SERVER_TEST
, 80));
351 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST
, SERVER_LOCALHOST
, 8080));
352 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,8080));
354 return cdmServerInfoList
;
357 public String
getName() {
361 public String
getServer() {
366 public int getPort() {
371 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
372 if(instances
.isEmpty()) {
378 public static CdmRemoteSource
getDevServerRemoteSource() {
379 String value
= System
.getProperty("cdm.server.dev.port");
380 boolean available
= false;
381 CdmInstanceInfo devInstance
= null;
382 if(value
!= null && !value
.isEmpty()) {
383 int devPort
= Integer
.valueOf(value
);
384 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
);
386 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
387 available
= devCii
.pingInstance(devInstance
, devPort
);
389 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
391 } catch (Exception e
) {
398 public class CdmInstanceInfo
{
399 private final String name
;
402 * The full path of the instance including the the prefix (if any).
403 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
404 * For a managed local server this would simply be "remoting"
406 private final String basePath
;
409 public CdmInstanceInfo(String name
, String basePath
) {
411 this.basePath
= basePath
;
415 public String
getName() {
419 public String
getBasePath() {