You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:59:08 UTC
[sling-org-apache-sling-resourcemerger] 03/05: SLING-3420 : Revert
implementation of ModifyingResourceProvider and provide an utility method
instead
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.resourcemerger-1.1.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourcemerger.git
commit 68dcf374e735823c81a9752cf2052ee5df380ec3
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Feb 28 09:21:27 2014 +0000
SLING-3420 : Revert implementation of ModifyingResourceProvider and provide an utility method instead
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/resourcemerger@1572894 13f79535-47bb-0310-9956-ffa450edef68
---
.../resourcemerger/api/ResourceMergerService.java | 18 +++
.../sling/resourcemerger/api/package-info.java | 3 +-
.../sling/resourcemerger/impl/MergedResource.java | 34 +---
.../impl/MergedResourceProvider.java | 178 ++-------------------
.../impl/MergedResourceProviderFactory.java | 13 ++
.../impl/MergedResourceProviderTest.java | 28 ----
6 files changed, 44 insertions(+), 230 deletions(-)
diff --git a/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java b/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
index 9ec7da9..80d03ba 100644
--- a/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
+++ b/src/main/java/org/apache/sling/resourcemerger/api/ResourceMergerService.java
@@ -54,4 +54,22 @@ public interface ResourceMergerService {
* @return Returns <code>true</code> if the provided {@link Resource} is a merged resource.
*/
boolean isMergedResource(Resource resource);
+
+ /**
+ * Return a resource path by taking the path of the merged resource, removing
+ * the mount point and replacing it with the search path.
+ *
+ * For example, if the provided search path is "/apps/" and the merged resource
+ * path is "/mnt/overlay/my/resource", the result will be "/apps/my/resource".
+ *
+ * @param searchPath The search path, this is an absolute path ending with a slash
+ * as returned by the resource resolver
+ * @param mergedResourcePath An absolute path to a merged resource
+ * @return The path to the resource
+ * @throws IllegalArgumentException If search path is not absolute or does not end
+ * with a slash or if the merged resource path
+ * is not within the space of the merged resources.
+ * @since 1.1
+ */
+ String getResourcePath(String searchPath, String mergedResourcePath);
}
diff --git a/src/main/java/org/apache/sling/resourcemerger/api/package-info.java b/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
index 2c28f9b..4d75dec 100644
--- a/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
+++ b/src/main/java/org/apache/sling/resourcemerger/api/package-info.java
@@ -20,7 +20,8 @@
/**
* Provides a service to merge multiple physical resources into a single one
*/
-@Version("1.0.0")
+@Version("1.1.0")
package org.apache.sling.resourcemerger.api;
import aQute.bnd.annotation.Version;
+
diff --git a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
index 34c98af..47f89ed 100644
--- a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
+++ b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
@@ -21,12 +21,9 @@ package org.apache.sling.resourcemerger.impl;
import java.util.List;
import org.apache.sling.api.resource.AbstractResource;
-import org.apache.sling.api.resource.ModifiableValueMap;
-import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
/**
@@ -49,9 +46,6 @@ public class MergedResource extends AbstractResource {
/** Cache value map. */
private final ValueMap properties;
- /** Root path */
- private final String mergedRootPath;
-
/**
* Constructor
*
@@ -64,8 +58,7 @@ public class MergedResource extends AbstractResource {
final String mergeRootPath,
final String relativePath,
final List<Resource> mappedResources,
- final List<ValueMap> valueMaps,
- final String mergedRootPath) {
+ final List<ValueMap> valueMaps) {
this.resolver = resolver;
this.path = (relativePath.length() == 0 ? mergeRootPath : mergeRootPath + "/" + relativePath);
this.properties = new MergedValueMap(valueMaps);
@@ -78,7 +71,6 @@ public class MergedResource extends AbstractResource {
i++;
}
metadata.put(MergedResourceConstants.METADATA_RESOURCES, resourcePaths);
- this.mergedRootPath = mergedRootPath;
}
/**
@@ -127,30 +119,6 @@ public class MergedResource extends AbstractResource {
if (type == ValueMap.class) {
return (AdapterType) this.properties;
}
- if (type == ModifiableValueMap.class) {
- final String paths[] = (String[])this.metadata.get(MergedResourceConstants.METADATA_RESOURCES);
- final String[] searchPaths = resolver.getSearchPath();
- final String lastSearchPath = searchPaths[searchPaths.length-1];
-
- if ( paths.length == 1 && paths[0].startsWith(lastSearchPath) ) {
- final Resource copyResource = resolver.getResource(paths[0]);
- if ( searchPaths.length == 1 ) {
- return (AdapterType)copyResource.adaptTo(ModifiableValueMap.class);
- }
- final String prefix = searchPaths[searchPaths.length-2];
- final String createPath = prefix + path.substring(this.mergedRootPath.length() + 1);
- try {
- final Resource newResource = ResourceUtil.getOrCreateResource(resolver, ResourceUtil.getParent(createPath),copyResource.getResourceType(), null, false);
- return (AdapterType)newResource.adaptTo(ModifiableValueMap.class);
- } catch ( final PersistenceException pe) {
- // we ignore this for now
- return null;
- }
- }
- final String resourcePath = paths[paths.length-1];
- final Resource rsrc = resolver.getResource(resourcePath);
- return (AdapterType)rsrc.adaptTo(ModifiableValueMap.class);
- }
return super.adaptTo(type);
}
diff --git a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
index ece03c0..f7b1bd0 100644
--- a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
+++ b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProvider.java
@@ -19,15 +19,11 @@
package org.apache.sling.resourcemerger.impl;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
-import java.util.Map;
import javax.servlet.http.HttpServletRequest;
-import org.apache.sling.api.resource.ModifyingResourceProvider;
-import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceProvider;
import org.apache.sling.api.resource.ResourceResolver;
@@ -39,7 +35,7 @@ import org.apache.sling.api.resource.ValueMap;
* access to {@link MergedResource} objects.
*/
public class MergedResourceProvider
- implements ResourceProvider, ModifyingResourceProvider {
+ implements ResourceProvider {
private final String mergeRootPath;
@@ -54,46 +50,23 @@ public class MergedResourceProvider
return getResource(resolver, path);
}
- private static final class ExcludeEntry {
-
- public final String name;
- public final boolean exclude;
-
- public ExcludeEntry(final String value) {
- if ( value.startsWith("!!") ) {
- this.name = value.substring(1);
- this.exclude = false;
- } else if ( value.startsWith("!") ) {
- this.name = value.substring(1);
- this.exclude = true;
- } else {
- this.name = value;
- this.exclude = false;
- }
- }
- }
-
private static final class ParentHidingHandler {
- private final List<ExcludeEntry> entries = new ArrayList<MergedResourceProvider.ExcludeEntry>();
+ private final String[] childrenToHideArray;
public ParentHidingHandler(final Resource parent) {
final ValueMap parentProps = ResourceUtil.getValueMap(parent);
- final String[] childrenToHideArray = parentProps.get(MergedResourceConstants.PN_HIDE_CHILDREN, String[].class);
- if ( childrenToHideArray != null ) {
- for(final String value : childrenToHideArray) {
- final ExcludeEntry entry = new ExcludeEntry(value);
- this.entries.add(entry);
- }
- }
+ this.childrenToHideArray = parentProps.get(MergedResourceConstants.PN_HIDE_CHILDREN, String[].class);
}
public boolean isHidden(final String name) {
boolean hidden = false;
- for(final ExcludeEntry entry : this.entries) {
- if ( entry.name.equals("*") || entry.name.equals(name) ) {
- hidden = !entry.exclude;
- break;
+ if ( this.childrenToHideArray != null ) {
+ for(final String entry : childrenToHideArray) {
+ if ( entry.equals("*") || entry.equals(name) ) {
+ hidden = true;
+ break;
+ }
}
}
return hidden;
@@ -173,7 +146,7 @@ public class MergedResourceProvider
if (!holder.resources.isEmpty()) {
// create a new merged resource based on the list of mapped physical resources
- return new MergedResource(resolver, mergeRootPath, relativePath, holder.resources, holder.valueMaps, this.mergeRootPath);
+ return new MergedResource(resolver, mergeRootPath, relativePath, holder.resources, holder.valueMaps);
}
return null;
}
@@ -272,135 +245,4 @@ public class MergedResourceProvider
}
return null;
}
-
- private ResourceHolder getAllResources(final ResourceResolver resolver,
- final String path,
- final String relativePath) {
- 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
- final Resource parent = resolver.getResource(ResourceUtil.getParent(fullPath));
- if ( parent != null ) {
- 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 holder;
- }
-
- /**
- * @see org.apache.sling.api.resource.ModifyingResourceProvider#create(org.apache.sling.api.resource.ResourceResolver, java.lang.String, java.util.Map)
- */
- public Resource create(final ResourceResolver resolver,
- final String path,
- final Map<String, Object> properties)
- throws PersistenceException {
- // we only support modifications if there is more than one search path
- final String[] searchPaths = resolver.getSearchPath();
- if ( searchPaths.length < 2 ) {
- throw new PersistenceException("Modifying is only supported with at least two search paths", null, path, null);
- }
- // check if the resource exists
- final Resource mountResource = this.getResource(resolver, path);
- if ( mountResource != null ) {
- throw new PersistenceException("Resource at " + path + " already exists.", null, path, null);
- }
- // creating of the root mount resource is not supported
- final String relativePath = getRelativePath(path);
- if ( relativePath == null || relativePath.length() == 0 ) {
- throw new PersistenceException("Resource at " + path + " can't be created.", null, path, null);
- }
-
- final String lastSearchPath = searchPaths[searchPaths.length-1];
- final ResourceHolder holder = this.getAllResources(resolver, path, relativePath);
- if ( holder.resources.size() == 0 || holder.resources.size() == 1 && holder.resources.get(0).getPath().startsWith(lastSearchPath) ) {
- final String useSearchPath = searchPaths[searchPaths.length-2];
-
- final String createPath = useSearchPath + path.substring(this.mergeRootPath.length() + 1);
- final Resource parentResource = ResourceUtil.getOrCreateResource(resolver, ResourceUtil.getParent(createPath), (String)null, null, false);
- resolver.create(parentResource, ResourceUtil.getName(createPath), properties);
- }
- // TODO check hiding flag
- return this.getResource(resolver, path);
- }
-
- /**
- * @see org.apache.sling.api.resource.ModifyingResourceProvider#delete(org.apache.sling.api.resource.ResourceResolver, java.lang.String)
- */
- public void delete(final ResourceResolver resolver, final String path)
- throws PersistenceException {
- // we only support modifications if there is more than one search path
- final String[] searchPaths = resolver.getSearchPath();
- if ( searchPaths.length < 2 ) {
- throw new PersistenceException("Modifying is only supported with at least two search paths");
- }
- // deleting of the root mount resource is not supported
- final String relativePath = getRelativePath(path);
- if ( relativePath == null || relativePath.length() == 0 ) {
- throw new PersistenceException("Resource at " + path + " can't be created.", null, path, null);
- }
-
- // check if the resource exists
- final Resource mntResource = this.getResource(resolver, path);
- if ( mntResource == null ) {
- throw new PersistenceException("Resource at " + path + " does not exist", null, path, null);
- }
- final ResourceHolder holder = this.getAllResources(resolver, path, relativePath);
- final String lastSearchPath = searchPaths[searchPaths.length-1];
-
- int deleted = 0;
- for(final Resource rsrc : holder.resources) {
- final String p = rsrc.getPath();
- if ( !p.startsWith(lastSearchPath) ) {
- resolver.delete(rsrc);
- deleted++;
- }
- }
- if ( deleted < holder.resources.size() ) {
- // create overlay resource which is hiding the other
- final String prefix = searchPaths[searchPaths.length-2];
- final String createPath = prefix + path.substring(this.mergeRootPath.length() + 1);
- final Resource parentResource = ResourceUtil.getOrCreateResource(resolver, ResourceUtil.getParent(createPath), (String)null, null, false);
- final Map<String, Object> properties = new HashMap<String, Object>();
- properties.put(MergedResourceConstants.PN_HIDE_RESOURCE, Boolean.TRUE);
- resolver.create(parentResource, ResourceUtil.getName(createPath), properties);
- }
- }
-
- /**
- * @see org.apache.sling.api.resource.ModifyingResourceProvider#revert(org.apache.sling.api.resource.ResourceResolver)
- */
- public void revert(final ResourceResolver resolver) {
- // the provider for the search paths will revert
- }
-
- /**
- * @see org.apache.sling.api.resource.ModifyingResourceProvider#commit(org.apache.sling.api.resource.ResourceResolver)
- */
- public void commit(final ResourceResolver resolver) throws PersistenceException {
- // the provider for the search paths will commit
- }
-
- /**
- * @see org.apache.sling.api.resource.ModifyingResourceProvider#hasChanges(org.apache.sling.api.resource.ResourceResolver)
- */
- public boolean hasChanges(final ResourceResolver resolver) {
- // the provider for the search paths will return in case of changes
- return false;
- }
}
diff --git a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
index 5cbdfb2..3357b8a 100644
--- a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
+++ b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderFactory.java
@@ -112,6 +112,19 @@ public class MergedResourceProviderFactory implements ResourceProviderFactory, R
return Boolean.TRUE.equals(resource.getResourceMetadata().get(MergedResourceConstants.METADATA_FLAG));
}
+ /**
+ * {@inheritDoc}
+ */
+ public String getResourcePath(final String searchPath, final String mergedResourcePath) {
+ if( searchPath == null || !searchPath.startsWith("/") || !searchPath.endsWith("/") ) {
+ throw new IllegalArgumentException("Provided path is not a valid search path: " + searchPath);
+ }
+ if ( mergedResourcePath == null || !mergedResourcePath.startsWith(this.mergeRootPath + "/") ) {
+ throw new IllegalArgumentException("Provided path does not point to a merged resource: " + mergedResourcePath);
+ }
+ return searchPath + mergedResourcePath.substring(this.mergeRootPath.length() + 1);
+ }
+
@Activate
protected void configure(final Map<String, Object> properties) {
mergeRootPath = PropertiesUtil.toString(properties.get(ResourceProvider.ROOTS), DEFAULT_ROOT);
diff --git a/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderTest.java b/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderTest.java
index 0034a79..315e4e4 100644
--- a/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderTest.java
+++ b/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceProviderTest.java
@@ -24,15 +24,12 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.Iterator;
import java.util.List;
-import org.apache.sling.api.resource.PersistenceException;
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.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.testing.resourceresolver.MockHelper;
import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;
@@ -153,29 +150,4 @@ public class MergedResourceProviderTest {
assertEquals("2", vm.get("e"));
assertEquals("x", vm.get("b"));
}
-
- @Test public void testSimpleCreateAndDelete() throws PersistenceException {
- final String path = "/merged/a/new";
- try {
- final Resource rsrc = this.provider.create(this.resolver, path, Collections.singletonMap("foo", (Object)"bla"));
- assertNotNull(rsrc);
- assertEquals(path, rsrc.getPath());
- final ValueMap vm = ResourceUtil.getValueMap(rsrc);
- assertEquals("bla", vm.get("foo"));
-
- final Resource realResource = this.resolver.getResource("/apps/a/new");
- assertNotNull(realResource);
- final ValueMap vmReal = ResourceUtil.getValueMap(realResource);
- assertEquals("bla", vmReal.get("foo"));
- assertNull(this.resolver.getResource("/libs/a/new"));
-
- this.provider.delete(this.resolver, path);
- assertNull(this.provider.getResource(this.resolver, path));
- assertNull(this.resolver.getResource("/libs/a/new"));
- assertNull(this.resolver.getResource("/apps/a/new"));
-
- } finally {
- this.resolver.revert();
- }
- }
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.