]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/a2/FsA2Source.java
Improve initialisation.
[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.SortedSet;
11 import java.util.TreeSet;
12
13 import org.argeo.init.osgi.OsgiBootUtils;
14 import org.osgi.framework.Version;
15
16 /** A file system {@link AbstractProvisioningSource} in A2 format. */
17 public class FsA2Source extends AbstractProvisioningSource implements A2Source {
18 private final Path base;
19
20 public FsA2Source(Path base) {
21 this.base = base;
22 }
23
24 void load() throws IOException {
25 DirectoryStream<Path> contributionPaths = Files.newDirectoryStream(base);
26 SortedSet<A2Contribution> contributions = new TreeSet<>();
27 contributions: for (Path contributionPath : contributionPaths) {
28 if (Files.isDirectory(contributionPath)) {
29 String contributionId = contributionPath.getFileName().toString();
30 if (A2Contribution.BOOT.equals(contributionId))// skip boot
31 continue contributions;
32 A2Contribution contribution = getOrAddContribution(contributionId);
33 contributions.add(contribution);
34 }
35 }
36
37 for (A2Contribution contribution : contributions) {
38 DirectoryStream<Path> modulePaths = Files.newDirectoryStream(base.resolve(contribution.getId()));
39 modules: for (Path modulePath : modulePaths) {
40 if (!Files.isDirectory(modulePath)) {
41 // OsgiBootUtils.debug("Registering " + modulePath);
42 String moduleFileName = modulePath.getFileName().toString();
43 int lastDot = moduleFileName.lastIndexOf('.');
44 String ext = moduleFileName.substring(lastDot + 1);
45 if (!"jar".equals(ext))
46 continue modules;
47 // String moduleName = moduleFileName.substring(0, lastDot);
48 // if (moduleName.endsWith("-SNAPSHOT"))
49 // moduleName = moduleName.substring(0, moduleName.length() - "-SNAPSHOT".length());
50 // int lastDash = moduleName.lastIndexOf('-');
51 // String versionStr = moduleName.substring(lastDash + 1);
52 // String componentName = moduleName.substring(0, lastDash);
53 // if(versionStr.endsWith("-SNAPSHOT")) {
54 // versionStr = readVersionFromModule(modulePath);
55 // }
56 Version version;
57 // try {
58 // version = new Version(versionStr);
59 // } catch (Exception e) {
60 String versionStr = readVersionFromModule(modulePath);
61 String componentName = readSymbolicNameFromModule(modulePath);
62 if (versionStr != null) {
63 version = new Version(versionStr);
64 } else {
65 OsgiBootUtils.debug("Ignore " + modulePath + " since version cannot be found");
66 continue modules;
67 }
68 // }
69 A2Component component = contribution.getOrAddComponent(componentName);
70 A2Module module = component.getOrAddModule(version, modulePath);
71 if (OsgiBootUtils.isDebug())
72 OsgiBootUtils.debug("Registered " + module);
73 }
74 }
75 }
76
77 }
78
79 @Override
80 public URI getUri() {
81 URI baseUri = base.toUri();
82 try {
83 if (baseUri.getScheme().equals("file")) {
84 return new URI(SCHEME_A2, null, base.toString(), null);
85 } else {
86 throw new UnsupportedOperationException("Unsupported scheme " + baseUri.getScheme());
87 }
88 } catch (URISyntaxException e) {
89 throw new IllegalStateException("Cannot build URI from " + baseUri, e);
90 }
91 }
92
93 public static void main(String[] args) {
94 try {
95 FsA2Source context = new FsA2Source(Paths.get(
96 "/home/mbaudier/dev/git/apache2/argeo-commons/dist/argeo-node/target/argeo-node-2.1.77-SNAPSHOT/share/osgi"));
97 context.load();
98 context.asTree();
99 } catch (Exception e) {
100 e.printStackTrace();
101 }
102 }
103
104 }