2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.remoting
.source
;
12 import java
.io
.FileInputStream
;
13 import java
.io
.FileNotFoundException
;
14 import java
.io
.FileOutputStream
;
15 import java
.io
.IOException
;
16 import java
.net
.Socket
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Collections
;
19 import java
.util
.Comparator
;
20 import java
.util
.List
;
22 import org
.apache
.commons
.lang
.StringUtils
;
23 import org
.apache
.http
.HttpEntity
;
24 import org
.apache
.http
.HttpResponse
;
25 import org
.apache
.http
.client
.ClientProtocolException
;
26 import org
.apache
.http
.client
.HttpClient
;
27 import org
.apache
.http
.client
.ResponseHandler
;
28 import org
.apache
.http
.client
.methods
.HttpGet
;
29 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
30 import org
.apache
.http
.params
.HttpConnectionParams
;
31 import org
.apache
.http
.params
.HttpParams
;
32 import org
.apache
.http
.util
.EntityUtils
;
33 import org
.apache
.log4j
.Logger
;
34 import org
.json
.JSONArray
;
35 import org
.json
.JSONException
;
36 import org
.json
.JSONObject
;
38 import com
.fasterxml
.jackson
.core
.JsonParseException
;
39 import com
.fasterxml
.jackson
.core
.type
.TypeReference
;
40 import com
.fasterxml
.jackson
.databind
.JsonMappingException
;
41 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
43 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
44 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
45 import eu
.etaxonomy
.cdm
.config
.CdmSourceException
;
46 import eu
.etaxonomy
.cdm
.database
.CdmPersistentDataSource
;
47 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
48 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
49 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerException
;
50 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerUtils
;
57 public class CdmServerInfo
{
58 public static final Logger logger
= Logger
.getLogger(CdmServerInfo
.class);
60 private final static String CDMSERVER_PREFIX
= "cdmserver/";
61 private final static String NAME_PRODUCTION
= "cybertaxonomy.org";
62 private final static String SERVER_PRODUCTION
= "api.cybertaxonomy.org";
64 private final static String NAME_DEMO_1
= "edit-WS I";
65 private final static String SERVER_DEMO_1
= "160.45.63.230";
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 String BASEPATH_LOCALHOST_DEV
= "";
76 public final static int NULL_PORT
= -1;
77 public final static String NULL_PORT_STRING
= "N/A";
79 private static final String CDM_REMOTE_SERVERS_CONFIG_FILE
= "cdm_remote_servers.json";
82 private final String name
;
83 private final String server
;
85 private final List
<CdmInstanceInfo
> instances
;
87 private String cdmlibServicesVersion
= "";
88 private String cdmlibServicesLastModified
= "";
90 private String prefix
= "";
92 private boolean ignoreCdmLibVersion
= false;
95 public CdmServerInfo(CdmServerInfoConfig parameterObject
) {
96 this.name
= parameterObject
.getName();
97 this.server
= parameterObject
.getServer();
98 this.port
= parameterObject
.getPort();
99 this.prefix
= parameterObject
.getPrefix();
100 this.ignoreCdmLibVersion
= parameterObject
.isIgnoreCdmLibVersion();
101 instances
= new ArrayList
<CdmInstanceInfo
>();
105 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
106 String _basePath
= basePath
;
107 if(isLocalhostMgd()) {
110 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, _basePath
);
116 public boolean isLocalhost() {
117 return name
.startsWith(SERVER_LOCALHOST
);
120 public boolean isLocalhostMgd() {
121 return NAME_LOCALHOST_MGD
.equals(name
);
124 public String
getCdmlibServicesVersion() {
125 return cdmlibServicesVersion
;
128 public String
getCdmlibLastModified() {
129 return cdmlibServicesLastModified
;
132 public void refreshInstances() throws CDMServerException
{
134 if(isLocalhostMgd()) {
135 addInstancesFromDataSourcesConfig();
137 addInstancesViaHttp();
139 Collections
.sort(instances
, new Comparator
<CdmInstanceInfo
>() {
141 public int compare(CdmInstanceInfo cii1
, CdmInstanceInfo cii2
)
143 return cii1
.getName().toString().compareTo(cii2
.getName().toString());
148 public void updateInfo() throws CDMServerException
{
149 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + prefix
+ "info.jsp";
150 String responseBody
= getResponse(url
);
151 if(responseBody
!= null) {
153 JSONObject info
= new JSONObject(responseBody
);
154 cdmlibServicesVersion
= info
.getString("cdmlibServicesVersion");
155 cdmlibServicesLastModified
= info
.getString("cdmlibServicesLastModified");
156 } catch (JSONException e
) {
157 throw new CDMServerException(e
);
162 public void addInstancesViaHttp() throws CDMServerException
{
164 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + prefix
+ "instances.jsp";
165 String responseBody
= getResponse(url
);
166 if(responseBody
!= null) {
168 JSONArray array
= new JSONArray(responseBody
);
169 for(int i
=0;i
<array
.length();i
++) {
170 JSONObject instance
= (JSONObject
)array
.get(i
);
171 if(instance
!= null) {
172 JSONObject conf
= (JSONObject
)instance
.get("configuration");
174 String instanceName
= conf
.getString("instanceName");
175 // we need to remove the first (char) forward slash from
177 String basePath
= conf
.getString("basePath").substring(1);
178 addInstance(instanceName
, basePath
);
179 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
183 } catch (JSONException e
) {
184 throw new CDMServerException(e
);
189 private String
getResponse(String url
) throws CDMServerException
{
190 HttpClient client
= new DefaultHttpClient();
191 HttpParams params
= client
.getParams();
193 HttpConnectionParams
.setConnectionTimeout(params
, 5000);
194 HttpConnectionParams
.setSoTimeout(params
, 5000);
196 HttpGet httpGet
= new HttpGet(url
);
198 logger
.info("Executing request " + httpGet
.getRequestLine());
200 // Create a custom response handler
201 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
204 public String
handleResponse(
205 final HttpResponse response
) throws ClientProtocolException
, IOException
{
206 int status
= response
.getStatusLine().getStatusCode();
207 if (status
>= 200 && status
< 300) {
208 HttpEntity entity
= response
.getEntity();
209 return entity
!= null ? EntityUtils
.toString(entity
) : null;
211 throw new ClientProtocolException("Unexpected response status: " + status
);
216 String responseBody
= null;
218 responseBody
= client
.execute(httpGet
, responseHandler
);
219 } catch (ClientProtocolException e
) {
220 throw new CDMServerException(e
);
221 } catch (IOException e
) {
222 throw new CDMServerException(e
);
227 public void addInstancesFromDataSourcesConfig() {
228 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
229 String datasourceNCName
= CDMServerUtils
.xmlNCNamefrom(dataSource
.getName());
230 logger
.info("Adding local instance " + dataSource
.getName() + " as " + datasourceNCName
);
231 addInstance(datasourceNCName
, datasourceNCName
);
235 public String
toString(String instanceName
, int port
) {
236 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
239 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
240 if(instanceName
== null) {
244 for(CdmInstanceInfo instance
: instances
) {
245 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
252 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
253 if(instance
!= null) {
254 return CdmRemoteSource
.NewInstance(name
,
257 instance
.getBasePath()
263 public boolean pingServer() {
264 if(isLocalhostMgd()) {
268 Socket s
= new Socket(server
, port
);
269 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
272 } catch (IOException ioe
) {
274 } catch (CDMServerException e
) {
280 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
282 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
284 if(crs
!= null && crs
.checkConnection()) {
285 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
288 } catch (CdmSourceException e
) {
289 throw new CDMServerException(e
);
295 public int compareDbSchemaVersion(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
297 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
298 String dbSchemaVersion
;
300 dbSchemaVersion
= crs
.getDbSchemaVersion();
301 } catch (CdmSourceException e
) {
302 throw new CDMServerException(e
);
306 if(dbSchemaVersion
!= null) {
307 return CdmMetaData
.compareVersion(dbSchemaVersion
, CdmMetaData
.getDbSchemaVersion(), 3, null);
309 throw new CDMServerException("Cannot determine editor db. schema version");
313 public int compareCdmlibServicesVersion() throws CdmSourceException
{
315 String serverVersion
= cdmlibServicesVersion
;
316 String serverCdmlibLastModified
= cdmlibServicesLastModified
;
317 if(ignoreCdmLibVersion
) {
320 return compareCdmlibServicesVersion(serverVersion
, serverCdmlibLastModified
);
326 * @param serverVersion
327 * @param editorVersion
328 * @throws CdmSourceException
330 public static int compareCdmlibServicesVersion(String serverVersion
, String serverCdmlibLastModified
) throws CdmSourceException
{
332 String editorVersion
= CdmApplicationState
.getCdmlibVersion();
333 String editorCdmlibLastModified
= CdmApplicationState
.getCdmlibLastModified();
336 if(StringUtils
.isBlank(serverVersion
) || StringUtils
.isBlank(editorVersion
)) {
337 throw new CdmSourceException("cdmlib-services server or editor version is empty");
340 String
[] serverVersionSplit
= serverVersion
.split("\\.");
341 String
[] editorVersionSplit
= editorVersion
.split("\\.");
343 if(serverVersionSplit
.length
< 3 || editorVersionSplit
.length
< 3 || serverVersionSplit
.length
> 4 || editorVersionSplit
.length
> 4) {
344 throw new CdmSourceException("cdmlib-services server or editor version is invalid");
347 Integer serverVersionPart
;
348 Integer editorVersionPart
;
350 for(int i
=0 ; i
<3 ; i
++) {
351 serverVersionPart
= Integer
.valueOf(serverVersionSplit
[i
]);
352 editorVersionPart
= Integer
.valueOf(editorVersionSplit
[i
]);
354 int partCompare
= serverVersionPart
.compareTo(editorVersionPart
);
355 if (partCompare
!= 0){
359 // at this point major, minor and patch versions are matching
361 if(StringUtils
.isBlank(serverCdmlibLastModified
) || StringUtils
.isBlank(editorCdmlibLastModified
)) {
362 throw new CdmSourceException("cdmlib-services server or editor version is empty");
365 String cdmServerIgnoreVersion
= System
.getProperty("cdm.server.version.lm.ignore");
366 if(StringUtils
.isBlank(cdmServerIgnoreVersion
) || !cdmServerIgnoreVersion
.equals("true")) {
367 Long serverLastModified
= Long
.valueOf(serverCdmlibLastModified
);
368 Long editorLastModified
= Long
.valueOf(editorCdmlibLastModified
);
369 return serverLastModified
.compareTo(editorLastModified
);
377 public static List
<CdmServerInfo
> getCdmServers() {
378 List
<CdmServerInfoConfig
> configList
= loadFromConfigFile(new File(CdmUtils
.PER_USER_CDM_FOLDER
, CDM_REMOTE_SERVERS_CONFIG_FILE
));
379 List
<CdmServerInfo
> serverInfoList
= new ArrayList
<CdmServerInfo
>(configList
.size());
380 for(CdmServerInfoConfig config
: configList
) {
381 serverInfoList
.add(new CdmServerInfo(config
));
383 // The local host managed server must not be in the config file
384 CdmServerInfoConfig localHostManagedConfig
= new CdmServerInfoConfig(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
, NULL_PORT
, CDMSERVER_PREFIX
, false);
385 serverInfoList
.add(new CdmServerInfo(localHostManagedConfig
));
386 return serverInfoList
;
392 * @throws IOException
393 * @throws FileNotFoundException
394 * @throws JsonMappingException
395 * @throws JsonParseException
397 private static List
<CdmServerInfoConfig
> loadFromConfigFile(File file
) {
399 List
<CdmServerInfoConfig
> serverList
= null;
401 ObjectMapper mapper
= new ObjectMapper();
404 logger
.info("The remote server config file '" + file
.getAbsolutePath() +
405 "' does not yet exist, it will be created based on the defaults.");
407 serverList
= createDefaultServerConfigList();
408 mapper
.writerWithDefaultPrettyPrinter().writeValue(new FileOutputStream(file
), serverList
);
410 } catch (IOException e
) {
411 throw new RuntimeException(e
);
415 serverList
= mapper
.readValue(new FileInputStream(file
), new TypeReference
<List
<CdmServerInfoConfig
>>(){});
416 } catch (IOException e
) {
417 throw new RuntimeException(e
);
430 private static List
<CdmServerInfoConfig
> createDefaultServerConfigList() {
432 List
<CdmServerInfoConfig
> serverInfoList
= new ArrayList
<CdmServerInfoConfig
>();
433 serverInfoList
.add(new CdmServerInfoConfig(NAME_PRODUCTION
, SERVER_PRODUCTION
, 80, "", false));
434 serverInfoList
.add(new CdmServerInfoConfig(NAME_DEMO_1
, SERVER_DEMO_1
, 80, CDMSERVER_PREFIX
, false));
435 serverInfoList
.add(new CdmServerInfoConfig(NAME_LOCALHOST
, SERVER_LOCALHOST
, 8080, CDMSERVER_PREFIX
, true));
436 return serverInfoList
;
439 public String
getName() {
443 public String
getServer() {
448 public int getPort() {
452 public void setPort(int port
) {
456 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
457 if(instances
.isEmpty()) {
463 public static CdmRemoteSource
getDevServerRemoteSource() {
464 String value
= System
.getProperty("cdm.server.dev.port");
465 boolean available
= false;
466 CdmInstanceInfo devInstance
= null;
467 if(value
!= null && !value
.isEmpty()) {
468 int devPort
= Integer
.valueOf(value
);
469 CdmServerInfo devCii
= new CdmServerInfo(new CdmServerInfoConfig(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
, "", false));
471 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
472 available
= devCii
.pingInstance(devInstance
, devPort
);
474 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
476 } catch (Exception e
) {
483 public class CdmInstanceInfo
{
484 private final String name
;
487 * The full path of the instance including the the prefix (if any).
488 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
489 * For a managed local server this would simply be ""
491 private final String basePath
;
494 public CdmInstanceInfo(String name
, String basePath
) {
496 this.basePath
= basePath
;
500 public String
getName() {
504 public String
getBasePath() {