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

rombert pushed a commit to branch issue/SLING-12356
in repository 
https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git

commit 021a9681e459650258f6f7bac9ac98480ae49f88
Author: Robert Munteanu <romb...@apache.org>
AuthorDate: Fri Jun 21 21:02:51 2024 +0200

    SLING-12356 - Allow configuring additional post process handlers
    
    Add an 'additionalPostProcessHandlers' parameters to AggregateFeaturesMojo
---
 .../feature/maven/mojos/AggregateFeaturesMojo.java | 36 +++++++++++++++++++---
 .../maven/mojos/AggregateFeaturesMojoTest.java     | 28 +++++++++++++++++
 2 files changed, 60 insertions(+), 4 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojo.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojo.java
index 53be836..727051d 100644
--- 
a/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojo.java
+++ 
b/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojo.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.feature.maven.mojos;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -23,6 +24,7 @@ import java.util.Properties;
 import java.util.ServiceLoader;
 import java.util.Spliterator;
 import java.util.Spliterators;
+import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 
 import org.apache.maven.plugin.MojoExecutionException;
@@ -60,7 +62,18 @@ public class AggregateFeaturesMojo extends 
AbstractIncludingFeatureMojo {
 
     @Parameter
     Map<String, Properties> handlerConfiguration = new HashMap<>();
-
+    
+    /**
+     * Additional post process handlers to use when aggregating
+     * 
+     * <p>Normally handlers are discovered using the {@link ServiceLoader} 
mechanism but some
+     * special-purpose handlers are not registered by default.</p>
+     * 
+     * <p>These handlers will be installed after the ones discovered by the 
{@link ServiceLoader}.</p>
+     */
+    @Parameter
+    List<String> additionalPostProcessHandlers = new ArrayList<>();
+    
     @Override
     public void execute() throws MojoExecutionException {
         checkPreconditions();
@@ -121,9 +134,7 @@ public class AggregateFeaturesMojo extends 
AbstractIncludingFeatureMojo {
                 
.addMergeExtensions(StreamSupport.stream(Spliterators.spliteratorUnknownSize(
                     ServiceLoader.load(MergeHandler.class).iterator(), 
Spliterator.ORDERED),
                     false).toArray(MergeHandler[]::new))
-                
.addPostProcessExtensions(StreamSupport.stream(Spliterators.spliteratorUnknownSize(
-                    ServiceLoader.load(PostProcessHandler.class).iterator(), 
Spliterator.ORDERED),
-                    false).toArray(PostProcessHandler[]::new));
+                
.addPostProcessExtensions(postProcessHandlers().toArray(PostProcessHandler[]::new));
             for (final ArtifactId rule : aggregate.getArtifactOverrideRules()) 
{
                 builderContext.addArtifactsOverride(rule);
             }
@@ -169,6 +180,23 @@ public class AggregateFeaturesMojo extends 
AbstractIncludingFeatureMojo {
         }
     }
 
+    private Stream<PostProcessHandler> postProcessHandlers() {
+        
+        Stream<PostProcessHandler> serviceLoaderHandlers = 
StreamSupport.stream(Spliterators.spliteratorUnknownSize(
+            ServiceLoader.load(PostProcessHandler.class).iterator(), 
Spliterator.ORDERED), false);
+
+        Stream<PostProcessHandler> additionalHandlers = 
this.additionalPostProcessHandlers.stream()
+            .map( extension -> {
+                try {
+                    return (PostProcessHandler) 
Class.forName(extension).getDeclaredConstructor().newInstance();
+                } catch (Exception e) {
+                    throw new RuntimeException("Unable to instantiate 
post-process extension " + extension, e);
+                }
+            });
+        
+        return Stream.concat(serviceLoaderHandlers, additionalHandlers);
+    }
+
     Feature assembleFeature(final ArtifactId newFeatureID, final 
BuilderContext builderContext,
             final Map<String, Feature> selection) throws 
MojoExecutionException {
         try {
diff --git 
a/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
 
b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
index 4448acb..1866fb6 100644
--- 
a/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
+++ 
b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesMojoTest.java
@@ -40,6 +40,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.BiConsumer;
 
 import org.apache.maven.artifact.Artifact;
@@ -54,8 +55,11 @@ import org.apache.maven.project.DefaultMavenProjectHelper;
 import org.apache.maven.project.MavenProject;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Bundles;
+import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.builder.BuilderContext;
+import org.apache.sling.feature.builder.HandlerContext;
+import org.apache.sling.feature.builder.PostProcessHandler;
 import org.apache.sling.feature.io.json.FeatureJSONReader;
 import org.apache.sling.feature.maven.FeatureConstants;
 import org.apache.sling.feature.maven.Preprocessor;
@@ -801,6 +805,20 @@ public class AggregateFeaturesMojoTest {
             assertEquals("More than one feature file for classifier myagg in 
project test.aggregate.project1 : [aggregate myagg, aggregate myagg]", 
e.getMessage());
         }
     }
+    
+    @Test
+    public void customAggregate() throws Exception {
+        
+        TestContext ctx = prepareTestContext("/aggregate-features/dir2", 
+                new String[] { "test_w.json", "test_y.json" }, // control the 
number of aggregations
+                (agg, mojo) -> 
mojo.additionalPostProcessHandlers.add(SpyAggregateHandler.class.getTypeName()) 
);
+        
+        ctx.getMojo().execute();
+        
+        // we expect 2 invocations because we have 2 feature files as input 
+        assertEquals("Additional postProcessHandler invocation count", 2, 
SpyAggregateHandler.invocationCount.get());
+        
+    }
 
     private Artifact createMockArtifact() {
         Artifact parentArtifact = Mockito.mock(Artifact.class);
@@ -874,4 +892,14 @@ public class AggregateFeaturesMojoTest {
             return featureMap;
         }
     }
+    
+    public static class SpyAggregateHandler implements PostProcessHandler {
+        
+        static final AtomicInteger invocationCount = new AtomicInteger(0);
+
+        @Override
+        public void postProcess(HandlerContext context, Feature feature, 
Extension extension) {
+            invocationCount.incrementAndGet();
+        }
+    }
 }

Reply via email to