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
.List
;
17 import org
.apache
.http
.HttpEntity
;
18 import org
.apache
.http
.HttpResponse
;
19 import org
.apache
.http
.client
.ClientProtocolException
;
20 import org
.apache
.http
.client
.HttpClient
;
21 import org
.apache
.http
.client
.ResponseHandler
;
22 import org
.apache
.http
.client
.methods
.HttpGet
;
23 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
24 import org
.apache
.http
.util
.EntityUtils
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.json
.JSONArray
;
27 import org
.json
.JSONException
;
28 import org
.json
.JSONObject
;
30 import eu
.etaxonomy
.cdm
.config
.CdmSourceException
;
31 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerException
;
38 public class CdmServerInfo
{
39 public static final Logger logger
= Logger
.getLogger(CdmServerInfo
.class);
41 private final static String CDMSERVER_PREFIX
= "cdmserver";
42 private final static String NAME_PRODUCTION
= "edit-production";
43 private final static String SERVER_PRODUCTION
= "dev.e-taxonomy.eu";
45 private final static String NAME_INTEGRATION
= "edit-integration";
46 private final static String SERVER_INTEGRATION
= "int.e-taxonomy.eu";
48 private final static String NAME_TEST
= "edit-test";
49 private final static String SERVER_TEST
= "test.e-taxonomy.eu";
51 private final static String SERVER_LOCALHOST
= "localhost";
52 private final static String NAME_LOCALHOST
= "localhost";
53 private final static String NAME_LOCALHOST_MGD
= "localhost mgd.";
55 private final static String NAME_LOCALHOST_DEV
= "localhost-dev";
56 private final static String NAME_INSTANCE_LOCALHOST_DEV
= "local-dev";
57 private final static String SERVER_LOCALHOST_DEV
= "localhost";
58 private final static int PORT_LOCALHOST_DEV
= 8080;
59 private final static String BASEPATH_LOCALHOST_DEV
= "";
61 private final String name
;
62 private final String server
;
63 private final int port
;
64 private final List
<CdmInstanceInfo
> instances
;
67 public CdmServerInfo(String name
, String server
, int port
) {
71 instances
= new ArrayList
<CdmInstanceInfo
>();
75 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
76 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, basePath
);
82 public boolean isLocalhost() {
83 return name
.startsWith(SERVER_LOCALHOST_DEV
);
85 public void refreshInstances() throws CDMServerException
{
87 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/instances.jsp";
89 HttpClient client
= new DefaultHttpClient();
90 HttpGet httpGet
= new HttpGet(url
);
92 logger
.info("Executing request " + httpGet
.getRequestLine());
94 // Create a custom response handler
95 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
98 public String
handleResponse(
99 final HttpResponse response
) throws ClientProtocolException
, IOException
{
100 int status
= response
.getStatusLine().getStatusCode();
101 if (status
>= 200 && status
< 300) {
102 HttpEntity entity
= response
.getEntity();
103 return entity
!= null ? EntityUtils
.toString(entity
) : null;
105 throw new ClientProtocolException("Unexpected response status: " + status
);
110 String responseBody
= null;
112 responseBody
= client
.execute(httpGet
, responseHandler
);
113 } catch (ClientProtocolException e
) {
114 throw new CDMServerException(e
);
115 } catch (IOException e
) {
116 throw new CDMServerException(e
);
120 if(responseBody
!= null) {
122 JSONArray array
= new JSONArray(responseBody
);
123 for(int i
=0;i
<array
.length();i
++) {
124 JSONObject instance
= (JSONObject
)array
.get(i
);
125 if(instance
!= null) {
126 JSONObject conf
= (JSONObject
)instance
.get("configuration");
128 String instanceName
= conf
.getString("instanceName");
129 // we need to remove the first (char) forward slash from
131 String basePath
= conf
.getString("basePath").substring(1);
132 addInstance(instanceName
, basePath
);
133 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
137 } catch (JSONException e
) {
138 throw new CDMServerException(e
);
144 public String
toString(String instanceName
, int port
) {
145 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
148 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
149 if(instanceName
== null) {
153 for(CdmInstanceInfo instance
: instances
) {
154 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
161 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
162 if(instance
!= null) {
163 return CdmRemoteSource
.NewInstance(name
,
166 instance
.getBasePath(),
172 public boolean pingServer(){
174 Socket s
= new Socket(server
, port
);
175 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
177 } catch (IOException ioe
) {
183 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
185 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
187 if(crs
!= null && crs
.checkConnection()) {
188 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
191 } catch (CdmSourceException e
) {
192 throw new CDMServerException(e
);
198 public static List
<CdmServerInfo
> getCdmServers() {
199 List
<CdmServerInfo
> cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
200 cdmServerInfoList
.add(new CdmServerInfo(NAME_PRODUCTION
, SERVER_PRODUCTION
, 80));
201 cdmServerInfoList
.add(new CdmServerInfo(NAME_INTEGRATION
, SERVER_INTEGRATION
, 80));
202 cdmServerInfoList
.add(new CdmServerInfo(NAME_TEST
, SERVER_TEST
, 80));
203 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST
, SERVER_LOCALHOST
, 8080));
204 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,8080));
205 return cdmServerInfoList
;
208 public String
getName() {
212 public String
getServer() {
217 public int getPort() {
222 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
223 if(instances
.isEmpty()) {
229 public static CdmRemoteSource
getDevServerRemoteSource() {
230 String value
= System
.getProperty("cdm.server.dev.port");
231 boolean available
= false;
232 CdmInstanceInfo devInstance
= null;
233 if(value
!= null && !value
.isEmpty()) {
234 int devPort
= Integer
.valueOf(value
);
235 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
);
237 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
238 available
= devCii
.pingInstance(devInstance
, devPort
);
240 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
242 } catch (Exception e
) {
249 public class CdmInstanceInfo
{
250 private final String name
;
251 private final String basePath
;
254 public CdmInstanceInfo(String name
, String basePath
) {
256 this.basePath
= basePath
;
260 public String
getName() {
264 public String
getBasePath() {