Revert start level changes
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / osgi / OsgiBoot.java
index 9f3bd25f783df4908aaa4c6bd8bfffa8c200597e..884461b12b5b0e5bb322ddd96705c619af6a0f1f 100644 (file)
@@ -355,8 +355,17 @@ 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, "<not set>") + ", using " + defaultStartLevel);
+                       OsgiBootUtils.debug("OSGi active start level: " + getProperty(PROP_OSGI_STARTLEVEL, "<not set>")
+                                       + ", using " + activeStartLevel);
+                       OsgiBootUtils.debug("Framework start level: " + frameworkStartLevel.getStartLevel() + " (initial: "
+                                       + initialStartLevel + ")");
+               }
 
                SortedMap<Integer, List<String>> startLevels = new TreeMap<Integer, List<String>>();
                computeStartLevels(startLevels, properties, defaultStartLevel);
@@ -383,13 +392,40 @@ public class OsgiBoot implements OsgiBootConstants {
                                }
                        }
                }
+
+               if (OsgiBootUtils.isDebug())
+                       OsgiBootUtils.debug("About to set framework start level to " + activeStartLevel + " ...");
+
                frameworkStartLevel.setStartLevel(activeStartLevel, (FrameworkEvent event) -> {
-                       if (OsgiBootUtils.isDebug())
-                               OsgiBootUtils.debug("Framework event: " + event);
-                       int initialStartLevel = frameworkStartLevel.getInitialBundleStartLevel();
-                       int startLevel = frameworkStartLevel.getStartLevel();
-                       OsgiBootUtils.debug("Framework start level: " + startLevel + " (initial: " + initialStartLevel + ")");
+                       if (event.getType() == FrameworkEvent.ERROR) {
+                               OsgiBootUtils.error("Start sequence failed", event.getThrowable());
+                       } else {
+                               if (OsgiBootUtils.isDebug())
+                                       OsgiBootUtils.debug("Framework started at level " + frameworkStartLevel.getStartLevel());
+                       }
                });
+
+//             // Start the framework level after level
+//             int currentStartLevel = frameworkStartLevel.getStartLevel();
+//             stages: for (int stage = currentStartLevel + 1; stage <= activeStartLevel; stage++) {
+//                     if (OsgiBootUtils.isDebug())
+//                             OsgiBootUtils.debug("Starting stage " + stage + "...");
+//                     final int nextStage = stage;
+//                     final CompletableFuture<FrameworkEvent> stageCompleted = new CompletableFuture<>();
+//                     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<Integer, List<String>> startLevels, Map<String, String> properties,
@@ -666,6 +702,7 @@ public class OsgiBoot implements OsgiBootConstants {
        private void refreshFramework() {
                Bundle systemBundle = bundleContext.getBundle(0);
                FrameworkWiring frameworkWiring = systemBundle.adapt(FrameworkWiring.class);
+               // TODO deal with refresh breaking native loading (e.g SWT)
                frameworkWiring.refreshBundles(null);
        }