Author: sseifert
Date: Fri Oct 14 09:21:49 2016
New Revision: 1764843

URL: http://svn.apache.org/viewvc?rev=1764843&view=rev
Log:
SLING-6152 Context-Aware Config: Config reference should be detected in 
ContextPathStrategy

Added:
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ContextResourceTestUtil.java
   (with props)
    
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextResource.java
   (with props)
    
sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/
    
sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/spi/
    
sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/spi/ContextResourceTest.java
   (with props)
Modified:
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexer.java
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategy.java
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIterator.java
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIterator.java
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexerTest.java
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategyTest.java
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java
    
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIteratorTest.java
    
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextPathStrategy.java

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java
 Fri Oct 14 09:21:49 2016
@@ -25,6 +25,7 @@ import java.util.List;
 
 import org.apache.sling.api.resource.Resource;
 import 
org.apache.sling.contextaware.config.resource.ConfigurationResourceResolver;
+import org.apache.sling.contextaware.config.resource.spi.ContextResource;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
 
@@ -48,9 +49,9 @@ public class ConfigurationResourceResolv
 
     @Override
     public String getContextPath(Resource resource) {
-        Iterator<Resource> it = 
contextPathStrategy.findContextResources(resource);
+        Iterator<ContextResource> it = 
contextPathStrategy.findContextResources(resource);
         if (it.hasNext()) {
-            return it.next().getPath();
+            return it.next().getResource().getPath();
         }
         else {
             return null;
@@ -60,9 +61,9 @@ public class ConfigurationResourceResolv
     @Override
     public Collection<String> getAllContextPaths(Resource resource) {
         final List<String> contextPaths = new ArrayList<>();
-        Iterator<Resource> contextResources = 
contextPathStrategy.findContextResources(resource);
+        Iterator<ContextResource> contextResources = 
contextPathStrategy.findContextResources(resource);
         while (contextResources.hasNext()) {
-            contextPaths.add(contextResources.next().getPath());
+            contextPaths.add(contextResources.next().getResource().getPath());
         }
         return contextPaths;
     }

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexer.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexer.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexer.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexer.java
 Fri Oct 14 09:21:49 2016
@@ -30,6 +30,7 @@ import org.apache.sling.commons.osgi.Ran
 import 
org.apache.sling.contextaware.config.resource.impl.util.ResourceEliminateDuplicatesIterator;
 import 
org.apache.sling.contextaware.config.resource.impl.util.ResourcePathCollatingIterator;
 import org.apache.sling.contextaware.config.resource.spi.ContextPathStrategy;
+import org.apache.sling.contextaware.config.resource.spi.ContextResource;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.component.annotations.ReferenceCardinality;
@@ -63,8 +64,8 @@ public class ContextPathStrategyMultiple
      * Merges all results from the detected implementations into a single 
answer.
      */
     @Override
-    public Iterator<Resource> findContextResources(Resource resource) {
-        List<Iterator<Resource>> allResults = getAllResults(resource);
+    public Iterator<ContextResource> findContextResources(Resource resource) {
+        List<Iterator<ContextResource>> allResults = getAllResults(resource);
         if (allResults.isEmpty()) {
             return Collections.emptyIterator();
         }
@@ -79,10 +80,10 @@ public class ContextPathStrategyMultiple
      * @param resource Start resource
      * @return List of all results
      */
-    private List<Iterator<Resource>> getAllResults(Resource resource) {
-        List<Iterator<Resource>> results = new ArrayList<>();
+    private List<Iterator<ContextResource>> getAllResults(Resource resource) {
+        List<Iterator<ContextResource>> results = new ArrayList<>();
         for (ContextPathStrategy item : items) {
-            Iterator<Resource> result = item.findContextResources(resource);
+            Iterator<ContextResource> result = 
item.findContextResources(resource);
             if (result.hasNext()) {
                 results.add(result);
             }
@@ -99,7 +100,7 @@ public class ContextPathStrategyMultiple
      * @return Merged result
      */
     @SuppressWarnings("unchecked")
-    private Iterator<Resource> mergeResults(List<Iterator<Resource>> 
allResults) {
+    private Iterator<ContextResource> 
mergeResults(List<Iterator<ContextResource>> allResults) {
         return new ResourceEliminateDuplicatesIterator(
                 new ResourcePathCollatingIterator(allResults)
         );

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultConfigurationResourceResolvingStrategy.java
 Fri Oct 14 09:21:49 2016
@@ -20,7 +20,6 @@ package org.apache.sling.contextaware.co
 
 import static 
org.apache.sling.contextaware.config.resource.impl.def.ConfigurationResourceNameConstants.PROPERTY_CONFIG_COLLECTION_INHERIT;
 import static 
org.apache.sling.contextaware.config.resource.impl.def.ConfigurationResourceNameConstants.PROPERTY_CONFIG_PROPERTY_INHERIT;
-import static 
org.apache.sling.contextaware.config.resource.impl.def.ConfigurationResourceNameConstants.PROPERTY_CONFIG_REF;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -46,6 +45,7 @@ import org.apache.sling.contextaware.con
 import 
org.apache.sling.contextaware.config.resource.impl.util.PathEliminateDuplicatesIterator;
 import 
org.apache.sling.contextaware.config.resource.impl.util.PathParentExpandIterator;
 import 
org.apache.sling.contextaware.config.resource.spi.ConfigurationResourceResolvingStrategy;
+import org.apache.sling.contextaware.config.resource.spi.ContextResource;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Deactivate;
@@ -75,10 +75,6 @@ public class DefaultConfigurationResourc
         @AttributeDefinition(name="Fallback paths",
                 description = "Global fallback configurations, ordered from 
most specific (checked first) to least specific.")
         String[] fallbackPaths() default {"/conf/global", "/apps/conf", 
"/libs/conf"};
-
-        @AttributeDefinition(name="Config ref. resource names",
-                description = "Names of resource to try to look up 
sling:config-ref property in. '.' is also supported as current resource.")
-        String[] configRefResourceNames() default { "." };
     }
 
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -133,13 +129,13 @@ public class DefaultConfigurationResourc
     @SuppressWarnings("unchecked")
     private Iterator<String> findConfigRefs(final Resource startResource) {
         // collect all context path resources
-        Iterator<Resource> contextResources = 
contextPathStrategy.findContextResources(startResource);
+        Iterator<ContextResource> contextResources = 
contextPathStrategy.findContextResources(startResource);
 
         // get config resource path for each context resource, filter out 
items where not reference could be resolved
         Iterator<String> configPaths = new FilterIterator(new 
TransformIterator(contextResources, new Transformer() {
                 @Override
                 public Object transform(Object input) {
-                    return getReference((Resource)input);
+                    return getReference((ContextResource)input);
                 }
             }), PredicateUtils.notNullPredicate());
 
@@ -147,19 +143,9 @@ public class DefaultConfigurationResourc
         return new PathEliminateDuplicatesIterator(new 
PathParentExpandIterator(config.configPath(), configPaths));
     }
 
-    private String getReference(final Resource resource) {
-        
-        // lookup reference string in any of the configured lookup resource 
names
-        String ref = null;
-        for (String name : config.configRefResourceNames()) {
-            Resource lookupResource = resource.getChild(name);
-            if (lookupResource != null) {
-                ref = lookupResource.getValueMap().get(PROPERTY_CONFIG_REF, 
String.class);
-                if (ref != null) {
-                    break;
-                }
-            }
-        }
+    private String getReference(final ContextResource contextResource) {
+        Resource resource = contextResource.getResource();
+        String ref = contextResource.getConfigRef();
 
         if (ref != null) {
             // if absolute path found we are (probably) done

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategy.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategy.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategy.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategy.java
 Fri Oct 14 09:21:49 2016
@@ -24,8 +24,10 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
+import org.apache.commons.lang3.ArrayUtils;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.contextaware.config.resource.spi.ContextPathStrategy;
+import org.apache.sling.contextaware.config.resource.spi.ContextResource;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.metatype.annotations.AttributeDefinition;
@@ -47,8 +49,8 @@ public class DefaultContextPathStrategy
         boolean enabled() default true;
 
         @AttributeDefinition(name="Config ref. resource names",
-                description = "Names of resource to try to look up 
sling:config-ref property in. '.' is also supported as current resource.")
-        String[] configRefResourceNames() default { "." };
+                description = "Names of resource to try to look up 
sling:config-ref property in. If list is empty only current resource is 
checked.")
+        String[] configRefResourceNames();
 
     }
 
@@ -62,7 +64,7 @@ public class DefaultContextPathStrategy
     }
 
     @Override
-    public Iterator<Resource> findContextResources(Resource resource) {
+    public Iterator<ContextResource> findContextResources(Resource resource) {
         if (!config.enabled()) {
             return Collections.emptyIterator();
         }
@@ -72,9 +74,9 @@ public class DefaultContextPathStrategy
     /**
      * Searches the resource hierarchy upwards for all context and returns the 
root resource for each of them.
      */
-    private class ConfigResourceIterator implements Iterator<Resource> {
+    private class ConfigResourceIterator implements Iterator<ContextResource> {
 
-        private Resource next;
+        private ContextResource next;
 
         public ConfigResourceIterator(Resource startResource) {
             next = findNextContextResource(startResource);
@@ -86,12 +88,12 @@ public class DefaultContextPathStrategy
         }
 
         @Override
-        public Resource next() {
+        public ContextResource next() {
             if (next == null) {
                 throw new NoSuchElementException();
             }
-            Resource result = next;
-            next = findNextContextResource(next.getParent());
+            ContextResource result = next;
+            next = findNextContextResource(next.getResource().getParent());
             return result;
         }
 
@@ -105,14 +107,15 @@ public class DefaultContextPathStrategy
          * @param startResource Resource to start searching
          * @return Next resource with sling:config-ref property or null if 
none found.
          */
-        private Resource findNextContextResource(Resource startResource) {
+        private ContextResource findNextContextResource(Resource 
startResource) {
             // start at resource, go up
             Resource resource = startResource;
 
             while (resource != null) {
-                if (hasConfigRef(resource)) {
+                String configRef = getConfigRef(resource);
+                if (configRef != null) {
                     log.trace("Found context path '{}'.", resource.getPath());
-                    return resource;
+                    return new ContextResource(resource, configRef);
                 }
                 // if getParent() returns null, stop
                 resource = resource.getParent();
@@ -122,14 +125,20 @@ public class DefaultContextPathStrategy
             return null;
         }
 
-        private boolean hasConfigRef(final Resource resource) {
+        private String getConfigRef(final Resource resource) {
+            if (ArrayUtils.isEmpty(config.configRefResourceNames())) {
+                return resource.getValueMap().get(PROPERTY_CONFIG_REF, 
String.class);
+            }
             for (String name : config.configRefResourceNames()) {
                 Resource lookupResource = resource.getChild(name);
-                if (lookupResource != null && 
lookupResource.getValueMap().get(PROPERTY_CONFIG_REF, String.class) != null) {
-                    return true;
+                if (lookupResource != null) {
+                    String configRef = 
lookupResource.getValueMap().get(PROPERTY_CONFIG_REF, String.class);
+                    if (configRef != null) {
+                        return configRef;
+                    }
                 }
             }
-            return false;
+            return null;
         }
 
     }

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIterator.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIterator.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIterator.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIterator.java
 Fri Oct 14 09:21:49 2016
@@ -24,20 +24,20 @@ import java.util.Set;
 
 import org.apache.commons.collections.Predicate;
 import org.apache.commons.collections.iterators.FilterIterator;
-import org.apache.sling.api.resource.Resource;
+import org.apache.sling.contextaware.config.resource.spi.ContextResource;
 
 /**
  * Iterator that eliminates duplicate resources (having same path).
  */
 public class ResourceEliminateDuplicatesIterator extends FilterIterator {
 
-    public ResourceEliminateDuplicatesIterator(Iterator<Resource> iterator) {
+    public ResourceEliminateDuplicatesIterator(Iterator<ContextResource> 
iterator) {
         super(iterator, new Predicate() {
             private final Set<String> resourcePaths = new HashSet<>();
             
             @Override
             public boolean evaluate(Object object) {
-                return resourcePaths.add(((Resource)object).getPath());
+                return 
resourcePaths.add(((ContextResource)object).getResource().getPath());
             }
             
         });

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIterator.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIterator.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIterator.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIterator.java
 Fri Oct 14 09:21:49 2016
@@ -23,7 +23,7 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.apache.commons.collections.iterators.CollatingIterator;
-import org.apache.sling.api.resource.Resource;
+import org.apache.sling.contextaware.config.resource.spi.ContextResource;
 
 /**
  * Expected a list of iterators containing paths, where each path is a direct 
or indirect parent of the previous one
@@ -32,16 +32,16 @@ import org.apache.sling.api.resource.Res
  */
 public class ResourcePathCollatingIterator extends CollatingIterator {
 
-    private static Comparator<Resource> PATH_LENGTH_COMPARATOR = new 
Comparator<Resource>() {
+    private static Comparator<ContextResource> PATH_LENGTH_COMPARATOR = new 
Comparator<ContextResource>() {
         @Override
-        public int compare(Resource o1, Resource o2) {
-            Integer length1 = o1.getPath().length();
-            Integer length2 = o2.getPath().length();
+        public int compare(ContextResource o1, ContextResource o2) {
+            Integer length1 = o1.getResource().getPath().length();
+            Integer length2 = o2.getResource().getPath().length();
             return length2.compareTo(length1);
         }
     };
 
-    public ResourcePathCollatingIterator(List<Iterator<Resource>> iterator) {
+    public ResourcePathCollatingIterator(List<Iterator<ContextResource>> 
iterator) {
         super(PATH_LENGTH_COMPARATOR, iterator);
     }
     

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexerTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexerTest.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexerTest.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ContextPathStrategyMultiplexerTest.java
 Fri Oct 14 09:21:49 2016
@@ -19,6 +19,8 @@
 package org.apache.sling.contextaware.config.resource.impl;
 
 import static 
org.apache.sling.contextaware.config.resource.impl.def.ConfigurationResourceNameConstants.PROPERTY_CONFIG_REF;
+import static 
org.apache.sling.contextaware.config.resource.impl.util.ContextResourceTestUtil.toContextResourceIterator;
+import static 
org.apache.sling.contextaware.config.resource.impl.util.ContextResourceTestUtil.toResourceIterator;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 
@@ -29,6 +31,7 @@ import java.util.List;
 import org.apache.sling.api.resource.Resource;
 import 
org.apache.sling.contextaware.config.resource.impl.def.DefaultContextPathStrategy;
 import org.apache.sling.contextaware.config.resource.spi.ContextPathStrategy;
+import org.apache.sling.contextaware.config.resource.spi.ContextResource;
 import org.apache.sling.hamcrest.ResourceIteratorMatchers;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;
@@ -61,20 +64,19 @@ public class ContextPathStrategyMultiple
     
     @Test
     public void testWithNoStrategies() {
-        Iterator<Resource> resources = 
underTest.findContextResources(site1Page1);
-        assertFalse(resources.hasNext());
+        assertFalse(underTest.findContextResources(site1Page1).hasNext());
     }
 
     @Test
     public void testWithDefaultStrategy() {
         context.registerInjectActivateService(new 
DefaultContextPathStrategy());
 
-        assertThat(underTest.findContextResources(site1Page1), 
ResourceIteratorMatchers.paths( 
+        
assertThat(toResourceIterator(underTest.findContextResources(site1Page1)), 
ResourceIteratorMatchers.paths( 
                 "/content/tenant1/region1/site1",
                 "/content/tenant1/region1",
                 "/content/tenant1"));
 
-        assertThat(underTest.findContextResources(site2Page1),  
ResourceIteratorMatchers.paths( 
+        
assertThat(toResourceIterator(underTest.findContextResources(site2Page1)), 
ResourceIteratorMatchers.paths( 
                 "/content/tenant1/region1/site2",
                 "/content/tenant1/region1",
                 "/content/tenant1"));
@@ -85,7 +87,7 @@ public class ContextPathStrategyMultiple
         registerContextPathStrategy("/content/tenant1");
         registerContextPathStrategy("/content/tenant1/region1/site1", 
"/content/tenant1/region1");
         
-        assertThat(underTest.findContextResources(site1Page1), 
ResourceIteratorMatchers.paths(
+        
assertThat(toResourceIterator(underTest.findContextResources(site1Page1)), 
ResourceIteratorMatchers.paths(
                 "/content/tenant1/region1/site1",
                 "/content/tenant1/region1",
                 "/content/tenant1"));
@@ -96,7 +98,7 @@ public class ContextPathStrategyMultiple
         registerContextPathStrategy("/content/tenant1", 
"/content/tenant1/region1");
         registerContextPathStrategy("/content/tenant1/region1/site1", 
"/content/tenant1/region1");
         
-        assertThat(underTest.findContextResources(site1Page1),  
ResourceIteratorMatchers.paths( 
+        
assertThat(toResourceIterator(underTest.findContextResources(site1Page1)), 
ResourceIteratorMatchers.paths( 
                 "/content/tenant1/region1/site1",
                 "/content/tenant1/region1",
                 "/content/tenant1"));
@@ -112,8 +114,8 @@ public class ContextPathStrategyMultiple
         }
         context.registerService(ContextPathStrategy.class, new 
ContextPathStrategy() {
             @Override
-            public Iterator<Resource> findContextResources(Resource resource) {
-                return resources.iterator();
+            public Iterator<ContextResource> findContextResources(Resource 
resource) {
+                return toContextResourceIterator(resources.iterator());
             }
         });
     }

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategyTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategyTest.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategyTest.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/def/DefaultContextPathStrategyTest.java
 Fri Oct 14 09:21:49 2016
@@ -19,6 +19,7 @@
 package org.apache.sling.contextaware.config.resource.impl.def;
 
 import static 
org.apache.sling.contextaware.config.resource.impl.def.ConfigurationResourceNameConstants.PROPERTY_CONFIG_REF;
+import static 
org.apache.sling.contextaware.config.resource.impl.util.ContextResourceTestUtil.*;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 
@@ -26,10 +27,13 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.contextaware.config.resource.spi.ContextPathStrategy;
 import org.apache.sling.hamcrest.ResourceIteratorMatchers;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.hamcrest.Matchers;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 public class DefaultContextPathStrategyTest {
     
     @Rule
@@ -54,15 +58,23 @@ public class DefaultContextPathStrategyT
     public void testFindContextPaths() {
         ContextPathStrategy underTest = 
context.registerInjectActivateService(new DefaultContextPathStrategy());
 
-        assertThat(underTest.findContextResources(site1Page1), 
ResourceIteratorMatchers.paths( 
+        
assertThat(toResourceIterator(underTest.findContextResources(site1Page1)), 
ResourceIteratorMatchers.paths( 
                 "/content/tenant1/region1/site1",
                 "/content/tenant1/region1",
                 "/content/tenant1"));
+        
assertThat(ImmutableList.copyOf(toConfigRefIterator(underTest.findContextResources(site1Page1))),
 Matchers.contains( 
+                "/conf/tenant1/region1/site1",
+                "/conf/tenant1/region1",
+                "/conf/tenant1"));
 
-        assertThat(underTest.findContextResources(site2Page1), 
ResourceIteratorMatchers.paths(
+        
assertThat(toResourceIterator(underTest.findContextResources(site2Page1)), 
ResourceIteratorMatchers.paths(
                 "/content/tenant1/region1/site2",
                 "/content/tenant1/region1",
                 "/content/tenant1"));
+        
assertThat(ImmutableList.copyOf(toConfigRefIterator(underTest.findContextResources(site2Page1))),
 Matchers.contains( 
+                "/conf/tenant1/region1/site2",
+                "/conf/tenant1/region1",
+                "/conf/tenant1"));
     }
 
     @Test
@@ -74,4 +86,23 @@ public class DefaultContextPathStrategyT
         assertFalse(underTest.findContextResources(site2Page1).hasNext());
     }
 
+    @Test
+    public void testConfigRefResourceNames() {
+        ContextPathStrategy underTest = 
context.registerInjectActivateService(new DefaultContextPathStrategy(),
+                "configRefResourceNames", new String[] { "jcr:content" });
+
+        context.build()
+            .resource("/content/tenant1/region1/jcr:content", 
PROPERTY_CONFIG_REF, "/conf/tenant1/region1");
+        
+        
assertThat(toResourceIterator(underTest.findContextResources(site1Page1)), 
ResourceIteratorMatchers.paths( 
+                "/content/tenant1/region1"));
+        
assertThat(ImmutableList.copyOf(toConfigRefIterator(underTest.findContextResources(site1Page1))),
 Matchers.contains( 
+                "/conf/tenant1/region1"));
+
+        
assertThat(toResourceIterator(underTest.findContextResources(site2Page1)), 
ResourceIteratorMatchers.paths(
+                "/content/tenant1/region1"));
+        
assertThat(ImmutableList.copyOf(toConfigRefIterator(underTest.findContextResources(site2Page1))),
 Matchers.contains( 
+                "/conf/tenant1/region1"));
+    }
+
 }

Added: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ContextResourceTestUtil.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ContextResourceTestUtil.java?rev=1764843&view=auto
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ContextResourceTestUtil.java
 (added)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ContextResourceTestUtil.java
 Fri Oct 14 09:21:49 2016
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.contextaware.config.resource.impl.util;
+
+import java.util.Iterator;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.apache.commons.collections.Transformer;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.contextaware.config.resource.spi.ContextResource;
+
+public final class ContextResourceTestUtil {
+    
+    private ContextResourceTestUtil() {
+        // static methods only
+    }
+    
+    @SuppressWarnings("unchecked")
+    public static Iterator<Resource> 
toResourceIterator(Iterator<ContextResource> contextResources) {
+        return IteratorUtils.transformedIterator(contextResources, new 
Transformer() {
+            @Override
+            public Object transform(Object input) {
+                return ((ContextResource)input).getResource();
+            }
+        });
+    }
+
+    @SuppressWarnings("unchecked")
+    public static Iterator<String> 
toConfigRefIterator(Iterator<ContextResource> contextResources) {
+        return IteratorUtils.transformedIterator(contextResources, new 
Transformer() {
+            @Override
+            public Object transform(Object input) {
+                return ((ContextResource)input).getConfigRef();
+            }
+        });
+    }
+
+    @SuppressWarnings("unchecked")
+    public static Iterator<ContextResource> 
toContextResourceIterator(Iterator<Resource> resources) {
+        return IteratorUtils.transformedIterator(resources, new Transformer() {
+            @Override
+            public Object transform(Object input) {
+                Resource resource = (Resource)input;
+                return new ContextResource(resource, "/conf-test" + 
resource.getPath());
+            }
+        });
+    }
+
+}

Propchange: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ContextResourceTestUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ContextResourceTestUtil.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Oct 14 09:21:49 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ContextResourceTestUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourceEliminateDuplicatesIteratorTest.java
 Fri Oct 14 09:21:49 2016
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.contextaware.config.resource.impl.util;
 
+import static 
org.apache.sling.contextaware.config.resource.impl.util.ContextResourceTestUtil.toContextResourceIterator;
+import static 
org.apache.sling.contextaware.config.resource.impl.util.ContextResourceTestUtil.toResourceIterator;
 import static org.junit.Assert.assertThat;
 
 import java.util.Iterator;
@@ -50,7 +52,7 @@ public class ResourceEliminateDuplicates
                 context.resourceResolver().getResource("/conf/a"),
                 context.resourceResolver().getResource("/conf/a/b/c"));
         
-        Iterator<Resource> result = new 
ResourceEliminateDuplicatesIterator(list.iterator());
+        Iterator<Resource> result = toResourceIterator(new 
ResourceEliminateDuplicatesIterator(toContextResourceIterator(list.iterator())));
         assertThat(result, ResourceIteratorMatchers.paths(
                 "/conf/a",
                 "/conf/a/b",

Modified: 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIteratorTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIteratorTest.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIteratorTest.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/util/ResourcePathCollatingIteratorTest.java
 Fri Oct 14 09:21:49 2016
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.contextaware.config.resource.impl.util;
 
+import static 
org.apache.sling.contextaware.config.resource.impl.util.ContextResourceTestUtil.toContextResourceIterator;
+import static 
org.apache.sling.contextaware.config.resource.impl.util.ContextResourceTestUtil.toResourceIterator;
 import static org.junit.Assert.assertThat;
 
 import java.util.Iterator;
@@ -53,7 +55,8 @@ public class ResourcePathCollatingIterat
                 context.resourceResolver().getResource("/conf/a/b"),
                 context.resourceResolver().getResource("/conf/a"));
         
-        Iterator<Resource> result = new 
ResourcePathCollatingIterator(ImmutableList.of(list1.iterator(), 
list2.iterator()));
+        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",

Modified: 
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextPathStrategy.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextPathStrategy.java?rev=1764843&r1=1764842&r2=1764843&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextPathStrategy.java
 (original)
+++ 
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextPathStrategy.java
 Fri Oct 14 09:21:49 2016
@@ -26,7 +26,7 @@ import org.apache.sling.api.resource.Res
 import org.osgi.annotation.versioning.ConsumerType;
 
 /**
- * Allows application to define a strategy to find context paths for content 
paths.
+ * Allows application to define a strategy to find context paths and config 
references for content paths.
  * A context paths is the root path of a "configuration context", which is a 
subtree in the resource hierarchy.
  * Each context may have it's own context-aware configuration attached to.0
  * If multiple context path strategy implementations are defined the results 
of them are merged.
@@ -37,10 +37,10 @@ public interface ContextPathStrategy {
     /**
      * Finds context paths for the given resource.
      * @param resource Context resource
-     * @return Root resource for each context found (in order of closest 
matching first).
+     * @return Root resource and config reference for each context found (in 
order of closest matching first).
      *      Only one of the parent resources or the resource itself may be 
included in the result.
      *      If none are found an empty list is returned.
      */
-    @Nonnull Iterator<Resource> findContextResources(@Nonnull Resource 
resource);
+    @Nonnull Iterator<ContextResource> findContextResources(@Nonnull Resource 
resource);
 
 }

Added: 
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextResource.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextResource.java?rev=1764843&view=auto
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextResource.java
 (added)
+++ 
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextResource.java
 Fri Oct 14 09:21:49 2016
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.contextaware.config.resource.spi;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.api.resource.Resource;
+import org.osgi.annotation.versioning.ProviderType;
+
+/**
+ * Holds reference to a context root resource and configuration reference path
+ * that was detected for the configuration context.
+ */
+@ProviderType
+public final class ContextResource {
+    
+    private final Resource resource;
+    private final String configRef;
+    private final String key;
+    
+    /**
+     * @param resource Context root resource
+     * @param configRef Config reference (normally a resource path).
+     *    May be null if the {@link ConfigurationResourceResolvingStrategy has 
it's own concept of detecting the matching configuration.
+     */
+    public ContextResource(@Nonnull Resource resource, String configRef) {
+        this.resource = resource;
+        this.configRef = configRef;
+        this.key = resource.getPath() + "|" + configRef;
+    }
+
+    /**
+     * @return Context root resource
+     */
+    public @Nonnull Resource getResource() {
+        return resource;
+    }
+
+    /**
+     * @return Config reference (normally a resource path).
+     *    May be null if the {@link ConfigurationResourceResolvingStrategy has 
it's own concept of detecting the matching configuration.
+     */
+    public @CheckForNull String getConfigRef() {
+        return configRef;
+    }
+
+    @Override
+    public int hashCode() {
+        return key.hashCode();
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof ContextResource) {
+            return StringUtils.equals(key, ((ContextResource)obj).key);
+        }
+        return false;
+    }
+
+}

Propchange: 
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextResource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextResource.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Oct 14 09:21:49 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/contrib/extensions/contextaware-config/spi/src/main/java/org/apache/sling/contextaware/config/resource/spi/ContextResource.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/spi/ContextResourceTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/spi/ContextResourceTest.java?rev=1764843&view=auto
==============================================================================
--- 
sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/spi/ContextResourceTest.java
 (added)
+++ 
sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/spi/ContextResourceTest.java
 Fri Oct 14 09:21:49 2016
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.contextaware.config.resource.spi;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class ContextResourceTest {
+    
+    @Rule
+    public SlingContext context = new SlingContext();
+    
+    private Resource resource1;
+    private Resource resource2;
+    
+    @Before
+    public void setUp() {
+        resource1 = context.create().resource("/content/test1");
+        resource2 = context.create().resource("/content/test2");
+    }
+
+    @Test
+    public void testGetter() {
+        ContextResource r1 = new ContextResource(resource1, "/conf/test");
+        assertEquals("/content/test1", r1.getResource().getPath());
+        assertEquals("/conf/test", r1.getConfigRef());
+    }
+
+    @Test
+    public void testEquals() {
+        assertTrue(new ContextResource(resource1, "/conf/test").equals(new 
ContextResource(resource1, "/conf/test")));
+        assertTrue(new ContextResource(resource1, null).equals(new 
ContextResource(resource1, null)));
+    }
+
+    @Test
+    public void testNotEquals() {
+        assertFalse(new ContextResource(resource1, "/conf/test").equals(new 
ContextResource(resource2, "/conf/test")));
+        assertFalse(new ContextResource(resource1, "/conf/test1").equals(new 
ContextResource(resource1, "/conf/test2")));
+        assertFalse(new ContextResource(resource1, null).equals(new 
ContextResource(resource1, "/conf/test")));
+    }
+
+}

Propchange: 
sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/spi/ContextResourceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/spi/ContextResourceTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Oct 14 09:21:49 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/contrib/extensions/contextaware-config/spi/src/test/java/org/apache/sling/contextaware/config/resource/spi/ContextResourceTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to