You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by pa...@apache.org on 2018/07/17 08:07:39 UTC

[sling-org-apache-sling-feature-launcher] branch master updated: Add a download artifacts only option (-dao ) that allows to prepopulate a cache

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

pauls pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-launcher.git


The following commit(s) were added to refs/heads/master by this push:
     new 7a37215  Add a download artifacts only option (-dao <dir>) that allows to prepopulate a cache
7a37215 is described below

commit 7a37215c6ee3e260d7d6381a55549ce9175cdc8e
Author: Karl Pauls <ka...@gmail.com>
AuthorDate: Tue Jul 17 10:07:29 2018 +0200

    Add a download artifacts only option (-dao <dir>) that allows to prepopulate a cache
---
 .../feature/launcher/impl/FeatureProcessor.java    | 34 +++++++++++++++
 .../apache/sling/feature/launcher/impl/Main.java   | 50 ++++++++++++++++++++--
 2 files changed, 81 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/launcher/impl/FeatureProcessor.java b/src/main/java/org/apache/sling/feature/launcher/impl/FeatureProcessor.java
index 1ca2107..cbdd069 100644
--- a/src/main/java/org/apache/sling/feature/launcher/impl/FeatureProcessor.java
+++ b/src/main/java/org/apache/sling/feature/launcher/impl/FeatureProcessor.java
@@ -33,6 +33,7 @@ import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.StringReader;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -158,4 +159,37 @@ public class FeatureProcessor {
             }
         }
     }
+
+    /**
+     * Prepare the cache
+     * - add all bundles
+     * - add all other artifacts (only if startup mode is INSTALL)
+     */
+    public static Map<Artifact, File> calculateArtifacts(final ArtifactManager artifactManager,
+        final Application app) throws Exception
+    {
+        Map<Artifact, File> result = new HashMap<>();
+        for (final Map.Entry<Integer, List<Artifact>> entry : app.getBundles().getBundlesByStartOrder().entrySet())
+        {
+            for (final Artifact a : entry.getValue())
+            {
+                final ArtifactHandler handler = artifactManager.getArtifactHandler(":" + a.getId().toMvnPath());
+                final File artifactFile = handler.getFile();
+
+                result.put(a, artifactFile);
+            }
+        }
+        for (final Extension ext : app.getExtensions())
+        {
+            if (ext.getType() == ExtensionType.ARTIFACTS)
+            {
+                for (final Artifact a : ext.getArtifacts())
+                {
+                    final ArtifactHandler handler = artifactManager.getArtifactHandler(":" + a.getId().toMvnPath());
+                    result.put(a, handler.getFile());
+                }
+            }
+        }
+        return result;
+    }
 }
diff --git a/src/main/java/org/apache/sling/feature/launcher/impl/Main.java b/src/main/java/org/apache/sling/feature/launcher/impl/Main.java
index 7b2c7ca..86cb017 100644
--- a/src/main/java/org/apache/sling/feature/launcher/impl/Main.java
+++ b/src/main/java/org/apache/sling/feature/launcher/impl/Main.java
@@ -24,6 +24,7 @@ import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.sling.feature.Application;
+import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.io.ArtifactHandler;
 import org.apache.sling.feature.io.ArtifactManager;
@@ -38,9 +39,12 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 
 /**
  * This is the launcher main class.
@@ -57,6 +61,8 @@ public class Main {
         return LOGGER;
     }
 
+    private static volatile File m_populate;
+
     /** Split a string into key and value */
     private static String[] split(final String val) {
         final int pos = val.indexOf('=');
@@ -84,6 +90,8 @@ public class Main {
         installerOption.setArgs(0);
         final Option cacheOption = new Option("c", true, "Set cache dir");
         final Option homeOption = new Option("p", true, "Set home dir");
+        final Option populateOption = new Option("dao", true, "Only download required artifacts into directory");
+
         options.addOption(repoOption);
         options.addOption(appOption);
         options.addOption(fwkProperties);
@@ -92,6 +100,7 @@ public class Main {
         options.addOption(installerOption);
         options.addOption(cacheOption);
         options.addOption(homeOption);
+        options.addOption(populateOption);
 
         final CommandLineParser clp = new BasicParser();
         try {
@@ -130,6 +139,12 @@ public class Main {
             if (cl.hasOption(homeOption.getOpt())) {
                 config.setHomeDirectory(new File(cl.getOptionValue(homeOption.getOpt())));
             }
+            if (cl.hasOption(populateOption.getOpt())) {
+                m_populate = new File(cl.getOptionValue(populateOption.getOpt()));
+                if (!m_populate.isDirectory() && !m_populate.mkdirs()) {
+                    throw new ParseException("Bad dao directory");
+                }
+            }
         } catch ( final ParseException pe) {
             Main.LOG().error("Unable to parse command line: {}", pe.getMessage(), pe);
 
@@ -187,6 +202,18 @@ public class Main {
                     @Override
                     public File getArtifactFile(final ArtifactId artifact) throws IOException {
                         final ArtifactHandler handler = aMgr.getArtifactHandler(":" + artifact.toMvnPath());
+                        if (m_populate != null) {
+                            File source = handler.getFile();
+                            File target = new File(m_populate, artifact.toMvnPath().replace('/', File.separatorChar));
+
+                            if (!target.isFile()) {
+                                if (Main.LOG().isDebugEnabled()) {
+                                    Main.LOG().debug("Populating {} with {}", target.getAbsolutePath(), source.getAbsolutePath());
+                                }
+                                target.getParentFile().mkdirs();
+                                Files.copy(source.toPath(), target.toPath());
+                            }
+                        }
                         return handler.getFile();
                     }
 
@@ -199,13 +226,30 @@ public class Main {
 
                 FeatureProcessor.prepareLauncher(launcherConfig, artifactManager, app);
 
+                Main.LOG().info("Using {} local artifacts, {} cached artifacts, and {} downloaded artifacts",
+                    launcherConfig.getLocalArtifacts(), launcherConfig.getCachedArtifacts(), launcherConfig.getDownloadedArtifacts());
+
+                if (m_populate != null) {
+                    Map<Artifact, File> local = FeatureProcessor.calculateArtifacts(artifactManager, app);
+                    for (Map.Entry<Artifact, File> entry : local.entrySet()) {
+                        File source = entry.getValue();
+                        File target = new File(m_populate, entry.getKey().getId().toMvnPath().replace('/', File.separatorChar));
+
+                        if (!target.isFile()) {
+                            if (Main.LOG().isDebugEnabled()) {
+                                Main.LOG().debug("Populating {} with {}", target.getAbsolutePath(), source.getAbsolutePath());
+                            }
+                            target.getParentFile().mkdirs();
+                            Files.copy(source.toPath(), target.toPath());
+                        }
+                    }
+                    return;
+                }
+
             } catch ( final Exception iae) {
                 Main.LOG().error("Error while assembling launcher: {}", iae.getMessage(), iae);
                 System.exit(1);
             }
-
-            Main.LOG().info("Using {} local artifacts, {} cached artifacts, and {} downloaded artifacts",
-                    launcherConfig.getLocalArtifacts(), launcherConfig.getCachedArtifacts(), launcherConfig.getDownloadedArtifacts());
         } finally {
             if ( artifactManager != null ) {
                 artifactManager.shutdown();