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
.cdm
.database
.CdmPersistentDataSource
;
32 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.server
.CDMServerException
;
40 public class CdmServerInfo
{
41 public static final Logger logger
= Logger
.getLogger(CdmServerInfo
.class);
43 private final static String CDMSERVER_PREFIX
= "cdmserver";
44 private final static String NAME_PRODUCTION
= "edit-production";
45 private final static String SERVER_PRODUCTION
= "dev.e-taxonomy.eu";
47 private final static String NAME_INTEGRATION
= "edit-integration";
48 private final static String SERVER_INTEGRATION
= "int.e-taxonomy.eu";
50 private final static String NAME_TEST
= "edit-test";
51 private final static String SERVER_TEST
= "test.e-taxonomy.eu";
53 public final static String SERVER_LOCALHOST
= "localhost";
54 private final static String NAME_LOCALHOST
= "localhost";
55 public final static String NAME_LOCALHOST_MGD
= "localhost mgd.";
57 private final static String NAME_LOCALHOST_DEV
= "localhost-dev";
58 private final static String NAME_INSTANCE_LOCALHOST_DEV
= "local-dev";
59 private final static String SERVER_LOCALHOST_DEV
= "localhost";
60 private final static int PORT_LOCALHOST_DEV
= 8080;
61 private final static String BASEPATH_LOCALHOST_DEV
= "";
63 private final String name
;
64 private final String server
;
65 private final int port
;
66 private final List
<CdmInstanceInfo
> instances
;
68 private static List
<CdmServerInfo
> cdmServerInfoList
;
70 public CdmServerInfo(String name
, String server
, int port
) {
74 instances
= new ArrayList
<CdmInstanceInfo
>();
78 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
79 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, basePath
);
85 public boolean isLocalhost() {
86 return name
.startsWith(SERVER_LOCALHOST
);
89 public boolean isLocalhostMgd() {
90 return NAME_LOCALHOST_MGD
.equals(name
);
93 public void refreshInstances() throws CDMServerException
{
95 if(isLocalhostMgd()) {
96 addInstancesFromDataSourcesConfig();
98 addInstancesViaHttp();
103 public void addInstancesViaHttp() throws CDMServerException
{
104 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/instances.jsp";
106 HttpClient client
= new DefaultHttpClient();
107 HttpGet httpGet
= new HttpGet(url
);
109 logger
.info("Executing request " + httpGet
.getRequestLine());
111 // Create a custom response handler
112 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
115 public String
handleResponse(
116 final HttpResponse response
) throws ClientProtocolException
, IOException
{
117 int status
= response
.getStatusLine().getStatusCode();
118 if (status
>= 200 && status
< 300) {
119 HttpEntity entity
= response
.getEntity();
120 return entity
!= null ? EntityUtils
.toString(entity
) : null;
122 throw new ClientProtocolException("Unexpected response status: " + status
);
127 String responseBody
= null;
129 responseBody
= client
.execute(httpGet
, responseHandler
);
130 } catch (ClientProtocolException e
) {
131 throw new CDMServerException(e
);
132 } catch (IOException e
) {
133 throw new CDMServerException(e
);
137 if(responseBody
!= null) {
139 JSONArray array
= new JSONArray(responseBody
);
140 for(int i
=0;i
<array
.length();i
++) {
141 JSONObject instance
= (JSONObject
)array
.get(i
);
142 if(instance
!= null) {
143 JSONObject conf
= (JSONObject
)instance
.get("configuration");
145 String instanceName
= conf
.getString("instanceName");
146 // we need to remove the first (char) forward slash from
148 String basePath
= conf
.getString("basePath").substring(1);
149 addInstance(instanceName
, basePath
);
150 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
154 } catch (JSONException e
) {
155 throw new CDMServerException(e
);
160 public void addInstancesFromDataSourcesConfig() {
162 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
163 logger
.warn("Adding local instance " + dataSource
.getName());
164 addInstance(dataSource
.getName(), dataSource
.getName());
169 public String
toString(String instanceName
, int port
) {
170 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
173 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
174 if(instanceName
== null) {
178 for(CdmInstanceInfo instance
: instances
) {
179 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
186 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
187 if(instance
!= null) {
188 return CdmRemoteSource
.NewInstance(name
,
191 instance
.getBasePath(),
197 public boolean pingServer(){
199 Socket s
= new Socket(server
, port
);
200 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
202 } catch (IOException ioe
) {
208 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
210 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
212 if(crs
!= null && crs
.checkConnection()) {
213 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
216 } catch (CdmSourceException e
) {
217 throw new CDMServerException(e
);
223 public static List
<CdmServerInfo
> getCdmServers() {
224 if(cdmServerInfoList
== null) {
225 cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
226 cdmServerInfoList
.add(new CdmServerInfo(NAME_PRODUCTION
, SERVER_PRODUCTION
, 80));
227 cdmServerInfoList
.add(new CdmServerInfo(NAME_INTEGRATION
, SERVER_INTEGRATION
, 80));
228 cdmServerInfoList
.add(new CdmServerInfo(NAME_TEST
, SERVER_TEST
, 80));
229 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST
, SERVER_LOCALHOST
, 8080));
230 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,8080));
232 return cdmServerInfoList
;
235 public String
getName() {
239 public String
getServer() {
244 public int getPort() {
249 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
250 if(instances
.isEmpty()) {
256 public static CdmRemoteSource
getDevServerRemoteSource() {
257 String value
= System
.getProperty("cdm.server.dev.port");
258 boolean available
= false;
259 CdmInstanceInfo devInstance
= null;
260 if(value
!= null && !value
.isEmpty()) {
261 int devPort
= Integer
.valueOf(value
);
262 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
);
264 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
265 available
= devCii
.pingInstance(devInstance
, devPort
);
267 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
269 } catch (Exception e
) {
276 public class CdmInstanceInfo
{
277 private final String name
;
280 * The full path of the instance including the the prefix (if any).
281 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
282 * For a managed local server this would simply be "remoting"
284 private final String basePath
;
287 public CdmInstanceInfo(String name
, String basePath
) {
289 this.basePath
= basePath
;
293 public String
getName() {
297 public String
getBasePath() {