You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:43:05 UTC

[sling-org-apache-sling-installer-factory-subsystems] 08/10: Separate install from start task

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.installer.factory.subsystems-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-factory-subsystems.git

commit 2c63895174fd15d1b60e8544bdeef44322b0307e
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Oct 10 16:12:45 2014 +0000

    Separate install from start task
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/installer/factories/subsystems@1630931 13f79535-47bb-0310-9956-ffa450edef68
---
 .../subsystems/impl/InstallSubsystemTask.java      |  7 +-
 ...lSubsystemTask.java => StartSubsystemTask.java} | 30 ++++----
 .../subsystems/impl/SubsystemInstaller.java        | 86 +++++++++++++---------
 .../subsystems/impl/UninstallSubsystemTask.java    |  4 +
 .../subsystems/impl/UpdateSubsystemTask.java       |  3 +
 5 files changed, 75 insertions(+), 55 deletions(-)

diff --git a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/InstallSubsystemTask.java b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/InstallSubsystemTask.java
index 230272c..0b1086f 100644
--- a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/InstallSubsystemTask.java
+++ b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/InstallSubsystemTask.java
@@ -28,6 +28,9 @@ import org.apache.sling.installer.api.tasks.TaskResource;
 import org.apache.sling.installer.api.tasks.TaskResourceGroup;
 import org.osgi.service.subsystem.Subsystem;
 
+/**
+ * This is the subsystem install task.
+ */
 public class InstallSubsystemTask extends InstallTask {
 
     private static final String INSTALL_ORDER = "53-";
@@ -46,8 +49,8 @@ public class InstallSubsystemTask extends InstallTask {
 
         try {
             final Subsystem sub = this.rootSubsystem.install(tr.getURL(), tr.getInputStream());
-            sub.start();
-            ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.INSTALLED));
+            ctx.addTaskToCurrentCycle(new StartSubsystemTask(this.getResourceGroup(), sub));
+            ctx.log("Installed new subsystem {}", sub);
         } catch (final IOException e) {
             ctx.log("Unable to install subsystem {} : {}", tr, e);
             ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.IGNORED));
diff --git a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/InstallSubsystemTask.java b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/StartSubsystemTask.java
similarity index 64%
copy from src/main/java/org/apache/sling/installer/factories/subsystems/impl/InstallSubsystemTask.java
copy to src/main/java/org/apache/sling/installer/factories/subsystems/impl/StartSubsystemTask.java
index 230272c..a5eb224 100644
--- a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/InstallSubsystemTask.java
+++ b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/StartSubsystemTask.java
@@ -18,8 +18,6 @@
  */
 package org.apache.sling.installer.factories.subsystems.impl;
 
-import java.io.IOException;
-
 import org.apache.sling.installer.api.tasks.ChangeStateTask;
 import org.apache.sling.installer.api.tasks.InstallTask;
 import org.apache.sling.installer.api.tasks.InstallationContext;
@@ -28,30 +26,28 @@ import org.apache.sling.installer.api.tasks.TaskResource;
 import org.apache.sling.installer.api.tasks.TaskResourceGroup;
 import org.osgi.service.subsystem.Subsystem;
 
-public class InstallSubsystemTask extends InstallTask {
+/**
+ * This is the subsystem start task.
+ */
+public class StartSubsystemTask extends InstallTask {
 
-    private static final String INSTALL_ORDER = "53-";
+    private static final String INSTALL_ORDER = "55-";
 
-    private final Subsystem rootSubsystem;
+    private final Subsystem subsystem;
 
-    public InstallSubsystemTask(final TaskResourceGroup grp, final Subsystem rootSubsystem) {
+    public StartSubsystemTask(final TaskResourceGroup grp, final Subsystem system) {
         super(grp);
-        this.rootSubsystem = rootSubsystem;
+        this.subsystem = system;
     }
 
     @Override
     public void execute(final InstallationContext ctx) {
         final TaskResource tr = this.getResource();
-        ctx.log("Installing new subsystem from {}", tr);
-
-        try {
-            final Subsystem sub = this.rootSubsystem.install(tr.getURL(), tr.getInputStream());
-            sub.start();
-            ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.INSTALLED));
-        } catch (final IOException e) {
-            ctx.log("Unable to install subsystem {} : {}", tr, e);
-            ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.IGNORED));
-        }
+        ctx.log("Starting subsystem from {}", tr);
+
+        this.subsystem.start();
+        ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.INSTALLED));
+        ctx.log("Started subsystem {}", this.subsystem);
     }
 
     @Override
diff --git a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/SubsystemInstaller.java b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/SubsystemInstaller.java
index 3b29961..c58d7a3 100644
--- a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/SubsystemInstaller.java
+++ b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/SubsystemInstaller.java
@@ -42,15 +42,19 @@ import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
 import org.osgi.service.subsystem.Subsystem;
+import org.osgi.service.subsystem.Subsystem.State;
 import org.osgi.service.subsystem.SubsystemConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * This is an extension for the OSGi installer
