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 String name
;
55 private final String server
;
56 private final int port
;
57 private final List
<CdmInstanceInfo
> instances
;
60 public CdmServerInfo(String name
, String server
, int port
) {
64 instances
= new ArrayList
<CdmInstanceInfo
>();
68 public void refreshInstances() throws CDMServerException
{
69 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/cdmserver/instances.jsp";
71 HttpClient client
= new DefaultHttpClient();
72 HttpGet httpGet
= new HttpGet(url
);
74 logger
.info("Executing request " + httpGet
.getRequestLine());
76 // Create a custom response handler
77 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
80 public String
handleResponse(
81 final HttpResponse response
) throws ClientProtocolException
, IOException
{
82 int status
= response
.getStatusLine().getStatusCode();
83 if (status
>= 200 && status
< 300) {
84 HttpEntity entity
= response
.getEntity();
85 return entity
!= null ? EntityUtils
.toString(entity
) : null;
87 throw new ClientProtocolException("Unexpected response status: " + status
);
92 String responseBody
= null;
94 responseBody
= client
.execute(httpGet
, responseHandler
);
95 } catch (ClientProtocolException e
) {
96 throw new CDMServerException(e
);
97 } catch (IOException e
) {
98 throw new CDMServerException(e
);
102 if(responseBody
!= null) {
104 JSONArray array
= new JSONArray(responseBody
);
105 for(int i
=0;i
<array
.length();i
++) {
106 JSONObject instance
= (JSONObject
)array
.get(i
);
107 if(instance
!= null) {
108 JSONObject conf
= (JSONObject
)instance
.get("configuration");
110 String instanceName
= conf
.getString("instanceName");
111 // we need to remove the first (char) forward slash from
113 String basePath
= conf
.getString("basePath").substring(1);
114 instances
.add(new CdmInstanceInfo(instanceName
, basePath
));
115 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
119 } catch (JSONException e
) {
120 throw new CDMServerException(e
);
126 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
127 if(instanceName
== null) {
131 for(CdmInstanceInfo instance
: instances
) {
132 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
139 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
) {
140 if(instance
!= null) {
141 return CdmRemoteSource
.NewInstance(name
,
144 instance
.getBasePath(),
150 public boolean pingServer(){
152 Socket s
= new Socket(server
, port
);
153 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
155 } catch (IOException ioe
) {
161 public boolean pingInstance(CdmInstanceInfo instance
) throws CDMServerException
{
163 CdmRemoteSource crs
= getCdmRemoteSource(instance
);
165 if(crs
!= null && crs
.checkConnection()) {
166 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
169 } catch (CdmSourceException e
) {
170 throw new CDMServerException(e
);
176 public static List
<CdmServerInfo
> getCdmServers() {
177 List
<CdmServerInfo
> cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
178 cdmServerInfoList
.add(new CdmServerInfo(NAME_PRODUCTION
, SERVER_PRODUCTION
, 80));
179 cdmServerInfoList
.add(new CdmServerInfo(NAME_INTEGRATION
, SERVER_INTEGRATION
, 80));
180 cdmServerInfoList
.add(new CdmServerInfo(NAME_TEST
, SERVER_TEST
, 80));
181 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST
, SERVER_LOCALHOST
, 8080));
182 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,8080));
183 return cdmServerInfoList
;
186 public String
getName() {
190 public String
getServer() {
195 public int getPort() {
200 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
201 if(instances
.isEmpty()) {
207 public class CdmInstanceInfo
{
208 private final String name
;
209 private final String basePath
;
212 public CdmInstanceInfo(String name
, String basePath
) {
214 this.basePath
= basePath
;
218 public String
getName() {
222 public String
getBasePath() {