]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/a2/FsA2Source.java
Introduce CMS-specific user APIs, based at this stage on OSGi UserAdmin
[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.TreeMap;
13
14 import org.argeo.init.osgi.OsgiBootUtils;
15 import org.osgi.framework.Version;
16
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;
21
22 // public FsA2Source(Path base) {
23 // this(base, new HashMap<>());
24 // }
25
26 public FsA2Source(Path base, Map<String, String> xOr, boolean useReference) {
27 super(useReference);
28 this.base = base;
29 this.xOr = new HashMap<>(xOr);
30 }
31
32 void load() throws IOException {
33 SortedMap<Path, A2Contribution> contributions = new TreeMap<>();
34
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);
44 } else {// variants
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);
50 }
51
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;
57 }
58 }
59
60 if (variantPath == null)
61 continue contributions;
62
63 // a variant was found, let's collect its contributions (also common ones in its
64 // parent)
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);
70 }
71 }
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);
77 }
78 }
79 }
80 }
81 }
82
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))
94 continue modules;
95 Version version;
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);
102 } else {
103 OsgiBootUtils.debug("Ignore " + modulePath + " since version cannot be found");
104 continue modules;
105 }
106 // }
107 A2Component component = contribution.getOrAddComponent(componentName);
108 A2Module module = component.getOrAddModule(version, modulePath);
109 if (OsgiBootUtils.isDebug())
110 OsgiBootUtils.debug("Registered " + module);
111 }
112 }
113 }
114
115 }
116
117 @Override
118 public URI getUri() {
119 URI baseUri = base.toUri();
120 try {
121 if (baseUri.getScheme().equals("file")) {
122 return new URI(SCHEME_A2, null, base.toString(), null);
123 } else {
124 throw new UnsupportedOperationException("Unsupported scheme " + baseUri.getScheme());
125 }
126 } catch (URISyntaxException e) {
127 throw new IllegalStateException("Cannot build URI from " + baseUri, e);
128 }
129 }
130
131 // public static void main(String[] args) {
132 // if (args.length == 0)
133 // throw new IllegalArgumentException("Usage: <path to A2 base>");
134 // try {
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);
139 // context.load();
140 // context.asTree();
141 // } catch (Exception e) {
142 // e.printStackTrace();
143 // }
144 // }
145
146 }