-
Notifications
You must be signed in to change notification settings - Fork 74
/
RuntimeInfoCatalog.java
226 lines (201 loc) · 7.29 KB
/
RuntimeInfoCatalog.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
/*******************************************************************************
* Copyright (c) 2008 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.runtime.runtimeinfo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jdt.annotation.NonNull;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public final class RuntimeInfoCatalog implements IRuntimeInfoCatalog {
public RuntimeInfo erlideRuntime;
public final Map<String, RuntimeInfo> runtimes;
public String defaultRuntimeName;
public RuntimeInfoCatalog() {
runtimes = Maps.newHashMap();
erlideRuntime = RuntimeInfo.NO_RUNTIME_INFO;
defaultRuntimeName = null;
}
@Override
public synchronized Collection<RuntimeInfo> getRuntimes() {
return Collections.unmodifiableCollection(runtimes.values());
}
@Override
public synchronized void setRuntimes(final Collection<RuntimeInfo> elements,
final String dfltRuntime, final String ideRuntime) {
runtimes.clear();
if (elements.isEmpty()) {
initializeRuntimesList();
}
for (final RuntimeInfo rt : elements) {
addRuntime(rt);
}
defaultRuntimeName = dfltRuntime;
if (defaultRuntimeName == null) {
setDefaultRuntimes();
}
erlideRuntime = runtimes
.get(ideRuntime != null ? ideRuntime : defaultRuntimeName);
if (erlideRuntime == null) {
erlideRuntime = getDefaultRuntime();
}
Assert.isNotNull(erlideRuntime);
}
@Override
public synchronized void addRuntime(final RuntimeInfo rt) {
if (rt.getVersion().isCompatible(OLDEST_SUPPORTED_VERSION)
&& !runtimes.containsKey(rt.getName())) {
runtimes.put(rt.getName(), rt);
}
}
@Override
public synchronized Collection<String> getRuntimeNames() {
return runtimes.keySet();
}
@Override
public boolean hasRuntimeWithName(final String name) {
return runtimes.containsKey(name);
}
@SuppressWarnings("null")
@Override
public @NonNull RuntimeInfo getRuntime(final String name) {
final RuntimeInfo rt = runtimes.get(name);
if (rt != null && rt.isValid()) {
return rt;
}
return RuntimeInfo.NO_RUNTIME_INFO;
}
@Override
public synchronized void removeRuntime(final String name) {
runtimes.remove(name);
if (!runtimes.isEmpty()) {
if (erlideRuntime.getName().equals(name)) {
erlideRuntime = runtimes.values().iterator().next();
}
if (defaultRuntimeName.equals(name)) {
defaultRuntimeName = runtimes.keySet().iterator().next();
}
} else {
erlideRuntime = RuntimeInfo.NO_RUNTIME_INFO;
defaultRuntimeName = null;
}
}
@Override
public synchronized String getDefaultRuntimeName() {
return defaultRuntimeName;
}
@Override
public synchronized void setDefaultRuntime(final String name) {
defaultRuntimeName = name;
}
private synchronized void setErlideRuntime(final @NonNull RuntimeInfo runtime) {
final RuntimeInfo old = erlideRuntime;
if (old == null || !old.equals(runtime)) {
erlideRuntime = runtime;
// this creates infinite recursion!
// BackendManagerImpl.getDefault().getIdeBackend().stop();
}
}
@SuppressWarnings("null")
@Override
public synchronized @NonNull RuntimeInfo getErlideRuntime() {
return erlideRuntime;
}
@Override
public synchronized @NonNull RuntimeInfo getDefaultRuntime() {
return getRuntime(getDefaultRuntimeName());
}
@Override
public RuntimeInfo getRuntime(final RuntimeVersion runtimeVersion,
final String runtimeName) {
final Collection<RuntimeInfo> vsns = VersionLocator.locateVersion(runtimeVersion,
runtimes.values(), false);
if (vsns.isEmpty()) {
return null;
} else if (vsns.size() == 1) {
return vsns.iterator().next();
} else {
for (final RuntimeInfo ri : vsns) {
if (runtimeName == null || ri.getName().equals(runtimeName)) {
return ri;
}
}
return vsns.iterator().next();
}
}
@Override
public List<String> getAllRuntimesVersions() {
final Collection<RuntimeInfo> rs = getRuntimes();
final List<String> myVersions = Lists.newArrayList();
for (final RuntimeInfo info : rs) {
final String version = info.getVersion().asMinor().toString();
if (!myVersions.contains(version)) {
myVersions.add(version);
}
}
Collections.sort(myVersions);
return myVersions;
}
/**
* If runtime is not set, try to locate one. The first one found as below is set as
* default. All "obvious" runtimes found are stored.
* <ul>
* <li>A system property <code>erlide.runtime</code> can be set to point to a
* location.</li>
* <li>A preference in the default scope <code>org.erlide.core/default_runtime</code>
* can be set to point to a location.</li>
* <li>Look for existing Erlang runtimes in a few obvious places and install them,
* choosing a suitable one as default.</li>
* </ul>
*
*/
@Override
public void initializeRuntimesList() {
final Collection<RuntimeInfo> found = RuntimeFinder.guessRuntimeLocations();
for (final RuntimeInfo info : found) {
addRuntime(info);
}
setDefaultRuntimes();
}
private void setDefaultRuntimes() {
final List<RuntimeInfo> list = new ArrayList<RuntimeInfo>(getRuntimes());
Collections.sort(list, new Comparator<RuntimeInfo>() {
@Override
public int compare(final RuntimeInfo o1, final RuntimeInfo o2) {
final int x = o2.getVersion().compareTo(o1.getVersion());
if (x != 0) {
return x;
}
return o2.getName().compareTo(o1.getName());
}
});
if (!list.isEmpty()) {
final String firstName = list.get(0).getName();
if (defaultRuntimeName == null) {
setDefaultRuntime(firstName);
}
// the erlide backend is the most recent stable version
for (final RuntimeInfo info : list) {
if (info.getVersion().isStable()) {
setErlideRuntime(info);
break;
}
}
if (erlideRuntime == null) {
setErlideRuntime(getDefaultRuntime());
}
}
}
}