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

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


The following commit(s) were added to refs/heads/master by this push:
     new c98017b  SLING-12303 : Global transformers are not sorted by service 
ranking
c98017b is described below

commit c98017bc16ee3355dcd00fe080b9c93e387f18dd
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Wed Apr 24 14:07:13 2024 +0200

    SLING-12303 : Global transformers are not sorted by service ranking
---
 .../org/apache/sling/rewriter/impl/FactoryCache.java  | 19 +++++++++++++++++--
 .../rewriter/impl/ProcessorConfigurationImpl.java     |  9 +++++----
 .../apache/sling/rewriter/impl/RewriterResponse.java  |  2 +-
 .../impl/TransformerFactoryServiceTracker.java        |  6 ++++++
 4 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java 
b/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java
index de4b8c6..8d7331b 100644
--- a/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java
+++ b/src/main/java/org/apache/sling/rewriter/impl/FactoryCache.java
@@ -27,6 +27,7 @@ import org.apache.sling.rewriter.SerializerFactory;
 import org.apache.sling.rewriter.Transformer;
 import org.apache.sling.rewriter.TransformerFactory;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.util.converter.Converter;
@@ -226,11 +227,13 @@ public class FactoryCache {
         return transformers;
     }
 
-    static final class TransformerFactoryEntry {
+    static final class TransformerFactoryEntry implements 
Comparable<TransformerFactoryEntry> {
         public final TransformerFactory factory;
 
         public final ProcessorConfiguration configuration;
 
+        public final int order;
+
         public TransformerFactoryEntry(final TransformerFactory factory, final 
ServiceReference<TransformerFactory> ref) {
             this.factory = factory;
             final Converter c = Converters.standardConverter();
@@ -246,8 +249,15 @@ public class FactoryCache {
                                    (resourceTypes == null || 
resourceTypes.length == 0) &&
                                    (selectors == null || selectors.length == 
0) &&
                                    !processError;
+
+            int order = 0;
+            final Object obj = ref.getProperty(Constants.SERVICE_RANKING);
+            if (obj instanceof Integer) {
+                order = (Integer)obj;
+            }
+            this.order = order;
             if ( !noCheckRequired ) {
-                this.configuration = new 
ProcessorConfigurationImpl(contentTypes, paths, extensions, resourceTypes, 
selectors, processError);
+                this.configuration = new 
ProcessorConfigurationImpl(contentTypes, paths, extensions, resourceTypes, 
selectors, order, processError);
             } else {
                 this.configuration = null;
             }
@@ -259,5 +269,10 @@ public class FactoryCache {
             }
             return configuration.match(context);
         }
+
+        @Override
+        public int compareTo(final TransformerFactoryEntry o) {
+            return this.order - o.order;
+        }
     }
 }
diff --git 
a/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java 
b/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java
index d0845c6..df5f036 100644
--- 
a/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java
+++ 
b/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java
@@ -124,6 +124,7 @@ public class ProcessorConfigurationImpl implements 
PipelineConfiguration {
                                       final String[] extensions,
                                       final String[] resourceTypes,
                                       final String[] selectors,
+                                      final int order,
                                       final boolean processError) {
         this.name = null;
         this.contentTypes = contentTypes != null && contentTypes.length == 0 ? 
null : contentTypes;
@@ -132,7 +133,7 @@ public class ProcessorConfigurationImpl implements 
PipelineConfiguration {
         this.extensions = extensions != null && extensions.length == 0 ? null 
: extensions;
         this.paths = paths != null && paths.length == 0 ? null : paths;
         this.unwrapResources = false;
-        this.order = 0;
+        this.order = order;
         this.generatorConfiguration = null;
         this.transformerConfigurations = null;
         this.serializerConfiguration = null;
@@ -214,9 +215,9 @@ public class ProcessorConfigurationImpl implements 
PipelineConfiguration {
         }
         pw.print("Process Error Response : ");
         pw.println(this.processErrorResponse);
-        if (this.name != null) {
-            pw.print("Order : ");
-            pw.println(this.order);
+        pw.print("Order : ");
+        pw.println(this.order);
+    if (this.name != null) {
             pw.print("Active : ");
             pw.println(this.isActive);
             pw.print("Valid : ");
diff --git a/src/main/java/org/apache/sling/rewriter/impl/RewriterResponse.java 
b/src/main/java/org/apache/sling/rewriter/impl/RewriterResponse.java
index cf32931..03769a0 100644
--- a/src/main/java/org/apache/sling/rewriter/impl/RewriterResponse.java
+++ b/src/main/java/org/apache/sling/rewriter/impl/RewriterResponse.java
@@ -170,7 +170,7 @@ class RewriterResponse
             if ( config.match(processorContext) ) {
                 try {
                     found = this.processorManager.getProcessor(config, 
processorContext);
-                    this.request.getRequestProgressTracker().log("Found 
processor for post processing {0}", config);
+                    this.request.getRequestProgressTracker().log("Found 
processor for config {0} : {1}", config, found);
                 } catch (final SlingException se) {
                     // if an exception occurs during setup of the pipeline and 
we are currently
                     // already processing an error, we ignore this!
diff --git 
a/src/main/java/org/apache/sling/rewriter/impl/TransformerFactoryServiceTracker.java
 
b/src/main/java/org/apache/sling/rewriter/impl/TransformerFactoryServiceTracker.java
index de28364..45f0cc7 100644
--- 
a/src/main/java/org/apache/sling/rewriter/impl/TransformerFactoryServiceTracker.java
+++ 
b/src/main/java/org/apache/sling/rewriter/impl/TransformerFactoryServiceTracker.java
@@ -135,6 +135,12 @@ final class TransformerFactoryServiceTracker extends 
HashingServiceTrackerCustom
                                 index++;
                             }
                         }
+                        if (globalFactories[0].length > 1) {
+                            Arrays.sort(globalFactories[0]);
+                        }
+                        if (globalFactories[1].length > 1) {
+                            Arrays.sort(globalFactories[1]);
+                        }
                         this.cached = globalFactories;
                     }
                     this.currentTrackingCount = this.getTrackingCount();

Reply via email to