]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java
Improve OSGi boot
[lgpl/argeo-commons.git] / osgi / runtime / org.argeo.osgi.boot / src / main / java / org / argeo / slc / osgiboot / OsgiBoot.java
index 325d3602840ea6be94fa0cacc4200c55b3219dec..d54fa88993e031f7f146e392454a46dcc9622dc3 100644 (file)
@@ -20,25 +20,52 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
 
 public class OsgiBoot {
+       public final static String SYMBOLIC_NAME_OSGI_BOOT = "org.argeo.osgi.boot";
+       public final static String SYMBOLIC_NAME_EQUINOX = "org.eclipse.osgi";
+
+       public final static String PROP_ARGEO_OSGI_DATA_DIR = "argeo.osgi.data.dir";
+
+       public final static String PROP_ARGEO_OSGI_START = "argeo.osgi.start";
+       public final static String PROP_ARGEO_OSGI_BUNDLES = "argeo.osgi.bundles";
+       public final static String PROP_ARGEO_OSGI_LOCATIONS = "argeo.osgi.locations";
+       public final static String PROP_ARGEO_OSGI_BASE_URL = "argeo.osgi.baseUrl";
+       public final static String PROP_ARGEO_OSGI_MODULES_URL = "argeo.osgi.modulesUrl";
+
+       public final static String PROP_ARGEO_OSGI_BOOT_DEBUG = "argeo.osgi.boot.debug";
+       public final static String PROP_ARGEO_OSGI_BOOT_DEFAULT_TIMEOUT = "argeo.osgi.boot.defaultTimeout";
+       public final static String PROP_ARGEO_OSGI_BOOT_MODULES_URL_SEPARATOR = "argeo.osgi.boot.modulesUrlSeparator";
+       public final static String PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE = "argeo.osgi.boot.systemPropertiesFile";
+
+       /** @deprecated */
        public final static String PROP_SLC_OSGI_START = "slc.osgi.start";
+       /** @deprecated */
        public final static String PROP_SLC_OSGI_BUNDLES = "slc.osgi.bundles";
+       /** @deprecated */
        public final static String PROP_SLC_OSGI_LOCATIONS = "slc.osgi.locations";
+       /** @deprecated */
        public final static String PROP_SLC_OSGI_BASE_URL = "slc.osgi.baseUrl";
+       /** @deprecated */
        public final static String PROP_SLC_OSGI_MODULES_URL = "slc.osgi.modulesUrl";
 
+       /** @deprecated */
        public final static String PROP_SLC_OSGIBOOT_DEBUG = "slc.osgiboot.debug";
+       /** @deprecated */
        public final static String PROP_SLC_OSGIBOOT_DEFAULT_TIMEOUT = "slc.osgiboot.defaultTimeout";
+       /** @deprecated */
        public final static String PROP_SLC_OSGIBOOT_MODULES_URL_SEPARATOR = "slc.osgiboot.modulesUrlSeparator";
+       /** @deprecated */
        public final static String PROP_SLC_OSGIBOOT_SYSTEM_PROPERTIES_FILE = "slc.osgiboot.systemPropertiesFile";
 
        public final static String DEFAULT_BASE_URL = "reference:file:";
        public final static String EXCLUDES_SVN_PATTERN = "**/.svn/**";
 
        private boolean debug = Boolean.valueOf(
-                       System.getProperty(PROP_SLC_OSGIBOOT_DEBUG, "false"))
-                       .booleanValue();
+                       System.getProperty(PROP_ARGEO_OSGI_BOOT_DEBUG, System.getProperty(
+                                       PROP_SLC_OSGIBOOT_DEBUG, "false"))).booleanValue();
        /** Default is 10s (set in constructor) */
        private long defaultTimeout;
 
