forked from ariatemplates/editor-frontend-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackend.java
More file actions
398 lines (313 loc) · 12.5 KB
/
Backend.java
File metadata and controls
398 lines (313 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package poc;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import poc.document.POCDocument;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
public class Backend {
/***************************************************************************
* Singleton
**************************************************************************/
private static Backend singleton = null;
/**
* Returns a singleton object, for convenience.
*
* Indeed, nothing prevents you from creating and managing your own instances.
*
* @return A singleton.
*/
public static Backend get() {
if (Backend.singleton == null) {
Backend.singleton = new Backend();
}
return Backend.singleton;
}
/***************************************************************************
* Initialization
**************************************************************************/
// -------------------------------------------------------------------- JSON
private Gson gson = null;
// -------------------------------------------------------------------- HTTP
private DefaultHttpClient httpclient = null;
// POST --------------------------------------------------------------------
private HttpPost rpc = null;
private static final String URL_PATH_RPC = "rpc";
private static final String HEADER_CONTENT_TYPE = "Content-Type";
private static final String HEADER_VALUE_CONTENT_TYPE = "application/json";
// GET ---------------------------------------------------------------------
private HttpGet shutdown = null;
private static final String URL_PATH_SHUTDOWN = "shutdown";
private HttpGet ping = null;
private static final String URL_PATH_PING = "ping";
private HttpGet guid = null;
private static final String URL_PATH_GUID = "80d007698d534c3d9355667f462af2b0";
// -------------------------------------------------------------------- URLs
private static final String URL_BASE = "http://localhost:3000/";
//
/**
* Builds a new backend instance.
*/
public Backend() {
this.httpclient = new DefaultHttpClient();
this.rpc = new HttpPost(Backend.URL_BASE + Backend.URL_PATH_RPC);
this.rpc.setHeader(Backend.HEADER_CONTENT_TYPE, Backend.HEADER_VALUE_CONTENT_TYPE);
this.shutdown = new HttpGet(Backend.URL_BASE + Backend.URL_PATH_SHUTDOWN);
this.ping = new HttpGet(Backend.URL_BASE + Backend.URL_PATH_PING);
this.guid = new HttpGet(Backend.URL_BASE + Backend.URL_PATH_GUID);
this.gson = new Gson();
}
/***************************************************************************
* Backend runtime management
**************************************************************************/
// FIXME Don't make it hard-coded and absolute!
// Use Eclipse preferences system
// Otherwise use PATH
// Otherwise use packaged node version, with a relative path
//private static final String programPath = "resources/app/";
private static final String[] command = {
"editor-backend"
};
private static final String OUTPUT_GUID = "e531ebf04fad4e17b890c0ac72789956";
private static final int POLLING_SLEEP_TIME = 50; // ms
private static final int POLLING_TIME_OUT = 1000; // ms
private Process process = null;
private Boolean isManagedExternally = null;
/**
* Tells whether the external backend server is running or not.
*
* It uses the safe identification method, to ensure that the server has not been unfortunately replaced. Otherwise, the ping method could have been used.
*/
private Boolean isExternalBackendRunning() {
try {
HttpResponse response = this.get(this.guid);
if (response.getStatusLine().getStatusCode() != 200) {
return false;
}
return EntityUtils.toString(response.getEntity()).equals(Backend.OUTPUT_GUID);
} catch (IOException exception) {
return false;
}
}
/**
* Tells whether the backend is running or not.
*
* @return true if the backend is running, false otherwise.
* @throws IOException
*/
public Boolean isRunning() {
// We don't know if it is an external process or not yet ---------------
// (first check in the program or after a stop)
if (this.isManagedExternally == null) {
if (this.isExternalBackendRunning()) {
this.isManagedExternally = true;
return true; // to avoid unnecessary duplicate request below
} else {
this.isManagedExternally = false;
}
}
// Externally managed --------------------------------------------------
if (this.isManagedExternally) {
return this.isExternalBackendRunning();
}
// We manage the process ourself ---------------------------------------
if (process == null) return false;
try {
process.exitValue();
return false;
} catch (IllegalThreadStateException e) {
return true;
}
}
/**
* If not running, starts the backend.
*
* FIXME Broken, can't automatically find the process inside PATH
*
* @return the created Process instance behind
*/
public Process start() throws IOException, InterruptedException {
if (!isRunning()) {
// Launches the process --------------------------------------------
ProcessBuilder processBuilder = new ProcessBuilder(command);
//processBuilder.directory(new File(programPath));
this.process = processBuilder.start();
// Polling to check the backend is fully set up --------------------
boolean started = false;
int time = 0;
while (!started && (time < Backend.POLLING_TIME_OUT)) {
try {
this.get(this.ping);
started = true;
} catch (IOException ex) {
Thread.sleep(Backend.POLLING_SLEEP_TIME);
time += Backend.POLLING_SLEEP_TIME;
}
}
}
return this.process;
}
/**
* If we manage the backend process ourself and it is running, stops it by sending a specific request, and ensures the process is stopped with process utilities.
*
* @return If the backend properly stopped under the request, returns its response (see <code>get</code>), otherwise returns <code>null</code>.
*
* @see isRunning
* @see get
*
* @throws IOException
*/
public HttpResponse stop() {
HttpResponse response = null;
if (!this.isManagedExternally && this.isRunning()) {
try {
response = this.get(this.shutdown);
} catch (IOException exception) {
this.process.destroy();
}
this.process = null;
this.isManagedExternally = null;
}
return response;
}
/***************************************************************************
* Backend communication
**************************************************************************/
// Mode service ------------------------------------------------------------
private static final String METHOD_EDITOR_EXEC = "exec";
private static final String ARGUMENT_GUID = "guid";
private static final String ARGUMENT_SERVICE = "svc";
private static final String ARGUMENT_SERVICE_ARGUMENT = "arg";
public Map<String, Object> service(Map<String, Object> guid, String service) throws ParseException, IOException, JsonSyntaxException, BackendException {
return this.service(guid, service, null);
}
public Map<String, Object> service(POCDocument document, String service, Map<String, Object> argument) throws ParseException, IOException, JsonSyntaxException, BackendException {
return this.service(document.getGUID(), service, argument);
}
public Map<String, Object> service(POCDocument document, String service) throws ParseException, IOException, JsonSyntaxException, BackendException {
return this.service(document, service, null);
}
/**
* For every RPC related to an editor service.
*
* @throws BackendException
*/
public Map<String, Object> service(Map<String, Object> guid, String service, Map<String, Object> serviceArgument) throws ParseException, IOException, JsonSyntaxException, BackendException {
Map<String, Object> argument = new HashMap<String, Object>();
argument.put(Backend.ARGUMENT_GUID, guid);
argument.put(Backend.ARGUMENT_SERVICE, service);
if (serviceArgument != null) {
argument.put(Backend.ARGUMENT_SERVICE_ARGUMENT, serviceArgument);
}
return this.editor(Backend.METHOD_EDITOR_EXEC, argument);
}
// Editor module -----------------------------------------------------------
private static final String MODULE_NAME_EDITOR = "editor";
public Map<String, Object> editor(String member) throws JsonSyntaxException, ParseException, IOException, BackendException {
return this.editor(member, null);
}
public Map<String, Object> editor(String member, Map<String, Object> argument) throws JsonSyntaxException, ParseException, IOException, BackendException {
return this.rpc(Backend.MODULE_NAME_EDITOR, member, argument);
}
// RPC ---------------------------------------------------------------------
private static final String KEY_MODULE = "module";
private static final String KEY_MEMBER = "method";
private static final String KEY_ARGUMENT = "argument";
public Map<String, Object> rpc(String module, String member) throws IOException, JsonSyntaxException, ParseException, BackendException {
return this.rpc(module, member, null);
}
/**
* Builds a JSON RPC request (compatible with the implementation of the backend) and executes it.
*
* @param module The name of the remote module
* @param member The name of the member to access inside the remote module
* @param argument If the member is expected to be a function, it will be called with the given argument
*
* FIXME The backend should allow to pass a list of arguments, instead of forcing to use an object.
* TODO When the backend will implement fields aliasing, change the names of the field: mod, member, arg
*
* @return The JSON result of the RPC.
*
* @see postJson
*
* @throws IOException
* @throws BackendException
*/
public Map<String, Object> rpc(String module, String member, Object argument) throws ParseException, IOException, JsonSyntaxException, BackendException {
Map<String, Object> object = new HashMap<String, Object>();
object.put(Backend.KEY_MODULE, module);
object.put(Backend.KEY_MEMBER, member);
object.put(Backend.KEY_ARGUMENT, argument);
String json = gson.toJson(object);
StringEntity content = new StringEntity(json);
this.rpc.setEntity(content);
return this.postJson(this.rpc);
}
/***************************************************************************
* Raw HTTP communication
**************************************************************************/
public static String getStringFromResponse(HttpResponse response) throws ParseException, IOException {
return EntityUtils.toString(response.getEntity());
}
/**
* Executes the given HTTP GET request with the internal HTTP client instance, and returns the response.
*
* @param request A HTTP GET request.
*
* @return the response
*
* @throws ClientProtocolException
* @throws ParseException
* @throws IOException
*/
private HttpResponse get(HttpGet request) throws ClientProtocolException, IOException {
return this.httpclient.execute(request);
}
/**
* Executes the given HTTP POST request with the internal HTTP client instance, and returns the response.
*
* @param request A HTTP POST request.
*
* @return the response
*
* @throws ClientProtocolException
* @throws ParseException
* @throws IOException
*/
private HttpResponse post(HttpPost request) throws ClientProtocolException, IOException {
return this.httpclient.execute(request);
}
// TODO Maybe return "primitive" types too? (not necessarily a key/value collection)
/**
* Sends the given HTTP POST request and returns the response content as a JSON object.
*
* @param request A HTTP POST request.
*
* @return the response content as a JSON object, that is a Map of Strings/Objects in the Java system.
*
* @throws ParseException
* @throws IOException
* @throws JsonSyntaxException
* @throws BackendException
*/
@SuppressWarnings("unchecked")
private Map<String, Object> postJson(HttpPost request) throws ParseException, IOException, JsonSyntaxException, BackendException {
HttpResponse response = this.post(request);
Map<String, Object> result = new HashMap<String, Object>();
result = gson.fromJson(Backend.getStringFromResponse(response), Map.class);
switch (response.getStatusLine().getStatusCode()) {
case 200:
return result;
default:
throw new BackendException(result);
}
}
}