This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.event.dea-1.0.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-event-dea.git
commit 49f716a2f9da0f6d46374b7c596d92a1d827abea Author: Carsten Ziegeler <[email protected]> AuthorDate: Thu May 28 08:54:14 2015 +0000 SLING-4758 : Make the DEA bundle usable on older Sling installations git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/dea@1682177 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 6 +-- .../event/dea/impl/DistributedEventReceiver.java | 8 ++-- .../event/dea/impl/DistributedEventSender.java | 4 +- .../sling/event/dea/impl/ResourceHelper.java | 52 +++++++++++++++++++++- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index a1bae10..11606d1 100644 --- a/pom.xml +++ b/pom.xml @@ -91,12 +91,12 @@ <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> - <version>5.0.0</version> + <version>4.2.0</version> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.compendium</artifactId> - <version>5.0.0</version> + <version>4.2.0</version> </dependency> <dependency> <groupId>org.apache.sling</groupId> @@ -113,7 +113,7 @@ <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.api</artifactId> - <version>2.8.0</version> + <version>2.4.0</version> <scope>provided</scope> </dependency> <!-- Testing --> diff --git a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventReceiver.java b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventReceiver.java index 5f6d2f9..995a0a9 100644 --- a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventReceiver.java +++ b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventReceiver.java @@ -97,7 +97,7 @@ public class DistributedEventReceiver private volatile Set<String> instances; /** The service registration. */ - private volatile ServiceRegistration<?> serviceRegistration; + private volatile ServiceRegistration serviceRegistration; public DistributedEventReceiver(final BundleContext bundleContext, final String rootPath, @@ -127,7 +127,7 @@ public class DistributedEventReceiver props.put("scheduler.period", 1800L); props.put("scheduler.concurrent", Boolean.FALSE); - final ServiceRegistration<?> reg = + final ServiceRegistration reg = bundleContext.registerService(new String[] {EventHandler.class.getName(), Runnable.class.getName(), TopologyEventListener.class.getName()}, @@ -301,7 +301,7 @@ public class DistributedEventReceiver final Resource baseResource = resolver.getResource(this.rootPath); // sanity check - should never be null if ( baseResource != null ) { - final ResourceUtil.BatchResourceRemover brr = ResourceUtil.getBatchResourceRemover(50); + final ResourceHelper.BatchResourceRemover brr = ResourceHelper.getBatchResourceRemover(50); final Iterator<Resource> iter = baseResource.listChildren(); while ( iter.hasNext() ) { final Resource rootResource = iter.next(); @@ -333,7 +333,7 @@ public class DistributedEventReceiver ResourceResolver resolver = null; try { resolver = this.resourceResolverFactory.getAdministrativeResourceResolver(null); - final ResourceUtil.BatchResourceRemover brr = ResourceUtil.getBatchResourceRemover(50); + final ResourceHelper.BatchResourceRemover brr = ResourceHelper.getBatchResourceRemover(50); final Resource baseResource = resolver.getResource(this.ownRootPath); // sanity check - should never be null diff --git a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventSender.java b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventSender.java index 95aa021..8bc982f 100644 --- a/src/main/java/org/apache/sling/event/dea/impl/DistributedEventSender.java +++ b/src/main/java/org/apache/sling/event/dea/impl/DistributedEventSender.java @@ -69,7 +69,7 @@ public class DistributedEventSender private final String ownRootPathWithSlash; - private volatile ServiceRegistration<?> serviceRegistration; + private volatile ServiceRegistration serviceRegistration; public DistributedEventSender(final BundleContext bundleContext, final String rootPath, @@ -92,7 +92,7 @@ public class DistributedEventSender props.put(EventConstants.EVENT_TOPIC, SlingConstants.TOPIC_RESOURCE_ADDED); props.put(EventConstants.EVENT_FILTER, "(path=" + rootPath + "/*)"); - final ServiceRegistration<?> reg = + final ServiceRegistration reg = bundleContext.registerService(new String[] {EventHandler.class.getName()}, DistributedEventSender.this, props); diff --git a/src/main/java/org/apache/sling/event/dea/impl/ResourceHelper.java b/src/main/java/org/apache/sling/event/dea/impl/ResourceHelper.java index 0694b74..4020704 100644 --- a/src/main/java/org/apache/sling/event/dea/impl/ResourceHelper.java +++ b/src/main/java/org/apache/sling/event/dea/impl/ResourceHelper.java @@ -27,7 +27,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +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.ResourceUtil; import org.apache.sling.api.resource.ValueMap; public abstract class ResourceHelper { @@ -83,7 +86,7 @@ public abstract class ResourceHelper { } public static ValueMap getValueMap(final Resource resource) throws InstantiationException { - final ValueMap vm = resource.getValueMap(); + final ValueMap vm = ResourceUtil.getValueMap(resource); // trigger full loading try { vm.size(); @@ -93,4 +96,51 @@ public abstract class ResourceHelper { } return vm; } + + /** + * A batch resource remover deletes resources in batches. Once the batch + * size (threshold) is reached, an intermediate commit is performed. Resource + * trees are deleted resource by resource starting with the deepest children first. + * Once all resources have been passed to the batch resource remover, a final + * commit needs to be called on the resource resolver. + */ + public static class BatchResourceRemover { + + private final int max; + + private int count; + + public BatchResourceRemover(final int batchSize) { + this.max = (batchSize < 1 ? 50 : batchSize); + } + + public void delete(final Resource rsrc) + throws PersistenceException { + final ResourceResolver resolver = rsrc.getResourceResolver(); + for(final Resource child : rsrc.getChildren()) { + delete(child); + } + resolver.delete(rsrc); + count++; + if ( count >= max ) { + resolver.commit(); + count = 0; + } + } + } + + /** + * Create a batch resource remover. + * A batch resource remove can be used to delete resources in batches. + * Once the passed in threshold of deleted resources is reached, an intermediate + * commit is called on the resource resolver. In addition the batch remover + * deletes a resource recursively. + * Once all resources to delete are passed to the remover, a final commit needs + * to be call on the resource resolver. + * @param threshold The threshold for the intermediate saves. + * @return A new batch resource remover. + */ + public static BatchResourceRemover getBatchResourceRemover(final int threshold) { + return new BatchResourceRemover(threshold); + } } \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
