-
Notifications
You must be signed in to change notification settings - Fork 22
/
CloudRebelAppHandler.java
484 lines (413 loc) · 15.9 KB
/
CloudRebelAppHandler.java
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*******************************************************************************
* Copyright (c) 2015, 2016 Pivotal Software, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*
* and the Apache License v2.0 is available at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
********************************************************************************/
package org.eclipse.cft.server.core.internal.jrebel;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.cloudfoundry.client.lib.domain.CloudApplication;
import org.eclipse.cft.server.core.internal.CloudErrorUtil;
import org.eclipse.cft.server.core.internal.CloudFoundryPlugin;
import org.eclipse.cft.server.core.internal.CloudFoundryServer;
import org.eclipse.cft.server.core.internal.CloudServerEvent;
import org.eclipse.cft.server.core.internal.CloudServerListener;
import org.eclipse.cft.server.core.internal.ExternalRestTemplate;
import org.eclipse.cft.server.core.internal.Messages;
import org.eclipse.cft.server.core.internal.application.ModuleChangeEvent;
import org.eclipse.cft.server.core.internal.client.AppUrlChangeEvent;
import org.eclipse.cft.server.core.internal.client.CloudFoundryApplicationModule;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.server.core.IModule;
import org.osgi.framework.Bundle;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException;
public abstract class CloudRebelAppHandler implements CloudServerListener {
private static final String ERROR_NO_APP = "No application module found. Application may no longer exist in the Cloud."; //$NON-NLS-1$
abstract public void register();
/**
* Checks if remoting is enabled for the given module. The module's
* associated deployed application start state is checked, as remoting can
* only be enabled if the application is running. In addition, checks are
* performed to see if a remoting agent is available in the Cloud runtime
*
*/
protected boolean isRemotingAgentRunning(IModule module, CloudFoundryServer server, int eventType,
IProgressMonitor monitor) {
CloudFoundryApplicationModule appModule = null;
try {
// Fetch the cached module first to avoid Cloud requests
appModule = server.getExistingCloudModule(module);
int attempts = 40;
SubMonitor subMonitor = SubMonitor.convert(monitor, 100 * (attempts + 1));
// Otherwise request for an updated module
if (appModule == null) {
appModule = server.getBehaviour().updateModuleWithBasicCloudInfo(module, subMonitor.newChild(100));
}
CloudApplication cloudApp = appModule != null ? appModule.getApplication() : null;
// Cloud application doesn't exist, no need to check agent
if (cloudApp == null) {
CloudFoundryPlugin.logError(ERROR_NO_APP);
return false;
}
while (attempts > 0 && !subMonitor.isCanceled()) {
if (findRemotingAgent(appModule, server)) {
return true;
}
try {
Thread.sleep(3000);
}
catch (InterruptedException e) {
// Ignore. Proceed to get updated module anyway
}
appModule = server.getBehaviour().updateModuleWithBasicCloudInfo(module, subMonitor.newChild(100));
attempts--;
}
if (!subMonitor.isCanceled()) {
printErrorToConsole(appModule, server, Messages.CloudRebelAppHandler_TIME_OUT_RESOLVING_REMOTING_AGENT);
CloudFoundryPlugin.logError(Messages.CloudRebelAppHandler_TIME_OUT_RESOLVING_REMOTING_AGENT);
}
}
catch (CoreException e) {
printErrorToConsole(appModule, server, NLS.bind(Messages.CloudRebelAppHandler_ERROR, e.getMessage()));
CloudFoundryPlugin.logError(e);
}
return false;
}
protected boolean findRemotingAgent(CloudFoundryApplicationModule appModule, CloudFoundryServer cloudServer)
throws CoreException {
if (appModule == null) {
throw CloudErrorUtil.toCoreException(ERROR_NO_APP);
}
if (appModule.getDeploymentInfo() == null) {
return false;
}
List<String> urls = appModule.getDeploymentInfo().getUris();
try {
String url = urls != null && !urls.isEmpty() ? "http://" + urls.get(0) + "/app" : null; //$NON-NLS-1$ //$NON-NLS-2$
if (url != null) {
HttpHeaders headers = new HttpHeaders();
headers.set("x-rebel-id", "random"); //$NON-NLS-1$ //$NON-NLS-2$
HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
ResponseEntity<String> responseEntity = new ExternalRestTemplate().exchange(url, HttpMethod.POST,
requestEntity, String.class);
return responseEntity != null && responseEntity.getHeaders() != null
&& responseEntity.getHeaders().containsKey("x-rebel-response");//$NON-NLS-1$
}
}
catch (RestClientException e) {
// Skip 404 errors..they may require retrying to connect to the
// agent until it becomes available.
if (!CloudErrorUtil.isNotFoundException(e)) {
throw CloudErrorUtil.toCoreException(e);
}
}
return false;
}
@Override
public void serverChanged(CloudServerEvent event) {
if (event.getServer() != null
&& (event.getType() == CloudServerEvent.EVENT_JREBEL_REMOTING_UPDATE || shouldReplaceRemotingUrl(event
.getType()))) {
List<IModule> modules = new ArrayList<IModule>();
if (event instanceof ModuleChangeEvent) {
final ModuleChangeEvent moduleEvent = (ModuleChangeEvent) event;
final IModule module = moduleEvent.getModule();
if (module != null) {
modules.add(module);
}
}
if (!modules.isEmpty()) {
updateModulesWithRebelProjects(modules, event);
}
}
}
/**
*
* @param modules must not be null.
* @param event
*/
protected void updateModulesWithRebelProjects(List<IModule> modules, CloudServerEvent event) {
for (IModule module : modules) {
final IModule mod = module;
final CloudServerEvent moduleEvent = event;
// Only check remoting agent if it is a JRebel project as the
// remoting check may
// require multiple requests to the Cloud and may be a slow running
// operation
if (isJRebelEnabled(module)) {
Job job = new Job(NLS.bind(Messages.CloudRebelAppHandler_UPDATING_JREBEL_REMOTING, module.getName())) {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
handleRebelProject(moduleEvent, mod, monitor);
}
catch (CoreException e) {
CloudFoundryPlugin.logError(e);
return e.getStatus();
}
return Status.OK_STATUS;
}
};
job.schedule();
}
}
}
protected void handleRebelProject(CloudServerEvent event, IModule module, IProgressMonitor monitor)
throws CoreException {
CloudFoundryServer cloudServer = event.getServer();
CloudFoundryApplicationModule cloudAppModule = cloudServer.getExistingCloudModule(module);
int eventType = event.getType();
List<String> oldUrls = null;
List<String> currentUrls = null;
if (event instanceof AppUrlChangeEvent) {
AppUrlChangeEvent appUrlEvent = (AppUrlChangeEvent) event;
oldUrls = appUrlEvent.getOldUrls();
currentUrls = appUrlEvent.getCurrentUrls();
}
else if (cloudAppModule != null && cloudAppModule.getDeploymentInfo() != null) {
currentUrls = cloudAppModule.getDeploymentInfo().getUris();
}
updateJRebelRemoting(cloudServer, eventType, module, oldUrls, currentUrls, monitor);
}
/**
* Only gets invoked if the module has an associated JRebel-enabled project.
* Therefore the module's associated project exists and is accessible.
*/
protected void updateJRebelRemoting(CloudFoundryServer cloudServer, int cloudEventType, IModule module,
List<String> oldUrls, List<String> currentUrls, IProgressMonitor monitor) throws CoreException {
Bundle bundle = getJRebelBundle();
if (bundle != null) {
Throwable error = null;
try {
Class<?> providerClass = bundle
.loadClass("org.zeroturnaround.eclipse.jrebel.remoting.RebelRemotingProvider"); //$NON-NLS-1$
if (providerClass != null) {
CloudFoundryApplicationModule appModule = cloudServer.getExistingCloudModule(module);
Method getRemotingProject = providerClass.getMethod("getRemotingProject", IProject.class); //$NON-NLS-1$
if (getRemotingProject != null) {
getRemotingProject.setAccessible(true);
// static method
IProject project = module.getProject();
Object remoteProjectObj = getRemotingProject.invoke(null, project);
if (remoteProjectObj != null
&& remoteProjectObj.getClass().getName()
.equals("org.zeroturnaround.eclipse.jrebel.remoting.RemotingProject")) { //$NON-NLS-1$
// This includes ALL URLs, not just the one
// pertaining to the application project. It may
// contain
// deployment URLs for other projects, including
// those not deployed to Cloud Foundry
URL[] existingRebelUrls = null;
Method getUrls = remoteProjectObj.getClass().getMethod("getRemoteUrls"); //$NON-NLS-1$
if (getUrls != null) {
getUrls.setAccessible(true);
Object urlList = getUrls.invoke(remoteProjectObj);
if (urlList instanceof URL[]) {
existingRebelUrls = (URL[]) urlList;
}
}
if (existingRebelUrls == null) {
existingRebelUrls = new URL[0];
}
List<String> currentAppUrls = new ArrayList<String>();
List<String> oldAppUrls = new ArrayList<String>();
if (currentUrls != null) {
currentAppUrls.addAll(currentUrls);
}
if (oldUrls != null) {
oldAppUrls.addAll(oldUrls);
}
List<URL> updatedRebelUrls = new ArrayList<URL>();
boolean replace = false;
// Only do an automatic replacement if an old URL
// is detected in JRebel remoting
if (shouldReplaceRemotingUrl(cloudEventType)) {
for (URL rebelUrl : existingRebelUrls) {
String authority = rebelUrl.getAuthority();
if (oldAppUrls.contains(authority)) {
replace = true;
break;
}
}
}
if (replace || cloudEventType == CloudServerEvent.EVENT_JREBEL_REMOTING_UPDATE) {
if (!currentAppUrls.isEmpty()) {
boolean changed = false;
// Remove obsolete app URLs. Do checks
// against
// the
// authority instead of the full
// URL, as URLs may not be identical between
// JRebel and the application, even if the
// authorities are.
for (URL rebelUrl : existingRebelUrls) {
String authority = rebelUrl.getAuthority();
if (oldAppUrls.contains(authority) && !currentAppUrls.contains(authority)) {
// Skip adding back the obsolete
// URL.
changed = true;
}
else {
updatedRebelUrls.add(rebelUrl);
}
}
// Now perform the following:
// 1. Check if another app URL is registered
// with JRebel remoting
// 2. If not, add the first encountered app
// URL to JRebel remoting. Otherwise, make
// no changes to JRebel remoting
String urlToAdd = currentAppUrls.get(0);
// Check if any of the current app URLs are
// in JRebel Remoting. If so, no need to add
// a URL
for (URL updatedRebelUrl : updatedRebelUrls) {
String updatedRebelAuthority = updatedRebelUrl.getAuthority();
if (currentAppUrls.contains(updatedRebelAuthority)) {
urlToAdd = null;
break;
}
}
if (urlToAdd != null) {
// Add the first new URL encountered
if (!urlToAdd.startsWith("http://") && !urlToAdd.startsWith("https://")) { //$NON-NLS-1$ //$NON-NLS-2$
urlToAdd = "http://" + urlToAdd; //$NON-NLS-1$
}
try {
URL toAdd = new URL(urlToAdd);
updatedRebelUrls.add(toAdd);
changed = true;
}
catch (MalformedURLException e) {
throw CloudErrorUtil.toCoreException(e);
}
}
if (changed) {
Method setRebelRemotingUrls = remoteProjectObj.getClass().getDeclaredMethod(
"setRemoteUrls", URL[].class); //$NON-NLS-1$
if (setRebelRemotingUrls != null) {
setRebelRemotingUrls.setAccessible(true);
setRebelRemotingUrls.invoke(remoteProjectObj,
new Object[] { updatedRebelUrls.toArray(new URL[0]) });
printToConsole(appModule, cloudServer, NLS.bind(
Messages.CloudRebelAppHandler_UPDATED_URL, updatedRebelUrls));
}
}
else {
printToConsole(appModule, cloudServer, Messages.CloudRebelAppHandler_UP_TO_DATE);
}
}
else {
throw CloudErrorUtil
.toCoreException("No Cloud application deployment URL found for " + module.getName() + ". Unable to automatically set a deployment URL in JRebel remoting."); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}
}
}
}
catch (ClassNotFoundException e) {
error = e;
}
catch (SecurityException e) {
error = e;
}
catch (NoSuchMethodException e) {
error = e;
}
catch (IllegalAccessException e) {
error = e;
}
catch (InvocationTargetException e) {
error = e;
}
catch (IllegalArgumentException e) {
error = e;
}
if (error != null) {
throw CloudErrorUtil.toCoreException(error);
}
}
}
protected boolean shouldReplaceRemotingUrl(int eventType) {
return eventType == CloudServerEvent.EVENT_APP_URL_CHANGED;
}
protected void printToConsole(CloudFoundryApplicationModule appModule, CloudFoundryServer server, String message) {
printToConsole(appModule, server, message, false);
}
protected void printErrorToConsole(CloudFoundryApplicationModule appModule, CloudFoundryServer server,
String message) {
printToConsole(appModule, server, message, true);
}
protected void printToConsole(CloudFoundryApplicationModule appModule, CloudFoundryServer server, String message,
boolean error) {
if (appModule != null && server != null) {
message = Messages.CloudRebelAppHandler_MESSAGE_PREFIX + " - " + message + '\n'; //$NON-NLS-1$
CloudFoundryPlugin.getCallback().printToConsole(server, appModule, message, false, error);
}
}
public static boolean isJRebelEnabled(IModule module) {
IProject project = module != null ? module.getProject() : null;
try {
return project != null && project.isAccessible()
&& project.hasNature("org.zeroturnaround.eclipse.remoting.remotingNature") //$NON-NLS-1$
&& project.hasNature("org.zeroturnaround.eclipse.jrebelNature"); //$NON-NLS-1$
}
catch (CoreException e) {
CloudFoundryPlugin.logError(e);
}
return false;
}
/**
*
* @return true if JRebel bundle is found. False otherwise
*/
public static Bundle getJRebelBundle() {
Bundle bundle = null;
try {
bundle = Platform.getBundle("org.zeroturnaround.eclipse.remoting"); //$NON-NLS-1$
}
catch (Throwable e) {
CloudFoundryPlugin.logError(e);
}
return bundle;
}
/**
*
* @return true if JRebel is installed in Eclipse. False otherwise.
*/
public static boolean isJRebelIDEInstalled() {
return getJRebelBundle() != null;
}
}