Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / source / CdmServerInfo.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.taxeditor.remoting.source;
11
12 import java.io.IOException;
13 import java.net.Socket;
14 import java.util.ArrayList;
15 import java.util.List;
16
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;
29
30 import eu.etaxonomy.cdm.config.CdmSourceException;
31 import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
32
33 /**
34 * @author cmathew
35 * @date 20 Jan 2015
36 *
37 */
38 public class CdmServerInfo {
39 public static final Logger logger = Logger.getLogger(CdmServerInfo.class);
40
41 private final static String NAME_PRODUCTION = "edit-production";
42 private final static String SERVER_PRODUCTION = "dev.e-taxonomy.eu";
43
44 private final static String NAME_INTEGRATION = "edit-integration";
45 private final static String SERVER_INTEGRATION = "int.e-taxonomy.eu";
46
47 private final static String NAME_TEST = "edit-test";
48 private final static String SERVER_TEST = "test.e-taxonomy.eu";
49
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.";
53
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 = "";
59
60 private final String name;
61 private final String server;
62 private final int port;
63 private final List<CdmInstanceInfo> instances;
64
65
66 public CdmServerInfo(String name, String server, int port) {
67 this.name = name;
68 this.server = server;
69 this.port = port;
70 instances = new ArrayList<CdmInstanceInfo>();
71 }
72
73
74 public CdmInstanceInfo addInstance(String name, String basePath) {
75 CdmInstanceInfo cii = new CdmInstanceInfo(name, basePath);
76 instances.add(cii);
77 return cii;
78
79 }
80
81 public void refreshInstances() throws CDMServerException {
82 String url = "http://" + server + ":" + String.valueOf(port) + "/cdmserver/instances.jsp";
83
84 HttpClient client = new DefaultHttpClient();
85 HttpGet httpGet = new HttpGet(url);
86
87 logger.info("Executing request " + httpGet.getRequestLine());
88
89 // Create a custom response handler
90 ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
91
92 @Override
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;
99 } else {
100 throw new ClientProtocolException("Unexpected response status: " + status);
101 }
102 }
103
104 };
105 String responseBody = null;
106 try {
107 responseBody = client.execute(httpGet, responseHandler);
108 } catch (ClientProtocolException e) {
109 throw new CDMServerException(e);
110 } catch (IOException e) {
111 throw new CDMServerException(e);
112 }
113
114
115 if(responseBody != null) {
116 try {
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");
122 if(conf != null) {
123 String instanceName = conf.getString("instanceName");
124 // we need to remove the first (char) forward slash from
125 // the base path
126 String basePath = conf.getString("basePath").substring(1);
127 addInstance(instanceName, basePath);
128 logger.info("Added instance with name : " + instanceName + ", basePath : " + basePath);
129 }
130 }
131 }
132 } catch (JSONException e) {
133 throw new CDMServerException(e);
134 }
135 }
136
137 }
138
139 public CdmInstanceInfo getInstanceFromName(String instanceName) {
140 if(instanceName == null) {
141 return null;
142 }
143
144 for(CdmInstanceInfo instance : instances) {
145 if(instance.getName() != null && instance.getName().equals(instanceName)) {
146 return instance;
147 }
148 }
149 return null;
150 }
151
152 public ICdmRemoteSource getCdmRemoteSource(CdmInstanceInfo instance) {
153 if(instance != null) {
154 return CdmRemoteSource.NewInstance(name,
155 server,
156 port,
157 instance.getBasePath(),
158 null);
159 }
160 return null;
161 }
162
163 public boolean pingServer(){
164 try {
165 Socket s = new Socket(server, port);
166 logger.info("[CDM-Server] Available @ " + server + ":" + port );
167 return true;
168 } catch (IOException ioe) {
169
170 }
171 return false;
172 }
173
174 public boolean pingInstance(CdmInstanceInfo instance) throws CDMServerException {
175
176 ICdmRemoteSource crs = getCdmRemoteSource(instance);
177 try {
178 if(crs != null && crs.checkConnection()) {
179 logger.info("[CDM-Server] Running @ " + server + ":" + port + " for instance " + instance);
180 return true;
181 }
182 } catch (CdmSourceException e) {
183 throw new CDMServerException(e);
184 }
185
186 return false;
187 }
188
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;
197 }
198
199 public String getName() {
200 return name;
201 }
202
203 public String getServer() {
204 return server;
205 }
206
207
208 public int getPort() {
209 return port;
210 }
211
212
213 public List<CdmInstanceInfo> getInstances() throws CDMServerException {
214 if(instances.isEmpty()) {
215 refreshInstances();
216 }
217 return instances;
218 }
219
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);
226 try {
227 devInstance = devCii.addInstance(NAME_INSTANCE_LOCALHOST_DEV, BASEPATH_LOCALHOST_DEV);
228 available = devCii.pingInstance(devInstance);
229 if(available) {
230 return devCii.getCdmRemoteSource(devInstance);
231 }
232 } catch (Exception e) {
233
234 }
235 }
236 return null;
237 }
238
239 public class CdmInstanceInfo {
240 private final String name;
241 private final String basePath;
242
243
244 public CdmInstanceInfo(String name, String basePath) {
245 this.name = name;
246 this.basePath = basePath;
247 }
248
249
250 public String getName() {
251 return name;
252 }
253
254 public String getBasePath() {
255 return basePath;
256 }
257 }
258 }