You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2015/05/21 14:10:50 UTC
svn commit: r1680839 - in /sling/trunk/bundles/extensions/discovery/impl: ./
src/main/java/org/apache/sling/discovery/impl/common/resource/
src/test/java/org/apache/sling/discovery/impl/common/resource/
Author: sseifert
Date: Thu May 21 12:10:50 2015
New Revision: 1680839
URL: http://svn.apache.org/r1680839
Log:
SLING-4733 Discovery Impl: Make dependency to JCR respository optional
Added:
sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/resource/ResourceHelperWithoutJcrTest.java (with props)
Modified:
sling/trunk/bundles/extensions/discovery/impl/pom.xml
sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/resource/ResourceHelper.java
Modified: sling/trunk/bundles/extensions/discovery/impl/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/pom.xml?rev=1680839&r1=1680838&r2=1680839&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/pom.xml (original)
+++ sling/trunk/bundles/extensions/discovery/impl/pom.xml Thu May 21 12:10:50 2015
@@ -256,5 +256,10 @@
<version>1.0.2</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.sling-mock</artifactId>
+ <version>1.2.0</version>
+ </dependency>
</dependencies>
</project>
Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/resource/ResourceHelper.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/resource/ResourceHelper.java?rev=1680839&r1=1680838&r2=1680839&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/resource/ResourceHelper.java (original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/resource/ResourceHelper.java Thu May 21 12:10:50 2015
@@ -80,12 +80,60 @@ public class ResourceHelper {
return sb;
}
+ /**
+ * Move resource to given path. Try to do it optimized via JCR API.
+ * If JCR is not available, fallback to Sling Resource API.
+ * @param res Source resource
+ * @param path Target path
+ * @throws PersistenceException
+ */
public static void moveResource(Resource res, String path) throws PersistenceException {
- try{
- Session session = res.adaptTo(Node.class).getSession();
- session.move(res.getPath(), path);
- } catch(RepositoryException re) {
- throw new PersistenceException(String.valueOf(re), re);
+ Node node = res.adaptTo(Node.class);
+ if (node != null) {
+ try {
+ Session session = node.getSession();
+ session.move(res.getPath(), path);
+ }
+ catch (RepositoryException re) {
+ throw new PersistenceException("Move from " + res.getPath() + " to " + path + " failed.", re);
+ }
+ }
+ else {
+ moveResourceWithResourceAPI(res, path);
+ }
+ }
+
+ /**
+ * Move resource to given path with Sling Resource API.
+ * @param res Source resource
+ * @param path target path
+ * @throws PersistenceException
+ */
+ private static void moveResourceWithResourceAPI(Resource res, String path) throws PersistenceException {
+ String parentPath = ResourceUtil.getParent(path);
+ Resource parent = res.getResourceResolver().getResource(parentPath);
+ if (parent == null) {
+ throw new PersistenceException("Parent resource does not exist: " + parentPath);
+ }
+
+ // make move with copy + delete
+ copyResourceWithResourceAPI(res, parent, ResourceUtil.getName(path));
+ res.getResourceResolver().delete(res);
+ }
+
+ /**
+ * Copy resource to given target with Sling Resource API.
+ * @param source Source resource
+ * @param destParent Destination parent
+ * @param name Destination resource name
+ * @throws PersistenceException
+ */
+ private static void copyResourceWithResourceAPI(Resource source, Resource destParent, String name) throws PersistenceException {
+ Resource copy = source.getResourceResolver().create(destParent, name, ResourceUtil.getValueMap(source));
+ Iterator<Resource> children = source.listChildren();
+ while (children.hasNext()) {
+ Resource child = children.next();
+ copyResourceWithResourceAPI(child, copy, child.getName());
}
}
Added: sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/resource/ResourceHelperWithoutJcrTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/resource/ResourceHelperWithoutJcrTest.java?rev=1680839&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/resource/ResourceHelperWithoutJcrTest.java (added)
+++ sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/resource/ResourceHelperWithoutJcrTest.java Thu May 21 12:10:50 2015
@@ -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.discovery.impl.common.resource;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Map;
+
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+public class ResourceHelperWithoutJcrTest {
+
+ private static final Map<String,Object> VALUEMAP_1 = ImmutableMap.<String, Object>builder()
+ .put("prop1", "value1")
+ .put("prop2", 25)
+ .build();
+ private static final Map<String,Object> VALUEMAP_2 = ImmutableMap.<String, Object>builder()
+ .put("prop1", "value2")
+ .put("prop5", true)
+ .build();
+
+ @Rule
+ public SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
+
+ /**
+ * Test moveResource method that normally relies to a JCR move operation without JCR = fallback to resource API.
+ * @throws PersistenceException
+ */
+ @Test
+ public void testMoveResource() throws PersistenceException {
+
+ // prepare some test content
+ Resource source = context.create().resource("/content/path1", VALUEMAP_1);
+ context.create().resource("/content/path1/child1", VALUEMAP_1);
+ context.create().resource("/content/path1/child1/child11", VALUEMAP_1);
+ context.create().resource("/content/path1/child1/child12", VALUEMAP_2);
+ context.create().resource("/content/path1/child2", VALUEMAP_2);
+
+ ResourceHelper.moveResource(source, "/content/path2");
+
+ assertNull(context.resourceResolver().getResource("/content/path1"));
+
+ Resource target = context.resourceResolver().getResource("/content/path2");
+ Resource target1 = context.resourceResolver().getResource("/content/path2/child1");
+ Resource target11 = context.resourceResolver().getResource("/content/path2/child1/child11");
+ Resource target12 = context.resourceResolver().getResource("/content/path2/child1/child12");
+ Resource target2 = context.resourceResolver().getResource("/content/path2/child2");
+
+ assertEquals(VALUEMAP_1, ResourceUtil.getValueMap(target));
+ assertEquals(VALUEMAP_1, ResourceUtil.getValueMap(target1));
+ assertEquals(VALUEMAP_1, ResourceUtil.getValueMap(target11));
+ assertEquals(VALUEMAP_2, ResourceUtil.getValueMap(target12));
+ assertEquals(VALUEMAP_2, ResourceUtil.getValueMap(target2));
+ }
+
+}
Propchange: sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/resource/ResourceHelperWithoutJcrTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/resource/ResourceHelperWithoutJcrTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Thu May 21 12:10:50 2015
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/resource/ResourceHelperWithoutJcrTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain