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
.http
.HttpEntity
;
20 import org
.apache
.http
.HttpResponse
;
21 import org
.apache
.http
.client
.ClientProtocolException
;
22 import org
.apache
.http
.client
.HttpClient
;
23 import org
.apache
.http
.client
.ResponseHandler
;
24 import org
.apache
.http
.client
.methods
.HttpGet
;
25 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
26 import org
.apache
.http
.util
.EntityUtils
;
27 import org
.apache
.log4j
.Logger
;
28 import org
.json
.JSONArray
;
29 import org
.json
.JSONException
;
30 import org
.json
.JSONObject
;
32 import eu
.etaxonomy
.cdm
.config
.CdmSourceException
;
33 import eu
.etaxonomy
.cdm
.database
.CdmPersistentDataSource
;
34 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
35 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerException
;
42 public class CdmServerInfo
{
43 public static final Logger logger
= Logger
.getLogger(CdmServerInfo
.class);
45 private final static String CDMSERVER_PREFIX
= "cdmserver";
46 private final static String NAME_PRODUCTION
= "edit-production";
47 private final static String SERVER_PRODUCTION
= "dev.e-taxonomy.eu";
49 private final static String NAME_INTEGRATION
= "edit-integration";
50 private final static String SERVER_INTEGRATION
= "int.e-taxonomy.eu";
52 private final static String NAME_DEMO_1
= "edit-WS I";
53 private final static String SERVER_DEMO_1
= "160.45.63.230";
55 private final static String NAME_DEMO_2
= "edit-WS II";
56 private final static String SERVER_DEMO_2
= "160.45.63.230";
58 private final static String NAME_TEST
= "edit-test";
59 private final static String SERVER_TEST
= "test.e-taxonomy.eu";
61 public final static String SERVER_LOCALHOST
= "localhost";
62 private final static String NAME_LOCALHOST
= "localhost";
63 public final static String NAME_LOCALHOST_MGD
= "localhost mgd.";
65 private final static String NAME_LOCALHOST_DEV
= "localhost-dev";
66 private final static String NAME_INSTANCE_LOCALHOST_DEV
= "local-dev";
67 private final static String SERVER_LOCALHOST_DEV
= "localhost";
68 private final static int PORT_LOCALHOST_DEV
= 8080;
69 private final static String BASEPATH_LOCALHOST_DEV
= "";
71 private final String name
;
72 private final String server
;
73 private final int port
;
74 private final List
<CdmInstanceInfo
> instances
;
76 private static List
<CdmServerInfo
> cdmServerInfoList
;
78 public CdmServerInfo(String name
, String server
, int port
) {
82 instances
= new ArrayList
<CdmInstanceInfo
>();
86 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
87 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, basePath
);
93 public boolean isLocalhost() {
94 return name
.startsWith(SERVER_LOCALHOST
);
97 public boolean isLocalhostMgd() {
98 return NAME_LOCALHOST_MGD
.equals(name
);
101 public void refreshInstances() throws CDMServerException
{
103 if(isLocalhostMgd()) {
104 addInstancesFromDataSourcesConfig();
106 addInstancesViaHttp();
108 Collections
.sort(instances
, new Comparator
<CdmInstanceInfo
>() {
110 public int compare(CdmInstanceInfo cii1
, CdmInstanceInfo cii2
)
112 return cii1
.getName().toString().compareTo(cii2
.getName().toString());
117 public void addInstancesViaHttp() throws CDMServerException
{
118 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/instances.jsp";
120 HttpClient client
= new DefaultHttpClient();
121 HttpGet httpGet
= new HttpGet(url
);
123 logger
.info("Executing request " + httpGet
.getRequestLine());
125 // Create a custom response handler
126 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
129 public String
handleResponse(
130 final HttpResponse response
) throws ClientProtocolException
, IOException
{
131 int status
= response
.getStatusLine().getStatusCode();
132 if (status
>= 200 && status
< 300) {
133 HttpEntity entity
= response
.getEntity();
134 return entity
!= null ? EntityUtils
.toString(entity
) : null;
136 throw new ClientProtocolException("Unexpected response status: " + status
);
141 String responseBody
= null;
143 responseBody
= client
.execute(httpGet
, responseHandler
);
144 } catch (ClientProtocolException e
) {
145 throw new CDMServerException(e
);
146 } catch (IOException e
) {
147 throw new CDMServerException(e
);
151 if(responseBody
!= null) {
153 JSONArray array
= new JSONArray(responseBody
);
154 for(int i
=0;i
<array
.length();i
++) {
155 JSONObject instance
= (JSONObject
)array
.get(i
);
156 if(instance
!= null) {
157 JSONObject conf
= (JSONObject
)instance
.get("configuration");
159 String instanceName
= conf
.getString("instanceName");
160 // we need to remove the first (char) forward slash from
162 String basePath
= conf
.getString("basePath").substring(1);
163 addInstance(instanceName
, basePath
);
164 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
168 } catch (JSONException e
) {
169 throw new CDMServerException(e
);
174 public void addInstancesFromDataSourcesConfig() {
176 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
177 logger
.warn("Adding local instance " + dataSource
.getName());
178 addInstance(dataSource
.getName(), dataSource
.getName());
183 public String
toString(String instanceName
, int port
) {
184 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
187 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
188 if(instanceName
== null) {
192 for(CdmInstanceInfo instance
: instances
) {
193 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
200 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
201 if(instance
!= null) {
202 return CdmRemoteSource
.NewInstance(name
,
205 instance
.getBasePath(),
211 public boolean pingServer(){
213 Socket s
= new Socket(server
, port
);
214 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
216 } catch (IOException ioe
) {
222 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
224 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
226 if(crs
!= null && crs
.checkConnection()) {
227 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
230 } catch (CdmSourceException e
) {
231 throw new CDMServerException(e
);
237 public static List
<CdmServerInfo
> getCdmServers() {
238 if(cdmServerInfoList
== null) {
239 cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
240 //cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80));
241 //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
242 cdmServerInfoList
.add(new CdmServerInfo(NAME_DEMO_1
, SERVER_DEMO_1
, 80));
243 cdmServerInfoList
.add(new CdmServerInfo(NAME_DEMO_2
, SERVER_DEMO_2
, 80));
244 cdmServerInfoList
.add(new CdmServerInfo(NAME_TEST
, SERVER_TEST
, 80));
245 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST
, SERVER_LOCALHOST
, 8080));
246 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,8080));
248 return cdmServerInfoList
;
251 public String
getName() {
255 public String
getServer() {
260 public int getPort() {
265 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
266 if(instances
.isEmpty()) {
272 public static CdmRemoteSource
getDevServerRemoteSource() {
273 String value
= System
.getProperty("cdm.server.dev.port");
274 boolean available
= false;
275 CdmInstanceInfo devInstance
= null;
276 if(value
!= null && !value
.isEmpty()) {
277 int devPort
= Integer
.valueOf(value
);
278 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
);
280 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
281 available
= devCii
.pingInstance(devInstance
, devPort
);
283 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
285 } catch (Exception e
) {
292 public class CdmInstanceInfo
{
293 private final String name
;
296 * The full path of the instance including the the prefix (if any).
297 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
298 * For a managed local server this would simply be "remoting"
300 private final String basePath
;
303 public CdmInstanceInfo(String name
, String basePath
) {
305 this.basePath
= basePath
;
309 public String
getName() {
313 public String
getBasePath() {