1 package org
.argeo
.init
.a2
;
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
;
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
;
25 /** Loads provisioning sources into an OSGi context. */
26 public class ProvisioningManager
{
28 OsgiContext osgiContext
;
29 List
<ProvisioningSource
> sources
= Collections
.synchronizedList(new ArrayList
<>());
31 public ProvisioningManager(BundleContext bc
) {
33 osgiContext
= new OsgiContext(bc
);
37 protected void addSource(ProvisioningSource source
) {
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
);
48 updatedBundles
.add(bundle
);
51 FrameworkWiring frameworkWiring
= bc
.getBundle(0).adapt(FrameworkWiring
.class);
52 frameworkWiring
.refreshBundles(updatedBundles
);
55 public void registerSource(String 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
);
64 Path base
= Paths
.get(baseStr
);
65 if (Files
.exists(base
)) {
66 FsA2Source source
= new FsA2Source(base
);
69 OsgiBootUtils
.info("Registered " + uri
+ " as source");
73 } catch (Exception e
) {
74 throw new A2Exception("Cannot add source " + uri
, e
);
78 public boolean registerDefaultSource() {
79 String frameworkLocation
= bc
.getProperty("osgi.framework");
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
);
95 } catch (Exception e
) {
96 OsgiBootUtils
.error("Cannot register default source based on framework location " + frameworkLocation
, e
);
101 public void install(String spec
) {
103 for (ProvisioningSource source
: sources
) {
104 installWholeSource(source
);
109 /** @return the new/updated bundle, or null if nothing was done. */
110 protected Bundle
installOrUpdate(A2Module module
) {
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
);
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
);
133 } catch (Exception e
) {
134 OsgiBootUtils
.error("Could not install module " + module
+ ": " + e
.getMessage(), e
);
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
);
149 A2Module module
= branch
.last();
150 Version moduleVersion
= module
.getVersion();
151 int compare
= moduleVersion
.compareTo(version
);
152 if (compare
> 0) {// update
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
);
167 FrameworkWiring frameworkWiring
= bc
.getBundle(0).adapt(FrameworkWiring
.class);
168 if (fragmentsUpdated
)// refresh all
169 frameworkWiring
.refreshBundles(null);
171 frameworkWiring
.refreshBundles(updatedBundles
);
172 return updatedBundles
;
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
);
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"));
184 if (framework
.getBundleContext().getBundles().length
== 1) {// initial
189 } catch (Exception e
) {
194 } catch (Exception e
) {