You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2014/12/30 11:40:12 UTC

svn commit: r1648500 - in /sling/trunk/installer: core/src/main/java/org/apache/sling/installer/core/impl/ core/src/main/java/org/apache/sling/installer/core/impl/tasks/ it/src/test/java/org/apache/sling/installer/it/

Author: cziegeler
Date: Tue Dec 30 10:40:11 2014
New Revision: 1648500

URL: http://svn.apache.org/r1648500
Log:
SLING-3749 : Intelligently manage start level during artifact handling

Modified:
    sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java
    sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/tasks/BundleUpdateTask.java
    sling/trunk/installer/it/src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java

Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java?rev=1648500&r1=1648499&r2=1648500&view=diff
==============================================================================
--- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java (original)
+++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/OsgiInstallerImpl.java Tue Dec 30 10:40:11 2014
@@ -57,10 +57,13 @@ import org.apache.sling.installer.api.ta
 import org.apache.sling.installer.api.tasks.TaskResource;
 import org.apache.sling.installer.api.tasks.TaskResourceGroup;
 import org.apache.sling.installer.api.tasks.TransformationResult;
+import org.apache.sling.installer.core.impl.tasks.BundleUpdateTask;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
+import org.osgi.service.startlevel.StartLevel;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -76,6 +79,11 @@ import org.slf4j.LoggerFactory;
 public class OsgiInstallerImpl
 implements OsgiInstaller, ResourceChangeListener, RetryHandler, InfoProvider, Runnable {
 
+    /**
+     * The name of the bundle context property defining handling of bundle updates
+     */
+    private static final String START_LEVEL_HANDLING = "sling.installer.switchstartlevel";
+
     /** The logger */
     private final Logger logger =  LoggerFactory.getLogger(this.getClass());
 
@@ -121,6 +129,8 @@ implements OsgiInstaller, ResourceChange
     private final InstallListener listener;
     private final AtomicLong backgroundTaskCounter = new AtomicLong();
 
+    /** Switch start level on bundle update? */
+    private final boolean switchStartLevel;
 
     /**
      *  Constructor
@@ -134,6 +144,7 @@ implements OsgiInstaller, ResourceChange
         final File f = FileDataStore.SHARED.getDataFile("RegisteredResourceList.ser");
         this.listener = new InstallListener(ctx, logger);
         this.persistentList = new PersistentResourceList(f, listener);
+        this.switchStartLevel = PropertiesUtil.toBoolean(ctx.getProperty(START_LEVEL_HANDLING), false);
     }
 
     /**
@@ -627,8 +638,101 @@ implements OsgiInstaller, ResourceChange
 
     /**
      * Execute all tasks
+     * @param tasks The tasks to executed.
+     * @return The action to perform after the execution.
      */
     private ACTION executeTasks(final SortedSet<InstallTask> tasks) {
+        if (this.switchStartLevel && this.hasBundleUpdateTask(tasks)) {
+            // StartLevel service is always available
+            final ServiceReference ref = ctx.getServiceReference(StartLevel.class.getName());
+            final StartLevel startLevel = (StartLevel) ctx.getService(ref);
+            try {
+                final int targetStartLevel = this.getLowestStartLevel(tasks, startLevel);
+                final int currentStartLevel = startLevel.getStartLevel();
+                if (targetStartLevel < currentStartLevel) {
+                    auditLogger.info("Switching to start level {}", targetStartLevel);
+                    try {
+                        startLevel.setStartLevel(targetStartLevel);
+                        // now we have to wait until the start level is reached
+                        while (startLevel.getStartLevel() > targetStartLevel) {
+                            try {
+                                Thread.sleep(300);
+                            } catch (final InterruptedException ie) {
+                                Thread.currentThread().interrupt();
+                            }
+
+                        }
+
+                        return doExecuteTasks(tasks);
+
+                    } finally {
+                        // restore old start level in any case
+                        startLevel.setStartLevel(currentStartLevel);
+                        auditLogger.info("Switching back to start level {} after performing the required " +
+                                "installation tasks", currentStartLevel);
+
+                    }
+                }
+
+            } finally {
+                ctx.ungetService(ref);
+            }
+        }
+        return doExecuteTasks(tasks);
+    }
+
+    /**
+     * Get the lowest start level for the update operation
+     */
+    private int getLowestStartLevel(final SortedSet<InstallTask> tasks, final StartLevel startLevel) {
+        final int currentStartLevel = startLevel.getStartLevel();
+        int startLevelToTarget = currentStartLevel;
+        for(final InstallTask task : tasks) {
+            if (task instanceof BundleUpdateTask){
+                final Bundle b = ((BundleUpdateTask) task).getBundle();
+                if (b != null) {
+                    try {
+                        final int bundleStartLevel = startLevel.getBundleStartLevel(b) - 1;
+                        if (bundleStartLevel < startLevelToTarget){
+                            startLevelToTarget = bundleStartLevel;
+                        }
+                    } catch ( final IllegalArgumentException iae) {
+                        // ignore - bundle is uninstalled
+                    }
+                }
+            }
+        }
+        // check installer start level
+        final int ownStartLevel = startLevel.getBundleStartLevel(ctx.getBundle());
+        if ( ownStartLevel > startLevelToTarget ) {
+            // we don't want to disable ourselves
+            startLevelToTarget = ownStartLevel;
+        }
+        return startLevelToTarget;
+    }
+
+    /**
+     * Check if a bundle update task will be executed.
+     */
+    private boolean hasBundleUpdateTask(final SortedSet<InstallTask> tasks) {
+        boolean result = false;
+        for(final InstallTask task : tasks){
+            if ( task.isAsynchronousTask() ) {
+                result = false; // async task is executed immediately#
+                break;
+            } else if (task instanceof BundleUpdateTask){
+                result = true;
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Execute all tasks
+     * @param tasks The tasks to executed.
+     * @return The action to perform after the execution.
+     */
+    private ACTION doExecuteTasks(final SortedSet<InstallTask> tasks) {
         if ( !tasks.isEmpty() ) {
 
             final InstallationContext ctx = new InstallationContext() {

Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/tasks/BundleUpdateTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/tasks/BundleUpdateTask.java?rev=1648500&r1=1648499&r2=1648500&view=diff
==============================================================================
--- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/tasks/BundleUpdateTask.java (original)
+++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/tasks/BundleUpdateTask.java Tue Dec 30 10:40:11 2014
@@ -139,4 +139,9 @@ public class BundleUpdateTask extends Ab
         return BUNDLE_UPDATE_ORDER + getSortableStartLevel() + "-" + getResource().getEntityId();
     }
 
+    public Bundle getBundle(){
+        final String symbolicName = (String)getResource().getAttribute(Constants.BUNDLE_SYMBOLICNAME);
+        return BundleInfo.getMatchingBundle(this.getBundleContext(), symbolicName, null);
+    }
+
 }

Modified: sling/trunk/installer/it/src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/it/src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java?rev=1648500&r1=1648499&r2=1648500&view=diff
==============================================================================
--- sling/trunk/installer/it/src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java (original)
+++ sling/trunk/installer/it/src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java Tue Dec 30 10:40:11 2014
@@ -395,6 +395,7 @@ public class OsgiInstallerTestBase imple
     	String localRepo = System.getProperty("maven.repo.local", "");
 
     	return options(
+
                 junitBundles(),
                 when( localRepo.length() > 0 ).useOptions(
                         systemProperty("org.ops4j.pax.url.mvn.localRepository").value(localRepo)
@@ -411,8 +412,8 @@ public class OsgiInstallerTestBase imple
         	            mavenBundle("org.apache.felix", "org.apache.felix.scr", "1.8.0"),
         	            mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.2.8"),
                         mavenBundle("org.apache.felix", "org.apache.felix.metatype", "1.0.2"),
-        	        	mavenBundle("org.apache.sling", "org.apache.sling.installer.core", POM_VERSION),
-                        mavenBundle("org.apache.sling", "org.apache.sling.installer.factory.configuration", CONFIG_VERSION)
+        	        	mavenBundle("org.apache.sling", "org.apache.sling.installer.core", POM_VERSION).startLevel(5),
+                        mavenBundle("org.apache.sling", "org.apache.sling.installer.factory.configuration", CONFIG_VERSION).startLevel(5)
         		)
         );
     }