-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
Workspace.java
1238 lines (1069 loc) · 32.7 KB
/
Workspace.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package aQute.bnd.build;
import static java.util.stream.Collectors.toList;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.naming.TimeLimitExceededException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import aQute.bnd.annotation.plugin.BndPlugin;
import aQute.bnd.connection.settings.ConnectionSettings;
import aQute.bnd.exporter.executable.ExecutableJarExporter;
import aQute.bnd.exporter.runbundles.RunbundlesExporter;
import aQute.bnd.header.Attrs;
import aQute.bnd.header.OSGiHeader;
import aQute.bnd.header.Parameters;
import aQute.bnd.http.HttpClient;
import aQute.bnd.maven.support.Maven;
import aQute.bnd.osgi.About;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Macro;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Verifier;
import aQute.bnd.resource.repository.ResourceRepositoryImpl;
import aQute.bnd.service.BndListener;
import aQute.bnd.service.RepositoryPlugin;
import aQute.bnd.service.action.Action;
import aQute.bnd.service.extension.ExtensionActivator;
import aQute.bnd.service.lifecycle.LifeCyclePlugin;
import aQute.bnd.service.repository.Prepare;
import aQute.bnd.service.repository.RepositoryDigest;
import aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor;
import aQute.bnd.url.MultiURLConnectionHandler;
import aQute.bnd.version.Version;
import aQute.bnd.version.VersionRange;
import aQute.lib.deployer.FileRepo;
import aQute.lib.hex.Hex;
import aQute.lib.io.IO;
import aQute.lib.io.IOConstants;
import aQute.lib.settings.Settings;
import aQute.lib.strings.Strings;
import aQute.lib.utf8properties.UTF8Properties;
import aQute.lib.zip.ZipUtil;
import aQute.libg.uri.URIUtil;
import aQute.service.reporter.Reporter;
public class Workspace extends Processor {
private final static Logger logger = LoggerFactory.getLogger(Workspace.class);
public static final File BND_DEFAULT_WS = IO.getFile("~/.bnd/default-ws");
public static final String BND_CACHE_REPONAME = "bnd-cache";
public static final String EXT = "ext";
public static final String BUILDFILE = "build.bnd";
public static final String CNFDIR = "cnf";
public static final String BNDDIR = "bnd";
public static final String CACHEDIR = "cache/" + About.CURRENT;
public static final String STANDALONE_REPO_CLASS = "aQute.bnd.repository.osgi.OSGiRepository";
static final int BUFFER_SIZE = IOConstants.PAGE_SIZE * 16;
private static final String PLUGIN_STANDALONE = "-plugin.standalone_";
private final Pattern EMBEDDED_REPO_TESTING_PATTERN = Pattern
.compile(".*biz\\.aQute\\.bnd\\.embedded-repo(-.*)?\\.jar");
static class WorkspaceData {
List<RepositoryPlugin> repositories;
}
private final static Map<File, WeakReference<Workspace>> cache = newHashMap();
static Processor defaults = null;
private final Map<String, Project> models = new HashMap<>();
private final Set<String> modelsUnderConstruction = new HashSet<>();
final Map<String, Action> commands = newMap();
final Maven maven = new Maven(
Processor.getExecutor());
private final AtomicBoolean offline = new AtomicBoolean();
Settings settings = new Settings();
WorkspaceRepository workspaceRepo = new WorkspaceRepository(this);
static String overallDriver = "unset";
static Parameters overallGestalt = new Parameters();
/**
* Signal a BndListener plugin. We ran an infinite bug loop :-(
*/
final ThreadLocal<Reporter> signalBusy = new ThreadLocal<>();
ResourceRepositoryImpl resourceRepositoryImpl;
private Parameters gestalt;
private String driver;
private final WorkspaceLayout layout;
final Set<Project> trail = Collections
.newSetFromMap(new ConcurrentHashMap<Project, Boolean>());
private WorkspaceData data = new WorkspaceData();
private File buildDir;
/**
* This static method finds the workspace and creates a project (or returns
* an existing project)
*
* @param projectDir
*/
public static Project getProject(File projectDir) throws Exception {
projectDir = projectDir.getAbsoluteFile();
assert projectDir.isDirectory();
Workspace ws = getWorkspace(projectDir.getParentFile());
return ws.getProject(projectDir.getName());
}
static synchronized public Processor getDefaults() {
if (defaults != null)
return defaults;
UTF8Properties props = new UTF8Properties();
try (InputStream propStream = Workspace.class.getResourceAsStream("defaults.bnd")) {
if (propStream != null) {
props.load(propStream);
} else {
System.err.println("Cannot load defaults");
}
} catch (IOException e) {
throw new IllegalArgumentException("Unable to load bnd defaults.", e);
}
defaults = new Processor(props, false);
return defaults;
}
public static Workspace createDefaultWorkspace() throws Exception {
Workspace ws = new Workspace(BND_DEFAULT_WS, CNFDIR);
return ws;
}
public static Workspace getWorkspace(File workspaceDir) throws Exception {
return getWorkspace(workspaceDir, CNFDIR);
}
public static Workspace getWorkspaceWithoutException(File workspaceDir) throws Exception {
try {
return getWorkspace(workspaceDir);
} catch (IllegalArgumentException e) {
return null;
}
}
/**
* /* Return the nearest workspace
*/
public static Workspace findWorkspace(File base) throws Exception {
File rover = base;
while (rover != null) {
File file = IO.getFile(rover, "cnf/build.bnd");
if (file.isFile())
return getWorkspace(rover);
rover = rover.getParentFile();
}
return null;
}
public static Workspace getWorkspace(File workspaceDir, String bndDir) throws Exception {
workspaceDir = workspaceDir.getAbsoluteFile();
// the cnf directory can actually be a
// file that redirects
while (workspaceDir.isDirectory()) {
File test = new File(workspaceDir, CNFDIR);
if (!test.exists())
test = new File(workspaceDir, bndDir);
if (test.isDirectory())
break;
if (test.isFile()) {
String redirect = IO.collect(test)
.trim();
test = getFile(test.getParentFile(), redirect).getAbsoluteFile();
workspaceDir = test;
}
if (!test.exists())
throw new IllegalArgumentException("No Workspace found from: " + workspaceDir);
}
synchronized (cache) {
WeakReference<Workspace> wsr = cache.get(workspaceDir);
Workspace ws;
if (wsr == null || (ws = wsr.get()) == null) {
ws = new Workspace(workspaceDir, bndDir);
cache.put(workspaceDir, new WeakReference<>(ws));
}
return ws;
}
}
public Workspace(File workspaceDir) throws Exception {
this(workspaceDir, CNFDIR);
}
public Workspace(File workspaceDir, String bndDir) throws Exception {
super(getDefaults());
workspaceDir = workspaceDir.getAbsoluteFile();
setBase(workspaceDir); // setBase before call to setFileSystem
this.layout = WorkspaceLayout.BND;
addBasicPlugin(new LoggingProgressPlugin());
setFileSystem(workspaceDir, bndDir);
}
public void setFileSystem(File workspaceDir, String bndDir) throws Exception {
workspaceDir = workspaceDir.getAbsoluteFile();
IO.mkdirs(workspaceDir);
assert workspaceDir.isDirectory();
synchronized (cache) {
WeakReference<Workspace> wsr = cache.get(getBase());
if ((wsr != null) && (wsr.get() == this)) {
cache.remove(getBase());
cache.put(workspaceDir, wsr);
}
}
File buildDir = new File(workspaceDir, bndDir).getAbsoluteFile();
if (!buildDir.isDirectory())
buildDir = new File(workspaceDir, CNFDIR).getAbsoluteFile();
setBuildDir(buildDir);
File buildFile = new File(buildDir, BUILDFILE).getAbsoluteFile();
if (!buildFile.isFile())
warning("No Build File in %s", workspaceDir);
setProperties(buildFile, workspaceDir);
propertiesChanged();
//
// There is a nasty bug/feature in Java that gives errors on our
// SSL use of github. The flag jsse.enableSNIExtension should be set
// to false. So here we provide a way to set system properties
// as early as possible
//
Attrs sysProps = OSGiHeader.parseProperties(mergeProperties(SYSTEMPROPERTIES));
for (Entry<String, String> e : sysProps.entrySet()) {
System.setProperty(e.getKey(), e.getValue());
}
}
private Workspace(WorkspaceLayout layout) throws Exception {
super(getDefaults());
this.layout = layout;
setBuildDir(IO.getFile(BND_DEFAULT_WS, CNFDIR));
}
public Project getProjectFromFile(File projectDir) {
projectDir = projectDir.getAbsoluteFile();
assert projectDir.isDirectory();
if (getBase().equals(projectDir.getParentFile())) {
return getProject(projectDir.getName());
}
return null;
}
public Project getProject(String bsn) {
synchronized (models) {
Project project = models.get(bsn);
if (project != null) {
return project;
}
if (!modelsUnderConstruction.add(bsn)) {
return null;
}
try {
File projectDir = getFile(bsn);
File bnd = getFile(projectDir, Project.BNDFILE);
if (!bnd.isFile()) {
return null;
}
project = new Project(this, projectDir);
if (!project.isValid()) {
return null;
}
models.put(bsn, project);
return project;
} finally {
modelsUnderConstruction.remove(bsn);
}
}
}
void removeProject(Project p) throws Exception {
if (p.isCnf())
return;
synchronized (models) {
models.remove(p.getName());
}
for (LifeCyclePlugin lp : getPlugins(LifeCyclePlugin.class)) {
lp.delete(p);
}
}
public boolean isPresent(String name) {
return models.containsKey(name);
}
public Collection<Project> getCurrentProjects() {
return models.values();
}
@Override
public boolean refresh() {
data = new WorkspaceData();
if (super.refresh()) {
for (Project project : getCurrentProjects()) {
project.propertiesChanged();
}
return true;
}
return false;
}
@Override
public void propertiesChanged() {
data = new WorkspaceData();
File extDir = new File(getBuildDir(), EXT);
File[] extensions = extDir.listFiles();
if (extensions != null) {
for (File extension : extensions) {
String extensionName = extension.getName();
if (extensionName.endsWith(".bnd")) {
extensionName = extensionName.substring(0, extensionName.length() - ".bnd".length());
try {
doIncludeFile(extension, false, getProperties(), "ext." + extensionName);
} catch (Exception e) {
exception(e, "PropertiesChanged: %s", e);
}
}
}
}
super.propertiesChanged();
}
public String _workspace(@SuppressWarnings("unused") String args[]) {
return getBase().getAbsolutePath()
.replace('\\', '/');
}
public void addCommand(String menu, Action action) {
commands.put(menu, action);
}
public void removeCommand(String menu) {
commands.remove(menu);
}
public void fillActions(Map<String, Action> all) {
all.putAll(commands);
}
public Collection<Project> getAllProjects() throws Exception {
try (Stream<Path> paths = Files.list(getBase().toPath())) {
List<Project> projects = paths
.filter(p -> Files.isDirectory(p) && Files.isRegularFile(p.resolve(Project.BNDPATH)))
.map(p -> getProject(p.getFileName()
.toString()))
.filter(Objects::nonNull)
.collect(toList());
return projects;
}
}
/**
* Inform any listeners that we changed a file (created/deleted/changed).
*
* @param f The changed file
*/
public void changedFile(File f) {
List<BndListener> listeners = getPlugins(BndListener.class);
for (BndListener l : listeners)
try {
l.changed(f);
} catch (Exception e) {
logger.debug("Exception in a BndListener changedFile method call", e);
}
}
public void bracket(boolean begin) {
List<BndListener> listeners = getPlugins(BndListener.class);
for (BndListener l : listeners)
try {
if (begin)
l.begin();
else
l.end();
} catch (Exception e) {
if (begin)
logger.debug("Exception in a BndListener begin method call", e);
else
logger.debug("Exception in a BndListener end method call", e);
}
}
public void signal(Reporter reporter) {
if (signalBusy.get() != null)
return;
signalBusy.set(reporter);
try {
List<BndListener> listeners = getPlugins(BndListener.class);
for (BndListener l : listeners)
try {
l.signal(this);
} catch (Exception e) {
logger.debug("Exception in a BndListener signal method call", e);
}
} catch (Exception e) {
// Ignore
} finally {
signalBusy.set(null);
}
}
@Override
public void signal() {
signal(this);
}
class CachedFileRepo extends FileRepo {
final Lock lock = new ReentrantLock();
boolean inited;
CachedFileRepo() {
super(BND_CACHE_REPONAME, getCache(BND_CACHE_REPONAME), false);
}
@Override
protected boolean init() throws Exception {
if (lock.tryLock(50, TimeUnit.SECONDS) == false)
throw new TimeLimitExceededException("Cached File Repo is locked and can't acquire it");
try {
if (super.init()) {
inited = true;
IO.mkdirs(root);
if (!root.isDirectory())
throw new IllegalArgumentException("Cache directory " + root + " not a directory");
try (InputStream in = getClass().getResourceAsStream(EMBEDDED_REPO)) {
if (in != null) {
unzip(in, root);
return true;
}
}
// We may be in unit test, look for
// biz.aQute.bnd.embedded-repo.jar on the
// classpath
StringTokenizer classPathTokenizer = new StringTokenizer(System.getProperty("java.class.path", ""),
File.pathSeparator);
while (classPathTokenizer.hasMoreTokens()) {
String classPathEntry = classPathTokenizer.nextToken()
.trim();
if (EMBEDDED_REPO_TESTING_PATTERN.matcher(classPathEntry)
.matches()) {
try (InputStream in = IO.stream(Paths.get(classPathEntry))) {
unzip(in, root);
return true;
}
}
}
error("Couldn't find biz.aQute.bnd.embedded-repo on the classpath");
return false;
} else
return false;
} finally {
lock.unlock();
}
}
private void unzip(InputStream in, File dir) throws Exception {
try (JarInputStream jin = new JarInputStream(in)) {
byte[] data = new byte[BUFFER_SIZE];
for (JarEntry jentry = jin.getNextJarEntry(); jentry != null; jentry = jin.getNextJarEntry()) {
if (jentry.isDirectory()) {
continue;
}
String jentryName = jentry.getName();
if (jentryName.startsWith("META-INF/")) {
continue;
}
File dest = getFile(dir, jentryName);
long modifiedTime = ZipUtil.getModifiedTime(jentry);
if (!dest.isFile() || dest.lastModified() < modifiedTime || modifiedTime <= 0) {
File dp = dest.getParentFile();
IO.mkdirs(dp);
try (OutputStream out = IO.outputStream(dest)) {
for (int size = jin.read(data); size > 0; size = jin.read(data)) {
out.write(data, 0, size);
}
}
}
}
}
}
}
public void syncCache() throws Exception {
CachedFileRepo cf = new CachedFileRepo();
cf.init();
cf.close();
}
public List<RepositoryPlugin> getRepositories() throws Exception {
if (data.repositories == null) {
data.repositories = getPlugins(RepositoryPlugin.class);
for (RepositoryPlugin repo : data.repositories) {
if (repo instanceof Prepare) {
((Prepare) repo).prepare();
}
}
}
return data.repositories;
}
public Collection<Project> getBuildOrder() throws Exception {
Set<Project> result = new LinkedHashSet<>();
for (Project project : getAllProjects()) {
Collection<Project> dependsOn = project.getDependson();
getBuildOrder(dependsOn, result);
result.add(project);
}
return result;
}
private void getBuildOrder(Collection<Project> dependsOn, Set<Project> result) throws Exception {
for (Project project : dependsOn) {
Collection<Project> subProjects = project.getDependson();
for (Project subProject : subProjects) {
result.add(subProject);
}
result.add(project);
}
}
public static Workspace getWorkspace(String path) throws Exception {
File file = IO.getFile(new File(""), path);
return getWorkspace(file);
}
public Maven getMaven() {
return maven;
}
@Override
protected void setTypeSpecificPlugins(Set<Object> list) {
try {
super.setTypeSpecificPlugins(list);
list.add(this);
list.add(maven);
list.add(settings);
if (!isTrue(getProperty(NOBUILDINCACHE))) {
CachedFileRepo repo = new CachedFileRepo();
list.add(repo);
}
resourceRepositoryImpl = new ResourceRepositoryImpl();
resourceRepositoryImpl.setCache(IO.getFile(getProperty(CACHEDIR, "~/.bnd/caches/shas")));
resourceRepositoryImpl.setExecutor(getExecutor());
resourceRepositoryImpl.setIndexFile(getFile(getBuildDir(), "repo.json"));
resourceRepositoryImpl.setURLConnector(new MultiURLConnectionHandler(this));
customize(resourceRepositoryImpl, null);
list.add(resourceRepositoryImpl);
//
// Exporters
//
list.add(new ExecutableJarExporter());
list.add(new RunbundlesExporter());
try {
HttpClient client = new HttpClient();
client.setOffline(getOffline());
client.setRegistry(this);
try (ConnectionSettings cs = new ConnectionSettings(this, client)) {
cs.readSettings();
}
list.add(client);
} catch (Exception e) {
exception(e, "Failed to load the communication settings");
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Add any extensions listed
*
* @param list
*/
@Override
protected void addExtensions(Set<Object> list) {
//
// <bsn>; version=<range>
//
Parameters extensions = getMergedParameters(EXTENSION);
Map<DownloadBlocker, Attrs> blockers = new HashMap<>();
for (Entry<String, Attrs> i : extensions.entrySet()) {
String bsn = removeDuplicateMarker(i.getKey());
String stringRange = i.getValue()
.get(VERSION_ATTRIBUTE);
logger.debug("Adding extension {}-{}", bsn, stringRange);
if (stringRange == null)
stringRange = Version.LOWEST.toString();
else if (!VersionRange.isVersionRange(stringRange)) {
error("Invalid version range %s on extension %s", stringRange, bsn);
continue;
}
try {
SortedSet<ResourceDescriptor> matches = resourceRepositoryImpl.find(null, bsn,
new VersionRange(stringRange));
if (matches.isEmpty()) {
error("Extension %s;version=%s not found in base repo", bsn, stringRange);
continue;
}
DownloadBlocker blocker = new DownloadBlocker(this);
blockers.put(blocker, i.getValue());
resourceRepositoryImpl.getResource(matches.last().id, blocker);
} catch (Exception e) {
error("Failed to load extension %s-%s, %s", bsn, stringRange, e);
}
}
logger.debug("Found extensions {}", blockers);
for (Entry<DownloadBlocker, Attrs> blocker : blockers.entrySet()) {
try {
String reason = blocker.getKey()
.getReason();
if (reason != null) {
error("Extension load failed: %s", reason);
continue;
}
@SuppressWarnings("resource")
URLClassLoader cl = new URLClassLoader(new URL[] {
blocker.getKey()
.getFile()
.toURI()
.toURL()
}, getClass().getClassLoader());
Enumeration<URL> manifests = cl.getResources("META-INF/MANIFEST.MF");
while (manifests.hasMoreElements()) {
try (InputStream is = manifests.nextElement()
.openStream()) {
Manifest m = new Manifest(is);
Parameters activators = new Parameters(m.getMainAttributes()
.getValue("Extension-Activator"), this);
for (Entry<String, Attrs> e : activators.entrySet()) {
try {
Class<?> c = cl.loadClass(e.getKey());
ExtensionActivator extensionActivator = (ExtensionActivator) c.getConstructor()
.newInstance();
customize(extensionActivator, blocker.getValue());
List<?> plugins = extensionActivator.activate(this, blocker.getValue());
list.add(extensionActivator);
if (plugins != null)
for (Object plugin : plugins) {
list.add(plugin);
}
} catch (ClassNotFoundException cnfe) {
error("Loading extension %s, extension activator missing: %s (ignored)", blocker,
e.getKey());
}
}
}
}
} catch (Exception e) {
error("failed to install extension %s due to %s", blocker, e);
}
}
}
public boolean isOffline() {
return offline.get();
}
public AtomicBoolean getOffline() {
return offline;
}
public Workspace setOffline(boolean on) {
offline.set(on);
return this;
}
/**
* Provide access to the global settings of this machine.
*
* @throws Exception
*/
public String _global(String[] args) throws Exception {
Macro.verifyCommand(args, "${global;<name>[;<default>]}, get a global setting from ~/.bnd/settings.json", null,
2, 3);
String key = args[1];
if (key.equals("key.public"))
return Hex.toHexString(settings.getPublicKey());
if (key.equals("key.private"))
return Hex.toHexString(settings.getPrivateKey());
String s = settings.get(key);
if (s != null)
return s;
if (args.length == 3)
return args[2];
return null;
}
public String _user(String[] args) throws Exception {
return _global(args);
}
/**
* Return the repository signature digests. These digests are a unique id
* for the contents of the repository
*/
public Object _repodigests(String[] args) throws Exception {
Macro.verifyCommand(args, "${repodigests;[;<repo names>]...}, get the repository digests", null, 1, 10000);
List<RepositoryPlugin> repos = getRepositories();
if (args.length > 1) {
repos: for (Iterator<RepositoryPlugin> it = repos.iterator(); it.hasNext();) {
String name = it.next()
.getName();
for (int i = 1; i < args.length; i++) {
if (name.equals(args[i])) {
continue repos;
}
}
it.remove();
}
}
List<String> digests = new ArrayList<>();
for (RepositoryPlugin repo : repos) {
try {
if (repo instanceof RepositoryDigest) {
byte[] digest = ((RepositoryDigest) repo).getDigest();
digests.add(Hex.toHexString(digest));
} else {
if (args.length != 1)
error("Specified repo %s for ${repodigests} was named but it is not found", repo.getName());
}
} catch (Exception e) {
if (args.length != 1)
error("Specified repo %s for digests is not found", repo.getName());
// else Ignore
}
}
return join(digests, ",");
}
public static Run getRun(File file) throws Exception {
if (!file.isFile()) {
return null;
}
File projectDir = file.getParentFile();
File workspaceDir = projectDir.getParentFile();
if (!workspaceDir.isDirectory()) {
return null;
}
Workspace ws = getWorkspaceWithoutException(workspaceDir);
if (ws == null) {
return null;
}
return new Run(ws, projectDir, file);
}
/**
* Report details of this workspace
*/
@Override
public void report(Map<String, Object> table) throws Exception {
super.report(table);
table.put("Workspace", toString());
table.put("Plugins", getPlugins(Object.class));
table.put("Repos", getRepositories());
table.put("Projects in build order", getBuildOrder());
}
public File getCache(String name) {
return getFile(buildDir, CACHEDIR + "/" + name);
}
/**
* Return the workspace repo
*/
public WorkspaceRepository getWorkspaceRepository() {
return workspaceRepo;
}
public void checkStructure() {
if (!getBuildDir().isDirectory())
error("No directory for cnf %s", getBuildDir());
else {
File build = IO.getFile(getBuildDir(), BUILDFILE);
if (build.isFile()) {
error("No %s file in %s", BUILDFILE, getBuildDir());
}
}
}
public File getBuildDir() {
return buildDir;
}
public void setBuildDir(File buildDir) {
this.buildDir = buildDir;
}
public boolean isValid() {
return IO.getFile(getBuildDir(), BUILDFILE)
.isFile();
}
public RepositoryPlugin getRepository(String repo) throws Exception {
for (RepositoryPlugin r : getRepositories()) {
if (repo.equals(r.getName())) {
return r;
}
}
return null;
}
@Override
public void close() {
synchronized (cache) {
WeakReference<Workspace> wsr = cache.get(getBase());
if ((wsr != null) && (wsr.get() == this)) {
cache.remove(getBase());
}
}
try {
super.close();
} catch (IOException e) {
/* For backwards compatibility, we ignore the exception */
}
}
/**
* Get the bnddriver, can be null if not set. The overallDriver is the
* environment that runs this bnd.
*/
public String getDriver() {
if (driver == null) {
driver = getProperty(Constants.BNDDRIVER, null);
if (driver != null)
driver = driver.trim();
}
if (driver != null)
return driver;
return overallDriver;
}
/**
* Set the driver of this environment
*/
public static void setDriver(String driver) {
overallDriver = driver;
}
/**
* Macro to return the driver. Without any arguments, we return the name of
* the driver. If there are arguments, we check each of the arguments
* against the name of the driver. If it matches, we return the driver name.
* If none of the args match the driver name we return an empty string
* (which is false).
*/
public String _driver(String args[]) {
if (args.length == 1) {
return getDriver();
}
String driver = getDriver();
if (driver == null)
driver = getProperty(Constants.BNDDRIVER);
if (driver != null) {
for (int i = 1; i < args.length; i++) {
if (args[i].equalsIgnoreCase(driver))
return driver;
}
}
return "";
}
/**
* Add a gestalt to all workspaces. The gestalt is a set of parts describing
* the environment. Each part has a name and optionally attributes. This
* method adds a gestalt to the VM. Per workspace it is possible to augment
* this.
*/
public static void addGestalt(String part, Attrs attrs) {
Attrs already = overallGestalt.get(part);
if (attrs == null)
attrs = new Attrs();
if (already != null) {
already.putAll(attrs);
} else
already = attrs;
overallGestalt.put(part, already);
}
/**
* Get the attrs for a gestalt part
*/
public Attrs getGestalt(String part) {
return getGestalt().get(part);
}
/**
* Get the attrs for a gestalt part
*/
public Parameters getGestalt() {
if (gestalt == null) {
gestalt = getMergedParameters(Constants.GESTALT);
gestalt.mergeWith(overallGestalt, false);
}
return gestalt;
}
/**
* Get the layout style of the workspace.
*/
public WorkspaceLayout getLayout() {
return layout;
}
/**
* The macro to access the gestalt