]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/a2/ProvisioningManager.java
Improve initialisation.
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / a2 / ProvisioningManager.java
1 package org.argeo.init.a2;
2
3 import java.io.File;
4 import java.net.URI;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.argeo.init.osgi.OsgiBootUtils;
18 import org.osgi.framework.Bundle;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.Constants;
21 import org.osgi.framework.Version;
22 import org.osgi.framework.launch.Framework;
23 import org.osgi.framework.wiring.FrameworkWiring;
24
25 /** Loads provisioning sources into an OSGi context. */
26 public class ProvisioningManager {
27 BundleContext bc;
28 OsgiContext osgiContext;
29 List<ProvisioningSource> sources = Collections.synchronizedList(new ArrayList<>());
30
31 public ProvisioningManager(BundleContext bc) {
32 this.bc = bc;
33 osgiContext = new OsgiContext(bc);
34 osgiContext.load();
35 }
36
37 protected void addSource(ProvisioningSource source) {
38 sources.add(source);
39 }
40
41 void installWholeSource(ProvisioningSource source) {
42 Set<Bundle> updatedBundles = new HashSet<>();
43 for (A2Contribution contribution : source.listContributions(null)) {
44 for (A2Component component : contribution.components.values()) {
45 A2Module module = component.last().last();
46 Bundle bundle = installOrUpdate(module);
47 if (bundle != null)
48 updatedBundles.add(bundle);
49 }
50 }
51 FrameworkWiring frameworkWiring = bc.getBundle(0).adapt(FrameworkWiring.class);
52 frameworkWiring.refreshBundles(updatedBundles);
53 }
54
55 public void registerSource(String uri) {
56 try {
57 URI u = new URI(uri);
58 if (A2Source.SCHEME_A2.equals(u.getScheme())) {
59 if (u.getHost() == null || "".equals(u.getHost())) {
60 String baseStr = u.getPath();
61 if (File.separatorChar == '\\') {// MS Windows
62 baseStr = baseStr.substring(1).replace('/', File.separatorChar);
63 }
64 Path base = Paths.get(baseStr);
65 if (Files.exists(base)) {
66 FsA2Source source = new FsA2Source(base);
67 source.load();
68 addSource(source);
69 OsgiBootUtils.info("Registered " + uri + " as source");
70 }
71 }
72 }
73 } catch (Exception e) {
74 throw new A2Exception("Cannot add source " + uri, e);
75 }
76 }
77
78 public boolean registerDefaultSource() {
79 String frameworkLocation = bc.getProperty("osgi.framework");
80 try {
81 URI frameworkLocationUri = new URI(frameworkLocation);
82 if ("file".equals(frameworkLocationUri.getScheme())) {
83 Path frameworkPath = Paths.get(frameworkLocationUri);
84 if (frameworkPath.getParent().getFileName().toString().equals(A2Contribution.BOOT)) {
85 Path base = frameworkPath.getParent().getParent();
86 String baseStr = base.toString();
87 if (File.separatorChar == '\\')// MS Windows
88 baseStr = '/' + baseStr.replace(File.separatorChar, '/');
89 URI baseUri = new URI(A2Source.SCHEME_A2, null, null, 0, baseStr, null, null);
90 registerSource(baseUri.toString());
91 OsgiBootUtils.debug("Default source from framework location " + frameworkLocation);
92 return true;
93 }
94 }
95 } catch (Exception e) {
96 OsgiBootUtils.error("Cannot register default source based on framework location " + frameworkLocation, e);
97 }
98 return false;
99 }
100
101 public void install(String spec) {
102 if (spec == null) {
103 for (ProvisioningSource source : sources) {
104 installWholeSource(source);
105 }
106 }
107 }
108
109 /** @return the new/updated bundle, or null if nothing was done. */
110 protected Bundle installOrUpdate(A2Module module) {
111 try {
112 ProvisioningSource moduleSource = module.getBranch().getComponent().getContribution().getSource();
113 Version moduleVersion = module.getVersion();
114 A2Branch osgiBranch = osgiContext.findBranch(module.getBranch().getComponent().getId(), moduleVersion);
115 if (osgiBranch == null) {
116 // Bundle bundle = bc.installBundle(module.getBranch().getCoordinates(),
117 // moduleSource.newInputStream(module.getLocator()));
118 Bundle bundle = moduleSource.install(bc, module);
119 if (OsgiBootUtils.isDebug())
120 OsgiBootUtils.debug("Installed bundle " + bundle.getLocation() + " with version " + moduleVersion);
121 return bundle;
122 } else {
123 A2Module lastOsgiModule = osgiBranch.last();
124 int compare = moduleVersion.compareTo(lastOsgiModule.getVersion());
125 if (compare > 0) {// update
126 Bundle bundle = (Bundle) lastOsgiModule.getLocator();
127 // bundle.update(moduleSource.newInputStream(module.getLocator()));
128 moduleSource.update(bundle, module);
129 OsgiBootUtils.info("Updated bundle " + bundle.getLocation() + " to version " + moduleVersion);
130 return bundle;
131 }
132 }
133 } catch (Exception e) {
134 OsgiBootUtils.error("Could not install module " + module + ": " + e.getMessage(), e);
135 }
136 return null;
137 }
138
139 public Collection<Bundle> update() {
140 boolean fragmentsUpdated = false;
141 Set<Bundle> updatedBundles = new HashSet<>();
142 bundles: for (Bundle bundle : bc.getBundles()) {
143 for (ProvisioningSource source : sources) {
144 String componentId = bundle.getSymbolicName();
145 Version version = bundle.getVersion();
146 A2Branch branch = source.findBranch(componentId, version);
147 if (branch == null)
148 continue bundles;
149 A2Module module = branch.last();
150 Version moduleVersion = module.getVersion();
151 int compare = moduleVersion.compareTo(version);
152 if (compare > 0) {// update
153 try {
154 source.update(bundle, module);
155 // bundle.update(in);
156 String fragmentHost = bundle.getHeaders().get(Constants.FRAGMENT_HOST);
157 if (fragmentHost != null)
158 fragmentsUpdated = true;
159 OsgiBootUtils.info("Updated bundle " + bundle.getLocation() + " to version " + moduleVersion);
160 updatedBundles.add(bundle);
161 } catch (Exception e) {
162 OsgiBootUtils.error("Cannot update with module " + module, e);
163 }
164 }
165 }
166 }
167 FrameworkWiring frameworkWiring = bc.getBundle(0).adapt(FrameworkWiring.class);
168 if (fragmentsUpdated)// refresh all
169 frameworkWiring.refreshBundles(null);
170 else
171 frameworkWiring.refreshBundles(updatedBundles);
172 return updatedBundles;
173 }
174
175 public static void main(String[] args) {
176 Map<String, String> configuration = new HashMap<>();
177 configuration.put("osgi.console", "2323");
178 Framework framework = OsgiBootUtils.launch(configuration);
179 try {
180 ProvisioningManager pm = new ProvisioningManager(framework.getBundleContext());
181 FsA2Source context = new FsA2Source(Paths.get(
182 "/home/mbaudier/dev/git/apache2/argeo-commons/dist/argeo-node/target/argeo-node-2.1.74-SNAPSHOT/argeo-node/share/osgi"));
183 context.load();
184 if (framework.getBundleContext().getBundles().length == 1) {// initial
185 pm.install(null);
186 } else {
187 pm.update();
188 }
189 } catch (Exception e) {
190 e.printStackTrace();
191 } finally {
192 try {
193 // framework.stop();
194 } catch (Exception e) {
195 e.printStackTrace();
196 }
197 }
198 }
199
200 }