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:53:29 UTC

[sling-org-apache-sling-launchpad-installer] 04/11: SLING.2649 : Add support for run modes

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

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

commit bca915d6d5b457a68b4417864249186c064f501f
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Nov 13 17:48:24 2012 +0000

    SLING.2649 : Add support for run modes
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/launchpad/installer@1408855 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  6 ++
 .../installer/impl/LaunchpadConfigInstaller.java   | 88 ++++++++++++++++++----
 .../launchpad/installer/impl/ServicesListener.java | 13 +++-
 3 files changed, 89 insertions(+), 18 deletions(-)

diff --git a/pom.xml b/pom.xml
index aa1697a..f33dada 100644
--- a/pom.xml
+++ b/pom.xml
@@ -73,5 +73,11 @@
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.settings</artifactId>
+            <version>1.1.0</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/launchpad/installer/impl/LaunchpadConfigInstaller.java b/src/main/java/org/apache/sling/launchpad/installer/impl/LaunchpadConfigInstaller.java
index 3f8c3dc..0bf1abf 100644
--- a/src/main/java/org/apache/sling/launchpad/installer/impl/LaunchpadConfigInstaller.java
+++ b/src/main/java/org/apache/sling/launchpad/installer/impl/LaunchpadConfigInstaller.java
@@ -25,6 +25,7 @@ import java.util.Dictionary;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.Set;
 
 import org.apache.sling.installer.api.InstallableResource;
 import org.apache.sling.installer.api.OsgiInstaller;
