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.cdm.database.CdmPersistentDataSource;
32 import eu.etaxonomy.cdm.database.ICdmDataSource;
33 import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
34
35 /**
36 * @author cmathew
37 * @date 20 Jan 2015
38 *
39 */
40 public class CdmServerInfo {
41 public static final Logger logger = Logger.getLogger(CdmServerInfo.class);
42
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";
46
47 private final static String NAME_INTEGRATION = "edit-integration";
48 private final static String SERVER_INTEGRATION = "int.e-taxonomy.eu";
49
50 private final static String NAME_TEST = "edit-test";
51 private final static String SERVER_TEST = "test.e-taxonomy.eu";
52
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.";
56
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 = "";
62
63 private final String name;
64 private final String server;
65 private final int port;
66 private final List<CdmInstanceInfo> instances;
67
68 private static List<CdmServerInfo> cdmServerInfoList;
69
70 public CdmServerInfo(String name, String server, int port) {
71 this.name = name;
72 this.server = server;
73 this.port = port;
74 instances = new ArrayList<CdmInstanceInfo>();
75 }
76
77
78 public CdmInstanceInfo addInstance(String name, String basePath) {
79 CdmInstanceInfo cii = new CdmInstanceInfo(name, basePath);
80 instances.add(cii);
81 return cii;
82
83 }
84
85 public boolean isLocalhost() {
86 return name.startsWith(SERVER_LOCALHOST);
87 }
88
89 public boolean isLocalhostMgd() {
90 return NAME_LOCALHOST_MGD.equals(name);
91 }
92
93 public void refreshInstances() throws CDMServerException {
94 instances.clear();
95 if(isLocalhostMgd()) {
96 addInstancesFromDataSourcesConfig();
97 } else {
98 addInstancesViaHttp();
99 }
100
101 }
102
103 public void addInstancesViaHttp() throws CDMServerException {
104 String url = "http://" + server + ":" + String.valueOf(port) + "/" + CDMSERVER_PREFIX + "/instances.jsp";
105
106 HttpClient client = new DefaultHttpClient();
107 HttpGet httpGet = new HttpGet(url);
108
109 logger.info("Executing request " + httpGet.getRequestLine());
110
111 // Create a custom response handler
112 ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
113
114 @Override
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;
121 } else {
122 throw new ClientProtocolException("Unexpected response status: " + status);
123 }
124 }
125
126 };
127 String responseBody = null;
128 try {
129 responseBody = client.execute(httpGet, responseHandler);
130 } catch (ClientProtocolException e) {
131 throw new CDMServerException(e);
132 } catch (IOException e) {
133 throw new CDMServerException(e);
134 }
135
136
137 if(responseBody != null) {
138 try {
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");
144 if(conf != null) {
145 String instanceName = conf.getString("instanceName");
146 // we need to remove the first (char) forward slash from
147 // the base path
148 String basePath = conf.getString("basePath").substring(1);
149 addInstance(instanceName, basePath);
150 logger.info("Added instance with name : " + instanceName + ", basePath : " + basePath);
151 }
152 }
153 }
154 } catch (JSONException e) {
155 throw new CDMServerException(e);
156 }
157 }
158 }
159
160 public void addInstancesFromDataSourcesConfig() {
161
162 for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
163 logger.warn("Adding local instance " + dataSource.getName());
164 addInstance(dataSource.getName(), dataSource.getName());
165 }
166
167 }
168
169 public String toString(String instanceName, int port) {
170 return server + ":" + String.valueOf(port) + "/" + instanceName;
171 }
172
173 public CdmInstanceInfo getInstanceFromName(String instanceName) {
174 if(instanceName == null) {
175 return null;
176 }
177
178 for(CdmInstanceInfo instance : instances) {
179 if(instance.getName() != null && instance.getName().equals(instanceName)) {
180 return instance;
181 }
182 }
183 return null;
184 }
185
186 public CdmRemoteSource getCdmRemoteSource(CdmInstanceInfo instance, int port) {
187 if(instance != null) {
188 return CdmRemoteSource.NewInstance(name,
189 server,
190 port,
191 instance.getBasePath(),
192 null);
193 }
194 return null;
195 }
196
197 public boolean pingServer(){
198 try {
199 Socket s = new Socket(server, port);
200 logger.info("[CDM-Server] Available @ " + server + ":" + port );
201 return true;
202 } catch (IOException ioe) {
203
204 }
205 return false;
206 }
207
208 public boolean pingInstance(CdmInstanceInfo instance, int port) throws CDMServerException {
209
210 ICdmRemoteSource crs = getCdmRemoteSource(instance, port);
211 try {
212 if(crs != null && crs.checkConnection()) {
213 logger.info("[CDM-Server] Running @ " + server + ":" + port + " for instance " + instance);
214 return true;
215 }
216 } catch (CdmSourceException e) {
217 throw new CDMServerException(e);
218 }
219
220 return false;
221 }
222
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));
231 }
232 return cdmServerInfoList;
233 }
234
235 public String getName() {
236 return name;
237 }
238
239 public String getServer() {
240 return server;
241 }
242
243
244 public int getPort() {
245 return port;
246 }
247
248
249 public List<CdmInstanceInfo> getInstances() throws CDMServerException {
250 if(instances.isEmpty()) {
251 refreshInstances();
252 }
253 return instances;
254 }
255
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);
263 try {
264 devInstance = devCii.addInstance(NAME_INSTANCE_LOCALHOST_DEV, BASEPATH_LOCALHOST_DEV);
265 available = devCii.pingInstance(devInstance, devPort);
266 if(available) {
267 return devCii.getCdmRemoteSource(devInstance, devPort);
268 }
269 } catch (Exception e) {
270
271 }
272 }
273 return null;
274 }
275
276 public class CdmInstanceInfo {
277 private final String name;
278
279 /**
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"
283 */
284 private final String basePath;
285
286
287 public CdmInstanceInfo(String name, String basePath) {
288 this.name = name;
289 this.basePath = basePath;
290 }
291
292
293 public String getName() {
294 return name;
295 }
296
297 public String getBasePath() {
298 return basePath;
299 }
300 }
301 }