You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by je...@apache.org on 2018/10/16 17:17:22 UTC
[sling-org-apache-sling-distribution-core] 01/01: example of sling
resource filter usage
This is an automated email from the ASF dual-hosted git repository.
jeb pushed a commit to branch example-rf
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-core.git
commit 4ed43e55ccec4f1cc588c2d04d2dd338b7f22cf5
Author: JE Bailey <ja...@sas.com>
AuthorDate: Tue Oct 16 13:13:32 2018 -0400
example of sling resource filter usage
---
pom.xml | 8 +
.../queue/impl/resource/ResourceIterator.java | 123 ---------------
.../impl/resource/ResourceQueueCleanupTask.java | 51 +++----
.../queue/impl/resource/ResourceQueueUtils.java | 62 +++-----
.../queue/impl/resource/ResourceIteratorTest.java | 166 ---------------------
5 files changed, 53 insertions(+), 357 deletions(-)
diff --git a/pom.xml b/pom.xml
index 8f62adf..d51a336 100644
--- a/pom.xml
+++ b/pom.xml
@@ -50,6 +50,7 @@
<properties>
<exam.version>4.11.0</exam.version>
+ <sling.java.version>8</sling.java.version>
</properties>
<!-- ======================================================================= -->
@@ -385,6 +386,13 @@
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.resource.filter</artifactId>
+ <version>1.0.0</version>
+ <type>bundle</type>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<reporting>
<plugins>
diff --git a/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceIterator.java b/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceIterator.java
deleted file mode 100644
index 4236d56..0000000
--- a/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceIterator.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * 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.distribution.queue.impl.resource;
-
-
-import org.apache.sling.api.resource.Resource;
-
-import java.util.Iterator;
-import java.util.Stack;
-
-public class ResourceIterator implements Iterator<Resource> {
-
- private final String folderResourceType;
- private final boolean includeFolders;
- private final boolean includeLeafs;
- private final boolean includeRoot = false;
- private final String rootPath;
-
- private Stack<Iterator<Resource>> folderIterators = new Stack<Iterator<Resource>>();
-
- private Resource currentFolder;
- private Iterator<Resource> currentIterator;
-
- private Resource next;
-
-
- public ResourceIterator(Resource root, String folderResourceType, boolean includeFolders, boolean includeLeafs) {
- this.folderResourceType = folderResourceType;
- this.includeFolders = includeFolders;
- this.includeLeafs = includeLeafs;
- this.rootPath = root.getPath();
-
- currentFolder = root;
- currentIterator = currentFolder.listChildren();
- next = seek();
- }
-
- @Override
- public boolean hasNext() {
- return next != null;
- }
-
- @Override
- public Resource next() {
- Resource result = next;
- next = seek();
- return result;
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- Resource seek() {
- Resource res;
- while ((res = seekAll()) != null) {
-
- if (rootPath.equals(res.getPath())) {
- if (includeRoot) {
- return res;
- } else {
- continue;
- }
- }
-
- if (includeFolders && res.isResourceType(folderResourceType)) {
- return res;
- }
-
- if (includeLeafs && !res.isResourceType(folderResourceType)) {
- return res;
- }
- }
-
- return null;
- }
-
- // depth first, post order (children before parents)
- Resource seekAll() {
-
- while (currentIterator != null) {
- if (currentIterator.hasNext()) {
- Resource res = currentIterator.next();
-
- if (res.isResourceType(folderResourceType)) {
- folderIterators.push(currentIterator);
-
- currentFolder = res;
- currentIterator = currentFolder.listChildren();
- } else {
- return res;
- }
- } else {
- Resource folder = currentFolder;
-
- currentFolder = currentFolder.getParent();
- currentIterator = folderIterators.empty() ? null : folderIterators.pop();
-
- return folder;
- }
- }
-
- return null;
- }
-}
diff --git a/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceQueueCleanupTask.java b/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceQueueCleanupTask.java
index f41b16e..ca2ae64 100644
--- a/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceQueueCleanupTask.java
+++ b/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceQueueCleanupTask.java
@@ -19,17 +19,19 @@
package org.apache.sling.distribution.queue.impl.resource;
+import java.util.Calendar;
+import java.util.Iterator;
+
+import org.apache.sling.api.SlingException;
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.distribution.util.impl.DistributionUtils;
+import org.apache.sling.resource.filter.ResourceFilterStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.Calendar;
-import java.util.Iterator;
-
public class ResourceQueueCleanupTask implements Runnable {
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -79,34 +81,25 @@ public class ResourceQueueCleanupTask implements Runnable {
now.add(Calendar.MINUTE, -5);
ResourceResolver resolver = root.getResourceResolver();
- ResourceIterator it = new ResourceIterator(root, ResourceQueueUtils.RESOURCE_FOLDER, true, false);
+
+ ResourceFilterStream rfs = root.adaptTo(ResourceFilterStream.class);
+ rfs.setChildSelector("[sling:resourceType] == 'sling:OrderedFolder'");
String nowPath = ResourceQueueUtils.getTimePath(now);
- while (it.hasNext()) {
- Resource res = it.next();
-
- String resPath = res.getPath().substring(root.getPath().length()+1);
-
- if (!res.isResourceType(ResourceQueueUtils.RESOURCE_FOLDER)) {
- continue;
- }
-
- // now 2018/03/15/03/48
- // res 2018/02/15
- if (!ResourceQueueUtils.isSafeToDelete(nowPath, resPath)) {
- continue;
- }
-
- if (res.hasChildren()) {
- continue;
- }
-
- log.debug("removing empty folder {}", res.getPath());
-
- resolver.delete(res);
- resolver.commit();
- }
+ rfs.stream().filter(resource ->{
+ String resPath = resource.getPath().substring(root.getPath().length()+1);
+ return ResourceQueueUtils.isSafeToDelete(nowPath, resPath);
+ }).filter(resource -> !resource.hasChildren()).forEach(resource -> delete(resolver,resource));
+
+ resolver.commit();
}
-}
+
+ private static void delete(ResourceResolver resolver,Resource resource) {
+ try {
+ resolver.delete(resource);
+ } catch (PersistenceException e) {
+ throw new SlingException("unable to delete resoure", e);
+ }
+ }}
diff --git a/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceQueueUtils.java b/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceQueueUtils.java
index 1408cb6..533c896 100644
--- a/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceQueueUtils.java
+++ b/src/main/java/org/apache/sling/distribution/queue/impl/resource/ResourceQueueUtils.java
@@ -20,6 +20,24 @@
package org.apache.sling.distribution.queue.impl.resource;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
+
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryManager;
+import javax.jcr.query.QueryResult;
+
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
@@ -31,27 +49,10 @@ import org.apache.sling.distribution.queue.DistributionQueueEntry;
import org.apache.sling.distribution.queue.DistributionQueueItem;
import org.apache.sling.distribution.queue.DistributionQueueItemState;
import org.apache.sling.distribution.queue.DistributionQueueItemStatus;
+import org.apache.sling.resource.filter.ResourceFilterStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
-import javax.jcr.NodeIterator;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-import javax.jcr.query.Query;
-import javax.jcr.query.QueryManager;
-import javax.jcr.query.QueryResult;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-import java.util.concurrent.atomic.AtomicLong;
-
public class ResourceQueueUtils {
// prefix for queue entry ids
@@ -150,28 +151,11 @@ public class ResourceQueueUtils {
}
static List<DistributionQueueEntry> getEntries(Resource queueRoot, int skip, int limit) {
- Iterator<Resource> it = new ResourceIterator(queueRoot, RESOURCE_FOLDER, false, true);
-
- List<DistributionQueueEntry> entries = new ArrayList<DistributionQueueEntry>();
-
- int i = 0;
- while (it.hasNext()) {
- Resource resource = it.next();
-
- if (i++ < skip) {
- continue;
- }
-
- DistributionQueueEntry entry = readEntry(queueRoot, resource);
- entries.add(entry);
-
- if (entries.size() >= limit) {
- break;
- }
- }
-
- return entries;
+ ResourceFilterStream rfs = queueRoot.adaptTo(ResourceFilterStream.class);
+ rfs.setChildSelector("[sling:resourceType] != 'sling:OrderedFolder'");
+ return rfs.stream().skip(skip).limit(limit).map(resource -> readEntry(queueRoot, resource))
+ .collect(Collectors.toList());
}
diff --git a/src/test/java/org/apache/sling/distribution/queue/impl/resource/ResourceIteratorTest.java b/src/test/java/org/apache/sling/distribution/queue/impl/resource/ResourceIteratorTest.java
deleted file mode 100644
index 285ca3a..0000000
--- a/src/test/java/org/apache/sling/distribution/queue/impl/resource/ResourceIteratorTest.java
+++ /dev/null
@@ -1,166 +0,0 @@
-/*
- * 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.distribution.queue.impl.resource;
-
-import org.apache.sling.api.resource.PersistenceException;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.testing.mock.sling.junit.SlingContext;
-import org.apache.sling.testing.resourceresolver.MockHelper;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import static org.junit.Assert.assertEquals;
-
-public class ResourceIteratorTest {
-
- static final String FOLDER_TYPE = ResourceQueueUtils.RESOURCE_FOLDER;
- static final String ITEM_TYPE = "nt:unstructured";
-
- @Rule
- public final SlingContext context = new SlingContext();
-
- @Before
- public void testSetup() {
- MockHelper helper = MockHelper.create(context.resourceResolver());
-
- helper.resource("/root").p("prop", "value")
- .resource("2018").p("sling:resourceType", FOLDER_TYPE)
- .resource("10").p("sling:resourceType", FOLDER_TYPE)
- .resource("15").p("sling:resourceType", FOLDER_TYPE)
- .resource("11").p("sling:resourceType", FOLDER_TYPE)
- .resource("35").p("sling:resourceType", FOLDER_TYPE)
- .resource("item1").p("sling:resourceType", ITEM_TYPE)
- .resource("../item2").p("sling:resourceType", ITEM_TYPE)
- .resource("../../36").p("sling:resourceType", FOLDER_TYPE)
- .resource("item3").p("sling:resourceType", ITEM_TYPE);
-
- helper.resource("/root2").p("prop", "value")
- .resource("2018").p("sling:resourceType", FOLDER_TYPE)
- .resource("10").p("sling:resourceType", FOLDER_TYPE)
- .resource("15").p("sling:resourceType", FOLDER_TYPE)
- .resource("11").p("sling:resourceType", FOLDER_TYPE)
- .resource("35").p("sling:resourceType", FOLDER_TYPE)
- .resource("../36").p("sling:resourceType", FOLDER_TYPE)
- .resource("item3").p("sling:resourceType", ITEM_TYPE);
-
- try {
- helper.commit();
- } catch (PersistenceException e) {
-
- }
-
- }
-
- @Test
- public void testEmptyRoot() {
- Resource root = context.resourceResolver().getResource("/root");
-
- ResourceIterator it = new ResourceIterator(root, ResourceQueueUtils.RESOURCE_FOLDER, false, false);
-
-
- test(it, new String[0]);
- }
-
- @Test
- public void testFullPath() {
- Resource root = context.resourceResolver().getResource("/root");
-
- ResourceIterator it = new ResourceIterator(root, ResourceQueueUtils.RESOURCE_FOLDER, true, true);
-
- test(it, new String[] {
- "/root/2018/10/15/11/35/item1",
- "/root/2018/10/15/11/35/item2",
- "/root/2018/10/15/11/35",
- "/root/2018/10/15/11/36/item3",
- "/root/2018/10/15/11/36",
- "/root/2018/10/15/11",
- "/root/2018/10/15",
- "/root/2018/10",
- "/root/2018"
- });
- }
-
- @Test
- public void testFullPath2() {
- Resource root = context.resourceResolver().getResource("/root2");
-
- ResourceIterator it = new ResourceIterator(root, ResourceQueueUtils.RESOURCE_FOLDER, true, true);
-
- test(it, new String[] {
- "/root2/2018/10/15/11/35",
- "/root2/2018/10/15/11/36/item3",
- "/root2/2018/10/15/11/36",
- "/root2/2018/10/15/11",
- "/root2/2018/10/15",
- "/root2/2018/10",
- "/root2/2018"
- });
- }
-
- @Test
- public void testFullPathNoFolder() {
- Resource root = context.resourceResolver().getResource("/root");
-
- ResourceIterator it = new ResourceIterator(root, ResourceQueueUtils.RESOURCE_FOLDER, false, true);
-
-
- test(it, new String[] {
- "/root/2018/10/15/11/35/item1",
- "/root/2018/10/15/11/35/item2",
- "/root/2018/10/15/11/36/item3",
- });
- }
-
- @Test
- public void testFullPathNoLeafs() {
- Resource root = context.resourceResolver().getResource("/root");
-
- ResourceIterator it = new ResourceIterator(root, ResourceQueueUtils.RESOURCE_FOLDER, true, false);
-
- test(it, new String[] {
- "/root/2018/10/15/11/35",
- "/root/2018/10/15/11/36",
- "/root/2018/10/15/11",
- "/root/2018/10/15",
- "/root/2018/10",
- "/root/2018"
- });
- }
-
- public void test(ResourceIterator it, String[] paths) {
- List<String> expected = Arrays.asList(paths);
-
- assertEquals(expected, toPaths(it));
- }
-
-
- List<String> toPaths(ResourceIterator it) {
- List<String> paths = new ArrayList<String>();
- while (it.hasNext()) {
- paths.add(it.next().getPath());
- }
- return paths;
- }
-}