Introduce runtime manager
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / a2 / ProvisioningManager.java
index 6a0836bdfdea80ffc6727140e49d2dd3dcd3a470..80006264c6cbb4b4acb7bd894a6142b16ab4b87f 100644 (file)
@@ -66,11 +66,19 @@ public class ProvisioningManager {
                        // XOR
                        Map<String, List<String>> properties = queryToMap(u);
                        Map<String, String> xOr = new HashMap<>();
+                       List<String> includes = null;
+                       List<String> excludes = null;
                        for (String key : properties.keySet()) {
                                List<String> lst = properties.get(key);
-                               if (lst.size() != 1)
-                                       throw new IllegalArgumentException("Invalid XOR definitions in " + uri);
-                               xOr.put(key, lst.get(0));
+                               if (A2Source.INCLUDE.equals(key)) {
+                                       includes = new ArrayList<>(lst);
+                               } else if (A2Source.EXCLUDE.equals(key)) {
+                                       excludes = new ArrayList<>(lst);
+                               } else {
+                                       if (lst.size() != 1)
+                                               throw new IllegalArgumentException("Invalid XOR definitions in " + uri);
+                                       xOr.put(key, lst.get(0));
+                               }
                        }
 
                        if (SCHEME_A2.equals(u.getScheme()) || SCHEME_A2_REFERENCE.equals(u.getScheme())) {
@@ -81,7 +89,8 @@ public class ProvisioningManager {
                                        }
                                        Path base = Paths.get(baseStr);
                                        if (Files.exists(base)) {
-                                               FsA2Source source = new FsA2Source(base, xOr, SCHEME_A2_REFERENCE.equals(u.getScheme()));
+                                               FsA2Source source = new FsA2Source(base, xOr, SCHEME_A2_REFERENCE.equals(u.getScheme()),
+                                                               includes, excludes);
                                                source.load();
                                                addSource(source);
                                                OsgiBootUtils.info("Registered " + uri + " as source");
@@ -91,7 +100,7 @@ public class ProvisioningManager {
                                                Path localLibBase = base.resolve(A2Contribution.LIB).resolve(localRelPath);
                                                if (Files.exists(localLibBase)) {
                                                        FsA2Source libSource = new FsA2Source(localLibBase, xOr,
-                                                                       SCHEME_A2_REFERENCE.equals(u.getScheme()));
+                                                                       SCHEME_A2_REFERENCE.equals(u.getScheme()), includes, excludes);
                                                        libSource.load();
                                                        addSource(libSource);
                                                        OsgiBootUtils.info("Registered OS-specific " + uri + " as source (" + localRelPath + ")");
@@ -149,21 +158,34 @@ public class ProvisioningManager {
                        Version moduleVersion = module.getVersion();
                        A2Branch osgiBranch = osgiContext.findBranch(module.getBranch().getComponent().getId(), moduleVersion);
                        if (osgiBranch == null) {
-//                             Bundle bundle = bc.installBundle(module.getBranch().getCoordinates(),
-//                                             moduleSource.newInputStream(module.getLocator()));
                                Bundle bundle = moduleSource.install(bc, module);
-                               if (OsgiBootUtils.isDebug())
-                                       OsgiBootUtils.debug("Installed bundle " + bundle.getLocation() + " with version " + moduleVersion);
+                               // TODO make it more dynamic, based on OSGi APIs
+                               osgiContext.registerBundle(bundle);
+//                             if (OsgiBootUtils.isDebug())
+//                                     OsgiBootUtils.debug("Installed bundle " + bundle.getLocation() + " with version " + moduleVersion);
                                return bundle;
                        } else {
                                A2Module lastOsgiModule = osgiBranch.last();
                                int compare = moduleVersion.compareTo(lastOsgiModule.getVersion());
-                               if (compare > 0) {// update
+                               if (compare >= 0) {// update (also if same version)
                                        Bundle bundle = (Bundle) lastOsgiModule.getLocator();
-//                                     bundle.update(moduleSource.newInputStream(module.getLocator()));
+                                       if (bundle.getBundleId() == 0)// ignore framework bundle
+                                               return null;
                                        moduleSource.update(bundle, module);
-                                       OsgiBootUtils.info("Updated bundle " + bundle.getLocation() + " to version " + moduleVersion);
+                                       // TODO make it more dynamic, based on OSGi APIs
+                                       // TODO remove old module? Or keep update history?
+                                       osgiContext.registerBundle(bundle);
+                                       if (compare == 0)
+                                               OsgiBootUtils
+                                                               .debug("Updated bundle " + bundle.getLocation() + " to same version " + moduleVersion);
+                                       else
+                                               OsgiBootUtils.info("Updated bundle " + bundle.getLocation() + " to version " + moduleVersion);
                                        return bundle;
+                               } else {
+                                       if (OsgiBootUtils.isDebug())
+                                               OsgiBootUtils.debug("Did not install as bundle module " + module
+                                                               + " since a module with higher version " + lastOsgiModule.getVersion()
+                                                               + " is already installed for branch " + osgiBranch);
                                }
                        }
                } catch (Exception e) {