X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.osgi.boot%2Fsrc%2Forg%2Fargeo%2Fosgi%2Fboot%2FOsgiBoot.java;h=bd49d99d47dcd8f465bb8f4f4d70268da7e5a289;hb=ebd927da42511bb5959000c50a39974c6cfa5f49;hp=64ace0c3ee4f8fc518d41a0c9ee67af126e70f22;hpb=e66b9893b0e511f8ab295e3cee42b7dc966f1597;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBoot.java b/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBoot.java index 64ace0c3e..bd49d99d4 100644 --- a/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBoot.java +++ b/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBoot.java @@ -83,8 +83,8 @@ public class OsgiBoot implements OsgiBootConstants { private boolean excludeSvn = Boolean.valueOf(System.getProperty(PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN, "false")) .booleanValue(); - // /** Default is 10s (set in constructor) */ - // private long defaultTimeout; + /** Default is 10s */ + private long defaultTimeout = 10000l; private final BundleContext bundleContext; @@ -232,15 +232,19 @@ public class OsgiBoot implements OsgiBootConstants { * START */ public void startBundles() { + startBundles(System.getProperties()); + } + + public void startBundles(Properties properties) { FrameworkStartLevel frameworkStartLevel = bundleContext.getBundle(0).adapt(FrameworkStartLevel.class); // default and active start levels from System properties Integer defaultStartLevel = new Integer( - Integer.parseInt(OsgiBootUtils.getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL, "4"))); - Integer activeStartLevel = new Integer(OsgiBootUtils.getProperty(PROP_OSGI_STARTLEVEL, "6")); + Integer.parseInt(getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL, "4"))); + Integer activeStartLevel = new Integer(getProperty(PROP_OSGI_STARTLEVEL, "6")); SortedMap> startLevels = new TreeMap>(); - computeStartLevels(startLevels, System.getProperties(), defaultStartLevel); + computeStartLevels(startLevels, properties, defaultStartLevel); // inverts the map for the time being, TODO optimise Map bundleStartLevels = new HashMap<>(); for (Integer level : startLevels.keySet()) { @@ -331,115 +335,107 @@ public class OsgiBoot implements OsgiBootConstants { } } - // /** - // * Start the provided list of bundles - // * - // * @return whether all bundles are now in active state - // */ - // private boolean startBundles(List bundlesToStart) { - // if (bundlesToStart.size() == 0) - // return true; - // - // // used to monitor ACTIVE states - // List startedBundles = new ArrayList(); - // // used to log the bundles not found - // List notFoundBundles = new ArrayList(bundlesToStart); - // - // Bundle[] bundles = bundleContext.getBundles(); - // long startBegin = System.currentTimeMillis(); - // for (int i = 0; i < bundles.length; i++) { - // Bundle bundle = bundles[i]; - // String symbolicName = bundle.getSymbolicName(); - // if (bundlesToStart.contains(symbolicName)) - // try { - // try { - // bundle.start(); - // if (debug) - // debug("Bundle " + symbolicName + " started"); - // } catch (Exception e) { - // OsgiBootUtils.warn("Start of bundle " + symbolicName + " failed because - // of " + e - // + ", maybe bundle is not yet resolved," + " waiting and trying again."); - // waitForBundleResolvedOrActive(startBegin, bundle); - // bundle.start(); - // startedBundles.add(bundle); - // } - // notFoundBundles.remove(symbolicName); - // } catch (Exception e) { - // OsgiBootUtils.warn("Bundle " + symbolicName + " cannot be started: " + - // e.getMessage()); - // if (debug) - // e.printStackTrace(); - // // was found even if start failed - // notFoundBundles.remove(symbolicName); - // } - // } - // - // for (int i = 0; i < notFoundBundles.size(); i++) - // OsgiBootUtils.warn("Bundle '" + notFoundBundles.get(i) + "' not started - // because it was not found."); - // - // // monitors that all bundles are started - // long beginMonitor = System.currentTimeMillis(); - // boolean allStarted = !(startedBundles.size() > 0); - // List notStarted = new ArrayList(); - // while (!allStarted && (System.currentTimeMillis() - beginMonitor) < - // defaultTimeout) { - // notStarted = new ArrayList(); - // allStarted = true; - // for (int i = 0; i < startedBundles.size(); i++) { - // Bundle bundle = (Bundle) startedBundles.get(i); - // // TODO check behaviour of lazs bundles - // if (bundle.getState() != Bundle.ACTIVE) { - // allStarted = false; - // notStarted.add(bundle.getSymbolicName()); - // } - // } - // try { - // Thread.sleep(100); - // } catch (InterruptedException e) { - // // silent - // } - // } - // long duration = System.currentTimeMillis() - beginMonitor; - // - // if (!allStarted) - // for (int i = 0; i < notStarted.size(); i++) - // OsgiBootUtils.warn("Bundle '" + notStarted.get(i) + "' not ACTIVE after " - // + (duration / 1000) + "s"); - // - // return allStarted; - // } + /** + * Start the provided list of bundles + * + * @return whether all bundles are now in active state + * @deprecated + */ + @Deprecated + public boolean startBundles(List bundlesToStart) { + if (bundlesToStart.size() == 0) + return true; - // /** Waits for a bundle to become active or resolved */ - // private void waitForBundleResolvedOrActive(long startBegin, Bundle - // bundle) throws Exception { - // int originalState = bundle.getState(); - // if ((originalState == Bundle.RESOLVED) || (originalState == - // Bundle.ACTIVE)) - // return; - // - // String originalStateStr = OsgiBootUtils.stateAsString(originalState); - // - // int currentState = bundle.getState(); - // while (!(currentState == Bundle.RESOLVED || currentState == - // Bundle.ACTIVE)) { - // long now = System.currentTimeMillis(); - // if ((now - startBegin) > defaultTimeout * 10) - // throw new Exception("Bundle " + bundle.getSymbolicName() + " was not - // RESOLVED or ACTIVE after " - // + (now - startBegin) + "ms (originalState=" + originalStateStr + ", - // currentState=" - // + OsgiBootUtils.stateAsString(currentState) + ")"); - // - // try { - // Thread.sleep(100l); - // } catch (InterruptedException e) { - // // silent - // } - // currentState = bundle.getState(); - // } - // } + // used to monitor ACTIVE states + List startedBundles = new ArrayList(); + // used to log the bundles not found + List notFoundBundles = new ArrayList(bundlesToStart); + + Bundle[] bundles = bundleContext.getBundles(); + long startBegin = System.currentTimeMillis(); + for (int i = 0; i < bundles.length; i++) { + Bundle bundle = bundles[i]; + String symbolicName = bundle.getSymbolicName(); + if (bundlesToStart.contains(symbolicName)) + try { + try { + bundle.start(); + if (debug) + debug("Bundle " + symbolicName + " started"); + } catch (Exception e) { + OsgiBootUtils.warn("Start of bundle " + symbolicName + " failed because of " + e + + ", maybe bundle is not yet resolved," + " waiting and trying again."); + waitForBundleResolvedOrActive(startBegin, bundle); + bundle.start(); + startedBundles.add(bundle); + } + notFoundBundles.remove(symbolicName); + } catch (Exception e) { + OsgiBootUtils.warn("Bundle " + symbolicName + " cannot be started: " + e.getMessage()); + if (debug) + e.printStackTrace(); + // was found even if start failed + notFoundBundles.remove(symbolicName); + } + } + + for (int i = 0; i < notFoundBundles.size(); i++) + OsgiBootUtils.warn("Bundle '" + notFoundBundles.get(i) + "' not started because it was not found."); + + // monitors that all bundles are started + long beginMonitor = System.currentTimeMillis(); + boolean allStarted = !(startedBundles.size() > 0); + List notStarted = new ArrayList(); + while (!allStarted && (System.currentTimeMillis() - beginMonitor) < defaultTimeout) { + notStarted = new ArrayList(); + allStarted = true; + for (int i = 0; i < startedBundles.size(); i++) { + Bundle bundle = (Bundle) startedBundles.get(i); + // TODO check behaviour of lazs bundles + if (bundle.getState() != Bundle.ACTIVE) { + allStarted = false; + notStarted.add(bundle.getSymbolicName()); + } + } + try { + Thread.sleep(100); + } catch (InterruptedException e) { + // silent + } + } + long duration = System.currentTimeMillis() - beginMonitor; + + if (!allStarted) + for (int i = 0; i < notStarted.size(); i++) + OsgiBootUtils.warn("Bundle '" + notStarted.get(i) + "' not ACTIVE after " + (duration / 1000) + "s"); + + return allStarted; + } + + /** Waits for a bundle to become active or resolved */ + private void waitForBundleResolvedOrActive(long startBegin, Bundle bundle) throws Exception { + int originalState = bundle.getState(); + if ((originalState == Bundle.RESOLVED) || (originalState == Bundle.ACTIVE)) + return; + + String originalStateStr = OsgiBootUtils.stateAsString(originalState); + + int currentState = bundle.getState(); + while (!(currentState == Bundle.RESOLVED || currentState == Bundle.ACTIVE)) { + long now = System.currentTimeMillis(); + if ((now - startBegin) > defaultTimeout * 10) + throw new Exception("Bundle " + bundle.getSymbolicName() + " was not RESOLVED or ACTIVE after " + + (now - startBegin) + "ms (originalState=" + originalStateStr + ", currentState=" + + OsgiBootUtils.stateAsString(currentState) + ")"); + + try { + Thread.sleep(100l); + } catch (InterruptedException e) { + // silent + } + currentState = bundle.getState(); + } + } /* * BUNDLE PATTERNS INSTALLATION @@ -453,7 +449,7 @@ public class OsgiBoot implements OsgiBootConstants { * effects. */ public List getBundlesUrls() { - String bundlePatterns = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BUNDLES); + String bundlePatterns = getProperty(PROP_ARGEO_OSGI_BUNDLES); return getBundlesUrls(bundlePatterns); } @@ -462,7 +458,7 @@ public class OsgiBoot implements OsgiBootConstants { * default base url */ public List getBundlesUrls(String bundlePatterns) { - String baseUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BASE_URL, DEFAULT_BASE_URL); + String baseUrl = getProperty(PROP_ARGEO_OSGI_BASE_URL, DEFAULT_BASE_URL); return getBundlesUrls(baseUrl, bundlePatterns); } @@ -522,11 +518,15 @@ public class OsgiBoot implements OsgiBootConstants { * DISTRIBUTION JAR INSTALLATION */ public List getDistributionUrls() { + String distributionUrl = getProperty(PROP_ARGEO_OSGI_DISTRIBUTION_URL); + String baseUrl = getProperty(PROP_ARGEO_OSGI_BASE_URL); + return getDistributionUrls(distributionUrl, baseUrl); + } + + public List getDistributionUrls(String distributionUrl, String baseUrl) { List urls = new ArrayList(); - String distributionUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_DISTRIBUTION_URL); if (distributionUrl == null) return urls; - String baseUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BASE_URL); DistributionBundle distributionBundle; if (baseUrl != null && !(distributionUrl.startsWith("http") || distributionUrl.startsWith("file"))) { @@ -654,6 +654,33 @@ public class OsgiBoot implements OsgiBootConstants { // return url; // } + /** + * Gets a property value + * + * @return null when defaultValue is "" + */ + public String getProperty(String name, String defaultValue) { + String value = bundleContext.getProperty(name); + if (value == null) + return defaultValue; // may be null + else + return value; + +// if (defaultValue != null) +// value = System.getProperty(name, defaultValue); +// else +// value = System.getProperty(name); +// +// if (value == null || value.equals("")) +// return null; +// else +// return value; + } + + public String getProperty(String name) { + return getProperty(name, null); + } + /* * BEAN METHODS */