forked from erlang/erlide_eclipse
/
BackendManager.java
467 lines (421 loc) · 15.7 KB
/
BackendManager.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
/*******************************************************************************
* Copyright (c) 2005 Vlad Dumitrescu and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Vlad Dumitrescu
*******************************************************************************/
package org.erlide.backend.internal;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.xtext.xbase.lib.Pair;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.erlide.backend.BackendCore;
import org.erlide.backend.BackendData;
import org.erlide.backend.BackendException;
import org.erlide.backend.BackendUtils;
import org.erlide.backend.IBackend;
import org.erlide.backend.IBackendFactory;
import org.erlide.backend.IBackendListener;
import org.erlide.backend.IBackendManager;
import org.erlide.backend.ICodeBundle;
import org.erlide.backend.ICodeBundle.CodeContext;
import org.erlide.backend.events.ErlangEventHandler;
import org.erlide.backend.events.ErlangEventPublisher;
import org.erlide.model.root.ErlModelManager;
import org.erlide.model.root.IErlModel;
import org.erlide.model.root.IErlProject;
import org.erlide.runtime.IRpcSite;
import org.erlide.runtime.epmd.IEpmdListener;
import org.erlide.runtime.runtimeinfo.RuntimeInfo;
import org.erlide.utils.ErlLogger;
import org.erlide.utils.SystemConfiguration;
import org.osgi.framework.Bundle;
import org.osgi.service.event.Event;
import com.ericsson.otp.erlang.RuntimeVersion;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
public final class BackendManager implements IEpmdListener, IBackendManager {
public enum BackendEvent {
ADDED, REMOVED, MODULE_LOADED
}
private volatile IBackend ideBackend;
private final Object ideBackendLock = new Object();
private final Map<IProject, Set<IBackend>> executionBackends;
private final Map<String, IBackend> buildBackends;
final List<IBackendListener> listeners;
private final Map<Bundle, ICodeBundle> codeBundles;
private final Set<IBackend> allBackends;
private final BackendManagerLaunchListener launchListener;
private final IBackendFactory factory;
private final RuntimeInfo erlideRuntimeInfo;
public BackendManager(final RuntimeInfo erlideRuntimeInfo,
final IBackendFactory factory) {
this.factory = factory;
this.erlideRuntimeInfo = erlideRuntimeInfo;
ideBackend = null;
executionBackends = Maps.newHashMap();
buildBackends = Maps.newHashMap();
allBackends = Sets.newHashSet();
listeners = Lists.newArrayList();
codeBundles = Maps.newHashMap();
loadCodepathExtensions();
launchListener = new BackendManagerLaunchListener(this, DebugPlugin
.getDefault().getLaunchManager());
registerGlobalEventhandlers();
}
@SuppressWarnings("unused")
private void tryStartEpmdProcess() {
final RuntimeInfo info = erlideRuntimeInfo;
if (info == null || info.getOtpHome() == null) {
ErlLogger.error("Trying to start with a null runtime info");
return;
}
// FIXME the location of epmd is OS dependent... look for it!
// Win: $OTP/erts-$VSN/bin/epmd
// Lin: $OTP/bin/$TARGET/epmd
// Others: ?
final String[] cmdline = new String[] {
info.getOtpHome() + "/bin/epmd", "-daemon" };
try {
Runtime.getRuntime().exec(cmdline);
ErlLogger.info("Epmd started.");
} catch (final IOException e) {
// ErlLogger.info("Could not start epmd! " + e.getMessage());
}
}
private void registerGlobalEventhandlers() {
new ErlangEventHandler("*", null) {
@Override
public void handleEvent(final Event event) {
if (SystemConfiguration
.hasFeatureEnabled("erlide.eventhandler.debug")) {
ErlLogger.info("erlang event : "
+ ErlangEventPublisher.dumpEvent(event));
}
}
}.register();
}
@Override
public IBackend createExecutionBackend(final BackendData data) {
ErlLogger.debug("create execution backend " + data.getNodeName());
final IBackend b = factory.createBackend(data);
addBackend(b);
notifyBackendChange(b, BackendEvent.ADDED, null, null);
return b;
}
private void addBackend(final IBackend b) {
synchronized (allBackends) {
allBackends.add(b);
}
}
@Override
public IBackend getBuildBackend(final IProject project)
throws BackendException {
final IErlProject erlProject = ErlModelManager.getErlangModel()
.getErlangProject(project);
if (erlProject == null) {
return null;
}
final RuntimeInfo info = erlProject.getRuntimeInfo();
if (info == null) {
ErlLogger.info("Project %s has no runtime info, using ide",
project.getName());
if (ideBackend == null) {
throw new BackendException(
"IDE backend is not created - check configuration!");
}
return ideBackend;
}
final String version = info.getVersion().asMajor().toString();
IBackend b = buildBackends.get(version);
if (b == null) {
b = factory.createBuildBackend(info);
buildBackends.put(version, b);
addBackend(b);
notifyBackendChange(b, BackendEvent.ADDED, null, null);
}
return b;
}
@Override
public IBackend getIdeBackend() {
// System.out.println("GET ide" + Thread.currentThread());
if (ideBackend == null) {
synchronized (ideBackendLock) {
if (ideBackend == null) {
ideBackend = factory.createIdeBackend();
// XXX CODEDB ??? addBackend(ideBackend);
notifyBackendChange(ideBackend, BackendEvent.ADDED, null,
null);
}
}
}
// System.out.println(">>> " + ideBackend);
return ideBackend;
}
void notifyBackendChange(final IBackend b, final BackendEvent type,
final IProject project, final String moduleName) {
if (listeners == null) {
return;
}
final Object[] copiedListeners = listeners.toArray();
for (final Object element : copiedListeners) {
final IBackendListener listener = (IBackendListener) element;
switch (type) {
case ADDED:
listener.runtimeAdded(b);
break;
case REMOVED:
listener.runtimeRemoved(b);
break;
case MODULE_LOADED:
listener.moduleLoaded(b, project, moduleName);
break;
default:
// ignore
}
}
}
@Override
public synchronized void updateNodeStatus(final String host,
final Collection<String> started, final Collection<String> stopped) {
for (final String b : started) {
final String name = b + "@" + host;
// ErlLogger.debug("(epmd) started: '%s'", name);
remoteNodeStatus(name, true, null);
}
for (final String b : stopped) {
final String name = b + "@" + host;
// ErlLogger.debug("(epmd) stopped: '%s'", name);
remoteNodeStatus(name, false, null);
}
}
private void remoteNodeStatus(final String node, final boolean up,
final Object info) {
if (!up) {
for (final Entry<IProject, Set<IBackend>> e : executionBackends
.entrySet()) {
for (final IBackend be : e.getValue()) {
final String bnode = be.getData().getQualifiedNodeName();
// TODO this is not very reliable
// we have to get the real node name... how?
if (bnode.startsWith(node)) {
removeExecutionBackend(e.getKey(), be);
break;
}
}
}
}
}
@Override
public synchronized void removeExecutionBackend(final IProject project,
final IBackend b) {
final IErlModel model = ErlModelManager.getErlangModel();
b.removeProjectPath(model.findProject(project));
Set<IBackend> list = executionBackends.get(project);
if (list == null) {
list = Sets.newHashSet();
executionBackends.put(project, list);
}
list.remove(b);
}
@Override
public void loadCodepathExtensions() {
final IExtensionPoint exPnt = BackendUtils.getCodepathExtension();
// TODO listen to changes to the registry!
final IExtension[] extensions = exPnt.getExtensions();
for (int e = 0; e < extensions.length; e++) {
final IExtension extension = extensions[e];
if (!extension.isValid()) {
continue;
}
addCodeBundle(extension);
}
}
private void addCodeBundle(final IExtension extension) {
final String pluginId = extension.getContributor().getName();
final Bundle plugin = Platform.getBundle(pluginId);
final Map<String, CodeContext> paths = Maps.newHashMap();
final List<Pair<String, String>> inits = Lists.newArrayList();
for (final IConfigurationElement el : extension
.getConfigurationElements()) {
if ("beam_dir".equals(el.getName())) {
final String dir = el.getAttribute("path");
final String t = el.getAttribute("context").toUpperCase();
final CodeContext type = Enum.valueOf(CodeContext.class, t);
paths.put(dir, type);
} else if ("init".equals(el.getName())) {
final String module = el.getAttribute("module");
final String function = el.getAttribute("function");
inits.add(new Pair<String, String>(module, function));
} else {
ErlLogger
.error("Unknown code bundle element: %s", el.getName());
}
}
addBundle(plugin, paths, inits);
}
@Override
public void addBundle(final Bundle b, final Map<String, CodeContext> paths,
final Collection<Pair<String, String>> inits) {
final ICodeBundle p = findBundle(b);
if (p != null) {
return;
}
final CodeBundleImpl pp = new CodeBundleImpl(b, paths, inits);
getCodeBundles().put(b, pp);
forEachBackend(new Procedure1<IBackend>() {
@Override
public void apply(final IBackend bb) {
bb.registerCodeBundle(pp);
}
});
}
private ICodeBundle findBundle(final Bundle b) {
return getCodeBundles().get(b);
}
@Override
public Map<Bundle, ICodeBundle> getCodeBundles() {
return codeBundles;
}
@Override
public void forEachBackend(final Procedure1<IBackend> visitor) {
for (final IBackend b : getAllBackends()) {
visitor.apply(b);
}
}
@Override
public IRpcSite getByName(final String nodeName) {
final Collection<IBackend> list = getAllBackends();
for (final IBackend b : list) {
if (b.getName().equals(nodeName)) {
return b.getRpcSite();
}
}
return null;
}
@Override
public IRpcSite getByVersion(final RuntimeVersion version) {
final RuntimeInfo info = BackendCore.getRuntimeInfoCatalog()
.getRuntime(version, null);
if (info == null) {
return null;
}
final Collection<IBackend> list = getAllBackends();
for (final IBackend b : list) {
if (b.getName().equals(info.getName())) {
return b.getRpcSite();
}
}
return null;
}
@Override
public IRpcSite getByProject(final String name) {
final IProject prj = ResourcesPlugin.getWorkspace().getRoot()
.getProject(name);
return getByProject(prj);
}
@Override
public IRpcSite getByProject(final IProject project) {
try {
return getBuildBackend(project).getRpcSite();
} catch (final BackendException e) {
return null;
}
}
@Override
public Collection<IBackend> getAllBackends() {
synchronized (allBackends) {
return Collections.unmodifiableCollection(allBackends);
}
}
@Override
public void moduleLoaded(final IBackend b, final IProject project,
final String moduleName) {
notifyBackendChange(b, BackendEvent.MODULE_LOADED, project, moduleName);
}
@Override
public synchronized Set<IBackend> getExecutionBackends(
final IProject project) {
final Set<IBackend> bs = executionBackends.get(project);
if (bs == null) {
return Collections.emptySet();
}
return Collections.unmodifiableSet(bs);
}
@Override
public void addBackendListener(final IBackendListener listener) {
listeners.add(listener);
}
@Override
public void removeBackendListener(final IBackendListener listener) {
listeners.remove(listener);
}
@Override
public synchronized void addExecutionBackend(final IProject project,
final IBackend b) {
Set<IBackend> list = executionBackends.get(project);
if (list == null) {
list = Sets.newHashSet();
executionBackends.put(project, list);
}
list.add(b);
final IErlModel model = ErlModelManager.getErlangModel();
b.addProjectPath(model.findProject(project));
}
@Override
public void dispose(final IBackend backend) {
if (backend != null && backend != ideBackend) {
backend.dispose();
}
}
@Override
public void dispose() {
for (final IBackend b : buildBackends.values()) {
b.dispose();
}
if (ideBackend != null) {
ideBackend.dispose();
}
final ILaunch[] launches = DebugPlugin.getDefault().getLaunchManager()
.getLaunches();
launchListener.launchesTerminated(launches);
launchListener.dispose();
}
@Override
public IBackend getBackendForLaunch(final ILaunch launch) {
for (final IBackend backend : allBackends) {
if (backend.getLaunch() == launch) {
return backend;
}
}
return null;
}
@Override
public void terminateBackendsForLaunch(final ILaunch launch) {
final IBackend b = getBackendForLaunch(launch);
if (b != null) {
b.dispose();
}
}
@Override
public void removeBackendsForLaunch(final ILaunch launch) {
}
}