- * It listens for files ending with ".esa" and a proper manifest. Though subsystems does
- * not require a complete manifest, the installer supports only subsystems with the
- * basic info.
+ * It listens for files ending with ".esa" and a proper subsystem manifest.
+ * Though subsystems does not require a complete manifest, the installer supports
+ * only subsystems with the basic info (name and version).
+ *
+ * As subsystems currently do not support an update, an uninstall/install is done
+ * instead - which will lose bundle private data, bound configurations etc.
  */
 public class SubsystemInstaller
     implements ResourceTransformer, InstallTaskFactory {
@@ -175,45 +179,55 @@ public class SubsystemInstaller
                 // search a subsystem with the symbolic name
                 final ServiceReference<Subsystem> ref = this.getSubsystemReference(info.symbolicName);
 
-                final Version newVersion = new Version(info.version);
-                final Version oldVersion = (ref == null ? null : (Version)ref.getProperty("subsystem.version"));
-
-                // Install
-                if ( rsrc.getState() == ResourceState.INSTALL ) {
-                    if ( oldVersion != null ) {
-
-                        final int compare = oldVersion.compareTo(newVersion);
-                        if (compare < 0) {
-                            // installed version is lower -> update
-                            result = new UpdateSubsystemTask(toActivate, this.bundleContext, ref, this.rootSubsystem);
-                        } else if ( compare == 0 && isSnapshot(newVersion) ) {
-                            // same version but snapshot -> update
-                            result = new UpdateSubsystemTask(toActivate, this.bundleContext, ref, this.rootSubsystem);
+                final Subsystem currentSubsystem = (ref != null ? this.bundleContext.getService(ref) : null);
+                try {
+                    final Version newVersion = new Version(info.version);
+                    final Version oldVersion = (ref == null ? null : (Version)ref.getProperty("subsystem.version"));
+
+                    // Install
+                    if ( rsrc.getState() == ResourceState.INSTALL ) {
+                        if ( oldVersion != null ) {
+
+                            final int compare = oldVersion.compareTo(newVersion);
+                            if (compare < 0) {
+                                // installed version is lower -> update
+                                result = new UpdateSubsystemTask(toActivate, this.bundleContext, ref, this.rootSubsystem);
+                            } else if ( compare == 0 && isSnapshot(newVersion) ) {
+                                // same version but snapshot -> update
+                                result = new UpdateSubsystemTask(toActivate, this.bundleContext, ref, this.rootSubsystem);
+                            } else if ( compare == 0 && currentSubsystem != null && currentSubsystem.getState() != State.ACTIVE ) {
+                                // try to start the version
+                                result = new StartSubsystemTask(toActivate, currentSubsystem);
+                            } else {
+                                logger.info("{} is not installed, subsystem with same or higher version is already installed: {}", info, newVersion);
+                                result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
+                            }
                         } else {
-                            logger.debug("{} is not installed, subsystem with same or higher version is already installed: {}", info, newVersion);
-                            result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
+                            result = new InstallSubsystemTask(toActivate, this.rootSubsystem);
                         }
-                    } else {
-                        result = new InstallSubsystemTask(toActivate, this.rootSubsystem);
-                    }
 
-                // Uninstall
-                } else if ( rsrc.getState() == ResourceState.UNINSTALL ) {
-                    if ( oldVersion == null ) {
-                        logger.error("Nothing to uninstall. {} is currently not installed.", info);
-                        result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
-                    } else {
-
-                        final int compare = oldVersion.compareTo(newVersion);
-                        if ( compare == 0 ) {
-                            result = new UninstallSubsystemTask(toActivate, this.bundleContext, ref);
-                        } else {
-                            logger.error("Nothing to uninstall. {} is currently not installed, different version is installed {}", info, oldVersion);
+                    // Uninstall
+                    } else if ( rsrc.getState() == ResourceState.UNINSTALL ) {
+                        if ( oldVersion == null ) {
+                            logger.error("Nothing to uninstall. {} is currently not installed.", info);
                             result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
+                        } else {
+
+                            final int compare = oldVersion.compareTo(newVersion);
+                            if ( compare == 0 ) {
+                                result = new UninstallSubsystemTask(toActivate, this.bundleContext, ref);
+                            } else {
+                                logger.error("Nothing to uninstall. {} is currently not installed, different version is installed {}", info, oldVersion);
+                                result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
+                            }
                         }
+                    } else {
+                        result = null;
+                    }
+                } finally {
+                    if ( currentSubsystem != null ) {
+                        this.bundleContext.ungetService(ref);
                     }
-                } else {
-                    result = null;
                 }
             }
         } else {
diff --git a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/UninstallSubsystemTask.java b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/UninstallSubsystemTask.java
index 4ae0dab..a499c61 100644
--- a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/UninstallSubsystemTask.java
+++ b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/UninstallSubsystemTask.java
@@ -28,6 +28,9 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.subsystem.Subsystem;
 
+/**
+ * Uninstall a subsystem
+ */
 public class UninstallSubsystemTask extends InstallTask {
 
     private static final String INSTALL_ORDER = "52-";
@@ -55,6 +58,7 @@ public class UninstallSubsystemTask extends InstallTask {
             if ( subsystem != null ) {
                 subsystem.uninstall();
                 ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.UNINSTALLED));
+                ctx.log("Uninstalled subsystem {}", subsystem);
             } else {
                 ctx.log("Unable to uninstall subsystem {}.", tr);
                 ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.IGNORED));
diff --git a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/UpdateSubsystemTask.java b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/UpdateSubsystemTask.java
index cb6939c..781fcee 100644
--- a/src/main/java/org/apache/sling/installer/factories/subsystems/impl/UpdateSubsystemTask.java
+++ b/src/main/java/org/apache/sling/installer/factories/subsystems/impl/UpdateSubsystemTask.java
@@ -28,6 +28,9 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.subsystem.Subsystem;
 
+/**
+ * Update subsystem by uninstalling and installing it.
+ */
 public class UpdateSubsystemTask extends InstallTask {
 
     private static final String INSTALL_ORDER = "54-";

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.