sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1622390 - in /sling/trunk/contrib/extensions/resourcemerger: ./ src/main/java/org/apache/sling/resourcemerger/impl/ src/main/java/org/apache/sling/resourcemerger/spi/ src/test/java/org/apache/sling/resourcemerger/impl/
Date Thu, 04 Sep 2014 01:37:49 GMT
Author: justin
Date: Thu Sep  4 01:37:48 2014
New Revision: 1622390

URL: http://svn.apache.org/r1622390
Log:
SLING-3423 / SLING-3657 - adding MergedResourcePicker for implementing additional merged resource
selection algorithms. Implmeneting OverridingResourcePicker to provide /mnt/override which
merges resources based on the resource type hierarchy

Added:
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourcePickerWhiteboard.java
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProvider.java
      - copied, changed from r1622318, sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProviderFactory.java
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/OverridingResourcePicker.java
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/MergedResourcePicker.java
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/package-info.java
    sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/OverridingResourceProviderTest.java
Removed:
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
Modified:
    sling/trunk/contrib/extensions/resourcemerger/pom.xml
    sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
    sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderTest.java

Modified: sling/trunk/contrib/extensions/resourcemerger/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/pom.xml?rev=1622390&r1=1622389&r2=1622390&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/pom.xml (original)
+++ sling/trunk/contrib/extensions/resourcemerger/pom.xml Thu Sep  4 01:37:48 2014
@@ -67,6 +67,16 @@
         </dependency>
 
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+
+        <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
         </dependency>
@@ -89,11 +99,6 @@
             <artifactId>junit</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.compendium</artifactId>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
             <version>0.2.0</version>

Added: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourcePickerWhiteboard.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourcePickerWhiteboard.java?rev=1622390&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourcePickerWhiteboard.java
(added)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourcePickerWhiteboard.java
Thu Sep  4 01:37:48 2014
@@ -0,0 +1,81 @@
+/*
+ * 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.resourcemerger.impl;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.sling.api.resource.ResourceProvider;
+import org.apache.sling.api.resource.ResourceProviderFactory;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.resourcemerger.spi.MergedResourcePicker;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
+
+@Component
+public class MergedResourcePickerWhiteboard implements ServiceTrackerCustomizer {
+
+    private ServiceTracker tracker;
+
+    private BundleContext bundleContext;
+
+    @Activate
+    protected void activate(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+        tracker = new ServiceTracker(bundleContext, MergedResourcePicker.class.getName(),
this);
+        tracker.open();
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        tracker.close();
+    }
+
+    public Object addingService(ServiceReference reference) {
+        MergedResourcePicker picker = (MergedResourcePicker) bundleContext.getService(reference);
+        String mergeRoot = PropertiesUtil.toString(reference.getProperty(MergedResourcePicker.MERGE_ROOT),
null);
+        if (mergeRoot != null) {
+            ResourceProviderFactory providerFactory = new MergingResourceProviderFactory(mergeRoot,
picker);
+            Dictionary<Object, Object> props = new Hashtable<Object, Object>();
+            props.put(ResourceProvider.ROOTS, mergeRoot);
+            props.put(ResourceProvider.OWNS_ROOTS, true);
+            return bundleContext.registerService(ResourceProviderFactory.class.getName(),
providerFactory, props);
+        } else {
+            return null;
+        }
+    }
+
+    public void modifiedService(ServiceReference reference, Object service) {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void removedService(ServiceReference reference, Object service) {
+        if (service instanceof ServiceRegistration) {
+            ((ServiceRegistration) service).unregister();
+        }
+    }
+
+}

Modified: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java?rev=1622390&r1=1622389&r2=1622390&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
(original)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
Thu Sep  4 01:37:48 2014
@@ -18,55 +18,54 @@
  */
 package org.apache.sling.resourcemerger.impl;
 
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.NonExistingResource;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceProvider;
-import org.apache.sling.api.resource.ResourceProviderFactory;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.resourcemerger.api.ResourceMergerService;
+import org.apache.sling.resourcemerger.spi.MergedResourcePicker;
 
 @Component(label = "Apache Sling Merged Resource Provider Factory",
            description = "This resource provider delivers merged resources based on the search
paths.",
            metatype=true)
