]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBoot.java
Make OSGi boot more robust against badly formatted bundles.
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / boot / OsgiBoot.java
index bdf7134b85441df762d5ca3120e4e51b43e6f768..4c3ee40f0c2095da9860af6f4ef4316a72ae5c59 100644 (file)
@@ -19,6 +19,7 @@ import static org.argeo.osgi.boot.OsgiBootUtils.debug;
 import static org.argeo.osgi.boot.OsgiBootUtils.warn;
 
 import java.io.File;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -30,6 +31,7 @@ import java.util.SortedMap;
 import java.util.StringTokenizer;
 import java.util.TreeMap;
 
+import org.argeo.osgi.boot.a2.ProvisioningManager;
 import org.argeo.osgi.boot.internal.springutil.AntPathMatcher;
 import org.argeo.osgi.boot.internal.springutil.PathMatcher;
 import org.argeo.osgi.boot.internal.springutil.SystemPropertyUtils;
@@ -49,6 +51,8 @@ import org.osgi.framework.wiring.FrameworkWiring;
  */
 public class OsgiBoot implements OsgiBootConstants {
        public final static String PROP_ARGEO_OSGI_START = "argeo.osgi.start";
+       public final static String PROP_ARGEO_OSGI_SOURCES = "argeo.osgi.sources";
+
        public final static String PROP_ARGEO_OSGI_BUNDLES = "argeo.osgi.bundles";
        public final static String PROP_ARGEO_OSGI_BASE_URL = "argeo.osgi.baseUrl";
        public final static String PROP_ARGEO_OSGI_LOCAL_CACHE = "argeo.osgi.localCache";
@@ -89,14 +93,30 @@ public class OsgiBoot implements OsgiBootConstants {
        private final BundleContext bundleContext;
        private final String localCache;
 
+       private final ProvisioningManager provisioningManager;
+
        /*
         * INITIALIZATION
         */
        /** Constructor */
        public OsgiBoot(BundleContext bundleContext) {
                this.bundleContext = bundleContext;
-               localCache = getProperty(PROP_ARGEO_OSGI_LOCAL_CACHE,
-                               "file://" + System.getProperty("user.home") + "/.m2/repository/");
+               String homeUri = Paths.get(System.getProperty("user.home")).toUri().toString();
+               localCache = getProperty(PROP_ARGEO_OSGI_LOCAL_CACHE, homeUri + ".m2/repository/");
+
+               provisioningManager = new ProvisioningManager(bundleContext);
+               String sources = getProperty(PROP_ARGEO_OSGI_SOURCES);
+               if (sources == null) {
+                       provisioningManager.registerDefaultSource();
+               } else {
+                       for (String source : sources.split(",")) {
+                               provisioningManager.registerSource(source);
+                       }
+               }
+       }
+
+       ProvisioningManager getProvisioningManager() {
+               return provisioningManager;
        }
 
        /*
@@ -112,6 +132,7 @@ public class OsgiBoot implements OsgiBootConstants {
                                        .info("OSGi bootstrap starting" + (osgiInstancePath != null ? " (" + osgiInstancePath + ")" : ""));
                        installUrls(getBundlesUrls());
                        installUrls(getDistributionUrls());
+                       provisioningManager.install(null);
                        startBundles();
                        long duration = System.currentTimeMillis() - begin;
                        OsgiBootUtils.info("OSGi bootstrap completed in " + Math.round(((double) duration) / 1000) + "s ("
@@ -142,6 +163,10 @@ public class OsgiBoot implements OsgiBootConstants {
                System.out.println();
        }
 
+       public void update() {
+               provisioningManager.update();
+       }
+
        /*
         * INSTALLATION
         */
@@ -215,16 +240,21 @@ public class OsgiBoot implements OsgiBootConstants {
                                }
                        }
                } catch (BundleException e) {
+                       final String ALREADY_INSTALLED = "is already installed";
                        String message = e.getMessage();
                        if ((message.contains("Bundle \"" + SYMBOLIC_NAME_OSGI_BOOT + "\"")
                                        || message.contains("Bundle \"" + SYMBOLIC_NAME_EQUINOX + "\""))
-                                       && message.contains("is already installed")) {
+                                       && message.contains(ALREADY_INSTALLED)) {
                                // silent, in order to avoid warnings: we know that both
                                // have already been installed...
                        } else {
-                               OsgiBootUtils.warn("Could not install bundle from " + url + ": " + message);
+                               if (message.contains(ALREADY_INSTALLED)) {
+                                       if (OsgiBootUtils.isDebug())
+                                               OsgiBootUtils.warn("Duplicate install from " + url + ": " + message);
+                               } else
+                                       OsgiBootUtils.warn("Could not install bundle from " + url + ": " + message);
                        }
-                       if (OsgiBootUtils.debug && !message.contains("is already installed"))
+                       if (OsgiBootUtils.debug && !message.contains(ALREADY_INSTALLED))
                                e.printStackTrace();
                }
        }