From: Mathieu Date: Fri, 4 Nov 2022 06:42:51 +0000 (+0100) Subject: Follow start levels more closely X-Git-Tag: v2.3.11~31 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=af52952e94d59bba22d0ac861e7777b60cf06c78 Follow start levels more closely --- diff --git a/org.argeo.init/src/org/argeo/init/osgi/OsgiBoot.java b/org.argeo.init/src/org/argeo/init/osgi/OsgiBoot.java index 223effb54..c26fad668 100644 --- a/org.argeo.init/src/org/argeo/init/osgi/OsgiBoot.java +++ b/org.argeo.init/src/org/argeo/init/osgi/OsgiBoot.java @@ -19,6 +19,8 @@ import java.util.Set; import java.util.SortedMap; import java.util.StringTokenizer; import java.util.TreeMap; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import org.argeo.init.a2.A2Source; @@ -356,15 +358,16 @@ public class OsgiBoot implements OsgiBootConstants { FrameworkStartLevel frameworkStartLevel = bundleContext.getBundle(0).adapt(FrameworkStartLevel.class); // default and active start levels from System properties - Integer defaultStartLevel = Integer.parseInt(getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL, "4")); - Integer activeStartLevel = Integer.parseInt(getProperty(PROP_OSGI_STARTLEVEL, "6")); + int initialStartLevel = frameworkStartLevel.getInitialBundleStartLevel(); + int defaultStartLevel = Integer.parseInt(getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL, "4")); + int activeStartLevel = Integer.parseInt(getProperty(PROP_OSGI_STARTLEVEL, "6")); if (OsgiBootUtils.isDebug()) { OsgiBootUtils.debug("OSGi default start level: " + getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL, "") + ", using " + defaultStartLevel); OsgiBootUtils.debug("OSGi active start level: " + getProperty(PROP_OSGI_STARTLEVEL, "") + ", using " + activeStartLevel); OsgiBootUtils.debug("Framework start level: " + frameworkStartLevel.getStartLevel() + " (initial: " - + frameworkStartLevel.getInitialBundleStartLevel() + ")"); + + initialStartLevel + ")"); } SortedMap> startLevels = new TreeMap>(); @@ -396,18 +399,27 @@ public class OsgiBoot implements OsgiBootConstants { if (OsgiBootUtils.isDebug()) OsgiBootUtils.debug("About to set framework start level to " + activeStartLevel + " ..."); - // Start the framework asynchronously - ForkJoinPool.commonPool().execute(() -> { - frameworkStartLevel.setStartLevel(activeStartLevel, (FrameworkEvent event) -> { - if (OsgiBootUtils.isDebug()) - OsgiBootUtils.debug("Framework event: " + event); - int initialStartLevel = frameworkStartLevel.getInitialBundleStartLevel(); - int startLevel = frameworkStartLevel.getStartLevel(); - if (OsgiBootUtils.isDebug()) - OsgiBootUtils - .debug("Framework start level: " + startLevel + " (initial: " + initialStartLevel + ")"); + // Start the framework level after level + stages: for (int stage = initialStartLevel; stage <= activeStartLevel; stage++) { + OsgiBootUtils.info("Starting stage " + stage + "..."); + final int nextStage = stage; + final CompletableFuture stageCompleted = new CompletableFuture<>(); + ForkJoinPool.commonPool().execute(() -> { + frameworkStartLevel.setStartLevel(nextStage, (FrameworkEvent event) -> { + stageCompleted.complete(event); + }); }); - }); + FrameworkEvent event; + try { + event = stageCompleted.get(); + } catch (InterruptedException | ExecutionException e) { + throw new IllegalStateException("Cannot continue start", e); + } + if (event.getThrowable() != null) { + OsgiBootUtils.error("Stage " + nextStage + " failed, aborting start.", event.getThrowable()); + break stages; + } + } } private static void computeStartLevels(SortedMap> startLevels, Map properties,