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
.nio
.file
.Paths
;
10 import java
.util
.HashMap
;
12 import java
.util
.SortedMap
;
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 Map
<String
, String
> xOr
;
23 public FsA2Source(Path base
) {
24 this(base
, new HashMap
<>());
27 public FsA2Source(Path base
, Map
<String
, String
> 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
))
95 // String moduleName = moduleFileName.substring(0, lastDot);
96 // if (moduleName.endsWith("-SNAPSHOT"))
97 // moduleName = moduleName.substring(0, moduleName.length() - "-SNAPSHOT".length());
98 // int lastDash = moduleName.lastIndexOf('-');
99 // String versionStr = moduleName.substring(lastDash + 1);
100 // String componentName = moduleName.substring(0, lastDash);
101 // if(versionStr.endsWith("-SNAPSHOT")) {
102 // versionStr = readVersionFromModule(modulePath);
106 // version = new Version(versionStr);
107 // } catch (Exception e) {
108 String
[] nameVersion
= readNameVersionFromModule(modulePath
);
109 String componentName
= nameVersion
[0];
110 String versionStr
= nameVersion
[1];
111 if (versionStr
!= null) {
112 version
= new Version(versionStr
);
114 OsgiBootUtils
.debug("Ignore " + modulePath
+ " since version cannot be found");
118 A2Component component
= contribution
.getOrAddComponent(componentName
);
119 A2Module module
= component
.getOrAddModule(version
, modulePath
);
120 if (OsgiBootUtils
.isDebug())
121 OsgiBootUtils
.debug("Registered " + module
);
129 public URI
getUri() {
130 URI baseUri
= base
.toUri();
132 if (baseUri
.getScheme().equals("file")) {
133 return new URI(SCHEME_A2
, null, base
.toString(), null);
135 throw new UnsupportedOperationException("Unsupported scheme " + baseUri
.getScheme());
137 } catch (URISyntaxException e
) {
138 throw new IllegalStateException("Cannot build URI from " + baseUri
, e
);
142 public static void main(String
[] args
) {
143 if (args
.length
== 0)
144 throw new IllegalArgumentException("Usage: <path to A2 base>");
146 Map
<String
, String
> xOr
= new HashMap
<>();
147 xOr
.put("osgi", "equinox");
148 xOr
.put("swt", "rap");
149 FsA2Source context
= new FsA2Source(Paths
.get(args
[0]), xOr
);
152 } catch (Exception e
) {