1 package org
.argeo
.init
.a2
;
3 import static org
.argeo
.init
.a2
.A2Source
.SCHEME_A2
;
4 import static org
.argeo
.init
.a2
.A2Source
.SCHEME_A2_REFERENCE
;
7 import java
.io
.UnsupportedEncodingException
;
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
;
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
;
32 /** Loads provisioning sources into an OSGi context. */
33 public class ProvisioningManager
{
35 OsgiContext osgiContext
;
36 List
<ProvisioningSource
> sources
= Collections
.synchronizedList(new ArrayList
<>());
38 public ProvisioningManager(BundleContext bc
) {
40 osgiContext
= new OsgiContext(bc
);
44 protected void addSource(ProvisioningSource source
) {
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
);
55 updatedBundles
.add(bundle
);
58 // FrameworkWiring frameworkWiring = bc.getBundle(0).adapt(FrameworkWiring.class);
59 // frameworkWiring.refreshBundles(updatedBundles);
62 public void registerSource(String uri
) {
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
);
72 throw new IllegalArgumentException("Invalid XOR definitions in " + uri
);
73 xOr
.put(key
, lst
.get(0));
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
);
82 Path base
= Paths
.get(baseStr
);
83 if (Files
.exists(base
)) {
84 FsA2Source source
= new FsA2Source(base
, xOr
, SCHEME_A2_REFERENCE
.equals(u
.getScheme()));
87 OsgiBootUtils
.info("Registered " + uri
+ " as source");
89 // OS specific / native
90 String localRelPath
= A2Contribution
.localOsArchRelativePath();
91 Path localLibBase
= base
.resolve(A2Contribution
.LIB
).resolve(localRelPath
);
92 if (Files
.exists(localLibBase
)) {
93 FsA2Source libSource
= new FsA2Source(localLibBase
, xOr
,
94 SCHEME_A2_REFERENCE
.equals(u
.getScheme()));
97 OsgiBootUtils
.info("Registered OS-specific " + uri
+ " as source (" + localRelPath
+ ")");
100 OsgiBootUtils
.debug("Source " + base
+ " does not exist, ignoring.");
103 throw new UnsupportedOperationException(
104 "Remote installation is not yet supported, cannot add source " + u
);
107 throw new IllegalArgumentException("Unkown scheme: for source " + u
);
109 } catch (Exception e
) {
110 throw new A2Exception("Cannot add source " + uri
, e
);
114 public boolean registerDefaultSource() {
115 String frameworkLocation
= bc
.getProperty("osgi.framework");
117 URI frameworkLocationUri
= new URI(frameworkLocation
);
118 if ("file".equals(frameworkLocationUri
.getScheme())) {
119 Path frameworkPath
= Paths
.get(frameworkLocationUri
);
120 if (frameworkPath
.getParent().getFileName().toString().equals(A2Contribution
.BOOT
)) {
121 Path base
= frameworkPath
.getParent().getParent();
122 String baseStr
= base
.toString();
123 if (File
.separatorChar
== '\\')// MS Windows
124 baseStr
= '/' + baseStr
.replace(File
.separatorChar
, '/');
125 URI baseUri
= new URI(A2Source
.SCHEME_A2
, null, null, 0, baseStr
, null, null);
126 registerSource(baseUri
.toString());
127 OsgiBootUtils
.debug("Default source from framework location " + frameworkLocation
);
131 } catch (Exception e
) {
132 OsgiBootUtils
.error("Cannot register default source based on framework location " + frameworkLocation
, e
);
137 public void install(String spec
) {
139 for (ProvisioningSource source
: sources
) {
140 installWholeSource(source
);
145 /** @return the new/updated bundle, or null if nothing was done. */
146 protected Bundle
installOrUpdate(A2Module module
) {
148 ProvisioningSource moduleSource
= module
.getBranch().getComponent().getContribution().getSource();
149 Version moduleVersion
= module
.getVersion();
150 A2Branch osgiBranch
= osgiContext
.findBranch(module
.getBranch().getComponent().getId(), moduleVersion
);
151 if (osgiBranch
== null) {
152 // Bundle bundle = bc.installBundle(module.getBranch().getCoordinates(),
153 // moduleSource.newInputStream(module.getLocator()));
154 Bundle bundle
= moduleSource
.install(bc
, module
);
155 if (OsgiBootUtils
.isDebug())
156 OsgiBootUtils
.debug("Installed bundle " + bundle
.getLocation() + " with version " + moduleVersion
);
159 A2Module lastOsgiModule
= osgiBranch
.last();
160 int compare
= moduleVersion
.compareTo(lastOsgiModule
.getVersion());
161 if (compare
> 0) {// update
162 Bundle bundle
= (Bundle
) lastOsgiModule
.getLocator();
163 // bundle.update(moduleSource.newInputStream(module.getLocator()));
164 moduleSource
.update(bundle
, module
);
165 OsgiBootUtils
.info("Updated bundle " + bundle
.getLocation() + " to version " + moduleVersion
);
169 } catch (Exception e
) {
170 OsgiBootUtils
.error("Could not install module " + module
+ ": " + e
.getMessage(), e
);
175 public Collection
<Bundle
> update() {
176 boolean fragmentsUpdated
= false;
177 Set
<Bundle
> updatedBundles
= new HashSet
<>();
178 bundles
: for (Bundle bundle
: bc
.getBundles()) {
179 for (ProvisioningSource source
: sources
) {
180 String componentId
= bundle
.getSymbolicName();
181 Version version
= bundle
.getVersion();
182 A2Branch branch
= source
.findBranch(componentId
, version
);
185 A2Module module
= branch
.last();
186 Version moduleVersion
= module
.getVersion();
187 int compare
= moduleVersion
.compareTo(version
);
188 if (compare
> 0) {// update
190 source
.update(bundle
, module
);
191 // bundle.update(in);
192 String fragmentHost
= bundle
.getHeaders().get(Constants
.FRAGMENT_HOST
);
193 if (fragmentHost
!= null)
194 fragmentsUpdated
= true;
195 OsgiBootUtils
.info("Updated bundle " + bundle
.getLocation() + " to version " + moduleVersion
);
196 updatedBundles
.add(bundle
);
197 } catch (Exception e
) {
198 OsgiBootUtils
.error("Cannot update with module " + module
, e
);
203 FrameworkWiring frameworkWiring
= bc
.getBundle(0).adapt(FrameworkWiring
.class);
204 if (fragmentsUpdated
)// refresh all
205 frameworkWiring
.refreshBundles(null);
207 frameworkWiring
.refreshBundles(updatedBundles
);
208 return updatedBundles
;
211 private static Map
<String
, List
<String
>> queryToMap(URI uri
) {
212 return queryToMap(uri
.getQuery());
215 private static Map
<String
, List
<String
>> queryToMap(String queryPart
) {
217 final Map
<String
, List
<String
>> query_pairs
= new LinkedHashMap
<String
, List
<String
>>();
218 if (queryPart
== null)
220 final String
[] pairs
= queryPart
.split("&");
221 for (String pair
: pairs
) {
222 final int idx
= pair
.indexOf("=");
223 final String key
= idx
> 0 ? URLDecoder
.decode(pair
.substring(0, idx
), StandardCharsets
.UTF_8
.name())
225 if (!query_pairs
.containsKey(key
)) {
226 query_pairs
.put(key
, new LinkedList
<String
>());
228 final String value
= idx
> 0 && pair
.length() > idx
+ 1
229 ? URLDecoder
.decode(pair
.substring(idx
+ 1), StandardCharsets
.UTF_8
.name())
231 query_pairs
.get(key
).add(value
);
234 } catch (UnsupportedEncodingException e
) {
235 throw new IllegalArgumentException("Cannot convert " + queryPart
+ " to map", e
);
239 // public static void main(String[] args) {
240 // if (args.length == 0)
241 // throw new IllegalArgumentException("Usage: <path to A2 base>");
242 // Map<String, String> configuration = new HashMap<>();
243 // configuration.put("osgi.console", "2323");
244 // configuration.put("org.osgi.framework.bootdelegation",
245 // "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");
246 // Framework framework = OsgiBootUtils.launch(configuration);
248 // ProvisioningManager pm = new ProvisioningManager(framework.getBundleContext());
249 // Map<String, String> xOr = new HashMap<>();
250 // xOr.put("osgi", "equinox");
251 // xOr.put("swt", "rap");
252 // FsA2Source context = new FsA2Source(Paths.get(args[0]), xOr);
254 // pm.addSource(context);
255 // if (framework.getBundleContext().getBundles().length == 1) {// initial
261 // Thread.sleep(2000);
263 // Bundle[] bundles = framework.getBundleContext().getBundles();
264 // Arrays.sort(bundles, (b1, b2) -> b1.getSymbolicName().compareTo(b2.getSymbolicName()));
265 // for (Bundle b : bundles)
266 // if (b.getState() == Bundle.RESOLVED || b.getState() == Bundle.STARTING || b.getState() == Bundle.ACTIVE)
267 // System.out.println(b.getSymbolicName() + " " + b.getVersion());
269 // System.err.println(b.getSymbolicName() + " " + b.getVersion() + " (" + b.getState() + ")");
270 // } catch (Exception e) {
271 // e.printStackTrace();
275 // } catch (Exception e) {
276 // e.printStackTrace();