]> git.argeo.org Git - lgpl/argeo-commons.git/blob - a2/ProvisioningSource.java
Prepare next development cycle
[lgpl/argeo-commons.git] / a2 / ProvisioningSource.java
1 package org.argeo.osgi.boot.a2;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.util.Collections;
9 import java.util.Map;
10 import java.util.SortedMap;
11 import java.util.TreeMap;
12 import java.util.jar.JarInputStream;
13 import java.util.jar.Manifest;
14
15 import org.argeo.osgi.boot.OsgiBootException;
16 import org.osgi.framework.Constants;
17 import org.osgi.framework.Version;
18
19 abstract class ProvisioningSource {
20 final Map<String, A2Contribution> contributions = Collections.synchronizedSortedMap(new TreeMap<>());
21
22 A2Contribution getOrAddContribution(String contributionId) {
23 if (contributions.containsKey(contributionId))
24 return contributions.get(contributionId);
25 else
26 return new A2Contribution(this, contributionId);
27 }
28
29 void asTree(String prefix, StringBuffer buf) {
30 if (prefix == null)
31 prefix = "";
32 for (String contributionId : contributions.keySet()) {
33 buf.append(prefix);
34 buf.append(contributionId);
35 buf.append('\n');
36 A2Contribution contribution = contributions.get(contributionId);
37 contribution.asTree(prefix + " ", buf);
38 }
39 }
40
41 void asTree() {
42 StringBuffer buf = new StringBuffer();
43 asTree("", buf);
44 System.out.println(buf);
45 }
46
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));
53 break components;
54 }
55 }
56 }
57 if (res.size() == 0)
58 return null;
59 // TODO explicit contribution priorities
60 return res.get(res.lastKey());
61
62 }
63
64 A2Branch findBranch(String componentId, Version version) {
65 A2Component component = findComponent(componentId);
66 if (component == null)
67 return null;
68 String branchId = version.getMajor() + "." + version.getMinor();
69 if (!component.branches.containsKey(branchId))
70 return null;
71 return component.branches.get(branchId);
72 }
73
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);
78 return versionStr;
79 } catch (IOException e) {
80 throw new OsgiBootException("Cannot read manifest from " + modulePath, e);
81 }
82 }
83
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);
91 return symbolicName;
92 } catch (IOException e) {
93 throw new OsgiBootException("Cannot read manifest from " + modulePath, e);
94 }
95 }
96
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();
102 } else {
103 throw new IllegalArgumentException("Unsupported module locator type " + locator.getClass());
104 }
105 }
106 }