Skip to content

Commit

Permalink
#841 change listener to resource change listener (#846)
Browse files Browse the repository at this point in the history
* 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 <pankajpr@adobe.com>
  • Loading branch information
joerghoh and pankaj-parashar authored Dec 13, 2021
1 parent 7b97cf1 commit e518f6f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceChange> changes) {
changes.forEach(change -> {
logger.debug("Resource event: {} at: {} isExternal", change.getType(), change.getPath(), change.isExternal());
});

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -38,19 +38,21 @@ 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<LoggingEvent> events = logger.getLoggingEvents();
assertEquals(1, events.size());
LoggingEvent event = events.get(0);

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))
);
}
}
}

0 comments on commit e518f6f

Please sign in to comment.