Merge pull request #4 from apache/issues/SLING-7968

SLING-7968: Use FeatureProvider instead of ArtifactManager.
diff --git a/pom.xml b/pom.xml
index 5888278..71888e5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -70,7 +70,7 @@
             <plugin>
                 <groupId>org.codehaus.plexus</groupId>
                 <artifactId>plexus-component-metadata</artifactId>
-                <version>1.5.5</version>
+                <version>1.7.1</version>
                 <executions>
                     <execution>
                         <id>generate-metadata</id>
@@ -154,6 +154,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature</artifactId>
+            <version>0.1.3-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.feature.analyser</artifactId>
             <version>0.1.3-SNAPSHOT</version>
         </dependency>
@@ -298,12 +303,6 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.feature</artifactId>
-            <version>0.1.3-SNAPSHOT</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <scope>test</scope>
diff --git a/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
index 69e1afb..e6492ea 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java
@@ -17,18 +17,24 @@
 package org.apache.sling.maven.slingstart;
 
 import org.apache.maven.MavenExecutionException;
-import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.project.MavenProject;
-import org.apache.sling.feature.io.file.ArtifactManager;
-import org.apache.sling.feature.io.file.ArtifactManagerConfig;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.builder.FeatureProvider;
+import org.apache.sling.feature.io.json.FeatureJSONReader;
 import org.apache.sling.feature.modelconverter.FeatureToProvisioning;
 import org.apache.sling.maven.slingstart.ModelPreprocessor.Environment;
 import org.apache.sling.maven.slingstart.ModelPreprocessor.ProjectInfo;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
@@ -38,31 +44,40 @@
 public class FeatureModelConverter {
     static final String BUILD_DIR = "provisioning/converted";
 
-    public static void convert(MavenSession session, Environment env) throws MavenExecutionException {
-        Map<String, ProjectInfo> projs = env.modelProjects;
-        for (ProjectInfo pi : projs.values()) {
-            convert(session, pi.project);
+    public static Feature getFeature(ArtifactId id, MavenSession session, MavenProject project, ArtifactHandlerManager manager, ArtifactResolver resolver) {
+        try {
+            File file = ModelUtils.getArtifact(project, session, manager, resolver, id.getGroupId(), id.getArtifactId(), id.getVersion(), id.getType(), id.getClassifier()).getFile();
+            try (Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8")) {
+                return FeatureJSONReader.read(reader, file.toURI().toURL().toString());
+            }
+        } catch (Exception ex) {
+            throw new RuntimeException(ex);
         }
     }
 
-    private static void convert(MavenSession session, MavenProject project) throws MavenExecutionException {
+    public static void convert(MavenSession session, Environment env) throws MavenExecutionException {
+        Map<String, ProjectInfo> projs = env.modelProjects;
+        for (ProjectInfo pi : projs.values()) {
+            convert(session, pi.project, env.artifactHandlerManager, env.resolver);
+        }
+    }
+
+    private static void convert(MavenSession session, MavenProject project, ArtifactHandlerManager manager, ArtifactResolver resolver) throws MavenExecutionException {
         File featuresDir = new File(project.getBasedir(), "src/main/features");
 
         File[] files = featuresDir.listFiles();
         if (files == null || files.length == 0)
             return;
 
-        ArtifactManager am;
         try {
-            am = getArtifactManager(project, session);
-        } catch (IOException ex) {
-            throw new MavenExecutionException("Unable to obtain artifactManager", ex);
+            convert(files, project, id -> getFeature(id, session, project, manager, resolver));
+        } catch (RuntimeException ex) {
+            throw new MavenExecutionException(ex.getMessage(), ex);
         }
 
-        convert(files, project, am);
     }
 
-    static void convert(File[] files, MavenProject project, ArtifactManager am) throws MavenExecutionException {
+    static void convert(File[] files, MavenProject project, FeatureProvider fp) throws MavenExecutionException {
         File processedFeaturesDir = new File(project.getBuild().getDirectory(), "features/processed");
         processedFeaturesDir.mkdirs();
 
@@ -88,7 +103,7 @@
                     continue;
                 }
                 File genFile = new File(targetDir, f.getName() + ".txt");
-                FeatureToProvisioning.convert(f, genFile, am, substedFiles.toArray(new File[] {}));
+                FeatureToProvisioning.convert(f, genFile, fp, substedFiles.toArray(new File[] {}));
             }
         } catch (Exception e) {
             throw new MavenExecutionException("Cannot convert feature files to provisioning model", e);
@@ -132,17 +147,4 @@
     private static String replaceAll(String s, String key, String value) {
         return s.replaceAll("\\Q${" + key + "}\\E", value);
     }
-
-    private static ArtifactManager getArtifactManager(MavenProject project, MavenSession session)
-            throws IOException {
-        List<String> repos = new ArrayList<>();
-        repos.add(session.getLocalRepository().getUrl());
-        for (ArtifactRepository ar : project.getRemoteArtifactRepositories()) {
-            repos.add(ar.getUrl());
-        }
-
-        final ArtifactManagerConfig amConfig = new ArtifactManagerConfig();
-        amConfig.setRepositoryUrls(repos.toArray(new String[] {}));
-        return ArtifactManager.getArtifactManager(amConfig);
-    }
 }
diff --git a/src/main/java/org/apache/sling/maven/slingstart/GenerateResourcesMojo.java b/src/main/java/org/apache/sling/maven/slingstart/GenerateResourcesMojo.java
index bd3a16f..296977d 100644
--- a/src/main/java/org/apache/sling/maven/slingstart/GenerateResourcesMojo.java
+++ b/src/main/java/org/apache/sling/maven/slingstart/GenerateResourcesMojo.java
@@ -18,24 +18,17 @@
 
 import org.apache.maven.MavenExecutionException;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
-import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
-import org.apache.sling.feature.io.file.ArtifactManager;
-import org.apache.sling.feature.io.file.ArtifactManagerConfig;
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 
 import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
 
 @Mojo(
         name = "generate-resources",
@@ -66,29 +59,17 @@
     protected MojoExecution mojoExecution;
 
     @Override
-    public void execute() throws MojoExecutionException, MojoFailureException {
+    public void execute() throws MojoExecutionException {
         File[] featureFiles = featuresDirectory.listFiles();
         if (featureFiles == null)
             return;
 
         try {
-            FeatureModelConverter.convert(featureFiles, project, getArtifactManager());
+            FeatureModelConverter.convert(featureFiles, project, id -> FeatureModelConverter.getFeature(id, mavenSession, project, artifactHandlerManager, resolver));
         } catch (MavenExecutionException e) {
             throw new MojoExecutionException("Cannot convert feature files to provisioning model.", e);
-        } catch (IOException e) {
+        } catch (RuntimeException e) {
             throw new MojoExecutionException("Problem obtaining artifact manager.", e);
         }
     }
-
-    private ArtifactManager getArtifactManager() throws IOException {
-        List<String> repos = new ArrayList<>();
-        repos.add(mavenSession.getLocalRepository().getUrl());
-        for (ArtifactRepository ar : project.getRemoteArtifactRepositories()) {
-            repos.add(ar.getUrl());
-        }
-
-        final ArtifactManagerConfig amConfig = new ArtifactManagerConfig();
-        amConfig.setRepositoryUrls(repos.toArray(new String[] {}));
-        return ArtifactManager.getArtifactManager(amConfig);
-    }
 }