]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/a2/FsA2Source.java
Repare next release cycle
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / a2 / FsA2Source.java
1 package org.argeo.init.a2;
2
3 import java.io.IOException;
4 import java.net.URI;
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;
10 import java.util.Map;
11 import java.util.SortedMap;
12 import java.util.StringJoiner;
13 import java.util.TreeMap;
14
15 import org.argeo.init.osgi.OsgiBootUtils;
16 import org.osgi.framework.Version;
17
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;
22
23 // public FsA2Source(Path base) {
24 // this(base, new HashMap<>());
25 // }
26
27 public FsA2Source(Path base, Map<String, String> variantsXOr, boolean usingReference) {
28 super(usingReference);
29 this.base = base;
30 this.variantsXOr = new HashMap<>(variantsXOr);
31 }
32
33 void load() throws IOException {
34 SortedMap<Path, A2Contribution> contributions = new TreeMap<>();
35
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);
45 } else {// variants
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);
51 }
52
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;
58 }
59 }
60
61 if (variantPath == null)
62 continue contributions;
63
64 // a variant was found, let's collect its contributions (also common ones in its
65 // parent)
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);
71 }
72 }
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);
78 }
79 }
80 }
81 }
82 }
83
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))
95 continue modules;
96 Version version;
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);
103 } else {
104 OsgiBootUtils.debug("Ignore " + modulePath + " since version cannot be found");
105 continue modules;
106 }
107 // }
108 A2Component component = contribution.getOrAddComponent(componentName);
109 A2Module module = component.getOrAddModule(version, modulePath);
110 if (OsgiBootUtils.isDebug())
111 OsgiBootUtils.debug("Registered " + module);
112 }
113 }
114 }
115
116 }
117
118 @Override
119 public URI getUri() {
120 URI baseUri = base.toUri();
121 try {
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));
128 }
129 queryPart = sj.toString();
130 }
131 return new URI(isUsingReference() ? SCHEME_A2_REFERENCE : SCHEME_A2, null, base.toString(), queryPart,
132 null);
133 } else {
134 throw new UnsupportedOperationException("Unsupported scheme " + baseUri.getScheme());
135 }
136 } catch (URISyntaxException e) {
137 throw new IllegalStateException("Cannot build URI from " + baseUri, e);
138 }
139 }
140
141 protected Map<String, String> getVariantsXOr() {
142 return variantsXOr;
143 }
144
145 // public static void main(String[] args) {
146 // if (args.length == 0)
147 // throw new IllegalArgumentException("Usage: <path to A2 base>");
148 // try {
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);
153 // context.load();
154 // context.asTree();
155 // } catch (Exception e) {
156 // e.printStackTrace();
157 // }
158 // }
159
160 }