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
= "";
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
= "";
92 public CdmServerInfo(String name
, String server
, int port
) {
96 instances
= new ArrayList
<CdmInstanceInfo
>();
101 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
102 String _basePath
= basePath
;
103 if(isLocalhostMgd()) {
106 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, _basePath
);
112 public boolean isLocalhost() {
113 return name
.startsWith(SERVER_LOCALHOST
);
116 public boolean isLocalhostMgd() {
117 return NAME_LOCALHOST_MGD
.equals(name
);
120 public String
getCdmlibServicesVersion() {
121 return cdmlibServicesVersion
;
124 public String
getCdmlibLastModified() {
125 return cdmlibServicesLastModified
;
128 public void refreshInstances() throws CDMServerException
{
130 if(isLocalhostMgd()) {
131 addInstancesFromDataSourcesConfig();
133 addInstancesViaHttp();
135 Collections
.sort(instances
, new Comparator
<CdmInstanceInfo
>() {
137 public int compare(CdmInstanceInfo cii1
, CdmInstanceInfo cii2
)
139 return cii1
.getName().toString().compareTo(cii2
.getName().toString());
144 public void updateInfo() throws CDMServerException
{
145 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/info.jsp";
146 String responseBody
= getResponse(url
);
147 if(responseBody
!= null) {
149 JSONObject info
= new JSONObject(responseBody
);
150 cdmlibServicesVersion
= info
.getString("cdmlibServicesVersion");
151 cdmlibServicesLastModified
= info
.getString("cdmlibServicesLastModified");
152 } catch (JSONException e
) {
153 throw new CDMServerException(e
);
158 public void addInstancesViaHttp() throws CDMServerException
{
160 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/instances.jsp";
161 String responseBody
= getResponse(url
);
162 if(responseBody
!= null) {
164 JSONArray array
= new JSONArray(responseBody
);
165 for(int i
=0;i
<array
.length();i
++) {
166 JSONObject instance
= (JSONObject
)array
.get(i
);
167 if(instance
!= null) {
168 JSONObject conf
= (JSONObject
)instance
.get("configuration");
170 String instanceName
= conf
.getString("instanceName");
171 // we need to remove the first (char) forward slash from
173 String basePath
= conf
.getString("basePath").substring(1);
174 addInstance(instanceName
, basePath
);
175 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
179 } catch (JSONException e
) {
180 throw new CDMServerException(e
);
185 private String
getResponse(String url
) throws CDMServerException
{
186 HttpClient client
= new DefaultHttpClient();
187 HttpParams params
= client
.getParams();
189 HttpConnectionParams
.setConnectionTimeout(params
, 5000);
190 HttpConnectionParams
.setSoTimeout(params
, 5000);
192 HttpGet httpGet
= new HttpGet(url
);
194 logger
.info("Executing request " + httpGet
.getRequestLine());
196 // Create a custom response handler
197 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
200 public String
handleResponse(
201 final HttpResponse response
) throws ClientProtocolException
, IOException
{
202 int status
= response
.getStatusLine().getStatusCode();
203 if (status
>= 200 && status
< 300) {
204 HttpEntity entity
= response
.getEntity();
205 return entity
!= null ? EntityUtils
.toString(entity
) : null;
207 throw new ClientProtocolException("Unexpected response status: " + status
);
212 String responseBody
= null;
214 responseBody
= client
.execute(httpGet
, responseHandler
);
215 } catch (ClientProtocolException e
) {
216 throw new CDMServerException(e
);
217 } catch (IOException e
) {
218 throw new CDMServerException(e
);
223 public void addInstancesFromDataSourcesConfig() {
224 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
225 String datasourceNCName
= CDMServerUtils
.xmlNCNamefrom(dataSource
.getName());
226 logger
.info("Adding local instance " + dataSource
.getName() + " as " + datasourceNCName
);
227 addInstance(datasourceNCName
, datasourceNCName
);
231 public String
toString(String instanceName
, int port
) {
232 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
235 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
236 if(instanceName
== null) {
240 for(CdmInstanceInfo instance
: instances
) {
241 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
248 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
249 if(instance
!= null) {
250 return CdmRemoteSource
.NewInstance(name
,
253 instance
.getBasePath(),
259 public boolean pingServer() {
260 if(isLocalhostMgd()) {
264 Socket s
= new Socket(server
, port
);
265 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
268 } catch (IOException ioe
) {
270 } catch (CDMServerException e
) {
276 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
278 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
280 if(crs
!= null && crs
.checkConnection()) {
281 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
284 } catch (CdmSourceException e
) {
285 throw new CDMServerException(e
);
291 public int compareDbSchemaVersion(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
293 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
294 String dbSchemaVersion
;
296 dbSchemaVersion
= crs
.getDbSchemaVersion();
297 } catch (CdmSourceException e
) {
298 throw new CDMServerException(e
);
302 if(dbSchemaVersion
!= null) {
303 return CdmMetaData
.compareVersion(dbSchemaVersion
, CdmMetaData
.getDbSchemaVersion(), 3, null);
305 throw new CDMServerException("Cannot determine editor db. schema version");
309 public int compareCdmlibServicesVersion() throws CdmSourceException
{
311 String serverVersion
= cdmlibServicesVersion
;
312 String serverCdmlibLastModified
= cdmlibServicesLastModified
;
314 return compareCdmlibServicesVersion(serverVersion
, serverCdmlibLastModified
);
319 * @param serverVersion
320 * @param editorVersion
321 * @throws CdmSourceException
323 public static int compareCdmlibServicesVersion(String serverVersion
, String serverCdmlibLastModified
) throws CdmSourceException
{
325 String editorVersion
= CdmApplicationState
.getCdmlibVersion();
326 String editorCdmlibLastModified
= CdmApplicationState
.getCdmlibLastModified();
329 if(StringUtils
.isBlank(serverVersion
) || StringUtils
.isBlank(editorVersion
)) {
330 throw new CdmSourceException("cdmlib-services server or editor version is empty");
333 String
[] serverVersionSplit
= serverVersion
.split("\\.");
334 String
[] editorVersionSplit
= editorVersion
.split("\\.");
336 if(serverVersionSplit
.length
< 3 || editorVersionSplit
.length
< 3 || serverVersionSplit
.length
> 4 || editorVersionSplit
.length
> 4) {
337 throw new CdmSourceException("cdmlib-services server or editor version is invalid");
340 Integer serverVersionPart
;
341 Integer editorVersionPart
;
343 for(int i
=0 ; i
<3 ; i
++) {
344 serverVersionPart
= Integer
.valueOf(serverVersionSplit
[i
]);
345 editorVersionPart
= Integer
.valueOf(editorVersionSplit
[i
]);
347 int partCompare
= serverVersionPart
.compareTo(editorVersionPart
);
348 if (partCompare
!= 0){
352 // at this point major, minor and patch versions are matching
354 if(StringUtils
.isBlank(serverCdmlibLastModified
) || StringUtils
.isBlank(editorCdmlibLastModified
)) {
355 throw new CdmSourceException("cdmlib-services server or editor version is empty");
358 String cdmServerIgnoreVersion
= System
.getProperty("cdm.server.version.lm.ignore");
359 if(StringUtils
.isBlank(cdmServerIgnoreVersion
) || !cdmServerIgnoreVersion
.equals("true")) {
360 Long serverLastModified
= Long
.valueOf(serverCdmlibLastModified
);
361 Long editorLastModified
= Long
.valueOf(editorCdmlibLastModified
);
362 return serverLastModified
.compareTo(editorLastModified
);
370 public static List
<CdmServerInfo
> getCdmServers() {
371 if(cdmServerInfoList
== null) {
372 cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
373 cdmServerInfoList
.add(new CdmServerInfo(NAME_PRODUCTION
, SERVER_PRODUCTION
, 80));
374 //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
375 cdmServerInfoList
.add(new CdmServerInfo(NAME_DEMO_1
, SERVER_DEMO_1
, 80));
376 // cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_2, SERVER_DEMO_2, 80));
377 //cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
378 // cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
379 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,NULL_PORT
));
381 return cdmServerInfoList
;
384 public String
getName() {
388 public String
getServer() {
393 public int getPort() {
397 public void setPort(int port
) {
401 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
402 if(instances
.isEmpty()) {
408 public static CdmRemoteSource
getDevServerRemoteSource() {
409 String value
= System
.getProperty("cdm.server.dev.port");
410 boolean available
= false;
411 CdmInstanceInfo devInstance
= null;
412 if(value
!= null && !value
.isEmpty()) {
413 int devPort
= Integer
.valueOf(value
);
414 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
);
416 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
417 available
= devCii
.pingInstance(devInstance
, devPort
);
419 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
421 } catch (Exception e
) {
428 public class CdmInstanceInfo
{
429 private final String name
;
432 * The full path of the instance including the the prefix (if any).
433 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
434 * For a managed local server this would simply be ""
436 private final String basePath
;
439 public CdmInstanceInfo(String name
, String basePath
) {
441 this.basePath
= basePath
;
445 public String
getName() {
449 public String
getBasePath() {