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 if (Files
.exists(variantPath
.getParent())) {
67 for (Path variantContributionPath
: Files
.newDirectoryStream(variantPath
.getParent())) {
68 String variantContributionId
= variantContributionPath
.getFileName().toString();
69 if (variantContributionId
.contains(".")) {
70 A2Contribution contribution
= getOrAddContribution(variantContributionId
);
71 contributions
.put(variantContributionPath
, contribution
);
75 if (Files
.exists(variantPath
)) {
76 for (Path variantContributionPath
: Files
.newDirectoryStream(variantPath
)) {
77 String variantContributionId
= variantContributionPath
.getFileName().toString();
78 if (variantContributionId
.contains(".")) {
79 A2Contribution contribution
= getOrAddContribution(variantContributionId
);
80 contributions
.put(variantContributionPath
, contribution
);
88 for (Path contributionPath
: contributions
.keySet()) {
89 String contributionId
= contributionPath
.getFileName().toString();
90 A2Contribution contribution
= getOrAddContribution(contributionId
);
91 DirectoryStream
<Path
> modulePaths
= Files
.newDirectoryStream(contributionPath
);
92 modules
: for (Path modulePath
: modulePaths
) {
93 if (!Files
.isDirectory(modulePath
)) {
94 // OsgiBootUtils.debug("Registering " + modulePath);
95 String moduleFileName
= modulePath
.getFileName().toString();
96 int lastDot
= moduleFileName
.lastIndexOf('.');
97 String ext
= moduleFileName
.substring(lastDot
+ 1);
98 if (!"jar".equals(ext
))
101 // TODO optimise? check attributes?
102 String
[] nameVersion
= readNameVersionFromModule(modulePath
);
103 String componentName
= nameVersion
[0];
104 String versionStr
= nameVersion
[1];
105 if (versionStr
!= null) {
106 version
= new Version(versionStr
);
108 OsgiBootUtils
.debug("Ignore " + modulePath
+ " since version cannot be found");
112 A2Component component
= contribution
.getOrAddComponent(componentName
);
113 A2Module module
= component
.getOrAddModule(version
, modulePath
);
114 if (OsgiBootUtils
.isDebug())
115 OsgiBootUtils
.debug("Registered " + module
);
123 public URI
getUri() {
124 URI baseUri
= base
.toUri();
126 if (baseUri
.getScheme().equals("file")) {
127 String queryPart
= "";
128 if (!getVariantsXOr().isEmpty()) {
129 StringJoiner sj
= new StringJoiner("&");
130 for (String key
: getVariantsXOr().keySet()) {
131 sj
.add(key
+ "=" + getVariantsXOr().get(key
));
133 queryPart
= sj
.toString();
135 return new URI(isUsingReference() ? SCHEME_A2_REFERENCE
: SCHEME_A2
, null, base
.toString(), queryPart
,
138 throw new UnsupportedOperationException("Unsupported scheme " + baseUri
.getScheme());
140 } catch (URISyntaxException e
) {
141 throw new IllegalStateException("Cannot build URI from " + baseUri
, e
);
145 protected Map
<String
, String
> getVariantsXOr() {
149 // public static void main(String[] args) {
150 // if (args.length == 0)
151 // throw new IllegalArgumentException("Usage: <path to A2 base>");
153 // Map<String, String> xOr = new HashMap<>();
154 // xOr.put("osgi", "equinox");
155 // xOr.put("swt", "rap");
156 // FsA2Source context = new FsA2Source(Paths.get(args[0]), xOr);
159 // } catch (Exception e) {
160 // e.printStackTrace();