-@Service(value = {ResourceProviderFactory.class, ResourceMergerService.class})
-@Properties({
-    @Property(name = ResourceProvider.ROOTS, value=MergedResourceProviderFactory.DEFAULT_ROOT,
-            label="Root",
-            description="The mount point of merged resources"),
-    @Property(name = ResourceProvider.OWNS_ROOTS, boolValue=true, propertyPrivate=true)
-})
+@Service
+@Property(name=MergedResourcePicker.MERGE_ROOT, value=MergedResourceProviderFactory.DEFAULT_ROOT,
+    label="Root",
+    description="The mount point of merged resources")
 /**
  * The <code>MergedResourceProviderFactory</code> creates merged resource
  * providers and implements the <code>ResourceMergerService</code>.
  */
-public class MergedResourceProviderFactory implements ResourceProviderFactory, ResourceMergerService
{
+public class MergedResourceProviderFactory implements MergedResourcePicker, ResourceMergerService
{
 
     public static final String DEFAULT_ROOT = "/mnt/overlay";
 
     private String mergeRootPath;
 
-    /**
-     * {@inheritDoc}
-     */
-    public ResourceProvider getResourceProvider(final Map<String, Object> stringObjectMap)
-    throws LoginException {
-        return new MergedResourceProvider(mergeRootPath);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public ResourceProvider getAdministrativeResourceProvider(final Map<String, Object>
stringObjectMap)
-    throws LoginException {
-        return new MergedResourceProvider(mergeRootPath);
+    public Iterator<Resource> pickResources(ResourceResolver resolver, String relativePath)
{
+        List<Resource> resources = new ArrayList<Resource>();
+        final String[] searchPaths = resolver.getSearchPath();
+        for (int i = searchPaths.length - 1; i >= 0; i--) {
+            final String basePath = searchPaths[i];
+            final String fullPath = basePath + relativePath;
+            Resource resource = resolver.getResource(fullPath);
+            if (resource != null) {
+                resources.add(resource);
+            } else {
+                resources.add(new NonExistingResource(resolver, fullPath));
+            }
+        }
+        return resources.iterator();
     }
 
     /**
@@ -128,8 +127,5 @@ public class MergedResourceProviderFacto
     @Activate
     protected void configure(final Map<String, Object> properties) {
         mergeRootPath = PropertiesUtil.toString(properties.get(ResourceProvider.ROOTS), DEFAULT_ROOT);
-        if (mergeRootPath.endsWith("/")) {
-            mergeRootPath = mergeRootPath.substring(0, mergeRootPath.length() - 1);
-        }
     }
 }

Copied: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProvider.java
(from r1622318, sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java)
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProvider.java?p2=sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProvider.java&p1=sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java&r1=1622318&r2=1622390&rev=1622390&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
(original)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProvider.java
Thu Sep  4 01:37:48 2014
@@ -29,25 +29,17 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.resourcemerger.spi.MergedResourcePicker;
 
-/**
- * The <code>MergedResourceProvider</code> is the resource provider providing
- * access to {@link MergedResource} objects.
- */
-public class MergedResourceProvider
-    implements ResourceProvider {
+class MergingResourceProvider implements ResourceProvider {
 
     private final String mergeRootPath;
 
-    public MergedResourceProvider(final String mergeRootPath) {
-        this.mergeRootPath = mergeRootPath;
-    }
+    private final MergedResourcePicker picker;
 
-    /**
-     * {@inheritDoc}
-     */
-    public Resource getResource(final ResourceResolver resolver, final HttpServletRequest
request, final String path) {
-        return getResource(resolver, path);
+    MergingResourceProvider(String mergeRootPath, MergedResourcePicker picker) {
+        this.mergeRootPath = mergeRootPath;
+        this.picker = picker;
     }
 
     private static final class ParentHidingHandler {
@@ -55,7 +47,7 @@ public class MergedResourceProvider
         private final String[] childrenToHideArray;
 
         public ParentHidingHandler(final Resource parent) {
-            if ( parent == null ) {
+            if (parent == null) {
                 this.childrenToHideArray = null;
             } else {
                 final ValueMap parentProps = parent.getValueMap();
@@ -65,9 +57,9 @@ public class MergedResourceProvider
 
         public boolean isHidden(final String name) {
             boolean hidden = false;
-            if ( this.childrenToHideArray != null ) {
-                for(final String entry : childrenToHideArray) {
-                    if ( entry.equals("*") || entry.equals(name) ) {
+            if (this.childrenToHideArray != null) {
+                for (final String entry : childrenToHideArray) {
+                    if (entry.equals("*") || entry.equals(name)) {
                         hidden = true;
                         break;
                     }
@@ -77,42 +69,6 @@ public class MergedResourceProvider
         }
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public Resource getResource(final ResourceResolver resolver, final String path) {
-        final String relativePath = getRelativePath(path);
-
-        if ( relativePath != null ) {
-            final ResourceHolder holder = new ResourceHolder(ResourceUtil.getName(path));
-
-            // Loop over provided base paths, start with least import
-            final String[] searchPaths = resolver.getSearchPath();
-            for(int i=searchPaths.length-1; i >= 0; i--) {
-                final String basePath = searchPaths[i];
-
-                // Try to get the corresponding physical resource for this base path
-                final String fullPath = basePath + relativePath;
-
-                // check parent for hiding
-                // SLING 3521 : if parent is not readable, nothing is hidden
-                final Resource parent = resolver.getResource(ResourceUtil.getParent(fullPath));
-                final boolean hidden = new ParentHidingHandler(parent).isHidden(holder.name);
-                if ( hidden ) {
-                    holder.resources.clear();
-                } else {
-                    final Resource baseRes = resolver.getResource(fullPath);
-                    if (baseRes != null) {
-                        holder.resources.add(baseRes);
-                    }
-                }
-            }
-            return createMergedResource(resolver, relativePath, holder);
-        }
-
-        return null;
-    }
-
     private static final class ResourceHolder {
         public final String name;
         public final List<Resource> resources = new ArrayList<Resource>();
@@ -126,18 +82,17 @@ public class MergedResourceProvider
     /**
      * Create the merged resource based on the provided resources
      */
-    private Resource createMergedResource(final ResourceResolver resolver,
-            final String relativePath,
+    private Resource createMergedResource(final ResourceResolver resolver, final String relativePath,
             final ResourceHolder holder) {
         int index = 0;
-        while ( index < holder.resources.size() ) {
+        while (index < holder.resources.size()) {
             final Resource baseRes = holder.resources.get(index);
             // check if resource is hidden
             final ValueMap props = baseRes.getValueMap();
             holder.valueMaps.add(props);
-            if ( props.get(MergedResourceConstants.PN_HIDE_RESOURCE, Boolean.FALSE) ) {
+            if (props.get(MergedResourceConstants.PN_HIDE_RESOURCE, Boolean.FALSE)) {
                 // clear everything up to now
-                for(int i=0;i<=index;i++) {
+                for (int i = 0; i <= index; i++) {
                     holder.resources.remove(0);
                 }
                 holder.valueMaps.clear();
@@ -155,73 +110,122 @@ public class MergedResourceProvider
     }
 
     /**
+     * Gets the relative path out of merge root path
+     *
+     * @param path Absolute path
+     * @return Relative path
+     */
+    private String getRelativePath(String path) {
+        if (path.startsWith(mergeRootPath)) {
+            path = path.substring(mergeRootPath.length());
+            if (path.length() == 0) {
+                return path;
+            } else if (path.charAt(0) == '/') {
+                return path.substring(1);
+            }
+        }
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Resource getResource(ResourceResolver resolver, String path) {
+        final String relativePath = getRelativePath(path);
+
+        if (relativePath != null) {
+            final ResourceHolder holder = new ResourceHolder(ResourceUtil.getName(path));
+
+            final Iterator<Resource> resources = picker.pickResources(resolver, relativePath);
+
+            if (!resources.hasNext()) {
+                return null;
+            }
+
+            while (resources.hasNext()) {
+                Resource resource = resources.next();
+                // check parent for hiding
+                // SLING 3521 : if parent is not readable, nothing is hidden
+                final Resource parent = resource.getParent();
+                final boolean hidden = new ParentHidingHandler(parent).isHidden(holder.name);
+                if (hidden) {
+                    holder.resources.clear();
+                } else if (!ResourceUtil.isNonExistingResource(resource)) {
+                    holder.resources.add(resource);
+                }
+            }
+            return createMergedResource(resolver, relativePath, holder);
+        }
+
+        return null;
+    }
+
+    /**
      * {@inheritDoc}
      */
-    public Iterator<Resource> listChildren(final Resource resource) {
+    public Iterator<Resource> listChildren(Resource resource) {
         final ResourceResolver resolver = resource.getResourceResolver();
 
         final String relativePath = getRelativePath(resource.getPath());
 
-        if ( relativePath != null ) {
+        if (relativePath != null) {
             final List<ResourceHolder> candidates = new ArrayList<ResourceHolder>();
 
-            // Loop over provided base paths, start with least import
-            final String[] searchPaths = resolver.getSearchPath();
-            for(int i=searchPaths.length-1; i >= 0; i--) {
-                final String basePath = searchPaths[i];
-                final Resource parentResource = resolver.getResource(basePath + relativePath);
-                if ( parentResource != null ) {
-                    final ParentHidingHandler handler = new ParentHidingHandler(parentResource);
-                    for(final Resource child : parentResource.getChildren()) {
-                        final String rsrcName = child.getName();
-                        ResourceHolder holder = null;
-                        for(final ResourceHolder current : candidates) {
-                            if ( current.name.equals(rsrcName) ) {
-                                holder = current;
-                                break;
-                            }
-                        }
-                        if ( holder == null ) {
-                            holder = new ResourceHolder(rsrcName);
-                            candidates.add(holder);
+            final Iterator<Resource> resources = picker.pickResources(resolver, relativePath);
+
+            while (resources.hasNext()) {
+                Resource parentResource = resources.next();
+                final ParentHidingHandler handler = new ParentHidingHandler(parentResource);
+                for (final Resource child : parentResource.getChildren()) {
+                    final String rsrcName = child.getName();
+                    ResourceHolder holder = null;
+                    for (final ResourceHolder current : candidates) {
+                        if (current.name.equals(rsrcName)) {
+                            holder = current;
+                            break;
                         }
-                        holder.resources.add(child);
+                    }
+                    if (holder == null) {
+                        holder = new ResourceHolder(rsrcName);
+                        candidates.add(holder);
+                    }
+                    holder.resources.add(child);
 
-                        // Check if children need reordering
-                        int orderBeforeIndex = -1;
-                        final ValueMap vm = child.getValueMap();
-                        final String orderBefore = vm.get(MergedResourceConstants.PN_ORDER_BEFORE,
String.class);
-                        if (orderBefore != null && !orderBefore.equals(rsrcName))
{
-                            // search entry
-                            int index = 0;
-                            while (index < candidates.size()) {
-                                final ResourceHolder current = candidates.get(index);
-                                if ( current.name.equals(orderBefore) ) {
-                                    orderBeforeIndex = index;
-                                    break;
-                                }
-                                index++;
+                    // Check if children need reordering
+                    int orderBeforeIndex = -1;
+                    final ValueMap vm = child.getValueMap();
+                    final String orderBefore = vm.get(MergedResourceConstants.PN_ORDER_BEFORE,
String.class);
+                    if (orderBefore != null && !orderBefore.equals(rsrcName)) {
+                        // search entry
+                        int index = 0;
+                        while (index < candidates.size()) {
+                            final ResourceHolder current = candidates.get(index);
+                            if (current.name.equals(orderBefore)) {
+                                orderBeforeIndex = index;
+                                break;
                             }
+                            index++;
                         }
+                    }
 
-                        if (orderBeforeIndex > -1) {
-                            candidates.add(orderBeforeIndex, holder);
-                            candidates.remove(candidates.size() - 1);
-                        }
+                    if (orderBeforeIndex > -1) {
+                        candidates.add(orderBeforeIndex, holder);
+                        candidates.remove(candidates.size() - 1);
                     }
-                    final Iterator<ResourceHolder> iter = candidates.iterator();
-                    while ( iter.hasNext() ) {
-                        final ResourceHolder holder = iter.next();
-                        if ( handler.isHidden(holder.name) ) {
-                            iter.remove();
-                        }
+                }
+                final Iterator<ResourceHolder> iter = candidates.iterator();
+                while (iter.hasNext()) {
+                    final ResourceHolder holder = iter.next();
+                    if (handler.isHidden(holder.name)) {
+                        iter.remove();
                     }
                 }
             }
             final List<Resource> children = new ArrayList<Resource>();
-            for(final ResourceHolder holder : candidates) {
-                final Resource mergedResource = this.createMergedResource(resolver, (relativePath.length()
== 0 ? holder.name : relativePath + '/' + holder.name), holder);
-                if ( mergedResource != null ) {
+            for (final ResourceHolder holder : candidates) {
+                final Resource mergedResource = this.createMergedResource(resolver,
+                        (relativePath.length() == 0 ? holder.name : relativePath + '/' +
holder.name), holder);
+                if (mergedResource != null) {
                     children.add(mergedResource);
                 }
             }
@@ -231,21 +235,12 @@ public class MergedResourceProvider
         return null;
     }
 
+
     /**
-     * Gets the relative path out of merge root path
-     *
-     * @param path Absolute path
-     * @return Relative path
+     * {@inheritDoc}
      */
-    private String getRelativePath(String path) {
-        if ( path.startsWith(mergeRootPath) ) {
-            path = path.substring(mergeRootPath.length());
-            if ( path.length() == 0 ) {
-                return path;
-            } else if ( path.charAt(0) == '/' ) {
-                return path.substring(1);
-            }
-        }
-        return null;
+    public Resource getResource(final ResourceResolver resolver, final HttpServletRequest
request, final String path) {
+        return getResource(resolver, path);
     }
+
 }

Added: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProviderFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProviderFactory.java?rev=1622390&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProviderFactory.java
(added)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/MergingResourceProviderFactory.java
Thu Sep  4 01:37:48 2014
@@ -0,0 +1,48 @@
+/*
+ * 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.resourcemerger.impl;
+
+import java.util.Map;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.ResourceProvider;
+import org.apache.sling.api.resource.ResourceProviderFactory;
+import org.apache.sling.resourcemerger.spi.MergedResourcePicker;
+
+class MergingResourceProviderFactory implements ResourceProviderFactory {
+
+    private final String mergeRootPath;
+
+    private final MergedResourcePicker picker;
+
+    MergingResourceProviderFactory(String mergeRootPath, MergedResourcePicker picker) {
+        this.mergeRootPath = mergeRootPath;
+        this.picker = picker;
+    }
+
+    public ResourceProvider getResourceProvider(Map<String, Object> authenticationInfo)
throws LoginException {
+        return new MergingResourceProvider(mergeRootPath, picker);
+    }
+
+    public ResourceProvider getAdministrativeResourceProvider(Map<String, Object> authenticationInfo)
+            throws LoginException {
+        return new MergingResourceProvider(mergeRootPath, picker);
+    }
+
+}

Added: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/OverridingResourcePicker.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/OverridingResourcePicker.java?rev=1622390&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/OverridingResourcePicker.java
(added)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/impl/OverridingResourcePicker.java
Thu Sep  4 01:37:48 2014
@@ -0,0 +1,91 @@
+/*
+ * 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.resourcemerger.impl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.NonExistingResource;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.resourcemerger.spi.MergedResourcePicker;
+
+@Component
+@Service
+@Property(name = MergedResourcePicker.MERGE_ROOT, value = "/mnt/override")
+public class OverridingResourcePicker implements MergedResourcePicker {
+
+    public Iterator<Resource> pickResources(ResourceResolver resolver, String relativePath)
{
+        String absPath = "/" + relativePath;
+        final List<Resource> resources = new ArrayList<Resource>();
+
+        Resource currentTarget = resolver.getResource(absPath);
+
+        if (currentTarget != null) {
+            resources.add(currentTarget);
+
+            while (currentTarget != null) {
+                final Resource inheritanceRootResource = findInheritanceRoot(currentTarget);
+                if (inheritanceRootResource == null) {
+                    currentTarget = null;
+                } else {
+                    final String relPath = currentTarget.getPath()
+                            .substring(inheritanceRootResource.getPath().length());
+                    final String superType = inheritanceRootResource.getResourceSuperType();
+                    if (superType == null) {
+                        currentTarget = null;
+                    } else {
+                        final String superTypeChildPath = superType + relPath;
+                        final Resource superTypeResource = resolver.getResource(superTypeChildPath);
+                        if (superTypeResource != null) {
+                            resources.add(superTypeResource);
+                            currentTarget = superTypeResource;
+                        } else {
+                            resources.add(new NonExistingResource(resolver, superTypeChildPath));
+                            currentTarget = null;
+                        }
+                    }
+                }
+            }
+
+            Collections.reverse(resources);
+        }
+        return resources.iterator();
+    }
+
+    private Resource findInheritanceRoot(Resource target) {
+        String superType = target.getResourceSuperType();
+        if (superType != null) {
+            return target;
+        } else {
+            Resource parent = target.getParent();
+            if (parent == null) {
+                return null;
+            } else {
+                return findInheritanceRoot(parent);
+            }
+        }
+    }
+
+}

Added: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/MergedResourcePicker.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/MergedResourcePicker.java?rev=1622390&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/MergedResourcePicker.java
(added)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/MergedResourcePicker.java
Thu Sep  4 01:37:48 2014
@@ -0,0 +1,54 @@
+/*
+ * 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.resourcemerger.spi;
+
+import java.util.Iterator;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+
+import aQute.bnd.annotation.ConsumerType;
+
+/**
+ * Service interface which can be implemented to define an algorithm used to pick
+ * resources to be merged. For each picker service, a separate ResourceProviderFactory
+ * will be exposed at the MERGE_ROOT of the picker.
+ */
+@ConsumerType
+public interface MergedResourcePicker {
+
+    /**
+     * Service property name identifying the root path for the merged resources.
+     * By convention, starts, with /mnt, although this is by no means required.
+     * The value of this service property must not end in a slash.
+     */
+    String MERGE_ROOT = "merge.root";
+
+    /**
+     * Method invoked by the MergingResourceProvider to identify the resources to be merged
for a given
+     * relative path. The resources returned may be either resources returned from the ResourceResolver
+     * directory or an instance of NonExistingResource.
+     * 
+     * @param resolver the ResourceResolver
+     * @param relativePath the path relative to the merge root
+     * @return an iterator of Resource objects
+     */
+    Iterator<Resource> pickResources(ResourceResolver resolver, String relativePath);
+
+}

Added: sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/package-info.java?rev=1622390&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/package-info.java
(added)
+++ sling/trunk/contrib/extensions/resourcemerger/src/main/java/org/apache/sling/resourcemerger/spi/package-info.java
Thu Sep  4 01:37:48 2014
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provides a service to merge multiple physical resources into a single one
+ */
+@Version("1.0.0")
+package org.apache.sling.resourcemerger.spi;
+
+import aQute.bnd.annotation.Version;
+

Modified: sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderTest.java?rev=1622390&r1=1622389&r2=1622390&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderTest.java
(original)
+++ sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderTest.java
Thu Sep  4 01:37:48 2014
@@ -28,6 +28,7 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.api.resource.ValueMap;
@@ -41,7 +42,7 @@ public class MergedResourceProviderTest 
 
     private ResourceResolver resolver;
 
-    private MergedResourceProvider provider;
+    private ResourceProvider provider;
 
     @Before public void setup() throws Exception {
         final MockResourceResolverFactoryOptions options = new MockResourceResolverFactoryOptions();
@@ -71,7 +72,7 @@ public class MergedResourceProviderTest 
                                             .resource(".Z")
                                         .commit();
 
-        this.provider = new MergedResourceProvider("/merged");
+        this.provider = new MergingResourceProvider("/merged", new MergedResourceProviderFactory());
     }
 
     @Test public void testHideChildren() {

Added: sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/OverridingResourceProviderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/OverridingResourceProviderTest.java?rev=1622390&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/OverridingResourceProviderTest.java
(added)
+++ sling/trunk/contrib/extensions/resourcemerger/src/test/java/org/apache/sling/resourcemerger/impl/OverridingResourceProviderTest.java
Thu Sep  4 01:37:48 2014
@@ -0,0 +1,114 @@
+/*
+ * 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.resourcemerger.impl;
+
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+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.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.testing.resourceresolver.MockHelper;
+import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;
+import org.apache.sling.testing.resourceresolver.MockResourceResolverFactoryOptions;
+import org.junit.Before;
+import org.junit.Test;
+
+public class OverridingResourceProviderTest {
+    
+    private static final String SUPER_TYPE = "sling:resourceSuperType";
+
+    private ResourceResolver resolver;
+
+    private MergingResourceProvider provider;
+
+    /*
+     * Tree is:
+     * /apps/a/1/a
+     * /apps/a/1/b
+     * /apps/a/1/b/1
+     * /apps/a/1/c
+     * /apps/a/2/c
+     */
+    @Before
+    public void setup() throws Exception {
+        final MockResourceResolverFactoryOptions options = new MockResourceResolverFactoryOptions();
+        options.setSearchPaths(new String[] {"/apps", "/libs"});
+        final ResourceResolverFactory factory = new MockResourceResolverFactory(options);
+        this.resolver = factory.getAdministrativeResourceResolver(null);
+        MockHelper.create(this.resolver)
+                    .resource("/apps")
+                    .resource("a")
+                    .resource("1").p("a", "1").p("b", "1")
+                    .resource("a").p("1", "a").p("2", "b")
+                    .resource(".b").p("1", "a").p("2", "b")
+                    .resource("1")
+                    .resource("/apps/a/1/c").p("1", "a").p("2", "b")
+                    .resource("/apps/a/2").p(SUPER_TYPE, "a/1").p("b", "2").p(MergedResourceConstants.PN_HIDE_CHILDREN,
new String[] {"b"})
+                    .resource("c").p("1", "c")
+                    .commit();
+
+        this.provider = new MergingResourceProvider("/override", new OverridingResourcePicker());
+    }
+
+    @Test
+    public void testOverridingOnTarget() {
+        final Resource rsrcA2 = this.provider.getResource(this.resolver, "/override/apps/a/2");
+        final ValueMap vm = rsrcA2.adaptTo(ValueMap.class);
+        assertNotNull(vm);
+        assertEquals(3, vm.size()); //3rd is resource:superType
+        assertEquals("1", vm.get("a"));
+        assertEquals("2", vm.get("b"));
+    }
+
+    @Test
+    public void testOverridingViaParent() {
+        final Resource rsrcA2 = this.provider.getResource(this.resolver, "/override/apps/a/2/c");
+        final ValueMap vm = rsrcA2.adaptTo(ValueMap.class);
+        assertNotNull(vm);
+        assertEquals(2, vm.size());
+        assertEquals("c", vm.get("1"));
+        assertEquals("b", vm.get("2"));
+    }
+
+    @Test
+    public void testHideChildrenFromList() {
+        final Resource rsrcA2 = this.provider.getResource(this.resolver, "/override/apps/a/2");
+        final Iterator<Resource> children = this.provider.listChildren(rsrcA2);
+        final List<String> names = new ArrayList<String>();
+        while (children.hasNext()) {
+            names.add(children.next().getName());
+        }
+        assertTrue(names.contains("a"));
+        assertFalse(names.contains("b"));
+        assertTrue(names.contains("c"));
+    }
+
+    @Test
+    public void testHideChildrenFromGet() {
+        assertNotNull(this.provider.getResource(this.resolver, "/override/apps/a/1/b/1"));
+        assertNull(this.provider.getResource(this.resolver, "/override/apps/a/2/b"));
+        assertNull(this.provider.getResource(this.resolver, "/override/apps/a/2/b/1"));
+    }
+
+}



Mime
View raw message