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 2019/05/16 13:14:16 UTC

[sling-org-apache-sling-feature-launcher] branch issues/SLING-8421 created (now 8aed5f5)

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

pauls pushed a change to branch issues/SLING-8421
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-launcher.git.


      at 8aed5f5  SLING-8421: Allow artifact providers that work with URLs instead of Files.

This branch includes the following new commits:

     new 8aed5f5  SLING-8421: Allow artifact providers that work with URLs instead of Files.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-org-apache-sling-feature-launcher] 01/01: SLING-8421: Allow artifact providers that work with URLs instead of Files.

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 8aed5f519691a268eed55444e54df60685bf63d0
Author: Karl Pauls <ka...@gmail.com>
AuthorDate: Thu May 16 15:14:03 2019 +0200

    SLING-8421: Allow artifact providers that work with URLs instead of Files.
---
 .../sling/feature/launcher/impl/Bootstrap.java     |   6 +
 .../launcher/impl/ExtensionContextImpl.java        |  19 ++
 .../feature/launcher/impl/FeatureProcessor.java    |  48 +-----
 .../sling/feature/launcher/impl/Installation.java  | 192 ++++++++++++++++++++-
 .../launcher/impl/launchers/AbstractRunner.java    |   2 -
 .../sling/feature/launcher/spi/Launcher.java       |   2 +-
 .../launcher/spi/LauncherPrepareContext.java       |   3 +
 .../feature/launcher/spi/LauncherRunContext.java   |  14 ++
 8 files changed, 235 insertions(+), 51 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/launcher/impl/Bootstrap.java b/src/main/java/org/apache/sling/feature/launcher/impl/Bootstrap.java
index d950a6b..288dbed 100644
--- a/src/main/java/org/apache/sling/feature/launcher/impl/Bootstrap.java
+++ b/src/main/java/org/apache/sling/feature/launcher/impl/Bootstrap.java
@@ -147,6 +147,12 @@ public class Bootstrap {
                     }
 
                     @Override
