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
.taxeditor
.remoting
.server
.CDMServerException
;
43 public class CdmServerInfo
{
44 public static final Logger logger
= Logger
.getLogger(CdmServerInfo
.class);
46 private final static String CDMSERVER_PREFIX
= "cdmserver";
47 private final static String NAME_PRODUCTION
= "cybertaxonomy.org";
48 private final static String SERVER_PRODUCTION
= "api.cybertaxonomy.org";
50 private final static String NAME_INTEGRATION
= "edit-integration";
51 private final static String SERVER_INTEGRATION
= "int.e-taxonomy.eu";
53 private final static String NAME_DEMO_1
= "edit-WS I";
54 private final static String SERVER_DEMO_1
= "160.45.63.230";
56 private final static String NAME_DEMO_2
= "edit-WS II";
57 private final static String SERVER_DEMO_2
= "160.45.63.231";
59 private final static String NAME_TEST
= "edit-test";
60 private final static String SERVER_TEST
= "test.e-taxonomy.eu";
62 public final static String SERVER_LOCALHOST
= "localhost";
63 private final static String NAME_LOCALHOST
= "localhost";
64 public final static String NAME_LOCALHOST_MGD
= "localhost mgd.";
66 private final static String NAME_LOCALHOST_DEV
= "localhost-dev";
67 private final static String NAME_INSTANCE_LOCALHOST_DEV
= "local-dev";
68 private final static String SERVER_LOCALHOST_DEV
= "localhost";
69 private final static int PORT_LOCALHOST_DEV
= 8080;
70 private final static String BASEPATH_LOCALHOST_DEV
= "";
72 public final static int NULL_PORT
= -1;
73 public final static String NULL_PORT_STRING
= "N/A";
76 private final String name
;
77 private final String server
;
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 String _basePath
= basePath
;
98 if(isLocalhostMgd()) {
101 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, _basePath
);
107 public boolean isLocalhost() {
108 return name
.startsWith(SERVER_LOCALHOST
);
111 public boolean isLocalhostMgd() {
112 return NAME_LOCALHOST_MGD
.equals(name
);
115 public String
getCdmlibServicesVersion() {
116 return cdmlibServicesVersion
;
119 public String
getCdmlibLastModified() {
120 return cdmlibServicesLastModified
;
123 public void refreshInstances() throws CDMServerException
{
125 if(isLocalhostMgd()) {
126 addInstancesFromDataSourcesConfig();
128 addInstancesViaHttp();
130 Collections
.sort(instances
, new Comparator
<CdmInstanceInfo
>() {
132 public int compare(CdmInstanceInfo cii1
, CdmInstanceInfo cii2
)
134 return cii1
.getName().toString().compareTo(cii2
.getName().toString());
139 public void updateInfo() throws CDMServerException
{
140 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/info.jsp";
141 String responseBody
= getResponse(url
);
142 if(responseBody
!= null) {
144 JSONObject info
= new JSONObject(responseBody
);
145 cdmlibServicesVersion
= info
.getString("cdmlibServicesVersion");
146 cdmlibServicesLastModified
= info
.getString("cdmlibServicesLastModified");
147 } catch (JSONException e
) {
148 throw new CDMServerException(e
);
153 public void addInstancesViaHttp() throws CDMServerException
{
155 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/instances.jsp";
156 String responseBody
= getResponse(url
);
157 if(responseBody
!= null) {
159 JSONArray array
= new JSONArray(responseBody
);
160 for(int i
=0;i
<array
.length();i
++) {
161 JSONObject instance
= (JSONObject
)array
.get(i
);
162 if(instance
!= null) {
163 JSONObject conf
= (JSONObject
)instance
.get("configuration");
165 String instanceName
= conf
.getString("instanceName");
166 // we need to remove the first (char) forward slash from
168 String basePath
= conf
.getString("basePath").substring(1);
169 addInstance(instanceName
, basePath
);
170 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
174 } catch (JSONException e
) {
175 throw new CDMServerException(e
);
180 private String
getResponse(String url
) throws CDMServerException
{
181 HttpClient client
= new DefaultHttpClient();
182 HttpParams params
= client
.getParams();
184 HttpConnectionParams
.setConnectionTimeout(params
, 5000);
185 HttpConnectionParams
.setSoTimeout(params
, 5000);
187 HttpGet httpGet
= new HttpGet(url
);
189 logger
.info("Executing request " + httpGet
.getRequestLine());
191 // Create a custom response handler
192 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
195 public String
handleResponse(
196 final HttpResponse response
) throws ClientProtocolException
, IOException
{
197 int status
= response
.getStatusLine().getStatusCode();
198 if (status
>= 200 && status
< 300) {
199 HttpEntity entity
= response
.getEntity();
200 return entity
!= null ? EntityUtils
.toString(entity
) : null;
202 throw new ClientProtocolException("Unexpected response status: " + status
);
207 String responseBody
= null;
209 responseBody
= client
.execute(httpGet
, responseHandler
);
210 } catch (ClientProtocolException e
) {
211 throw new CDMServerException(e
);
212 } catch (IOException e
) {
213 throw new CDMServerException(e
);
218 public void addInstancesFromDataSourcesConfig() {
219 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
220 logger
.info("Adding local instance " + dataSource
.getName());
221 addInstance(dataSource
.getName(), dataSource
.getName());
225 public String
toString(String instanceName
, int port
) {
226 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
229 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
230 if(instanceName
== null) {
234 for(CdmInstanceInfo instance
: instances
) {
235 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
242 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
243 if(instance
!= null) {
244 return CdmRemoteSource
.NewInstance(name
,
247 instance
.getBasePath(),
253 public boolean pingServer() {
254 if(isLocalhostMgd()) {
258 Socket s
= new Socket(server
, port
);
259 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
262 } catch (IOException ioe
) {
264 } catch (CDMServerException e
) {
270 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
272 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
274 if(crs
!= null && crs
.checkConnection()) {
275 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
278 } catch (CdmSourceException e
) {
279 throw new CDMServerException(e
);
285 public int compareDbSchemaVersion(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
287 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
288 String dbSchemaVersion
;
290 dbSchemaVersion
= crs
.getDbSchemaVersion();
291 } catch (CdmSourceException e
) {
292 throw new CDMServerException(e
);
296 if(dbSchemaVersion
!= null) {
297 return CdmMetaData
.compareVersion(dbSchemaVersion
, CdmMetaData
.getDbSchemaVersion(), 3, null);
299 throw new CDMServerException("Cannot determine editor db. schema version");
303 public int compareCdmlibServicesVersion() throws CdmSourceException
{
305 String serverVersion
= cdmlibServicesVersion
;
306 String serverCdmlibLastModified
= cdmlibServicesLastModified
;
308 return compareCdmlibServicesVersion(serverVersion
, serverCdmlibLastModified
);
313 * @param serverVersion
314 * @param editorVersion
315 * @throws CdmSourceException
317 public static int compareCdmlibServicesVersion(String serverVersion
, String serverCdmlibLastModified
) throws CdmSourceException
{
319 String editorVersion
= CdmApplicationState
.getCdmlibVersion();
320 String editorCdmlibLastModified
= CdmApplicationState
.getCdmlibLastModified();
323 if(StringUtils
.isBlank(serverVersion
) || StringUtils
.isBlank(editorVersion
)) {
324 throw new CdmSourceException("cdmlib-services server or editor version is empty");
327 String
[] serverVersionSplit
= serverVersion
.split("\\.");
328 String
[] editorVersionSplit
= editorVersion
.split("\\.");
330 if(serverVersionSplit
.length
< 3 || editorVersionSplit
.length
< 3 || serverVersionSplit
.length
> 4 || editorVersionSplit
.length
> 4) {
331 throw new CdmSourceException("cdmlib-services server or editor version is invalid");
334 Integer serverVersionPart
;
335 Integer editorVersionPart
;
337 for(int i
=0 ; i
<3 ; i
++) {
338 serverVersionPart
= Integer
.valueOf(serverVersionSplit
[i
]);
339 editorVersionPart
= Integer
.valueOf(editorVersionSplit
[i
]);
341 int partCompare
= serverVersionPart
.compareTo(editorVersionPart
);
342 if (partCompare
!= 0){
346 // at this point major, minor and patch versions are matching
348 if(StringUtils
.isBlank(serverCdmlibLastModified
) || StringUtils
.isBlank(editorCdmlibLastModified
)) {
349 throw new CdmSourceException("cdmlib-services server or editor version is empty");
352 String cdmServerIgnoreVersion
= System
.getProperty("cdm.server.version.lm.ignore");
353 if(StringUtils
.isBlank(cdmServerIgnoreVersion
) || !cdmServerIgnoreVersion
.equals("true")) {
354 Long serverLastModified
= Long
.valueOf(serverCdmlibLastModified
);
355 Long editorLastModified
= Long
.valueOf(editorCdmlibLastModified
);
356 return serverLastModified
.compareTo(editorLastModified
);
364 public static List
<CdmServerInfo
> getCdmServers() {
365 if(cdmServerInfoList
== null) {
366 cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
367 cdmServerInfoList
.add(new CdmServerInfo(NAME_PRODUCTION
, SERVER_PRODUCTION
, 80));
368 //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
369 cdmServerInfoList
.add(new CdmServerInfo(NAME_DEMO_1
, SERVER_DEMO_1
, 80));
370 // cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_2, SERVER_DEMO_2, 80));
371 //cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
372 // cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
373 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,NULL_PORT
));
375 return cdmServerInfoList
;
378 public String
getName() {
382 public String
getServer() {
387 public int getPort() {
391 public void setPort(int port
) {
395 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
396 if(instances
.isEmpty()) {
402 public static CdmRemoteSource
getDevServerRemoteSource() {
403 String value
= System
.getProperty("cdm.server.dev.port");
404 boolean available
= false;
405 CdmInstanceInfo devInstance
= null;
406 if(value
!= null && !value
.isEmpty()) {
407 int devPort
= Integer
.valueOf(value
);
408 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
);
410 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
411 available
= devCii
.pingInstance(devInstance
, devPort
);
413 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
415 } catch (Exception e
) {
422 public class CdmInstanceInfo
{
423 private final String name
;
426 * The full path of the instance including the the prefix (if any).
427 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
428 * For a managed local server this would simply be ""
430 private final String basePath
;
433 public CdmInstanceInfo(String name
, String basePath
) {
435 this.basePath
= basePath
;
439 public String
getName() {
443 public String
getBasePath() {