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 NAME_PRODUCTION
= "edit-production";
42 private final static String SERVER_PRODUCTION
= "dev.e-taxonomy.eu";
44 private final static String NAME_INTEGRATION
= "edit-integration";
45 private final static String SERVER_INTEGRATION
= "int.e-taxonomy.eu";
47 private final static String NAME_TEST
= "edit-test";
48 private final static String SERVER_TEST
= "test.e-taxonomy.eu";
50 private final static String SERVER_LOCALHOST
= "localhost";
51 private final static String NAME_LOCALHOST
= "localhost";
52 private final static String NAME_LOCALHOST_MGD
= "localhost mgd.";
54 private final static String NAME_LOCALHOST_DEV
= "localhost-dev";
55 private final static String NAME_INSTANCE_LOCALHOST_DEV
= "local-dev";
56 private final static String SERVER_LOCALHOST_DEV
= "localhost";
57 private final static int PORT_LOCALHOST_DEV
= 8080;
58 private final static String BASEPATH_LOCALHOST_DEV
= "";
60 private final String name
;
61 private final String server
;
62 private final int port
;
63 private final List
<CdmInstanceInfo
> instances
;
66 public CdmServerInfo(String name
, String server
, int port
) {
70 instances
= new ArrayList
<CdmInstanceInfo
>();
74 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
75 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, basePath
);
81 public void refreshInstances() throws CDMServerException
{
82 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/cdmserver/instances.jsp";
84 HttpClient client
= new DefaultHttpClient();
85 HttpGet httpGet
= new HttpGet(url
);
87 logger
.info("Executing request " + httpGet
.getRequestLine());
89 // Create a custom response handler
90 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
93 public String
handleResponse(
94 final HttpResponse response
) throws ClientProtocolException
, IOException
{
95 int status
= response
.getStatusLine().getStatusCode();
96 if (status
>= 200 && status
< 300) {
97 HttpEntity entity
= response
.getEntity();
98 return entity
!= null ? EntityUtils
.toString(entity
) : null;
100 throw new ClientProtocolException("Unexpected response status: " + status
);
105 String responseBody
= null;
107 responseBody
= client
.execute(httpGet
, responseHandler
);
108 } catch (ClientProtocolException e
) {
109 throw new CDMServerException(e
);
110 } catch (IOException e
) {
111 throw new CDMServerException(e
);
115 if(responseBody
!= null) {
117 JSONArray array
= new JSONArray(responseBody
);
118 for(int i
=0;i
<array
.length();i
++) {
119 JSONObject instance
= (JSONObject
)array
.get(i
);
120 if(instance
!= null) {
121 JSONObject conf
= (JSONObject
)instance
.get("configuration");
123 String instanceName
= conf
.getString("instanceName");
124 // we need to remove the first (char) forward slash from
126 String basePath
= conf
.getString("basePath").substring(1);
127 addInstance(instanceName
, basePath
);
128 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
132 } catch (JSONException e
) {
133 throw new CDMServerException(e
);
139 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
140 if(instanceName
== null) {
144 for(CdmInstanceInfo instance
: instances
) {
145 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
152 public ICdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
) {
153 if(instance
!= null) {
154 return CdmRemoteSource
.NewInstance(name
,
157 instance
.getBasePath(),
163 public boolean pingServer(){
165 Socket s
= new Socket(server
, port
);
166 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
168 } catch (IOException ioe
) {
174 public boolean pingInstance(CdmInstanceInfo instance
) throws CDMServerException
{
176 ICdmRemoteSource crs
= getCdmRemoteSource(instance
);
178 if(crs
!= null && crs
.checkConnection()) {
179 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
182 } catch (CdmSourceException e
) {
183 throw new CDMServerException(e
);
189 public static List
<CdmServerInfo
> getCdmServers() {
190 List
<CdmServerInfo
> cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
191 cdmServerInfoList
.add(new CdmServerInfo(NAME_PRODUCTION
, SERVER_PRODUCTION
, 80));
192 cdmServerInfoList
.add(new CdmServerInfo(NAME_INTEGRATION
, SERVER_INTEGRATION
, 80));
193 cdmServerInfoList
.add(new CdmServerInfo(NAME_TEST
, SERVER_TEST
, 80));
194 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST
, SERVER_LOCALHOST
, 8080));
195 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,8080));
196 return cdmServerInfoList
;
199 public String
getName() {
203 public String
getServer() {
208 public int getPort() {
213 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
214 if(instances
.isEmpty()) {
220 public static ICdmRemoteSource
getDevServerRemoteSource() {
221 String value
=System
.getProperty("cdm.server.dev.activate");
222 boolean available
= false;
223 CdmInstanceInfo devInstance
= null;
224 if(value
!= null && value
.equals("true")) {
225 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, PORT_LOCALHOST_DEV
);
227 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
228 available
= devCii
.pingInstance(devInstance
);
230 return devCii
.getCdmRemoteSource(devInstance
);
232 } catch (Exception e
) {
239 public class CdmInstanceInfo
{
240 private final String name
;
241 private final String basePath
;
244 public CdmInstanceInfo(String name
, String basePath
) {
246 this.basePath
= basePath
;
250 public String
getName() {
254 public String
getBasePath() {