/
Bndrun.java
305 lines (263 loc) · 9.12 KB
/
Bndrun.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
package biz.aQute.resolve;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.osgi.resource.Requirement;
import org.osgi.service.resolver.ResolutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import aQute.bnd.build.Container;
import aQute.bnd.build.Run;
import aQute.bnd.build.Workspace;
import aQute.bnd.build.WorkspaceLayout;
import aQute.bnd.build.model.BndEditModel;
import aQute.bnd.build.model.clauses.HeaderClause;
import aQute.bnd.build.model.clauses.VersionedClause;
import aQute.bnd.build.model.conversions.CollectionFormatter;
import aQute.bnd.build.model.conversions.Converter;
import aQute.bnd.build.model.conversions.HeaderClauseFormatter;
import aQute.bnd.exceptions.Exceptions;
import aQute.bnd.header.Parameters;
import aQute.bnd.help.Syntax;
import aQute.bnd.help.instructions.ResolutionInstructions;
import aQute.bnd.help.instructions.ResolutionInstructions.ResolveMode;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.resource.FilterParser;
import aQute.bnd.osgi.resource.FilterParser.Expression;
import aQute.lib.io.IO;
import aQute.lib.utf8properties.UTF8Properties;
import aQute.libg.cryptography.SHA1;
/**
* This is a resolving version of the Run class. The name of this class is known
* in the super class so do not rename it or change {@link Run}
*/
public class Bndrun extends Run {
Logger logger = LoggerFactory
.getLogger(Bndrun.class);
final BndEditModel model;
final ResolutionInstructions resolutionInstructions;
private static final Converter<String, Collection<? extends HeaderClause>> runbundlesListFormatter = new CollectionFormatter<>(
",", new HeaderClauseFormatter(), null, "", "");
/**
* Create a Bndrun that will be stand alone if it contains -standalone. In
* that case the given workspace is ignored. Otherwise, the workspace must
* be a valid workspace.
*/
public static Bndrun createBndrun(Workspace workspace, File file) throws Exception {
Processor processor;
if (workspace != null) {
Bndrun run = new Bndrun(workspace, file);
if (run.getProperties()
.get(STANDALONE) == null) {
return run;
}
// -standalone specified
processor = run;
} else {
processor = new Processor();
processor.setProperties(file);
}
Workspace standaloneWorkspace = Workspace.createStandaloneWorkspace(processor, file.toURI());
Bndrun run = new Bndrun(standaloneWorkspace, file);
return run;
}
public Bndrun(BndEditModel model) throws Exception {
super(model.getWorkspace(), model.getProject()
.getPropertiesFile());
this.model = model;
this.resolutionInstructions = Syntax.getInstructions(model.getProject(), ResolutionInstructions.class);
}
public Bndrun(Workspace workspace, File propertiesFile) throws Exception {
super(workspace, propertiesFile);
this.model = new BndEditModel(this);
this.resolutionInstructions = Syntax.getInstructions(model.getProject(), ResolutionInstructions.class);
}
/**
* Use the resolver to calculate the <code>-runbundles</code> required to
* execute the bndrun configuration.
* <p>
* Use the return value with {@link Run#setProperty(String, String)} with
* key {@link Constants#RUNBUNDLES}
*
* @param failOnChanges if the build should fail when changes to the
* <code>-runbundles</code> are detected
* @param writeOnChanges if the bndrun file should be updated when changes
* to the <code>-runbundles</code> are detected are detected
* @return the calculated <code>-runbundles</code>
* @throws Exception
*/
public String resolve(boolean failOnChanges, boolean writeOnChanges) throws Exception {
return resolve(failOnChanges, writeOnChanges, runbundlesListFormatter);
}
public <T> T resolve(boolean failOnChanges, boolean writeOnChanges,
Converter<T, Collection<? extends HeaderClause>> runbundlesFormatter) throws Exception {
RunResolution resolution = RunResolution.resolve(this, this, null);
if (!resolution.isOK()) {
throw resolution.exception;
}
update(resolution, failOnChanges, writeOnChanges);
return runbundlesFormatter.convert(model.getRunBundles());
}
public RunResolution resolve(ResolutionCallback... callbacks) throws Exception {
RunResolution resolution = RunResolution.resolve(this, this, Arrays.asList(callbacks))
.reportException();
if (!resolution.isOK()) {
if (resolution.exception instanceof ResolutionException) {
ResolutionException re = (ResolutionException) resolution.exception;
FilterParser filterParser = new FilterParser();
for (Requirement r : re.getUnresolvedRequirements()) {
Expression parse = filterParser.parse(r);
error(" -> %s : %s", r.getNamespace(), parse);
}
error(" log: %s", resolution.log);
return resolution;
}
throw resolution.exception;
}
return resolution;
}
public boolean update(RunResolution resolution, boolean failOnChanges, boolean writeOnChanges) throws Exception {
List<VersionedClause> runBundlesBeforeUpdate = model.getRunBundles();
if (resolution.updateBundles(model)) {
if (failOnChanges) {
error("Fail on changes set to true (--xchange,-x) and there are changes");
error(" Existing runbundles %s", runBundlesBeforeUpdate);
error(" Calculated runbundles %s", resolution.getRunBundles());
} else {
if (writeOnChanges) {
try {
model.saveChanges();
} catch (Exception e) {
error("Could not save runbundles in their properties file %s. %s", model.getProject(),
e.getMessage());
}
}
}
return true;
}
return false;
}
public BndEditModel getModel() {
return model;
}
/**
* We override to resolve before we get the runbundles.
*/
@Override
public Collection<Container> getRunbundles() throws Exception {
Parameters gestalt = getWorkspace().getGestalt();
ResolveMode resolve = resolutionInstructions.resolve();
if (resolve == null)
resolve = ResolveMode.manual;
switch (resolve) {
case auto :
case manual :
default :
break;
case cache :
return cache();
case batch :
if (!gestalt.containsKey(Constants.GESTALT_BATCH) && !super.getRunbundles().isEmpty())
break;
// fall through
case beforelaunch :
return RunResolution.getRunBundles(this, true)
.map(this::parseRunbundles)
.orElseThrow(IllegalArgumentException::new);
}
return super.getRunbundles();
}
enum CacheReason {
NO_CACHE_FILE,
NOT_A_BND_LAYOUT,
CACHE_STALE_PROJECT,
CACHE_STALE_WORKSPACE,
USE_CACHE,
INVALID_CACHE;
}
CacheReason testReason;
private Collection<Container> cache() throws Exception {
Workspace ws = getWorkspace();
File ours = getPropertiesFile();
File cache = getCacheFile(ours);
long cacheLastModified = cache.lastModified();
CacheReason reason;
if (ws.getLayout() != WorkspaceLayout.BND)
reason = CacheReason.NOT_A_BND_LAYOUT;
else if (!cache.isFile())
reason = CacheReason.NO_CACHE_FILE;
else if (cacheLastModified < ws.lastModified())
reason = CacheReason.CACHE_STALE_WORKSPACE;
else if (cacheLastModified < lastModified())
reason = CacheReason.CACHE_STALE_PROJECT;
else
reason = CacheReason.USE_CACHE;
testReason = reason;
trace("force = %s", reason);
try (Processor p = new Processor()) {
if (cache.isFile())
p.setProperties(cache);
if (reason == CacheReason.USE_CACHE) {
trace("attempting to use cache");
String runbundles = p.getProperty(Constants.RUNBUNDLES);
Collection<Container> containers = parseRunbundles(runbundles);
if (isAllOk(containers)) {
trace("from cache %s", containers);
return containers;
} else {
testReason = CacheReason.INVALID_CACHE;
trace("the cached bundles were not ok, will resolve");
}
}
trace("resolving");
IO.delete(cache);
if (cache.isFile()) {
throw new IllegalStateException("cannot delete cache file " + cache);
}
RunResolution resolved = RunResolution.resolve(this, Collections.emptyList());
if (!resolved.isOK()) {
throw new IllegalStateException(resolved.report(false));
}
String spec = resolved.getRunBundlesAsString();
List<Container> containers = parseRunbundles(spec);
if (isAllOk(containers)) {
UTF8Properties props = new UTF8Properties(p.getProperties());
props.setProperty(Constants.RUNBUNDLES, spec);
cache.getParentFile()
.mkdirs();
props.store(cache);
}
return containers;
}
}
/**
* Return the file used to cache the resolved solution for the given file
*
* @param file the file to find the cached file for
* @return the cached file
*/
public File getCacheFile(File file) {
try {
String path = file.getAbsolutePath();
String digest = SHA1.digest(path.getBytes(StandardCharsets.UTF_8))
.asHex();
return getWorkspace().getCache("resolved-cache/".concat(digest)
.concat(".resolved"));
} catch (Exception e) {
// not gonna happen
throw Exceptions.duck(e);
}
}
private boolean isAllOk(Collection<Container> containers) {
for (Container c : containers) {
if (c.getError() != null) {
return false;
}
}
return true;
}
}