]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/a2/FsA2Source.java
Add account-related RFC 2307bis LDAP objects and attributes
[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.nio.file.Paths;
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.SortedMap;
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 Map<String, String> xOr;
22
23 public FsA2Source(Path base) {
24 this(base, new HashMap<>());
25 }
26
27 public FsA2Source(Path base, Map<String, String> xOr) {
28 this.base = base;
29 this.xOr = 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 // 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);
103 // }
104 Version version;
105 // try {
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);
113 } else {
114 OsgiBootUtils.debug("Ignore " + modulePath + " since version cannot be found");
115 continue modules;
116 }
117 // }
118 A2Component component = contribution.getOrAddComponent(componentName);
119 A2Module module = component.getOrAddModule(version, modulePath);
120 if (OsgiBootUtils.isDebug())
121 OsgiBootUtils.debug("Registered " + module);
122 }
123 }
124 }
125
126 }
127
128 @Override
129 public URI getUri() {
130 URI baseUri = base.toUri();
131 try {
132 if (baseUri.getScheme().equals("file")) {
133 return new URI(SCHEME_A2, null, base.toString(), null);
134 } else {
135 throw new UnsupportedOperationException("Unsupported scheme " + baseUri.getScheme());
136 }
137 } catch (URISyntaxException e) {
138 throw new IllegalStateException("Cannot build URI from " + baseUri, e);
139 }
140 }
141
142 public static void main(String[] args) {
143 if (args.length == 0)
144 throw new IllegalArgumentException("Usage: <path to A2 base>");
145 try {
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);
150 context.load();
151 context.asTree();
152 } catch (Exception e) {
153 e.printStackTrace();
154 }
155 }
156
157 }