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
.SortedSet
;
11 import java
.util
.TreeSet
;
13 import org
.argeo
.init
.osgi
.OsgiBootUtils
;
14 import org
.osgi
.framework
.Version
;
16 /** A file system {@link AbstractProvisioningSource} in A2 format. */
17 public class FsA2Source
extends AbstractProvisioningSource
implements A2Source
{
18 private final Path base
;
20 public FsA2Source(Path base
) {
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
);
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
))
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);
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
);
65 OsgiBootUtils
.debug("Ignore " + modulePath
+ " since version cannot be found");
69 A2Component component
= contribution
.getOrAddComponent(componentName
);
70 A2Module module
= component
.getOrAddModule(version
, modulePath
);
71 if (OsgiBootUtils
.isDebug())
72 OsgiBootUtils
.debug("Registered " + module
);
81 URI baseUri
= base
.toUri();
83 if (baseUri
.getScheme().equals("file")) {
84 return new URI(SCHEME_A2
, null, base
.toString(), null);
86 throw new UnsupportedOperationException("Unsupported scheme " + baseUri
.getScheme());
88 } catch (URISyntaxException e
) {
89 throw new IllegalStateException("Cannot build URI from " + baseUri
, e
);
93 public static void main(String
[] args
) {
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"));
99 } catch (Exception e
) {