@@ -41,27 +42,37 @@ public class LaunchpadConfigInstaller {
     /**
      * Resources supplied under this path by
      * LaunchpadContentProvider are considered for installation
+     */
+    private static final String ROOT_PATH = "resources";
+
+    /**
+     * Resources supplied under this path by
+     * LaunchpadContentProvider are considered for installation
      * as configurations
      */
-    private static final String ROOT_CONFIG_PATH = "resources/config";
+    private static final String CONFIG_NAME = "config";
 
     /**
      * Resources supplied under this path by
      * LaunchpadContentProvider are considered for installation
      * as files
      */
-    private static final String ROOT_INSTALL_PATH = "resources/install";
+    private static final String INSTALL_NAME = "install";
+    private static final String INSTALL_PREFIX = "install.";
 
     /** Artifact priority. */
     private static final Integer PRIORITY = new Integer(50);
+    private static final int PRIORITY_BOOST = 5;
 
     /**
      * Check the path for installable artifacts.
      */
     private static boolean checkPath(final LaunchpadContentProvider resourceProvider,
+            final Set<String> activeRunModes,
             final Collection<InstallableResource> installables,
             final String rootPath,
-            final String resourceType) {
+            final String resourceType,
+            final Integer prio) {
         int count = 0;
         final Logger logger = LoggerFactory.getLogger(LaunchpadConfigInstaller.class);
         final Iterator<String> configPaths = resourceProvider.getChildren(rootPath);
@@ -73,18 +84,27 @@ public class LaunchpadConfigInstaller {
                 if ( path.endsWith("/") ) {
                     path = path.substring(0, path.length() - 1);
                 }
-                if ( !checkPath(resourceProvider, installables, path, resourceType) ) {
-                    logger.info("Launchpad {} will be installed: {}", resourceType, path);
+                if ( !checkPath(resourceProvider, activeRunModes, installables, path, resourceType, prio) ) {
                     final URL url = resourceProvider.getResource(path);
                     Dictionary<String, Object> dict = null;
                     if ( InstallableResource.TYPE_FILE.equals(resourceType) ) {
                         dict = new Hashtable<String, Object>();
-                        dict.put(InstallableResource.INSTALLATION_HINT, hint);
+                        if ( !hint.startsWith(INSTALL_NAME) ) {
+                            dict.put(InstallableResource.INSTALLATION_HINT, hint);
+                        }
                         try {
                             dict.put(InstallableResource.RESOURCE_URI_HINT, url.toURI().toString());
                         } catch (final URISyntaxException e) {
                             // we just ignore this
                         }
+                    } else {
+                        // if this is a configuration, hint could be run Modes
+                        if ( !hint.equals(CONFIG_NAME) ) {
+                            if ( isActive(hint, activeRunModes) == 0 ) {
+                                logger.debug("Launchpad ignoring {} : {} due to unactivated run mode", resourceType, path);
+                                continue;
+                            }
+                        }
                     }
                     long lastModified = -1;
                     try {
@@ -92,9 +112,10 @@ public class LaunchpadConfigInstaller {
                     } catch (final IOException e) {
                         // we ignore this
                     }
+                    logger.debug("Launchpad {} will be installed: {}", resourceType, path);
                     final String digest = (lastModified > 0 ? String.valueOf(lastModified) : null);
                     final InputStream stream = resourceProvider.getResourceAsStream(path);
-                    installables.add(new InstallableResource(path, stream, dict, digest, resourceType, PRIORITY));
+                    installables.add(new InstallableResource(path, stream, dict, digest, resourceType, prio));
                     count++;
                 }
             }
@@ -102,22 +123,59 @@ public class LaunchpadConfigInstaller {
         return count > 0;
     }
 
+    private static int isActive(final String runModesString, final Set<String> activeRunModes) {
+        final String[] runModes = runModesString.split(".");
+        boolean active = true;
+        for(final String mode : runModes) {
+            if ( !activeRunModes.contains(mode) ) {
+                active = false;
+            }
+        }
+        return active ? runModes.length : 0;
+    }
+
     /**
      * Install artifacts
      */
     public static void install(final OsgiInstaller installer,
-            final LaunchpadContentProvider resourceProvider) {
+            final LaunchpadContentProvider resourceProvider,
+            final Set<String> activeRunModes) {
         final Logger logger = LoggerFactory.getLogger(LaunchpadConfigInstaller.class);
-        logger.info("Activating launchpad config installer, configuration path={}, install path={}",
-                ROOT_CONFIG_PATH, ROOT_INSTALL_PATH);
+        logger.info("Activating launchpad config installer, configuration path={}/{}, install path={}/{}",
+                new Object[] {ROOT_PATH, CONFIG_NAME, ROOT_PATH, INSTALL_NAME});
 
         final Collection<InstallableResource> installables = new HashSet<InstallableResource>();
 
-        // configurations
-        checkPath(resourceProvider, installables, ROOT_CONFIG_PATH, InstallableResource.TYPE_PROPERTIES);
-
-        // files
-        checkPath(resourceProvider, installables, ROOT_INSTALL_PATH, InstallableResource.TYPE_FILE);
+        final Iterator<String> configPaths = resourceProvider.getChildren(ROOT_PATH);
+        if ( configPaths != null ) {
+            while ( configPaths.hasNext() ) {
+                String path = configPaths.next();
+                logger.debug("Found launchpad resource {}", path);
+                if ( path.endsWith("/") ) {
+                    path = path.substring(0, path.length() - 1);
+                }
+                final int namePos = path.lastIndexOf('/');
+                String name = path.substring(namePos + 1);
+                if ( name.equals(CONFIG_NAME) ) {
+                    // configurations
+                    checkPath(resourceProvider, activeRunModes, installables, path, InstallableResource.TYPE_PROPERTIES, PRIORITY);
+                } else if ( name.equals(INSTALL_NAME) ) {
+                    // files
+                    checkPath(resourceProvider, activeRunModes, installables, path, InstallableResource.TYPE_FILE, PRIORITY);
+                } else if ( name.startsWith(INSTALL_PREFIX) ) {
+                    final int activeModes = isActive(name.substring(INSTALL_PREFIX.length()), activeRunModes);
+                    if ( activeModes > 0 ) {
+                        // files
+                        final int prio = PRIORITY + PRIORITY_BOOST * activeModes;
+                        checkPath(resourceProvider, activeRunModes, installables, path, InstallableResource.TYPE_FILE, prio);
+                    } else {
+                        logger.debug("Ignoring path {} due to unactivated run mode", path);
+                    }
+                } else {
+                    logger.debug("Ignoring path {} - not an installation path", path);
+                }
+            }
+        }
 
         final InstallableResource [] toInstall = installables.toArray(new InstallableResource []{});
         installer.registerResources("launchpad", (toInstall));
diff --git a/src/main/java/org/apache/sling/launchpad/installer/impl/ServicesListener.java b/src/main/java/org/apache/sling/launchpad/installer/impl/ServicesListener.java
index 732b53c..febf1b1 100644
--- a/src/main/java/org/apache/sling/launchpad/installer/impl/ServicesListener.java
+++ b/src/main/java/org/apache/sling/launchpad/installer/impl/ServicesListener.java
@@ -25,6 +25,7 @@ import org.apache.sling.installer.api.OsgiInstaller;
 import org.apache.sling.installer.api.event.InstallationListener;
 import org.apache.sling.launchpad.api.LaunchpadContentProvider;
 import org.apache.sling.launchpad.api.StartupHandler;
+import org.apache.sling.settings.SlingSettingsService;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
@@ -51,6 +52,9 @@ public class ServicesListener {
     /** The listener for the startup handler. */
     private final Listener startupListener;
 
+    /** The listener for the settings service. */
+    private final Listener settingsListener;
+
     /** The registration of the launchpad listener. */
     private ServiceRegistration launchpadListenerReg;
 
@@ -67,9 +71,11 @@ public class ServicesListener {
         this.installerListener = new Listener(OsgiInstaller.class.getName());
         this.providerListener = new Listener(LaunchpadContentProvider.class.getName());
         this.startupListener = new Listener(StartupHandler.class.getName());
+        this.settingsListener = new Listener(SlingSettingsService.class.getName());
         this.startupListener.start();
         this.installerListener.start();
         this.providerListener.start();
+        this.settingsListener.start();
     }
 
     /**
@@ -82,8 +88,8 @@ public class ServicesListener {
         final OsgiInstaller installer = (OsgiInstaller)this.installerListener.getService();
         final LaunchpadContentProvider lcp = (LaunchpadContentProvider)this.providerListener.getService();
         final StartupHandler handler = (StartupHandler)this.startupListener.getService();
-
-        if ( installer != null && lcp != null && handler != null ) {
+        final SlingSettingsService settings = (SlingSettingsService)this.settingsListener.getService();
+        if ( installer != null && lcp != null && handler != null && settings != null ) {
             if ( !this.installed ) {
                 this.installed = true;
                 this.launchpadListener = new LaunchpadListener(handler);
@@ -91,7 +97,7 @@ public class ServicesListener {
                 props.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Launchpad Startup Listener");
                 props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
                 this.launchpadListenerReg = this.bundleContext.registerService(InstallationListener.class.getName(), launchpadListener, props);
-                LaunchpadConfigInstaller.install(installer, lcp);
+                LaunchpadConfigInstaller.install(installer, lcp, settings.getRunModes());
             }
         }
     }
@@ -103,6 +109,7 @@ public class ServicesListener {
         this.installerListener.deactivate();
         this.providerListener.deactivate();
         this.startupListener.deactivate();
+        this.settingsListener.deactivate();
         if ( this.launchpadListenerReg != null ) {
             this.launchpadListener.stop();
             this.launchpadListenerReg.unregister();

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