Set second demo server ip
[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.Collections;
16 import java.util.Comparator;
17 import java.util.List;
18
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;
31
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;
36
37 /**
38 * @author cmathew
39 * @date 20 Jan 2015
40 *
41 */
42 public class CdmServerInfo {
43 public static final Logger logger = Logger.getLogger(CdmServerInfo.class);
44
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";
48
49 private final static String NAME_INTEGRATION = "edit-integration";
50 private final static String SERVER_INTEGRATION = "int.e-taxonomy.eu";
51
52 private final static String NAME_DEMO_1 = "edit-WS I";
53 private final static String SERVER_DEMO_1 = "160.45.63.230";
54
55 private final static String NAME_DEMO_2 = "edit-WS II";
56 private final static String SERVER_DEMO_2 = "160.45.63.231";
57
58 private final static String NAME_TEST = "edit-test";
59 private final static String SERVER_TEST = "test.e-taxonomy.eu";
60
61 public final static String SERVER_LOCALHOST = "localhost";
62 private final static String NAME_LOCALHOST = "localhost";
63 public final static String NAME_LOCALHOST_MGD = "localhost mgd.";
64
65 private final static String NAME_LOCALHOST_DEV = "localhost-dev";
66 private final static String NAME_INSTANCE_LOCALHOST_DEV = "local-dev";
67 private final static String SERVER_LOCALHOST_DEV = "localhost";
68 private final static int PORT_LOCALHOST_DEV = 8080;
69 private final static String BASEPATH_LOCALHOST_DEV = "";
70
71 private final String name;
72 private final String server;
73 private final int port;
74 private final List<CdmInstanceInfo> instances;
75
76 private static List<CdmServerInfo> cdmServerInfoList;
77
78 public CdmServerInfo(String name, String server, int port) {
79 this.name = name;
80 this.server = server;
81 this.port = port;
82 instances = new ArrayList<CdmInstanceInfo>();
83 }
84
85
86 public CdmInstanceInfo addInstance(String name, String basePath) {
87 CdmInstanceInfo cii = new CdmInstanceInfo(name, basePath);
88 instances.add(cii);
89 return cii;
90
91 }
92
93 public boolean isLocalhost() {
94 return name.startsWith(SERVER_LOCALHOST);
95 }
96
97 public boolean isLocalhostMgd() {
98 return NAME_LOCALHOST_MGD.equals(name);
99 }
100
101 public void refreshInstances() throws CDMServerException {
102 instances.clear();
103 if(isLocalhostMgd()) {
104 addInstancesFromDataSourcesConfig();
105 } else {
106 addInstancesViaHttp();
107 }
108 Collections.sort(instances, new Comparator<CdmInstanceInfo>() {
109 @Override
110 public int compare(CdmInstanceInfo cii1, CdmInstanceInfo cii2)
111 {
112 return cii1.getName().toString().compareTo(cii2.getName().toString());
113 }
114 });
115 }
116
117 public void addInstancesViaHttp() throws CDMServerException {
118 String url = "http://" + server + ":" + String.valueOf(port) + "/" + CDMSERVER_PREFIX + "/instances.jsp";
119
120 HttpClient client = new DefaultHttpClient();
121 HttpGet httpGet = new HttpGet(url);
122
123 logger.info("Executing request " + httpGet.getRequestLine());
124
125 // Create a custom response handler
126 ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
127
128 @Override
129 public String handleResponse(
130 final HttpResponse response) throws ClientProtocolException, IOException {
131 int status = response.getStatusLine().getStatusCode();
132 if (status >= 200 && status < 300) {
133 HttpEntity entity = response.getEntity();
134 return entity != null ? EntityUtils.toString(entity) : null;
135 } else {
136 throw new ClientProtocolException("Unexpected response status: " + status);
137 }
138 }
139
140 };
141 String responseBody = null;
142 try {
143 responseBody = client.execute(httpGet, responseHandler);
144 } catch (ClientProtocolException e) {
145 throw new CDMServerException(e);
146 } catch (IOException e) {
147 throw new CDMServerException(e);
148 }
149
150
151 if(responseBody != null) {
152 try {
153 JSONArray array = new JSONArray(responseBody);
154 for(int i=0;i<array.length();i++) {
155 JSONObject instance = (JSONObject)array.get(i);
156 if(instance != null) {
157 JSONObject conf = (JSONObject)instance.get("configuration");
158 if(conf != null) {
159 String instanceName = conf.getString("instanceName");
160 // we need to remove the first (char) forward slash from
161 // the base path
162 String basePath = conf.getString("basePath").substring(1);
163 addInstance(instanceName, basePath);
164 logger.info("Added instance with name : " + instanceName + ", basePath : " + basePath);
165 }
166 }
167 }
168 } catch (JSONException e) {
169 throw new CDMServerException(e);
170 }
171 }
172 }
173
174 public void addInstancesFromDataSourcesConfig() {
175
176 for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
177 logger.warn("Adding local instance " + dataSource.getName());
178 addInstance(dataSource.getName(), dataSource.getName());
179 }
180
181 }
182
183 public String toString(String instanceName, int port) {
184 return server + ":" + String.valueOf(port) + "/" + instanceName;
185 }
186
187 public CdmInstanceInfo getInstanceFromName(String instanceName) {
188 if(instanceName == null) {
189 return null;
190 }
191
192 for(CdmInstanceInfo instance : instances) {
193 if(instance.getName() != null && instance.getName().equals(instanceName)) {
194 return instance;
195 }
196 }
197 return null;
198 }
199
200 public CdmRemoteSource getCdmRemoteSource(CdmInstanceInfo instance, int port) {
201 if(instance != null) {
202 return CdmRemoteSource.NewInstance(name,
203 server,
204 port,
205 instance.getBasePath(),
206 null);
207 }
208 return null;
209 }
210
211 public boolean pingServer(){
212 try {
213 Socket s = new Socket(server, port);
214 logger.info("[CDM-Server] Available @ " + server + ":" + port );
215 return true;
216 } catch (IOException ioe) {
217
218 }
219 return false;
220 }
221
222 public boolean pingInstance(CdmInstanceInfo instance, int port) throws CDMServerException {
223
224 ICdmRemoteSource crs = getCdmRemoteSource(instance, port);
225 try {
226 if(crs != null && crs.checkConnection()) {
227 logger.info("[CDM-Server] Running @ " + server + ":" + port + " for instance " + instance);
228 return true;
229 }
230 } catch (CdmSourceException e) {
231 throw new CDMServerException(e);
232 }
233
234 return false;
235 }
236
237 public static List<CdmServerInfo> getCdmServers() {
238 if(cdmServerInfoList == null) {
239 cdmServerInfoList = new ArrayList<CdmServerInfo>();
240 //cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80));
241 //cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
242 cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_1, SERVER_DEMO_1, 80));
243 cdmServerInfoList.add(new CdmServerInfo(NAME_DEMO_2, SERVER_DEMO_2, 80));
244 cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
245 cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
246 cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST_MGD, SERVER_LOCALHOST,8080));
247 }
248 return cdmServerInfoList;
249 }
250
251 public String getName() {
252 return name;
253 }
254
255 public String getServer() {
256 return server;
257 }
258
259
260 public int getPort() {
261 return port;
262 }
263
264
265 public List<CdmInstanceInfo> getInstances() throws CDMServerException {
266 if(instances.isEmpty()) {
267 refreshInstances();
268 }
269 return instances;
270 }
271
272 public static CdmRemoteSource getDevServerRemoteSource() {
273 String value = System.getProperty("cdm.server.dev.port");
274 boolean available = false;
275 CdmInstanceInfo devInstance = null;
276 if(value != null && !value.isEmpty()) {
277 int devPort = Integer.valueOf(value);
278 CdmServerInfo devCii = new CdmServerInfo(NAME_LOCALHOST_DEV, SERVER_LOCALHOST_DEV, devPort);
279 try {
280 devInstance = devCii.addInstance(NAME_INSTANCE_LOCALHOST_DEV, BASEPATH_LOCALHOST_DEV);
281 available = devCii.pingInstance(devInstance, devPort);
282 if(available) {
283 return devCii.getCdmRemoteSource(devInstance, devPort);
284 }
285 } catch (Exception e) {
286
287 }
288 }
289 return null;
290 }
291
292 public class CdmInstanceInfo {
293 private final String name;
294
295 /**
296 * The full path of the instance including the the prefix (if any).
297 * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
298 * For a managed local server this would simply be "remoting"
299 */
300 private final String basePath;
301
302
303 public CdmInstanceInfo(String name, String basePath) {
304 this.name = name;
305 this.basePath = basePath;
306 }
307
308
309 public String getName() {
310 return name;
311 }
312
313 public String getBasePath() {
314 return basePath;
315 }
316 }
317 }