1 package org
.argeo
.osgi
.boot
.a2
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.nio
.file
.Files
;
7 import java
.nio
.file
.Path
;
8 import java
.util
.Collections
;
10 import java
.util
.SortedMap
;
11 import java
.util
.TreeMap
;
12 import java
.util
.jar
.JarInputStream
;
13 import java
.util
.jar
.Manifest
;
15 import org
.argeo
.osgi
.boot
.OsgiBootException
;
16 import org
.osgi
.framework
.Constants
;
17 import org
.osgi
.framework
.Version
;
19 abstract class ProvisioningSource
{
20 final Map
<String
, A2Contribution
> contributions
= Collections
.synchronizedSortedMap(new TreeMap
<>());
22 A2Contribution
getOrAddContribution(String contributionId
) {
23 if (contributions
.containsKey(contributionId
))
24 return contributions
.get(contributionId
);
26 return new A2Contribution(this, contributionId
);
29 void asTree(String prefix
, StringBuffer buf
) {
32 for (String contributionId
: contributions
.keySet()) {
34 buf
.append(contributionId
);
36 A2Contribution contribution
= contributions
.get(contributionId
);
37 contribution
.asTree(prefix
+ " ", buf
);
42 StringBuffer buf
= new StringBuffer();
44 System
.out
.println(buf
);
47 A2Component
findComponent(String componentId
) {
48 SortedMap
<A2Contribution
, A2Component
> res
= new TreeMap
<>();
49 for (A2Contribution contribution
: contributions
.values()) {
50 components
: for (String componentIdKey
: contribution
.components
.keySet()) {
51 if (componentId
.equals(componentIdKey
)) {
52 res
.put(contribution
, contribution
.components
.get(componentIdKey
));
59 // TODO explicit contribution priorities
60 return res
.get(res
.lastKey());
64 A2Branch
findBranch(String componentId
, Version version
) {
65 A2Component component
= findComponent(componentId
);
66 if (component
== null)
68 String branchId
= version
.getMajor() + "." + version
.getMinor();
69 if (!component
.branches
.containsKey(branchId
))
71 return component
.branches
.get(branchId
);
74 protected String
readVersionFromModule(Path modulePath
) {
75 try (JarInputStream in
= new JarInputStream(newInputStream(modulePath
))) {
76 Manifest manifest
= in
.getManifest();
77 String versionStr
= manifest
.getMainAttributes().getValue(Constants
.BUNDLE_VERSION
);
79 } catch (IOException e
) {
80 throw new OsgiBootException("Cannot read manifest from " + modulePath
, e
);
84 protected String
readSymbolicNameFromModule(Path modulePath
) {
85 try (JarInputStream in
= new JarInputStream(newInputStream(modulePath
))) {
86 Manifest manifest
= in
.getManifest();
87 String symbolicName
= manifest
.getMainAttributes().getValue(Constants
.BUNDLE_SYMBOLICNAME
);
88 int semiColIndex
= symbolicName
.indexOf(';');
89 if (semiColIndex
>= 0)
90 symbolicName
= symbolicName
.substring(0, semiColIndex
);
92 } catch (IOException e
) {
93 throw new OsgiBootException("Cannot read manifest from " + modulePath
, e
);
97 InputStream
newInputStream(Object locator
) throws IOException
{
98 if (locator
instanceof Path
) {
99 return Files
.newInputStream((Path
) locator
);
100 } else if (locator
instanceof URL
) {
101 return ((URL
) locator
).openStream();
103 throw new IllegalArgumentException("Unsupported module locator type " + locator
.getClass());