sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sseif...@apache.org
Subject svn commit: r1772889 - in /sling/trunk/contrib/extensions/contextaware-config/impl/src: main/java/org/apache/sling/caconfig/resource/impl/def/ main/java/org/apache/sling/caconfig/resource/impl/util/ test/java/org/apache/sling/caconfig/resource/impl/util/
Date Tue, 06 Dec 2016 13:20:43 GMT
Author: sseifert
Date: Tue Dec  6 13:20:43 2016
New Revision: 1772889

URL: http://svn.apache.org/viewvc?rev=1772889&view=rev
Log:
SLING-6137 filter out context resources without configref for default strategy, and accept
context resources with different config refs in context path multiplexer

Modified:
    sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java
    sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIterator.java
    sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIterator.java
    sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java
    sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIteratorTest.java

Modified: sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java?rev=1772889&r1=1772888&r2=1772889&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java
(original)
+++ sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java
Tue Dec  6 13:20:43 2016
@@ -31,9 +31,11 @@ import java.util.NoSuchElementException;
 import java.util.Set;
 
 import org.apache.commons.collections.IteratorUtils;
+import org.apache.commons.collections.Predicate;
 import org.apache.commons.collections.PredicateUtils;
 import org.apache.commons.collections.Transformer;
 import org.apache.commons.collections.iterators.ArrayIterator;
+import org.apache.commons.collections.iterators.FilterIterator;
 import org.apache.commons.collections.iterators.IteratorChain;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -130,8 +132,15 @@ public class DefaultConfigurationResourc
     private Iterator<String> findConfigRefs(final Resource startResource, final String
bucketName) {
         final String notAllowedPostfix = "/" + bucketName;
 
-        // collect all context path resources
-        final Iterator<ContextResource> contextResources = contextPathStrategy.findContextResources(startResource);
+        // collect all context path resources (but filter out those without config reference)
+        final Iterator<ContextResource> contextResources = new FilterIterator(contextPathStrategy.findContextResources(startResource),
+                new Predicate() {
+                    @Override
+                    public boolean evaluate(Object object) {
+                        ContextResource contextResource = (ContextResource)object;
+                        return StringUtils.isNotBlank(contextResource.getConfigRef());
+                    }
+                });
 
         // get config resource path for each context resource, filter out items where not
reference could be resolved
         final Iterator<String> configPaths = new Iterator<String>() {

Modified: sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIterator.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIterator.java?rev=1772889&r1=1772888&r2=1772889&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIterator.java
(original)
+++ sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIterator.java
Tue Dec  6 13:20:43 2016
@@ -24,6 +24,7 @@ import java.util.Set;
 
 import org.apache.commons.collections.Predicate;
 import org.apache.commons.collections.iterators.FilterIterator;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.caconfig.resource.spi.ContextResource;
 
 /**
@@ -33,11 +34,13 @@ public class ResourceEliminateDuplicates
 
     public ResourceEliminateDuplicatesIterator(Iterator<ContextResource> iterator)
{
         super(iterator, new Predicate() {
-            private final Set<String> resourcePaths = new HashSet<>();
+            private final Set<String> keys = new HashSet<>();
             
             @Override
             public boolean evaluate(Object object) {
-                return resourcePaths.add(((ContextResource)object).getResource().getPath());
+                ContextResource contextResource = (ContextResource)object;
+                String key = contextResource.getResource().getPath() + "#" + StringUtils.defaultString(contextResource.getConfigRef());
+                return keys.add(key);
             }
             
         });

Modified: sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIterator.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIterator.java?rev=1772889&r1=1772888&r2=1772889&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIterator.java
(original)
+++ sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIterator.java
Tue Dec  6 13:20:43 2016
@@ -23,6 +23,7 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.apache.commons.collections.iterators.CollatingIterator;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.caconfig.resource.spi.ContextResource;
 
 /**
@@ -37,7 +38,11 @@ public class ResourcePathCollatingIterat
         public int compare(ContextResource o1, ContextResource o2) {
             Integer length1 = o1.getResource().getPath().length();
             Integer length2 = o2.getResource().getPath().length();
-            return length2.compareTo(length1);
+            int result = length2.compareTo(length1);
+            if (result == 0) {
+                result = StringUtils.defaultString(o1.getConfigRef()).compareTo(StringUtils.defaultString(o2.getConfigRef()));
+            }
+            return result;
         }
     };
 

Modified: sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java?rev=1772889&r1=1772888&r2=1772889&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java
(original)
+++ sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java
Tue Dec  6 13:20:43 2016
@@ -18,7 +18,6 @@
  */
 package org.apache.sling.caconfig.resource.impl.util;
 
-import static org.apache.sling.caconfig.resource.impl.util.ContextResourceTestUtil.toContextResourceIterator;
 import static org.apache.sling.caconfig.resource.impl.util.ContextResourceTestUtil.toResourceIterator;
 import static org.junit.Assert.assertThat;
 
@@ -26,6 +25,8 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.caconfig.resource.spi.ContextResource;
 import org.apache.sling.hamcrest.ResourceIteratorMatchers;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Rule;
@@ -42,21 +43,24 @@ public class ResourceEliminateDuplicates
     @Test
     public void testIterator() {
         context.build()
-            .resource("/conf/a")
-            .resource("/conf/a/b")
-            .resource("/conf/a/b/c");
+            .resource("/content/a")
+            .resource("/content/a/b")
+            .resource("/content/a/b/c");
         
-        List<Resource> list = ImmutableList.of(
-                context.resourceResolver().getResource("/conf/a"), 
-                context.resourceResolver().getResource("/conf/a/b"),
-                context.resourceResolver().getResource("/conf/a"),
-                context.resourceResolver().getResource("/conf/a/b/c"));
+        ResourceResolver rr = context.resourceResolver();
+        List<ContextResource> list = ImmutableList.of(
+                new ContextResource(rr.getResource("/content/a"), "/conf/a"), 
+                new ContextResource(rr.getResource("/content/a/b"), "/conf/a/b"),
+                new ContextResource(rr.getResource("/content/a"), "/conf/a"),
+                new ContextResource(rr.getResource("/content/a"), null),
+                new ContextResource(rr.getResource("/content/a/b/c"), "/conf/a/b"));
         
-        Iterator<Resource> result = toResourceIterator(new ResourceEliminateDuplicatesIterator(toContextResourceIterator(list.iterator())));
+        Iterator<Resource> result = toResourceIterator(new ResourceEliminateDuplicatesIterator(list.iterator()));
         assertThat(result, ResourceIteratorMatchers.paths(
-                "/conf/a",
-                "/conf/a/b",
-                "/conf/a/b/c"));
+                "/content/a",
+                "/content/a/b",
+                "/content/a",
+                "/content/a/b/c"));
     }
 
 }

