From e518f6fee7bcb58d98402a9fc08b91ca13bb9cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hoh?= Date: Mon, 13 Dec 2021 09:39:31 +0100 Subject: [PATCH] #841 change listener to resource change listener (#846) * Update Historical Versions (#838) As per System Requirements mentioned at https://github.com/adobe/aem-project-archetype/tree/master * #841 move to the recommended ResourceChangeListener Co-authored-by: Pankaj Parashar --- .../listeners/SimpleResourceListener.java | 30 +++++++++---------- .../listeners/SimpleResourceListenerTest.java | 28 +++++++++-------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/main/archetype/core/src/main/java/core/listeners/SimpleResourceListener.java b/src/main/archetype/core/src/main/java/core/listeners/SimpleResourceListener.java index 8db17cf00..996236ee2 100644 --- a/src/main/archetype/core/src/main/java/core/listeners/SimpleResourceListener.java +++ b/src/main/archetype/core/src/main/java/core/listeners/SimpleResourceListener.java @@ -15,35 +15,35 @@ */ package ${package}.core.listeners; -import org.apache.sling.api.SlingConstants; +import java.util.List; + +import org.apache.sling.api.resource.observation.ResourceChange; +import org.apache.sling.api.resource.observation.ResourceChangeListener; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.propertytypes.ServiceDescription; -import org.osgi.service.event.Event; -import org.osgi.service.event.EventConstants; -import org.osgi.service.event.EventHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * A service to demonstrate how changes in the resource tree - * can be listened for. It registers an event handler service. - * The component is activated immediately after the bundle is - * started through the immediate flag. + * can be listened for. * Please note, that apart from EventHandler services, * the immediate flag should not be set on a service. */ -@Component(service = EventHandler.class, - immediate = true, - property = { - EventConstants.EVENT_TOPIC + "=org/apache/sling/api/resource/Resource/*" - }) +@Component(service = ResourceChangeListener.class, + immediate = true +) @ServiceDescription("Demo to listen on changes in the resource tree") -public class SimpleResourceListener implements EventHandler { +public class SimpleResourceListener implements ResourceChangeListener { private final Logger logger = LoggerFactory.getLogger(getClass()); - public void handleEvent(final Event event) { - logger.debug("Resource event: {} at: {}", event.getTopic(), event.getProperty(SlingConstants.PROPERTY_PATH)); + @Override + public void onChange(List changes) { + changes.forEach(change -> { + logger.debug("Resource event: {} at: {} isExternal", change.getType(), change.getPath(), change.isExternal()); + }); + } } diff --git a/src/main/archetype/core/src/test/java/core/listeners/SimpleResourceListenerTest.java b/src/main/archetype/core/src/test/java/core/listeners/SimpleResourceListenerTest.java index 0be4042f1..84913b46c 100644 --- a/src/main/archetype/core/src/test/java/core/listeners/SimpleResourceListenerTest.java +++ b/src/main/archetype/core/src/test/java/core/listeners/SimpleResourceListenerTest.java @@ -15,21 +15,21 @@ */ package ${package}.core.listeners; -import java.util.Collections; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; import java.util.List; -import org.apache.sling.api.SlingConstants; +import org.apache.sling.api.resource.observation.ResourceChange; +import org.apache.sling.api.resource.observation.ResourceChange.ChangeType; import org.junit.jupiter.api.Test; -import org.osgi.service.event.Event; import uk.org.lidalia.slf4jext.Level; import uk.org.lidalia.slf4jtest.LoggingEvent; import uk.org.lidalia.slf4jtest.TestLogger; import uk.org.lidalia.slf4jtest.TestLoggerFactory; -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertEquals; - class SimpleResourceListenerTest { private SimpleResourceListener fixture = new SimpleResourceListener(); @@ -38,9 +38,10 @@ class SimpleResourceListenerTest { @Test void handleEvent() { - Event resourceEvent = new Event("event/topic", Collections.singletonMap(SlingConstants.PROPERTY_PATH, "/content/test")); - - fixture.handleEvent(resourceEvent); + + ResourceChange change = new ResourceChange(ChangeType.ADDED,"/content/test", false); + + fixture.onChange(Arrays.asList(change)); List events = logger.getLoggingEvents(); assertEquals(1, events.size()); @@ -48,9 +49,10 @@ void handleEvent() { assertAll( () -> assertEquals(Level.DEBUG, event.getLevel()), - () -> assertEquals(2, event.getArguments().size()), - () -> assertEquals("event/topic", event.getArguments().get(0)), - () -> assertEquals("/content/test", event.getArguments().get(1)) + () -> assertEquals(3, event.getArguments().size()), + () -> assertEquals(ChangeType.ADDED, event.getArguments().get(0)), + () -> assertEquals("/content/test", event.getArguments().get(1)), + () -> assertEquals(Boolean.FALSE,event.getArguments().get(2)) ); } -} +} \ No newline at end of file