@@ -50,19 +77,59 @@ public class OsgiBoot {
 
        public OsgiBoot(BundleContext bundleContext) {
                this.bundleContext = bundleContext;
-               defaultTimeout = Long.parseLong(getProperty(
+               defaultTimeout = Long.parseLong(getPropertyCompat(
+                               PROP_ARGEO_OSGI_BOOT_DEFAULT_TIMEOUT,
                                PROP_SLC_OSGIBOOT_DEFAULT_TIMEOUT, "10000"));
-               modulesUrlSeparator = getProperty(
+               modulesUrlSeparator = getPropertyCompat(
+                               PROP_ARGEO_OSGI_BOOT_MODULES_URL_SEPARATOR,
                                PROP_SLC_OSGIBOOT_MODULES_URL_SEPARATOR, ",");
+               initSystemProperties();
+       }
+
+       protected void initSystemProperties() {
+               String osgiInstanceArea = System.getProperty("osgi.instance.area");
+               String osgiInstanceAreaDefault = System
+                               .getProperty("osgi.instance.area.default");
+               String tempDir = System.getProperty("java.io.tmpdir");
+
+               File dataDir = null;
+               if (osgiInstanceArea != null) {
+                       // within OSGi with -data specified
+                       osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
+                       dataDir = new File(osgiInstanceArea);
+               } else if (osgiInstanceAreaDefault != null) {
+                       // within OSGi without -data specified
+                       osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
+                       dataDir = new File(osgiInstanceAreaDefault);
+               } else {// outside OSGi
+                       dataDir = new File(tempDir + File.separator + "argeoOsgiData");
+               }
+
+               System.setProperty(PROP_ARGEO_OSGI_DATA_DIR, dataDir.getAbsolutePath());
+       }
+
+       public static String removeFilePrefix(String url) {
+               if (url.startsWith("file:"))
+                       return url.substring("file:".length());
+               else if (url.startsWith("reference:file:"))
+                       return url.substring("reference:file:".length());
+               else
+                       return url;
        }
 
        public void bootstrap() {
-               info("SLC OSGi bootstrap starting...");
+               System.out.println();
+               info("OSGi bootstrap starting...");
+               info("Writable data directory : "
+                               + System.getProperty(PROP_ARGEO_OSGI_DATA_DIR)
+                               + " (set as system property " + PROP_ARGEO_OSGI_DATA_DIR + ")");
                installUrls(getBundlesUrls());
                installUrls(getLocationsUrls());
                installUrls(getModulesUrls());
+               checkUnresolved();
                startBundles();
-               info("SLC OSGi bootstrap completed");
+               info("OSGi bootstrap completed");
+               System.out.println();
        }
 
        public void installUrls(List urls) {
@@ -83,8 +150,17 @@ public class OsgiBoot {
                                                                + " from " + url);
                                }
                        } catch (BundleException e) {
-                               warn("Could not install bundle from " + url + ": "
-                                               + e.getMessage());
+                               String message = e.getMessage();
+                               if ((message.contains("Bundle \"" + SYMBOLIC_NAME_OSGI_BOOT
+                                               + "\"") || message.contains("Bundle \""
+                                               + SYMBOLIC_NAME_EQUINOX + "\""))
+                                               && message.contains("has already been installed")) {
+                                       // silent, in order to avoid warnings: we know that both
+                                       // have already been installed...
+                               } else {
+                                       warn("Could not install bundle from " + url + ": "
+                                                       + message);
+                               }
                                if (debug)
                                        e.printStackTrace();
                        }
@@ -132,7 +208,8 @@ public class OsgiBoot {
        }
 
        public void startBundles() {
-               String bundlesToStart = getProperty(PROP_SLC_OSGI_START);
+               String bundlesToStart = getPropertyCompat(PROP_ARGEO_OSGI_START,
+                               PROP_SLC_OSGI_START);
                startBundles(bundlesToStart);
        }
 
@@ -189,6 +266,28 @@ public class OsgiBoot {
                                        + " not started because it was not found.");
        }
 
+       protected void checkUnresolved() {
+               // Refresh
+               ServiceReference packageAdminRef = bundleContext
+                               .getServiceReference(PackageAdmin.class.getName());
+               PackageAdmin packageAdmin = (PackageAdmin) bundleContext
+                               .getService(packageAdminRef);
+               packageAdmin.resolveBundles(null);
+
+               Bundle[] bundles = bundleContext.getBundles();
+               List /* Bundle */unresolvedBundles = new ArrayList();
+               for (int i = 0; i < bundles.length; i++) {
+                       int bundleState = bundles[i].getState();
+                       if (!(bundleState == Bundle.ACTIVE
+                                       || bundleState == Bundle.RESOLVED || bundleState == Bundle.STARTING))
+                               unresolvedBundles.add(bundles[i]);
+               }
+
+               if (unresolvedBundles.size() != 0) {
+                       warn("Unresolved bundles " + unresolvedBundles);
+               }
+       }
+
        protected void waitForBundleResolvedOrActive(long startBegin, Bundle bundle)
                        throws Exception {
                int originalState = bundle.getState();
@@ -258,18 +357,22 @@ public class OsgiBoot {
        }
 
        public List getLocationsUrls() {
-               String baseUrl = getProperty(PROP_SLC_OSGI_BASE_URL, DEFAULT_BASE_URL);
-               String bundleLocations = getProperty(PROP_SLC_OSGI_LOCATIONS);
+               String baseUrl = getPropertyCompat(PROP_ARGEO_OSGI_BASE_URL,
+                               PROP_SLC_OSGI_BASE_URL, DEFAULT_BASE_URL);
+               String bundleLocations = getPropertyCompat(PROP_ARGEO_OSGI_LOCATIONS,
+                               PROP_SLC_OSGI_LOCATIONS);
                return getLocationsUrls(baseUrl, bundleLocations);
        }
 
        public List getModulesUrls() {
                List urls = new ArrayList();
-               String modulesUrlStr = getProperty(PROP_SLC_OSGI_MODULES_URL);
+               String modulesUrlStr = getPropertyCompat(PROP_ARGEO_OSGI_MODULES_URL,
+                               PROP_SLC_OSGI_MODULES_URL);
                if (modulesUrlStr == null)
                        return urls;
 
-               String baseUrl = getProperty(PROP_SLC_OSGI_BASE_URL);
+               String baseUrl = getPropertyCompat(PROP_ARGEO_OSGI_BASE_URL,
+                               PROP_SLC_OSGI_BASE_URL);
 
                Map installedBundles = getBundles();
 
@@ -383,7 +486,7 @@ public class OsgiBoot {
                bundleLocations = SystemPropertyUtils
                                .resolvePlaceholders(bundleLocations);
                if (debug)
-                       debug(PROP_SLC_OSGI_LOCATIONS + "=" + bundleLocations);
+                       debug(PROP_ARGEO_OSGI_LOCATIONS + "=" + bundleLocations);
 
                StringTokenizer st = new StringTokenizer(bundleLocations,
                                File.pathSeparator);
@@ -394,8 +497,10 @@ public class OsgiBoot {
        }
 
        public List getBundlesUrls() {
-               String baseUrl = getProperty(PROP_SLC_OSGI_BASE_URL, DEFAULT_BASE_URL);
-               String bundlePatterns = getProperty(PROP_SLC_OSGI_BUNDLES);
+               String baseUrl = getPropertyCompat(PROP_ARGEO_OSGI_BASE_URL,
+                               PROP_SLC_OSGI_BASE_URL, DEFAULT_BASE_URL);
+               String bundlePatterns = getPropertyCompat(PROP_ARGEO_OSGI_BUNDLES,
+                               PROP_SLC_OSGI_BUNDLES);
                return getBundlesUrls(baseUrl, bundlePatterns);
        }
 
@@ -408,7 +513,7 @@ public class OsgiBoot {
                bundlePatterns = SystemPropertyUtils
                                .resolvePlaceholders(bundlePatterns);
                if (debug)
-                       debug(PROP_SLC_OSGI_BUNDLES + "=" + bundlePatterns
+                       debug(PROP_ARGEO_OSGI_BUNDLES + "=" + bundlePatterns
                                        + " (excludeSvn=" + excludeSvn + ")");
 
                StringTokenizer st = new StringTokenizer(bundlePatterns, ",");
@@ -526,16 +631,16 @@ public class OsgiBoot {
        }
 
        protected static void info(Object obj) {
-               System.out.println("#OSGiBOOT# " + obj);
+               System.out.println("# OSGiBOOT      # " + obj);
        }
 
        protected void debug(Object obj) {
                if (debug)
-                       System.out.println("#OSGiBOOT DEBUG# " + obj);
+                       System.out.println("# OSGiBOOT DBG  # " + obj);
        }
 
        protected void warn(Object obj) {
-               System.out.println("# WARN " + obj);
+               System.out.println("# OSGiBOOT WARN # " + obj);
                // Because of a weird bug under Windows when starting it in a forked VM
                // if (System.getProperty("os.name").contains("Windows"))
                // System.out.println("# WARN " + obj);
@@ -560,6 +665,38 @@ public class OsgiBoot {
                return getProperty(name, null);
        }
 
+       protected String getPropertyCompat(String name, String oldName) {
+               return getPropertyCompat(name, oldName, null);
+       }
+
+       protected String getPropertyCompat(String name, String oldName,
+                       String defaultValue) {
+               String res = null;
+
+               if (defaultValue != null) {
+                       res = getProperty(name, defaultValue);
+                       if (res.equals(defaultValue)) {
+                               res = getProperty(oldName, defaultValue);
+                               if (!res.equals(defaultValue))
+                                       warnDeprecated(name, oldName);
+                       }
+               } else {
+                       res = getProperty(name, null);
+                       if (res == null) {
+                               res = getProperty(oldName, null);
+                               if (res != null)
+                                       warnDeprecated(name, oldName);
+                       }
+               }
+               return res;
+       }
+
+       protected void warnDeprecated(String name, String oldName) {
+               warn("Property '" + oldName
+                               + "' is deprecated and will be removed soon, use '" + name
+                               + "' instead.");
+       }
+
        public boolean getDebug() {
                return debug;
        }