]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ProvisioningManager.java
a237fdf48aecaa79349df7f0999c183c0e05a8e6
[lgpl/argeo-commons.git] / ProvisioningManager.java
1 package org.argeo.init.a2;
2
3 import static org.argeo.init.a2.A2Source.SCHEME_A2;
4 import static org.argeo.init.a2.A2Source.SCHEME_A2_REFERENCE;
5
6 import java.io.File;
7 import java.io.UnsupportedEncodingException;
8 import java.net.URI;
9 import java.net.URLDecoder;
10 import java.nio.charset.StandardCharsets;
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13 import java.nio.file.Paths;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.LinkedHashMap;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.argeo.init.osgi.OsgiBootUtils;
26 import org.osgi.framework.Bundle;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.Constants;
29 import org.osgi.framework.Version;
30 import org.osgi.framework.wiring.FrameworkWiring;
31
32 /** Loads provisioning sources into an OSGi context. */
33 public class ProvisioningManager {
34 BundleContext bc;
35 OsgiContext osgiContext;
36 List<ProvisioningSource> sources = Collections.synchronizedList(new ArrayList<>());
37
38 public ProvisioningManager(BundleContext bc) {
39 this.bc = bc;
40 osgiContext = new OsgiContext(bc);
41 osgiContext.load();
42 }
43
44 protected void addSource(ProvisioningSource source) {
45 sources.add(source);
46 }
47
48 void installWholeSource(ProvisioningSource source) {
49 Set<Bundle> updatedBundles = new HashSet<>();
50 for (A2Contribution contribution : source.listContributions(null)) {
51 for (A2Component component : contribution.components.values()) {
52 A2Module module = component.last().last();
53 Bundle bundle = installOrUpdate(module);
54 if (bundle != null)
55 updatedBundles.add(bundle);
56 }
57 }
58 FrameworkWiring frameworkWiring = bc.getBundle(0).adapt(FrameworkWiring.class);
59 frameworkWiring.refreshBundles(updatedBundles);
60 }
61
62 public void registerSource(String uri) {
63 try {
64 URI u = new URI(uri);
65
66 // XOR
67 Map<String, List<String>> properties = queryToMap(u);
68 Map<String, String> xOr = new HashMap<>();
69 for (String key : properties.keySet()) {
70 List<String> lst = properties.get(key);
71 if (lst.size() != 1)
72 throw new IllegalArgumentException("Invalid XOR definitions in " + uri);
73 xOr.put(key, lst.get(0));
74 }
75
76 if (SCHEME_A2.equals(u.getScheme()) || SCHEME_A2_REFERENCE.equals(u.getScheme())) {
77 if (u.getHost() == null || "".equals(u.getHost())) {
78 String baseStr = u.getPath();
79 if (File.separatorChar == '\\') {// MS Windows
80 baseStr = baseStr.substring(1).replace('/', File.separatorChar);
81 }
82 Path base = Paths.get(baseStr);
83 if (Files.exists(base)) {
84 FsA2Source source = new FsA2Source(base, xOr, SCHEME_A2_REFERENCE.equals(u.getScheme()));
85 source.load();
86 addSource(source);
87 OsgiBootUtils.info("Registered " + uri + " as source");
88 } else {
89 OsgiBootUtils.debug("Source " + base + " does not exist, ignoring.");
90 }
91 } else {
92 throw new UnsupportedOperationException(
93 "Remote installation is not yet supported, cannot add source " + u);
94 }
95 } else {
96 throw new IllegalArgumentException("Unkown scheme: for source " + u);
97 }
98 } catch (Exception e) {
99 throw new A2Exception("Cannot add source " + uri, e);
100 }
101 }
102
103 public boolean registerDefaultSource() {
104 String frameworkLocation = bc.getProperty("osgi.framework");
105 try {
106 URI frameworkLocationUri = new URI(frameworkLocation);
107 if ("file".equals(frameworkLocationUri.getScheme())) {
108 Path frameworkPath = Paths.get(frameworkLocationUri);
109 if (frameworkPath.getParent().getFileName().toString().equals(A2Contribution.BOOT)) {
110 Path base = frameworkPath.getParent().getParent();
111 String baseStr = base.toString();
112 if (File.separatorChar == '\\')// MS Windows
113 baseStr = '/' + baseStr.replace(File.separatorChar, '/');
114 URI baseUri = new URI(A2Source.SCHEME_A2, null, null, 0, baseStr, null, null);
115 registerSource(baseUri.toString());
116 OsgiBootUtils.debug("Default source from framework location " + frameworkLocation);
117 return true;
118 }
119 }
120 } catch (Exception e) {
121 OsgiBootUtils.error("Cannot register default source based on framework location " + frameworkLocation, e);
122 }
123 return false;
124 }
125
126 public void install(String spec) {
127 if (spec == null) {
128 for (ProvisioningSource source : sources) {
129 installWholeSource(source);
130 }
131 }
132 }
133
134 /** @return the new/updated bundle, or null if nothing was done. */
135 protected Bundle installOrUpdate(A2Module module) {
136 try {
137 ProvisioningSource moduleSource = module.getBranch().getComponent().getContribution().getSource();
138 Version moduleVersion = module.getVersion();
139 A2Branch osgiBranch = osgiContext.findBranch(module.getBranch().getComponent().getId(), moduleVersion);
140 if (osgiBranch == null) {
141 // Bundle bundle = bc.installBundle(module.getBranch().getCoordinates(),
142 // moduleSource.newInputStream(module.getLocator()));
143 Bundle bundle = moduleSource.install(bc, module);
144 if (OsgiBootUtils.isDebug())
145 OsgiBootUtils.debug("Installed bundle " + bundle.getLocation() + " with version " + moduleVersion);
146 return bundle;
147 } else {
148 A2Module lastOsgiModule = osgiBranch.last();
149 int compare = moduleVersion.compareTo(lastOsgiModule.getVersion());
150 if (compare > 0) {// update
151 Bundle bundle = (Bundle) lastOsgiModule.getLocator();
152 // bundle.update(moduleSource.newInputStream(module.getLocator()));
153 moduleSource.update(bundle, module);
154 OsgiBootUtils.info("Updated bundle " + bundle.getLocation() + " to version " + moduleVersion);
155 return bundle;
156 }
157 }
158 } catch (Exception e) {
159 OsgiBootUtils.error("Could not install module " + module + ": " + e.getMessage(), e);
160 }
161 return null;
162 }
163
164 public Collection<Bundle> update() {
165 boolean fragmentsUpdated = false;
166 Set<Bundle> updatedBundles = new HashSet<>();
167 bundles: for (Bundle bundle : bc.getBundles()) {
168 for (ProvisioningSource source : sources) {
169 String componentId = bundle.getSymbolicName();
170 Version version = bundle.getVersion();
171 A2Branch branch = source.findBranch(componentId, version);
172 if (branch == null)
173 continue bundles;
174 A2Module module = branch.last();
175 Version moduleVersion = module.getVersion();
176 int compare = moduleVersion.compareTo(version);
177 if (compare > 0) {// update
178 try {
179 source.update(bundle, module);
180 // bundle.update(in);
181 String fragmentHost = bundle.getHeaders().get(Constants.FRAGMENT_HOST);
182 if (fragmentHost != null)
183 fragmentsUpdated = true;
184 OsgiBootUtils.info("Updated bundle " + bundle.getLocation() + " to version " + moduleVersion);
185 updatedBundles.add(bundle);
186 } catch (Exception e) {
187 OsgiBootUtils.error("Cannot update with module " + module, e);
188 }
189 }
190 }
191 }
192 FrameworkWiring frameworkWiring = bc.getBundle(0).adapt(FrameworkWiring.class);
193 if (fragmentsUpdated)// refresh all
194 frameworkWiring.refreshBundles(null);
195 else
196 frameworkWiring.refreshBundles(updatedBundles);
197 return updatedBundles;
198 }
199
200 private static Map<String, List<String>> queryToMap(URI uri) {
201 return queryToMap(uri.getQuery());
202 }
203
204 private static Map<String, List<String>> queryToMap(String queryPart) {
205 try {
206 final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
207 if (queryPart == null)
208 return query_pairs;
209 final String[] pairs = queryPart.split("&");
210 for (String pair : pairs) {
211 final int idx = pair.indexOf("=");
212 final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8.name())
213 : pair;
214 if (!query_pairs.containsKey(key)) {
215 query_pairs.put(key, new LinkedList<String>());
216 }
217 final String value = idx > 0 && pair.length() > idx + 1
218 ? URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8.name())
219 : null;
220 query_pairs.get(key).add(value);
221 }
222 return query_pairs;
223 } catch (UnsupportedEncodingException e) {
224 throw new IllegalArgumentException("Cannot convert " + queryPart + " to map", e);
225 }
226 }
227
228 // public static void main(String[] args) {
229 // if (args.length == 0)
230 // throw new IllegalArgumentException("Usage: <path to A2 base>");
231 // Map<String, String> configuration = new HashMap<>();
232 // configuration.put("osgi.console", "2323");
233 // configuration.put("org.osgi.framework.bootdelegation",
234 // "com.sun.jndi.ldap,com.sun.jndi.ldap.sasl,com.sun.security.jgss,com.sun.jndi.dns,com.sun.nio.file,com.sun.nio.sctp,sun.nio.cs");
235 // Framework framework = OsgiBootUtils.launch(configuration);
236 // try {
237 // ProvisioningManager pm = new ProvisioningManager(framework.getBundleContext());
238 // Map<String, String> xOr = new HashMap<>();
239 // xOr.put("osgi", "equinox");
240 // xOr.put("swt", "rap");
241 // FsA2Source context = new FsA2Source(Paths.get(args[0]), xOr);
242 // context.load();
243 // pm.addSource(context);
244 // if (framework.getBundleContext().getBundles().length == 1) {// initial
245 // pm.install(null);
246 // } else {
247 // pm.update();
248 // }
249 //
250 // Thread.sleep(2000);
251 //
252 // Bundle[] bundles = framework.getBundleContext().getBundles();
253 // Arrays.sort(bundles, (b1, b2) -> b1.getSymbolicName().compareTo(b2.getSymbolicName()));
254 // for (Bundle b : bundles)
255 // if (b.getState() == Bundle.RESOLVED || b.getState() == Bundle.STARTING || b.getState() == Bundle.ACTIVE)
256 // System.out.println(b.getSymbolicName() + " " + b.getVersion());
257 // else
258 // System.err.println(b.getSymbolicName() + " " + b.getVersion() + " (" + b.getState() + ")");
259 // } catch (Exception e) {
260 // e.printStackTrace();
261 // } finally {
262 // try {
263 // framework.stop();
264 // } catch (Exception e) {
265 // e.printStackTrace();
266 // }
267 // }
268 // }
269
270 }