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_TEST
= "edit-test";
53 private final static String SERVER_TEST
= "test.e-taxonomy.eu";
55 public final static String SERVER_LOCALHOST
= "localhost";
56 private final static String NAME_LOCALHOST
= "localhost";
57 public final static String NAME_LOCALHOST_MGD
= "localhost mgd.";
59 private final static String NAME_LOCALHOST_DEV
= "localhost-dev";
60 private final static String NAME_INSTANCE_LOCALHOST_DEV
= "local-dev";
61 private final static String SERVER_LOCALHOST_DEV
= "localhost";
62 private final static int PORT_LOCALHOST_DEV
= 8080;
63 private final static String BASEPATH_LOCALHOST_DEV
= "";
65 private final String name
;
66 private final String server
;
67 private final int port
;
68 private final List
<CdmInstanceInfo
> instances
;
70 private static List
<CdmServerInfo
> cdmServerInfoList
;
72 public CdmServerInfo(String name
, String server
, int port
) {
76 instances
= new ArrayList
<CdmInstanceInfo
>();
80 public CdmInstanceInfo
addInstance(String name
, String basePath
) {
81 CdmInstanceInfo cii
= new CdmInstanceInfo(name
, basePath
);
87 public boolean isLocalhost() {
88 return name
.startsWith(SERVER_LOCALHOST
);
91 public boolean isLocalhostMgd() {
92 return NAME_LOCALHOST_MGD
.equals(name
);
95 public void refreshInstances() throws CDMServerException
{
97 if(isLocalhostMgd()) {
98 addInstancesFromDataSourcesConfig();
100 addInstancesViaHttp();
102 Collections
.sort(instances
, new Comparator
<CdmInstanceInfo
>() {
104 public int compare(CdmInstanceInfo cii1
, CdmInstanceInfo cii2
)
106 return cii1
.getName().toString().compareTo(cii2
.getName().toString());
111 public void addInstancesViaHttp() throws CDMServerException
{
112 String url
= "http://" + server
+ ":" + String
.valueOf(port
) + "/" + CDMSERVER_PREFIX
+ "/instances.jsp";
114 HttpClient client
= new DefaultHttpClient();
115 HttpGet httpGet
= new HttpGet(url
);
117 logger
.info("Executing request " + httpGet
.getRequestLine());
119 // Create a custom response handler
120 ResponseHandler
<String
> responseHandler
= new ResponseHandler
<String
>() {
123 public String
handleResponse(
124 final HttpResponse response
) throws ClientProtocolException
, IOException
{
125 int status
= response
.getStatusLine().getStatusCode();
126 if (status
>= 200 && status
< 300) {
127 HttpEntity entity
= response
.getEntity();
128 return entity
!= null ? EntityUtils
.toString(entity
) : null;
130 throw new ClientProtocolException("Unexpected response status: " + status
);
135 String responseBody
= null;
137 responseBody
= client
.execute(httpGet
, responseHandler
);
138 } catch (ClientProtocolException e
) {
139 throw new CDMServerException(e
);
140 } catch (IOException e
) {
141 throw new CDMServerException(e
);
145 if(responseBody
!= null) {
147 JSONArray array
= new JSONArray(responseBody
);
148 for(int i
=0;i
<array
.length();i
++) {
149 JSONObject instance
= (JSONObject
)array
.get(i
);
150 if(instance
!= null) {
151 JSONObject conf
= (JSONObject
)instance
.get("configuration");
153 String instanceName
= conf
.getString("instanceName");
154 // we need to remove the first (char) forward slash from
156 String basePath
= conf
.getString("basePath").substring(1);
157 addInstance(instanceName
, basePath
);
158 logger
.info("Added instance with name : " + instanceName
+ ", basePath : " + basePath
);
162 } catch (JSONException e
) {
163 throw new CDMServerException(e
);
168 public void addInstancesFromDataSourcesConfig() {
170 for(ICdmDataSource dataSource
: CdmPersistentDataSource
.getAllDataSources()){
171 logger
.warn("Adding local instance " + dataSource
.getName());
172 addInstance(dataSource
.getName(), dataSource
.getName());
177 public String
toString(String instanceName
, int port
) {
178 return server
+ ":" + String
.valueOf(port
) + "/" + instanceName
;
181 public CdmInstanceInfo
getInstanceFromName(String instanceName
) {
182 if(instanceName
== null) {
186 for(CdmInstanceInfo instance
: instances
) {
187 if(instance
.getName() != null && instance
.getName().equals(instanceName
)) {
194 public CdmRemoteSource
getCdmRemoteSource(CdmInstanceInfo instance
, int port
) {
195 if(instance
!= null) {
196 return CdmRemoteSource
.NewInstance(name
,
199 instance
.getBasePath(),
205 public boolean pingServer(){
207 Socket s
= new Socket(server
, port
);
208 logger
.info("[CDM-Server] Available @ " + server
+ ":" + port
);
210 } catch (IOException ioe
) {
216 public boolean pingInstance(CdmInstanceInfo instance
, int port
) throws CDMServerException
{
218 ICdmRemoteSource crs
= getCdmRemoteSource(instance
, port
);
220 if(crs
!= null && crs
.checkConnection()) {
221 logger
.info("[CDM-Server] Running @ " + server
+ ":" + port
+ " for instance " + instance
);
224 } catch (CdmSourceException e
) {
225 throw new CDMServerException(e
);
231 public static List
<CdmServerInfo
> getCdmServers() {
232 if(cdmServerInfoList
== null) {
233 cdmServerInfoList
= new ArrayList
<CdmServerInfo
>();
234 cdmServerInfoList
.add(new CdmServerInfo(NAME_PRODUCTION
, SERVER_PRODUCTION
, 80));
235 //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
236 cdmServerInfoList
.add(new CdmServerInfo(NAME_TEST
, SERVER_TEST
, 80));
237 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST
, SERVER_LOCALHOST
, 8080));
238 cdmServerInfoList
.add(new CdmServerInfo(NAME_LOCALHOST_MGD
, SERVER_LOCALHOST
,8080));
240 return cdmServerInfoList
;
243 public String
getName() {
247 public String
getServer() {
252 public int getPort() {
257 public List
<CdmInstanceInfo
> getInstances() throws CDMServerException
{
258 if(instances
.isEmpty()) {
264 public static CdmRemoteSource
getDevServerRemoteSource() {
265 String value
= System
.getProperty("cdm.server.dev.port");
266 boolean available
= false;
267 CdmInstanceInfo devInstance
= null;
268 if(value
!= null && !value
.isEmpty()) {
269 int devPort
= Integer
.valueOf(value
);
270 CdmServerInfo devCii
= new CdmServerInfo(NAME_LOCALHOST_DEV
, SERVER_LOCALHOST_DEV
, devPort
);
272 devInstance
= devCii
.addInstance(NAME_INSTANCE_LOCALHOST_DEV
, BASEPATH_LOCALHOST_DEV
);
273 available
= devCii
.pingInstance(devInstance
, devPort
);
275 return devCii
.getCdmRemoteSource(devInstance
, devPort
);
277 } catch (Exception e
) {
284 public class CdmInstanceInfo
{
285 private final String name
;
288 * The full path of the instance including the the prefix (if any).
289 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
290 * For a managed local server this would simply be "remoting"
292 private final String basePath
;
295 public CdmInstanceInfo(String name
, String basePath
) {
297 this.basePath
= basePath
;
301 public String
getName() {
305 public String
getBasePath() {