+                    public ArtifactHandler getArtifactHandler(ArtifactId artifactId) throws IOException
+                    {
+                        return artifactManager.getArtifactHandler(":" + artifactId.toMvnPath());
+                    }
+
+                    @Override
                     public void addAppJar(final File jar) {
                         try {
                             config.getInstallation().addAppJar(jar.toURI().toURL());
diff --git a/src/main/java/org/apache/sling/feature/launcher/impl/ExtensionContextImpl.java b/src/main/java/org/apache/sling/feature/launcher/impl/ExtensionContextImpl.java
index 8f3bb6d..6315da8 100644
--- a/src/main/java/org/apache/sling/feature/launcher/impl/ExtensionContextImpl.java
+++ b/src/main/java/org/apache/sling/feature/launcher/impl/ExtensionContextImpl.java
@@ -26,6 +26,7 @@ import java.util.Map;
 
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.file.ArtifactHandler;
 import org.apache.sling.feature.io.json.FeatureJSONReader;
 import org.apache.sling.feature.launcher.spi.LauncherPrepareContext;
 import org.apache.sling.feature.launcher.spi.extensions.ExtensionContext;
@@ -80,6 +81,12 @@ class ExtensionContextImpl implements ExtensionContext {
     }
 
     @Override
+    public Map<Integer, List<ArtifactHandler>> getBundleArtifactMap()
+    {
+        return installation.getBundleArtifactMap();
+    }
+
+    @Override
     public List<Object[]> getConfigurations() {
         return installation.getConfigurations();
     }
@@ -90,6 +97,12 @@ class ExtensionContextImpl implements ExtensionContext {
     }
 
     @Override
+    public List<ArtifactHandler> getInstallableArtifactHandlers()
+    {
+        return installation.getInstallableArtifactHandlers();
+    }
+
+    @Override
     public void addAppJar(File jar) {
         prepareContext.addAppJar(jar);
     }
@@ -105,6 +118,12 @@ class ExtensionContextImpl implements ExtensionContext {
     }
 
     @Override
+    public ArtifactHandler getArtifactHandler(ArtifactId artifactId) throws IOException
+    {
+        return prepareContext.getArtifactHandler(artifactId);
+    }
+
+    @Override
     public Feature getFeature(ArtifactId artifact) throws IOException {
         Feature f = loadedFeatures.get(artifact);
         if (f != null)
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 175d209..092424a 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
@@ -16,10 +16,10 @@
  */
 package org.apache.sling.feature.launcher.impl;
 
-import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
-import java.util.HashMap;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.util.List;
 import java.util.Map;
 import java.util.ServiceLoader;
@@ -31,7 +31,6 @@ import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Configuration;
 import org.apache.sling.feature.Extension;
-import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.builder.BuilderContext;
 import org.apache.sling.feature.builder.FeatureBuilder;
@@ -62,7 +61,7 @@ public class FeatureProcessor {
         final BuilderContext builderContext = new BuilderContext(id -> {
             try {
                 final ArtifactHandler handler = artifactManager.getArtifactHandler(id.toMvnUrl());
-                try (final FileReader r = new FileReader(handler.getFile())) {
+                try (final Reader r = new InputStreamReader(handler.getLocalURL().openStream(), "UTF-8")) {
                     final Feature f = FeatureJSONReader.read(r, handler.getUrl());
                     return f;
                 }
@@ -134,8 +133,7 @@ public class FeatureProcessor {
             final Feature app, Map<ArtifactId, Feature> loadedFeatures) throws Exception {
         for(final Map.Entry<Integer, List<Artifact>> entry : app.getBundles().getBundlesByStartOrder().entrySet()) {
             for(final Artifact a : entry.getValue()) {
-                final File artifactFile = ctx.getArtifactFile(a.getId());
-
+                final ArtifactHandler artifactFile = ctx.getArtifactHandler(a.getId());
                 config.getInstallation().addBundle(entry.getKey(), artifactFile);
             }
         }
@@ -167,42 +165,4 @@ public class FeatureProcessor {
             }
         }
     }
-
-    /**
-     * Prepare the cache
-     * - add all bundles
-     * - add all other artifacts (only if startup mode is INSTALL)
-     * @param artifactManager The artifact manager
-     * @param app the feature with the artifacts
-     * @return An Artifact to File mapping.
-     * @throws Exception when something goes wrong.
-     */
-    public static Map<Artifact, File> calculateArtifacts(final ArtifactManager artifactManager,
-        final Feature 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
-                    && ext.getName().equals(Extension.EXTENSION_NAME_CONTENT_PACKAGES))
-            {
-                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/Installation.java b/src/main/java/org/apache/sling/feature/launcher/impl/Installation.java
index 483359e..f9f6e0b 100644
--- a/src/main/java/org/apache/sling/feature/launcher/impl/Installation.java
+++ b/src/main/java/org/apache/sling/feature/launcher/impl/Installation.java
@@ -17,13 +17,20 @@
 package org.apache.sling.feature.launcher.impl;
 
 import java.io.File;
+import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.AbstractList;
+import java.util.AbstractMap;
+import java.util.AbstractSet;
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
+import org.apache.sling.feature.io.file.ArtifactHandler;
 import org.apache.sling.feature.launcher.spi.LauncherRunContext;
 import org.slf4j.Logger;
 
@@ -36,10 +43,10 @@ public class Installation implements LauncherRunContext {
     private final Map<String, String> fwkProperties = new HashMap<>();
 
     /** Bundle map */
-    private final Map<Integer, List<File>> bundleMap = new HashMap<>();
+    private final Map<Integer, List<ArtifactHandler>> bundleMap = new HashMap<>();
 
     /** Artifacts to be installed */
-    private final List<File> installables = new ArrayList<>();
+    private final List<ArtifactHandler> installables = new ArrayList<>();
 
     /** Configurations, they are installed on first start. */
     private final List<Object[]> configurations = new ArrayList<>();
@@ -71,7 +78,21 @@ public class Installation implements LauncherRunContext {
      * @param file The bundle file
      */
     public void addBundle(final Integer startLevel, final File file) {
-        List<File> files = bundleMap.get(startLevel);
+        List<ArtifactHandler> files = bundleMap.get(startLevel);
+        if ( files == null ) {
+            files = new ArrayList<>();
+            bundleMap.put(startLevel, files);
+        }
+        files.add(convert(file));
+    }
+
+    /**
+     * Add a bundle with the given start level
+     * @param startLevel The start level
+     * @param file The bundle file
+     */
+    public void addBundle(final Integer startLevel, final ArtifactHandler file) {
+        List<ArtifactHandler> files = bundleMap.get(startLevel);
         if ( files == null ) {
             files = new ArrayList<>();
             bundleMap.put(startLevel, files);
@@ -84,7 +105,7 @@ public class Installation implements LauncherRunContext {
      * @param file The file
      */
     public void addInstallableArtifact(final File file) {
-        this.installables.add(file);
+        this.installables.add(convert(file));
     }
 
     /**
@@ -115,6 +136,12 @@ public class Installation implements LauncherRunContext {
      */
     @Override
     public Map<Integer, List<File>> getBundleMap() {
+        return convert(this.bundleMap);
+    }
+
+    @Override
+    public Map<Integer, List<ArtifactHandler>> getBundleArtifactMap()
+    {
         return this.bundleMap;
     }
 
@@ -131,6 +158,12 @@ public class Installation implements LauncherRunContext {
      */
     @Override
     public List<File> getInstallableArtifacts() {
+        return convertHandlers(this.installables);
+    }
+
+    @Override
+    public List<ArtifactHandler> getInstallableArtifactHandlers()
+    {
         return this.installables;
     }
 
@@ -152,4 +185,155 @@ public class Installation implements LauncherRunContext {
     public Logger getLogger() {
         return this.logger;
     }
+
+    private ArtifactHandler convert(File file) {
+        if (file == null) {
+            return null;
+        }
+        try
+        {
+            return new ArtifactHandler(file.toURI().toURL().toExternalForm(), file);
+        }
+        catch (MalformedURLException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private List<File> convertHandlers(List<ArtifactHandler> list) {
+        if (list == null) {
+            return null;
+        }
+        return new AbstractList<File>()
+        {
+            @Override
+            public File get(int index)
+            {
+                return list.get(index).getFile();
+            }
+
+            @Override
+            public int size()
+            {
+                return list.size();
+            }
+
+            @Override
+            public File remove(int index)
+            {
+                ArtifactHandler handler = list.remove(index);
+                return handler != null ? handler.getFile() : null;
+            }
+
+            @Override
+            public File set(int index, File element)
+            {
+                ArtifactHandler handler = list.set(index, convert(element));
+                return handler != null ? handler.getFile() : null;
+            }
+
+            @Override
+            public void add(int index, File element)
+            {
+                list.add(index, convert(element));
+            }
+        };
+    }
+
+    private List<ArtifactHandler> convertFiles(List<File> list) {
+        if (list == null) {
+            return null;
+        }
+        return new AbstractList<ArtifactHandler>()
+        {
+            @Override
+            public ArtifactHandler get(int index)
+            {
+                return convert(list.get(index));
+            }
+
+            @Override
+            public int size()
+            {
+                return list.size();
+            }
+
+            @Override
+            public ArtifactHandler remove(int index)
+            {
+                return convert(list.remove(index));
+            }
+
+            @Override
+            public ArtifactHandler set(int index, ArtifactHandler element)
+            {
+                return convert(list.set(index, element != null ? element.getFile() : null));
+            }
+
+            @Override
+            public void add(int index, ArtifactHandler element)
+            {
+                list.add(index, element != null ? element.getFile() : null);
+            }
+        };
+    }
+
+    private Map<Integer, List<File>> convert(Map<Integer, List<ArtifactHandler>> map) {
+        return new AbstractMap<Integer, List<File>>()
+        {
+            @Override
+            public Set<Entry<Integer, List<File>>> entrySet()
+            {
+                Set<Map.Entry<Integer, List<ArtifactHandler>>> set = map.entrySet();
+                return new AbstractSet<Entry<Integer, List<File>>>()
+                {
+                    @Override
+                    public Iterator<Entry<Integer, List<File>>> iterator()
+                    {
+                        Iterator<Map.Entry<Integer, List<ArtifactHandler>>> iter = set.iterator();
+                        return new Iterator<Entry<Integer, List<File>>>()
+                        {
+                            @Override
+                            public boolean hasNext()
+                            {
+                                return iter.hasNext();
+                            }
+
+                            @Override
+                            public Entry<Integer, List<File>> next()
+                            {
+                                Entry<Integer, List<ArtifactHandler>> entry = iter.next();
+                                return new AbstractMap.SimpleEntry<Integer, List<File>>(entry.getKey(), convertHandlers(entry.getValue())) {
+                                    @Override
+                                    public List<File> setValue(List<File> value)
+                                    {
+                                        entry.setValue(convertFiles(value));
+                                        return super.setValue(value);
+                                    }
+                                };
+                            }
+
+                            @Override
+                            public void remove()
+                            {
+                                iter.remove();
+                            }
+                        };
+                    }
+
+                    @Override
+                    public int size()
+                    {
+                        return set.size();
+                    }
+                };
+            }
+
+            @Override
+            public List<File> put(Integer key, List<File> value)
+            {
+                return convertHandlers(map.put(key, convertFiles(value)));
+            }
+        };
+    }
 }
diff --git a/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java b/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java
index b4d4275..b81a152 100644
--- a/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java
+++ b/src/main/java/org/apache/sling/feature/launcher/impl/launchers/AbstractRunner.java
@@ -44,8 +44,6 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
-import org.osgi.framework.FrameworkEvent;
-import org.osgi.framework.FrameworkListener;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.launch.Framework;
 import org.osgi.framework.startlevel.BundleStartLevel;
diff --git a/src/main/java/org/apache/sling/feature/launcher/spi/Launcher.java b/src/main/java/org/apache/sling/feature/launcher/spi/Launcher.java
index 79aa1dc..9452f8f 100644
--- a/src/main/java/org/apache/sling/feature/launcher/spi/Launcher.java
+++ b/src/main/java/org/apache/sling/feature/launcher/spi/Launcher.java
@@ -38,7 +38,7 @@ public interface Launcher {
         }
 
         @Override
-        public final void addURL(URL url) {
+        public void addURL(URL url) {
             super.addURL(url);
         }
 
diff --git a/src/main/java/org/apache/sling/feature/launcher/spi/LauncherPrepareContext.java b/src/main/java/org/apache/sling/feature/launcher/spi/LauncherPrepareContext.java
index 624b9fd..79333c0 100644
--- a/src/main/java/org/apache/sling/feature/launcher/spi/LauncherPrepareContext.java
+++ b/src/main/java/org/apache/sling/feature/launcher/spi/LauncherPrepareContext.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.net.URL;
 
 import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.io.file.ArtifactHandler;
 import org.slf4j.Logger;
 
 /**
@@ -40,4 +41,6 @@ public interface LauncherPrepareContext {
     void addAppJar(URL url);
 
     File getArtifactFile(ArtifactId artifact) throws IOException;
+
+    ArtifactHandler getArtifactHandler(ArtifactId artifactId) throws IOException;
 }
diff --git a/src/main/java/org/apache/sling/feature/launcher/spi/LauncherRunContext.java b/src/main/java/org/apache/sling/feature/launcher/spi/LauncherRunContext.java
index c2ec088..89e6fc9 100644
--- a/src/main/java/org/apache/sling/feature/launcher/spi/LauncherRunContext.java
+++ b/src/main/java/org/apache/sling/feature/launcher/spi/LauncherRunContext.java
@@ -20,6 +20,8 @@ import java.io.File;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.io.file.ArtifactHandler;
 import org.slf4j.Logger;
 
 /**
@@ -40,6 +42,12 @@ public interface LauncherRunContext {
     Map<Integer, List<File>> getBundleMap();
 
     /**
+     * Bundle map, key is the start level, value is a list of handlers.
+     * @return The bundle map, might be empty
+     */
+    Map<Integer, List<ArtifactHandler>> getBundleArtifactMap();
+
+    /**
      * List of configurations.
      * The value in each is an object array with three values
      * <ol>
@@ -59,6 +67,12 @@ public interface LauncherRunContext {
     List<File> getInstallableArtifacts();
 
     /**
+     * List of installable artifacts.
+     * @return The list of files. The list might be empty.
+     */
+    List<ArtifactHandler> getInstallableArtifactHandlers();
+
+    /**
      * Get the central logger
      * 
      * @return The logger