From ee98a7cb6879e5f077d7fcefad6cd0573c3045cb Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Tue, 28 Jul 2009 14:00:41 +0000 Subject: [PATCH] Make OSGiBoot wait for bundles to be in status active or resolved git-svn-id: https://svn.argeo.org/slc/trunk@2823 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../java/org/argeo/slc/osgiboot/OsgiBoot.java | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java b/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java index 22a759892..828837063 100644 --- a/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java +++ b/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java @@ -150,8 +150,11 @@ public class OsgiBoot { if (bundlesToStart.size() == 0) return; - List notStartedBundles = new ArrayList(bundlesToStart); + // 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(); @@ -160,24 +163,28 @@ public class OsgiBoot { try { bundle.start(); } catch (Exception e) { - // start failed, maybe bundle is not yet resolved - waitForBundleResolvedOrActive(bundle); + warn("Start of bundle " + symbolicName + + " failed because of " + e + + ", maybe bundle is not yet resolved," + + " waiting and trying again."); + waitForBundleResolvedOrActive(startBegin, bundle); bundle.start(); } - - notStartedBundles.remove(symbolicName); + notFoundBundles.remove(symbolicName); } catch (Exception e) { warn("Bundle " + symbolicName + " cannot be started: " + e.getMessage()); + // was found even if start failed + notFoundBundles.remove(symbolicName); } } - for (int i = 0; i < notStartedBundles.size(); i++) - warn("Bundle " + notStartedBundles.get(i) + for (int i = 0; i < notFoundBundles.size(); i++) + warn("Bundle " + notFoundBundles.get(i) + " not started because it was not found."); } - protected void waitForBundleResolvedOrActive(Bundle bundle) + protected void waitForBundleResolvedOrActive(long startBegin, Bundle bundle) throws Exception { int originalState = bundle.getState(); if ((originalState == Bundle.RESOLVED) @@ -186,22 +193,22 @@ public class OsgiBoot { String originalStateStr = stateAsString(originalState); - long begin = System.currentTimeMillis(); - while ((bundle.getState() != Bundle.RESOLVED) - || (bundle.getState() != Bundle.ACTIVE)) { + int currentState = bundle.getState(); + while (!(currentState == Bundle.RESOLVED || currentState == Bundle.ACTIVE)) { long now = System.currentTimeMillis(); - if ((now - begin) > defaultTimeout) + if ((now - startBegin) > defaultTimeout) throw new Exception("Bundle " + bundle.getSymbolicName() - + " was not RESOLVED or ACTIVE after " + (now - begin) - + "ms (originalState=" + originalStateStr - + ", currentState=" + stateAsString(bundle.getState()) - + ")"); + + " was not RESOLVED or ACTIVE after " + + (now - startBegin) + "ms (originalState=" + + originalStateStr + ", currentState=" + + stateAsString(currentState) + ")"); try { Thread.sleep(100l); } catch (InterruptedException e) { // silent } + currentState = bundle.getState(); } } -- 2.39.5