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 public final static int NULL_PORT
= -1;
77 public final static String NULL_PORT_STRING
= "N/A";
80 private final String name
;
81 private final String server
;
83 private final List
<CdmInstanceInfo
> instances
;
85 private static List
<CdmServerInfo
> cdmServerInfoList
;
87 private String cdmlibServicesVersion
= "";
88 private String cdmlibServicesLastModified
= "";
91 public CdmServerInfo(String name
, String server
, int port
) {
95 instances
= new ArrayList
<CdmInstanceInfo
>();
100 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
101 String _basePath
= basePath
;
102 if(isLocalhostMgd()) {
105 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, _basePath
);
111 public boolean isLocalhost() {
112 return name
.startsWith(SERVER_LOCALHOST
);
115 public boolean isLocalhostMgd() {
116 return NAME_LOCALHOST_MGD
.equals(name
);
119 public String
getCdmlibServicesVersion() {
120 return cdmlibServicesVersion
;
123 public String
getCdmlibLastModified() {
124 return cdmlibServicesLastModified
;
127 public void refreshInstances() throws CDMServerException
{
129 if(isLocalhostMgd()) {
130 addInstancesFromDataSourcesConfig();
132 addInstancesViaHttp();
134 Collections
.sort(instances
, new Comparator
<CdmInstanceInfo
>() {
136 public int compare(CdmInstanceInfo cii1
, CdmInstanceInfo cii2
)
138 return cii1
.getName().toString().compareTo(cii2
.getName().toString());
143 public void updateInfo() throws CDMServerException
{
144 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/info.jsp";
145 String responseBody
= getResponse(url
);
146 if(responseBody
!= null) {
148 JSONObject info
= new JSONObject(responseBody
);
149 cdmlibServicesVersion
= info
.getString("cdmlibServicesVersion");
150 cdmlibServicesLastModified
= info
.getString("cdmlibServicesLastModified");
151 } catch (JSONException e
) {
152 throw new CDMServerException(e
);
157 public void addInstancesViaHttp() throws CDMServerException
{
159 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/instances.jsp";
160 String responseBody
= getResponse(url
);
161 if(responseBody
!= null) {
163 JSONArray array
= new JSONArray(responseBody
);
164 for(int i
=0;i
<array
.length();i
++) {
165 JSONObject instance
= (JSONObject
)array
.get(i
);
166 if(instance
!= null) {
167 JSONObject conf
= (JSONObject
)instance
.get("configuration");
169 String instanceName
= conf
.getString("instanceName");
170 // we need to remove the first (char) forward slash from
172 String basePath
= conf
.getString("basePath").substring(1);
173 addInstance(instanceName
, basePath
);
174 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
178 } catch (JSONException e
) {
179 throw new CDMServerException(e
);
184 private String
getResponse(String url
) throws CDMServerException
{
185 HttpClient client
= new DefaultHttpClient();
186 HttpParams params
= client
.getParams();
188 HttpConnectionParams
.setConnectionTimeout(params
, 5000);
189 HttpConnectionParams
.setSoTimeout(params
, 5000);
191 HttpGet httpGet
= new HttpGet(url
);
193 logger
.info("Executing request " + httpGet
.getRequestLine());
195 // Create a custom response handler
196 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
199 public String
handleResponse(
200 final HttpResponse response
) throws ClientProtocolException
, IOException
{
201 int status
= response
.getStatusLine().getStatusCode();
202 if (status
>= 200 && status
< 300) {
203 HttpEntity entity
= response
.getEntity();
204 return entity
!= null ? EntityUtils
.toString(entity
) : null;
206 throw new ClientProtocolException("Unexpected response status: " + status
);
211 String responseBody
= null;
213 responseBody
= client
.execute(httpGet
, responseHandler
);
214 } catch (ClientProtocolException e
) {
215 throw new CDMServerException(e
);
216 } catch (IOException e
) {
217 throw new CDMServerException(e
);
222 public void addInstancesFromDataSourcesConfig() {
223 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
224 logger
.info("Adding local instance " + dataSource
.getName());
225 addInstance(dataSource
.getName(), dataSource
.getName());
229 public String
toString(String instanceName
, int port
) {
230 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
233 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
234 if(instanceName
== null) {
238 for(CdmInstanceInfo instance
: instances
) {
239 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
246 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
247 if(instance
!= null) {
248 return CdmRemoteSource
.NewInstance(name
,
251 instance
.getBasePath(),
257 public boolean pingServer() {
258 if(isLocalhostMgd()) {
262 Socket s
= new Socket(server
, port
);
263 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
266 } catch (IOException ioe
) {
268 } catch (CDMServerException e
) {
274 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
276 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
278 if(crs
!= null && crs
.checkConnection()) {
279 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
282 } catch (CdmSourceException e
) {
283 throw new CDMServerException(e
);
289 public int compareDbSchemaVersion(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
291 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
292 String dbSchemaVersion
;
294 dbSchemaVersion
= crs
.getDbSchemaVersion();
295 } catch (CdmSourceException e
) {
296 throw new CDMServerException(e
);
300 if(dbSchemaVersion
!= null) {
301 return CdmMetaData
.compareVersion(dbSchemaVersion
, CdmMetaData
.getDbSchemaVersion(), 3, null);
303 throw new CDMServerException("Cannot determine editor db. schema version");
307 public int compareCdmlibServicesVersion() throws CdmSourceException
{
309 String serverVersion
= cdmlibServicesVersion
;
310 String serverCdmlibLastModified
= cdmlibServicesLastModified
;
312 return compareCdmlibServicesVersion(serverVersion
, serverCdmlibLastModified
);
317 * @param serverVersion
318 * @param editorVersion
319 * @throws CdmSourceException
321 public static int compareCdmlibServicesVersion(String serverVersion
, String serverCdmlibLastModified
) throws CdmSourceException
{
323 String editorVersion
= CdmApplicationState
.getCdmlibVersion();
324 String editorCdmlibLastModified
= CdmApplicationState
.getCdmlibLastModified();
327 if(StringUtils
.isBlank(serverVersion
) || StringUtils
.isBlank(editorVersion
)) {
328 throw new CdmSourceException("cdmlib-services server or editor version is empty");
331 String
[] serverVersionSplit
= serverVersion
.split("\\.");
332 String
[] editorVersionSplit
= editorVersion
.split("\\.");
334 if(serverVersionSplit
.length
< 3 || editorVersionSplit
.length
< 3 || serverVersionSplit
.length
> 4 || editorVersionSplit
.length
> 4) {
335 throw new CdmSourceException("cdmlib-services server or editor version is invalid");
338 Integer serverVersionPart
;
339 Integer editorVersionPart
;
341 for(int i
=0 ; i
<3 ; i
++) {
342 serverVersionPart
= Integer
.valueOf(serverVersionSplit
[i
]);
343 editorVersionPart
= Integer
.valueOf(editorVersionSplit
[i
]);
345 int partCompare
= serverVersionPart
.compareTo(editorVersionPart
);
346 if (partCompare
!= 0){
350 // at this point major, minor and patch versions are matching
352 if(StringUtils
.isBlank(serverCdmlibLastModified
) || StringUtils
.isBlank(editorCdmlibLastModified
)) {
353 throw new CdmSourceException("cdmlib-services server or editor version is empty");
356 String cdmServerIgnoreVersion
= System
.getProperty("cdm.server.version.lm.ignore");
357 if(StringUtils
.isBlank(cdmServerIgnoreVersion
) || !cdmServerIgnoreVersion
.equals("true")) {
358 Long serverLastModified
= Long
.valueOf(serverCdmlibLastModified
);
359 Long editorLastModified
= Long
.valueOf(editorCdmlibLastModified
);
360 return serverLastModified
.compareTo(editorLastModified
);
368 public static List
<CdmServerInfo
> getCdmServers() {
369 if(cdmServerInfoList
== null) {
370 cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
371 cdmServerInfoList
.add(new CdmServerInfo(NAME_PRODUCTION
, SERVER_PRODUCTION
, 80));
372 //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
373 cdmServerInfoList
.add(new CdmServerInfo(NAME_DEMO_1
, SERVER_DEMO_1
, 80));
374 // cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_2, SERVER_DEMO_2, 80));
375 //cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
376 // cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
377 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,NULL_PORT
));
379 return cdmServerInfoList
;
382 public String
getName() {
386 public String
getServer() {
391 public int getPort() {
395 public void setPort(int port
) {
399 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
400 if(instances
.isEmpty()) {
406 public static CdmRemoteSource
getDevServerRemoteSource() {
407 String value
= System
.getProperty("cdm.server.dev.port");
408 boolean available
= false;
409 CdmInstanceInfo devInstance
= null;
410 if(value
!= null && !value
.isEmpty()) {
411 int devPort
= Integer
.valueOf(value
);
412 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
);
414 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
415 available
= devCii
.pingInstance(devInstance
, devPort
);
417 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
419 } catch (Exception e
) {
426 public class CdmInstanceInfo
{
427 private final String name
;
430 * The full path of the instance including the the prefix (if any).
431 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
432 * For a managed local server this would simply be ""
434 private final String basePath
;
437 public CdmInstanceInfo(String name
, String basePath
) {
439 this.basePath
= basePath
;
443 public String
getName() {
447 public String
getBasePath() {