Modified: sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIteratorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIteratorTest.java?rev=1772889&r1=1772888&r2=1772889&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIteratorTest.java
(original)
+++ sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/caconfig/resource/impl/util/ResourcePathCollatingIteratorTest.java
Tue Dec  6 13:20:43 2016
@@ -20,12 +20,16 @@ package org.apache.sling.caconfig.resour
 
 import static org.apache.sling.caconfig.resource.impl.util.ContextResourceTestUtil.toContextResourceIterator;
 import static org.apache.sling.caconfig.resource.impl.util.ContextResourceTestUtil.toResourceIterator;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 
 import java.util.Iterator;
 import java.util.List;
 
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.caconfig.resource.spi.ContextResource;
 import org.apache.sling.hamcrest.ResourceIteratorMatchers;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Rule;
@@ -42,27 +46,55 @@ public class ResourcePathCollatingIterat
     @Test
     public void testIterator() {
         context.build()
-            .resource("/conf/a")
-            .resource("/conf/a/b")
-            .resource("/conf/a/b/c")
-            .resource("/conf/a/b/c/d");
+            .resource("/content/a")
+            .resource("/content/a/b")
+            .resource("/content/a/b/c")
+            .resource("/content/a/b/c/d");
         
+        ResourceResolver rr = context.resourceResolver();
         List<Resource> list1 = ImmutableList.of(
-                context.resourceResolver().getResource("/conf/a/b/c/d"), 
-                context.resourceResolver().getResource("/conf/a"));
+                rr.getResource("/content/a/b/c/d"), 
+                rr.getResource("/content/a"));
         List<Resource> list2 = ImmutableList.of(
-                context.resourceResolver().getResource("/conf/a/b/c"), 
-                context.resourceResolver().getResource("/conf/a/b"),
-                context.resourceResolver().getResource("/conf/a"));
+                rr.getResource("/content/a/b/c"), 
+                rr.getResource("/content/a/b"),
+                rr.getResource("/content/a"));
         
         Iterator<Resource> result = toResourceIterator(new ResourcePathCollatingIterator(ImmutableList.of(
                 toContextResourceIterator(list1.iterator()), toContextResourceIterator(list2.iterator()))));
         assertThat(result, ResourceIteratorMatchers.paths(
-                "/conf/a/b/c/d",
-                "/conf/a/b/c",
-                "/conf/a/b",
-                "/conf/a",
-                "/conf/a"));
+                "/content/a/b/c/d",
+                "/content/a/b/c",
+                "/content/a/b",
+                "/content/a",
+                "/content/a"));
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testWithConfigRef() {
+        context.build()
+            .resource("/content/a")
+            .resource("/content/a/b")
+            .resource("/content/a/b/c")
+            .resource("/content/a/b/c/d");
+        
+        ResourceResolver rr = context.resourceResolver();
+        List<ContextResource> list1 = ImmutableList.of(
+                new ContextResource(rr.getResource("/content/a"), "/conf/z"));
+        List<ContextResource> list2 = ImmutableList.of(
+                new ContextResource(rr.getResource("/content/a"), "/conf/a"));
+        
+        Iterator<ContextResource> result = new ResourcePathCollatingIterator(ImmutableList.of(list1.iterator(),
list2.iterator()));
+        ContextResource item1 = result.next();
+        ContextResource item2 = result.next();
+        assertFalse(result.hasNext());
+        
+        assertEquals("/content/a", item1.getResource().getPath());
+        assertEquals("/conf/a", item1.getConfigRef());
+
+        assertEquals("/content/a", item2.getResource().getPath());
+        assertEquals("/conf/z", item2.getConfigRef());
     }
 
 }



Mime
View raw message