1 package org
.argeo
.init
.a2
;
3 import java
.io
.IOException
;
5 import java
.net
.URISyntaxException
;
6 import java
.nio
.file
.DirectoryStream
;
7 import java
.nio
.file
.Files
;
8 import java
.nio
.file
.Path
;
9 import java
.util
.HashMap
;
11 import java
.util
.SortedMap
;
12 import java
.util
.TreeMap
;
14 import org
.argeo
.init
.osgi
.OsgiBootUtils
;
15 import org
.osgi
.framework
.Version
;
17 /** A file system {@link AbstractProvisioningSource} in A2 format. */
18 public class FsA2Source
extends AbstractProvisioningSource
implements A2Source
{
19 private final Path base
;
20 private final Map
<String
, String
> xOr
;
22 // public FsA2Source(Path base) {
23 // this(base, new HashMap<>());
26 public FsA2Source(Path base
, Map
<String
, String
> xOr
, boolean useReference
) {
29 this.xOr
= new HashMap
<>(xOr
);
32 void load() throws IOException
{
33 SortedMap
<Path
, A2Contribution
> contributions
= new TreeMap
<>();
35 DirectoryStream
<Path
> contributionPaths
= Files
.newDirectoryStream(base
);
36 contributions
: for (Path contributionPath
: contributionPaths
) {
37 if (Files
.isDirectory(contributionPath
)) {
38 String contributionId
= contributionPath
.getFileName().toString();
39 if (A2Contribution
.BOOT
.equals(contributionId
))// skip boot
40 continue contributions
;
41 if (contributionId
.contains(".")) {
42 A2Contribution contribution
= getOrAddContribution(contributionId
);
43 contributions
.put(contributionPath
, contribution
);
45 Path variantPath
= null;
46 // is it an explicit variant?
47 String variant
= xOr
.get(contributionPath
.getFileName().toString());
48 if (variant
!= null) {
49 variantPath
= contributionPath
.resolve(variant
);
52 // is there a default variant?
53 if (variantPath
== null) {
54 Path defaultPath
= contributionPath
.resolve(A2Contribution
.DEFAULT
);
55 if (Files
.exists(defaultPath
)) {
56 variantPath
= defaultPath
;
60 if (variantPath
== null)
61 continue contributions
;
63 // a variant was found, let's collect its contributions (also common ones in its
65 for (Path variantContributionPath
: Files
.newDirectoryStream(variantPath
.getParent())) {
66 String variantContributionId
= variantContributionPath
.getFileName().toString();
67 if (variantContributionId
.contains(".")) {
68 A2Contribution contribution
= getOrAddContribution(variantContributionId
);
69 contributions
.put(variantContributionPath
, contribution
);
72 for (Path variantContributionPath
: Files
.newDirectoryStream(variantPath
)) {
73 String variantContributionId
= variantContributionPath
.getFileName().toString();
74 if (variantContributionId
.contains(".")) {
75 A2Contribution contribution
= getOrAddContribution(variantContributionId
);
76 contributions
.put(variantContributionPath
, contribution
);
83 for (Path contributionPath
: contributions
.keySet()) {
84 String contributionId
= contributionPath
.getFileName().toString();
85 A2Contribution contribution
= getOrAddContribution(contributionId
);
86 DirectoryStream
<Path
> modulePaths
= Files
.newDirectoryStream(contributionPath
);
87 modules
: for (Path modulePath
: modulePaths
) {
88 if (!Files
.isDirectory(modulePath
)) {
89 // OsgiBootUtils.debug("Registering " + modulePath);
90 String moduleFileName
= modulePath
.getFileName().toString();
91 int lastDot
= moduleFileName
.lastIndexOf('.');
92 String ext
= moduleFileName
.substring(lastDot
+ 1);
93 if (!"jar".equals(ext
))
96 // TODO optimise? check attributes?
97 String
[] nameVersion
= readNameVersionFromModule(modulePath
);
98 String componentName
= nameVersion
[0];
99 String versionStr
= nameVersion
[1];
100 if (versionStr
!= null) {
101 version
= new Version(versionStr
);
103 OsgiBootUtils
.debug("Ignore " + modulePath
+ " since version cannot be found");
107 A2Component component
= contribution
.getOrAddComponent(componentName
);
108 A2Module module
= component
.getOrAddModule(version
, modulePath
);
109 if (OsgiBootUtils
.isDebug())
110 OsgiBootUtils
.debug("Registered " + module
);
118 public URI
getUri() {
119 URI baseUri
= base
.toUri();
121 if (baseUri
.getScheme().equals("file")) {
122 return new URI(SCHEME_A2
, null, base
.toString(), null);
124 throw new UnsupportedOperationException("Unsupported scheme " + baseUri
.getScheme());
126 } catch (URISyntaxException e
) {
127 throw new IllegalStateException("Cannot build URI from " + baseUri
, e
);
131 // public static void main(String[] args) {
132 // if (args.length == 0)
133 // throw new IllegalArgumentException("Usage: <path to A2 base>");
135 // Map<String, String> xOr = new HashMap<>();
136 // xOr.put("osgi", "equinox");
137 // xOr.put("swt", "rap");
138 // FsA2Source context = new FsA2Source(Paths.get(args[0]), xOr);
141 // } catch (Exception e) {
142 // e.printStackTrace();