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
.StringJoiner
;
13 import java
.util
.TreeMap
;
15 import org
.argeo
.init
.osgi
.OsgiBootUtils
;
16 import org
.osgi
.framework
.Version
;
18 /** A file system {@link AbstractProvisioningSource} in A2 format. */
19 public class FsA2Source
extends AbstractProvisioningSource
implements A2Source
{
20 private final Path base
;
21 private final Map
<String
, String
> variantsXOr
;
23 // public FsA2Source(Path base) {
24 // this(base, new HashMap<>());
27 public FsA2Source(Path base
, Map
<String
, String
> variantsXOr
, boolean usingReference
) {
28 super(usingReference
);
30 this.variantsXOr
= new HashMap
<>(variantsXOr
);
33 void load() throws IOException
{
34 SortedMap
<Path
, A2Contribution
> contributions
= new TreeMap
<>();
36 DirectoryStream
<Path
> contributionPaths
= Files
.newDirectoryStream(base
);
37 contributions
: for (Path contributionPath
: contributionPaths
) {
38 if (Files
.isDirectory(contributionPath
)) {
39 String contributionId
= contributionPath
.getFileName().toString();
40 if (A2Contribution
.BOOT
.equals(contributionId
))// skip boot
41 continue contributions
;
42 if (contributionId
.contains(".")) {
43 A2Contribution contribution
= getOrAddContribution(contributionId
);
44 contributions
.put(contributionPath
, contribution
);
46 Path variantPath
= null;
47 // is it an explicit variant?
48 String variant
= variantsXOr
.get(contributionPath
.getFileName().toString());
49 if (variant
!= null) {
50 variantPath
= contributionPath
.resolve(variant
);
53 // is there a default variant?
54 if (variantPath
== null) {
55 Path defaultPath
= contributionPath
.resolve(A2Contribution
.DEFAULT
);
56 if (Files
.exists(defaultPath
)) {
57 variantPath
= defaultPath
;
61 if (variantPath
== null)
62 continue contributions
;
64 // a variant was found, let's collect its contributions (also common ones in its
66 for (Path variantContributionPath
: Files
.newDirectoryStream(variantPath
.getParent())) {
67 String variantContributionId
= variantContributionPath
.getFileName().toString();
68 if (variantContributionId
.contains(".")) {
69 A2Contribution contribution
= getOrAddContribution(variantContributionId
);
70 contributions
.put(variantContributionPath
, contribution
);
73 for (Path variantContributionPath
: Files
.newDirectoryStream(variantPath
)) {
74 String variantContributionId
= variantContributionPath
.getFileName().toString();
75 if (variantContributionId
.contains(".")) {
76 A2Contribution contribution
= getOrAddContribution(variantContributionId
);
77 contributions
.put(variantContributionPath
, contribution
);
84 for (Path contributionPath
: contributions
.keySet()) {
85 String contributionId
= contributionPath
.getFileName().toString();
86 A2Contribution contribution
= getOrAddContribution(contributionId
);
87 DirectoryStream
<Path
> modulePaths
= Files
.newDirectoryStream(contributionPath
);
88 modules
: for (Path modulePath
: modulePaths
) {
89 if (!Files
.isDirectory(modulePath
)) {
90 // OsgiBootUtils.debug("Registering " + modulePath);
91 String moduleFileName
= modulePath
.getFileName().toString();
92 int lastDot
= moduleFileName
.lastIndexOf('.');
93 String ext
= moduleFileName
.substring(lastDot
+ 1);
94 if (!"jar".equals(ext
))
97 // TODO optimise? check attributes?
98 String
[] nameVersion
= readNameVersionFromModule(modulePath
);
99 String componentName
= nameVersion
[0];
100 String versionStr
= nameVersion
[1];
101 if (versionStr
!= null) {
102 version
= new Version(versionStr
);
104 OsgiBootUtils
.debug("Ignore " + modulePath
+ " since version cannot be found");
108 A2Component component
= contribution
.getOrAddComponent(componentName
);
109 A2Module module
= component
.getOrAddModule(version
, modulePath
);
110 if (OsgiBootUtils
.isDebug())
111 OsgiBootUtils
.debug("Registered " + module
);
119 public URI
getUri() {
120 URI baseUri
= base
.toUri();
122 if (baseUri
.getScheme().equals("file")) {
123 String queryPart
= "";
124 if (!getVariantsXOr().isEmpty()) {
125 StringJoiner sj
= new StringJoiner("&");
126 for (String key
: getVariantsXOr().keySet()) {
127 sj
.add(key
+ "=" + getVariantsXOr().get(key
));
129 queryPart
= sj
.toString();
131 return new URI(isUsingReference() ? SCHEME_A2_REFERENCE
: SCHEME_A2
, null, base
.toString(), queryPart
,
134 throw new UnsupportedOperationException("Unsupported scheme " + baseUri
.getScheme());
136 } catch (URISyntaxException e
) {
137 throw new IllegalStateException("Cannot build URI from " + baseUri
, e
);
141 protected Map
<String
, String
> getVariantsXOr() {
145 // public static void main(String[] args) {
146 // if (args.length == 0)
147 // throw new IllegalArgumentException("Usage: <path to A2 base>");
149 // Map<String, String> xOr = new HashMap<>();
150 // xOr.put("osgi", "equinox");
151 // xOr.put("swt", "rap");
152 // FsA2Source context = new FsA2Source(Paths.get(args[0]), xOr);
155 // } catch (Exception e) {
156 // e.printStackTrace();