]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java
Add license headers
[lgpl/argeo-commons.git] / osgi / runtime / org.argeo.osgi.boot / src / main / java / org / argeo / slc / osgiboot / OsgiBoot.java
index e8c9754c11ffea8ea7944b06981a7e1e1700ec7c..ccaa17cf48ac82e3e770a81a124bc5bd91d38b14 100644 (file)
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package org.argeo.slc.osgiboot;
 
 import java.io.BufferedReader;
@@ -20,8 +36,13 @@ 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";
@@ -34,7 +55,9 @@ public class OsgiBoot {
        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";
-
+       public final static String PROP_ARGEO_OSGI_BOOT_APPCLASS = "argeo.osgi.boot.appclass";
+       public final static String PROP_ARGEO_OSGI_BOOT_APPARGS = "argeo.osgi.boot.appargs";
+       
        /** @deprecated */
        public final static String PROP_SLC_OSGI_START = "slc.osgi.start";
        /** @deprecated */
@@ -54,7 +77,13 @@ public class OsgiBoot {
        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";
+       /** @deprecated */
+       public final static String PROP_SLC_OSGIBOOT_APPCLASS = "slc.osgiboot.appclass";
+       /** @deprecated */
+       public final static String PROP_SLC_OSGIBOOT_APPARGS = "slc.osgiboot.appargs";
 
+       
+       
        public final static String DEFAULT_BASE_URL = "reference:file:";
        public final static String EXCLUDES_SVN_PATTERN = "**/.svn/**";
 
@@ -72,10 +101,10 @@ public class OsgiBoot {
 
        public OsgiBoot(BundleContext bundleContext) {
                this.bundleContext = bundleContext;
-               defaultTimeout = Long.parseLong(getPropertyCompat(
+               defaultTimeout = Long.parseLong(OsgiBootUtils.getPropertyCompat(
                                PROP_ARGEO_OSGI_BOOT_DEFAULT_TIMEOUT,
                                PROP_SLC_OSGIBOOT_DEFAULT_TIMEOUT, "10000"));
-               modulesUrlSeparator = getPropertyCompat(
+               modulesUrlSeparator = OsgiBootUtils.getPropertyCompat(
                                PROP_ARGEO_OSGI_BOOT_MODULES_URL_SEPARATOR,
                                PROP_SLC_OSGIBOOT_MODULES_URL_SEPARATOR, ",");
                initSystemProperties();
@@ -101,7 +130,10 @@ public class OsgiBoot {
                }
 
                System.setProperty(PROP_ARGEO_OSGI_DATA_DIR, dataDir.getAbsolutePath());
-               info(PROP_ARGEO_OSGI_DATA_DIR + "=" + dataDir.getAbsolutePath());
+
+               // TODO: Load additional system properties from file
+               // Properties additionalSystemProperties = new Properties();
+               
        }
 
        public static String removeFilePrefix(String url) {
@@ -114,12 +146,22 @@ public class OsgiBoot {
        }
 
        public void bootstrap() {
-               info("SLC OSGi bootstrap starting...");
+               long begin = System.currentTimeMillis();
+               System.out.println();
+               OsgiBootUtils.info("OSGi bootstrap starting...");
+               OsgiBootUtils.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");
+               long duration = System.currentTimeMillis() - begin;
+               OsgiBootUtils.info("OSGi bootstrap completed in "
+                               + Math.round(((double) duration) / 1000) + "s (" + duration
+                               + "ms), " + bundleContext.getBundles().length + " bundles");
+               System.out.println();
        }
 
        public void installUrls(List urls) {
@@ -140,8 +182,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 {
+                                       OsgiBootUtils.warn("Could not install bundle from " + url + ": "
+                                                       + message);
+                               }
                                if (debug)
                                        e.printStackTrace();
                        }
@@ -162,7 +213,7 @@ public class OsgiBoot {
                                        URL url = new URL(urlStr);
                                        in = url.openStream();
                                        bundle.update(in);
-                                       info("Updated bundle " + moduleName + " from " + urlStr);
+                                       OsgiBootUtils.info("Updated bundle " + moduleName + " from " + urlStr);
                                } catch (Exception e) {
                                        throw new RuntimeException("Cannot update " + moduleName
                                                        + " from " + urlStr);
@@ -180,7 +231,7 @@ public class OsgiBoot {
                                                debug("Installed bundle " + bundle.getSymbolicName()
                                                                + " from " + urlStr);
                                } catch (BundleException e) {
-                                       warn("Could not install bundle from " + urlStr + ": "
+                                       OsgiBootUtils.warn("Could not install bundle from " + urlStr + ": "
                                                        + e.getMessage());
                                }
                        }
@@ -189,7 +240,7 @@ public class OsgiBoot {
        }
 
        public void startBundles() {
-               String bundlesToStart = getPropertyCompat(PROP_ARGEO_OSGI_START,
+               String bundlesToStart = OsgiBootUtils.getPropertyCompat(PROP_ARGEO_OSGI_START,
                                PROP_SLC_OSGI_START);
                startBundles(bundlesToStart);
        }
@@ -224,7 +275,7 @@ public class OsgiBoot {
                                        try {
                                                bundle.start();
                                        } catch (Exception e) {
-                                               warn("Start of bundle " + symbolicName
+                                               OsgiBootUtils.warn("Start of bundle " + symbolicName
                                                                + " failed because of " + e
                                                                + ", maybe bundle is not yet resolved,"
                                                                + " waiting and trying again.");
@@ -233,7 +284,7 @@ public class OsgiBoot {
                                        }
                                        notFoundBundles.remove(symbolicName);
                                } catch (Exception e) {
-                                       warn("Bundle " + symbolicName + " cannot be started: "
+                                       OsgiBootUtils.warn("Bundle " + symbolicName + " cannot be started: "
                                                        + e.getMessage());
                                        if (debug)
                                                e.printStackTrace();
@@ -243,10 +294,32 @@ public class OsgiBoot {
                }
 
                for (int i = 0; i < notFoundBundles.size(); i++)
-                       warn("Bundle " + notFoundBundles.get(i)
+                       OsgiBootUtils.warn("Bundle " + notFoundBundles.get(i)
                                        + " 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) {
+                       OsgiBootUtils.warn("Unresolved bundles " + unresolvedBundles);
+               }
+       }
+
        protected void waitForBundleResolvedOrActive(long startBegin, Bundle bundle)
                        throws Exception {
                int originalState = bundle.getState();
@@ -316,21 +389,21 @@ public class OsgiBoot {
        }
 
        public List getLocationsUrls() {
-               String baseUrl = getPropertyCompat(PROP_ARGEO_OSGI_BASE_URL,
+               String baseUrl = OsgiBootUtils.getPropertyCompat(PROP_ARGEO_OSGI_BASE_URL,
                                PROP_SLC_OSGI_BASE_URL, DEFAULT_BASE_URL);
-               String bundleLocations = getPropertyCompat(PROP_ARGEO_OSGI_LOCATIONS,
+               String bundleLocations = OsgiBootUtils.getPropertyCompat(PROP_ARGEO_OSGI_LOCATIONS,
                                PROP_SLC_OSGI_LOCATIONS);
                return getLocationsUrls(baseUrl, bundleLocations);
        }
 
        public List getModulesUrls() {
                List urls = new ArrayList();
-               String modulesUrlStr = getPropertyCompat(PROP_ARGEO_OSGI_MODULES_URL,
+               String modulesUrlStr = OsgiBootUtils.getPropertyCompat(PROP_ARGEO_OSGI_MODULES_URL,
                                PROP_SLC_OSGI_MODULES_URL);
                if (modulesUrlStr == null)
                        return urls;
 
-               String baseUrl = getPropertyCompat(PROP_ARGEO_OSGI_BASE_URL,
+               String baseUrl = OsgiBootUtils.getPropertyCompat(PROP_ARGEO_OSGI_BASE_URL,
                                PROP_SLC_OSGI_BASE_URL);
 
                Map installedBundles = getBundles();
@@ -356,13 +429,13 @@ public class OsgiBoot {
                                                        Constants.BUNDLE_VERSION).toString();
                                        int comp = compareVersions(bundleVersion, moduleVersion);
                                        if (comp > 0) {
-                                               warn("Installed version " + bundleVersion
+                                               OsgiBootUtils.warn("Installed version " + bundleVersion
                                                                + " of bundle " + moduleName
                                                                + " is newer than  provided version "
                                                                + moduleVersion);
                                        } else if (comp < 0) {
                                                urls.add(url);
-                                               info("Updated bundle " + moduleName + " with version "
+                                               OsgiBootUtils.info("Updated bundle " + moduleName + " with version "
                                                                + moduleVersion + " (old version was "
                                                                + bundleVersion + ")");
                                        } else {
@@ -456,9 +529,9 @@ public class OsgiBoot {
        }
 
        public List getBundlesUrls() {
-               String baseUrl = getPropertyCompat(PROP_ARGEO_OSGI_BASE_URL,
+               String baseUrl = OsgiBootUtils.getPropertyCompat(PROP_ARGEO_OSGI_BASE_URL,
                                PROP_SLC_OSGI_BASE_URL, DEFAULT_BASE_URL);
-               String bundlePatterns = getPropertyCompat(PROP_ARGEO_OSGI_BUNDLES,
+               String bundlePatterns = OsgiBootUtils.getPropertyCompat(PROP_ARGEO_OSGI_BUNDLES,
                                PROP_SLC_OSGI_BUNDLES);
                return getBundlesUrls(baseUrl, bundlePatterns);
        }
@@ -516,7 +589,7 @@ public class OsgiBoot {
                        File[] files = baseDir.listFiles();
 
                        if (files == null) {
-                               warn("Base dir " + baseDir + " has no children, exists="
+                               OsgiBootUtils.warn("Base dir " + baseDir + " has no children, exists="
                                                + baseDir.exists() + ", isDirectory="
                                                + baseDir.isDirectory());
                                return;
@@ -589,71 +662,9 @@ public class OsgiBoot {
                return (basePath + '/' + relativePath).replace('/', File.separatorChar);
        }
 
-       protected static void info(Object obj) {
-               System.out.println("#OSGiBOOT# " + obj);
-       }
-
        protected void debug(Object obj) {
                if (debug)
-                       System.out.println("#OSGiBOOT DEBUG# " + obj);
-       }
-
-       protected void warn(Object obj) {
-               System.out.println("# 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);
-               // else
-               // System.err.println("# WARN " + obj);
-       }
-
-       protected String getProperty(String name, String defaultValue) {
-               final String value;
-               if (defaultValue != null)
-                       value = System.getProperty(name, defaultValue);
-               else
-                       value = System.getProperty(name);
-
-               if (value == null || value.equals(""))
-                       return null;
-               else
-                       return value;
-       }
-
-       protected String getProperty(String name) {
-               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.");
+                       OsgiBootUtils.debug(obj);
        }
 
        public boolean getDebug() {