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
;
41 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerUtils
;
48 public class CdmServerInfo
{
49 public static final Logger logger
= Logger
.getLogger(CdmServerInfo
.class);
51 private final static String CDMSERVER_PREFIX
= "cdmserver/";
52 private final static String NAME_PRODUCTION
= "cybertaxonomy.org";
53 private final static String SERVER_PRODUCTION
= "api.cybertaxonomy.org";
55 private final static String NAME_INTEGRATION
= "edit-integration";
56 private final static String SERVER_INTEGRATION
= "int.e-taxonomy.eu";
58 private final static String NAME_DEMO_1
= "edit-WS I";
59 private final static String SERVER_DEMO_1
= "160.45.63.230";
61 private final static String NAME_DEMO_2
= "edit-WS II";
62 private final static String SERVER_DEMO_2
= "160.45.63.231";
64 private final static String NAME_TEST
= "edit-test";
65 private final static String SERVER_TEST
= "test.e-taxonomy.eu";
67 public final static String SERVER_LOCALHOST
= "localhost";
68 private final static String NAME_LOCALHOST
= "localhost";
69 public final static String NAME_LOCALHOST_MGD
= "localhost mgd.";
71 private final static String NAME_LOCALHOST_DEV
= "localhost-dev";
72 private final static String NAME_INSTANCE_LOCALHOST_DEV
= "local-dev";
73 private final static String SERVER_LOCALHOST_DEV
= "localhost";
74 private final static int PORT_LOCALHOST_DEV
= 8080;
75 private final static String BASEPATH_LOCALHOST_DEV
= "";
77 public final static int NULL_PORT
= -1;
78 public final static String NULL_PORT_STRING
= "N/A";
81 private final String name
;
82 private final String server
;
84 private final List
<CdmInstanceInfo
> instances
;
86 private static List
<CdmServerInfo
> cdmServerInfoList
;
88 private String cdmlibServicesVersion
= "";
89 private String cdmlibServicesLastModified
= "";
91 private String prefix
= "";
93 private boolean ignoreCdmLibVersion
= false;
96 public CdmServerInfo(String name
, String server
, int port
, String prefix
, boolean ignoreCdmLibVersion
) {
100 this.prefix
= prefix
;
101 this.ignoreCdmLibVersion
= ignoreCdmLibVersion
;
102 instances
= new ArrayList
<CdmInstanceInfo
>();
107 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
108 String _basePath
= basePath
;
109 if(isLocalhostMgd()) {
112 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, _basePath
);
118 public boolean isLocalhost() {
119 return name
.startsWith(SERVER_LOCALHOST
);
122 public boolean isLocalhostMgd() {
123 return NAME_LOCALHOST_MGD
.equals(name
);
126 public String
getCdmlibServicesVersion() {
127 return cdmlibServicesVersion
;
130 public String
getCdmlibLastModified() {
131 return cdmlibServicesLastModified
;
134 public void refreshInstances() throws CDMServerException
{
136 if(isLocalhostMgd()) {
137 addInstancesFromDataSourcesConfig();
139 addInstancesViaHttp();
141 Collections
.sort(instances
, new Comparator
<CdmInstanceInfo
>() {
143 public int compare(CdmInstanceInfo cii1
, CdmInstanceInfo cii2
)
145 return cii1
.getName().toString().compareTo(cii2
.getName().toString());
150 public void updateInfo() throws CDMServerException
{
151 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + prefix
+ "info.jsp";
152 String responseBody
= getResponse(url
);
153 if(responseBody
!= null) {
155 JSONObject info
= new JSONObject(responseBody
);
156 cdmlibServicesVersion
= info
.getString("cdmlibServicesVersion");
157 cdmlibServicesLastModified
= info
.getString("cdmlibServicesLastModified");
158 } catch (JSONException e
) {
159 throw new CDMServerException(e
);
164 public void addInstancesViaHttp() throws CDMServerException
{
166 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + prefix
+ "instances.jsp";
167 String responseBody
= getResponse(url
);
168 if(responseBody
!= null) {
170 JSONArray array
= new JSONArray(responseBody
);
171 for(int i
=0;i
<array
.length();i
++) {
172 JSONObject instance
= (JSONObject
)array
.get(i
);
173 if(instance
!= null) {
174 JSONObject conf
= (JSONObject
)instance
.get("configuration");
176 String instanceName
= conf
.getString("instanceName");
177 // we need to remove the first (char) forward slash from
179 String basePath
= conf
.getString("basePath").substring(1);
180 addInstance(instanceName
, basePath
);
181 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
185 } catch (JSONException e
) {
186 throw new CDMServerException(e
);
191 private String
getResponse(String url
) throws CDMServerException
{
192 HttpClient client
= new DefaultHttpClient();
193 HttpParams params
= client
.getParams();
195 HttpConnectionParams
.setConnectionTimeout(params
, 5000);
196 HttpConnectionParams
.setSoTimeout(params
, 5000);
198 HttpGet httpGet
= new HttpGet(url
);
200 logger
.info("Executing request " + httpGet
.getRequestLine());
202 // Create a custom response handler
203 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
206 public String
handleResponse(
207 final HttpResponse response
) throws ClientProtocolException
, IOException
{
208 int status
= response
.getStatusLine().getStatusCode();
209 if (status
>= 200 && status
< 300) {
210 HttpEntity entity
= response
.getEntity();
211 return entity
!= null ? EntityUtils
.toString(entity
) : null;
213 throw new ClientProtocolException("Unexpected response status: " + status
);
218 String responseBody
= null;
220 responseBody
= client
.execute(httpGet
, responseHandler
);
221 } catch (ClientProtocolException e
) {
222 throw new CDMServerException(e
);
223 } catch (IOException e
) {
224 throw new CDMServerException(e
);
229 public void addInstancesFromDataSourcesConfig() {
230 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
231 String datasourceNCName
= CDMServerUtils
.xmlNCNamefrom(dataSource
.getName());
232 logger
.info("Adding local instance " + dataSource
.getName() + " as " + datasourceNCName
);
233 addInstance(datasourceNCName
, datasourceNCName
);
237 public String
toString(String instanceName
, int port
) {
238 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
241 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
242 if(instanceName
== null) {
246 for(CdmInstanceInfo instance
: instances
) {
247 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
254 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
255 if(instance
!= null) {
256 return CdmRemoteSource
.NewInstance(name
,
259 instance
.getBasePath(),
265 public boolean pingServer() {
266 if(isLocalhostMgd()) {
270 Socket s
= new Socket(server
, port
);
271 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
274 } catch (IOException ioe
) {
276 } catch (CDMServerException e
) {
282 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
284 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
286 if(crs
!= null && crs
.checkConnection()) {
287 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
290 } catch (CdmSourceException e
) {
291 throw new CDMServerException(e
);
297 public int compareDbSchemaVersion(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
299 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
300 String dbSchemaVersion
;
302 dbSchemaVersion
= crs
.getDbSchemaVersion();
303 } catch (CdmSourceException e
) {
304 throw new CDMServerException(e
);
308 if(dbSchemaVersion
!= null) {
309 return CdmMetaData
.compareVersion(dbSchemaVersion
, CdmMetaData
.getDbSchemaVersion(), 3, null);
311 throw new CDMServerException("Cannot determine editor db. schema version");
315 public int compareCdmlibServicesVersion() throws CdmSourceException
{
317 String serverVersion
= cdmlibServicesVersion
;
318 String serverCdmlibLastModified
= cdmlibServicesLastModified
;
319 if(ignoreCdmLibVersion
) {
322 return compareCdmlibServicesVersion(serverVersion
, serverCdmlibLastModified
);
328 * @param serverVersion
329 * @param editorVersion
330 * @throws CdmSourceException
332 public static int compareCdmlibServicesVersion(String serverVersion
, String serverCdmlibLastModified
) throws CdmSourceException
{
334 String editorVersion
= CdmApplicationState
.getCdmlibVersion();
335 String editorCdmlibLastModified
= CdmApplicationState
.getCdmlibLastModified();
338 if(StringUtils
.isBlank(serverVersion
) || StringUtils
.isBlank(editorVersion
)) {
339 throw new CdmSourceException("cdmlib-services server or editor version is empty");
342 String
[] serverVersionSplit
= serverVersion
.split("\\.");
343 String
[] editorVersionSplit
= editorVersion
.split("\\.");
345 if(serverVersionSplit
.length
< 3 || editorVersionSplit
.length
< 3 || serverVersionSplit
.length
> 4 || editorVersionSplit
.length
> 4) {
346 throw new CdmSourceException("cdmlib-services server or editor version is invalid");
349 Integer serverVersionPart
;
350 Integer editorVersionPart
;
352 for(int i
=0 ; i
<3 ; i
++) {
353 serverVersionPart
= Integer
.valueOf(serverVersionSplit
[i
]);
354 editorVersionPart
= Integer
.valueOf(editorVersionSplit
[i
]);
356 int partCompare
= serverVersionPart
.compareTo(editorVersionPart
);
357 if (partCompare
!= 0){
361 // at this point major, minor and patch versions are matching
363 if(StringUtils
.isBlank(serverCdmlibLastModified
) || StringUtils
.isBlank(editorCdmlibLastModified
)) {
364 throw new CdmSourceException("cdmlib-services server or editor version is empty");
367 String cdmServerIgnoreVersion
= System
.getProperty("cdm.server.version.lm.ignore");
368 if(StringUtils
.isBlank(cdmServerIgnoreVersion
) || !cdmServerIgnoreVersion
.equals("true")) {
369 Long serverLastModified
= Long
.valueOf(serverCdmlibLastModified
);
370 Long editorLastModified
= Long
.valueOf(editorCdmlibLastModified
);
371 return serverLastModified
.compareTo(editorLastModified
);
379 public static List
<CdmServerInfo
> getCdmServers() {
380 if(cdmServerInfoList
== null) {
381 cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
382 // cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80, ""));
383 //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80, CDMSERVER_PREFIX));
384 cdmServerInfoList
.add(new CdmServerInfo(NAME_DEMO_1
, SERVER_DEMO_1
, 80, CDMSERVER_PREFIX
, false));
385 // cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_2, SERVER_DEMO_2, 80, CDMSERVER_PREFIX));
386 cdmServerInfoList
.add(new CdmServerInfo(NAME_TEST
, SERVER_TEST
, 80, CDMSERVER_PREFIX
, false));
387 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST
, SERVER_LOCALHOST
, 8080, CDMSERVER_PREFIX
, true));
388 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
, NULL_PORT
, CDMSERVER_PREFIX
, false));
390 return cdmServerInfoList
;
393 public String
getName() {
397 public String
getServer() {
402 public int getPort() {
406 public void setPort(int port
) {
410 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
411 if(instances
.isEmpty()) {
417 public static CdmRemoteSource
getDevServerRemoteSource() {
418 String value
= System
.getProperty("cdm.server.dev.port");
419 boolean available
= false;
420 CdmInstanceInfo devInstance
= null;
421 if(value
!= null && !value
.isEmpty()) {
422 int devPort
= Integer
.valueOf(value
);
423 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
, "", false);
425 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
426 available
= devCii
.pingInstance(devInstance
, devPort
);
428 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
430 } catch (Exception e
) {
437 public class CdmInstanceInfo
{
438 private final String name
;
441 * The full path of the instance including the the prefix (if any).
442 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
443 * For a managed local server this would simply be ""
445 private final String basePath
;
448 public CdmInstanceInfo(String name
, String basePath
) {
450 this.basePath
= basePath
;
454 public String
getName() {
458 public String
getBasePath() {