You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:27:20 UTC
[sling-org-apache-sling-discovery-commons] 05/07: SLING-5995 :
changing IdMapService from being an OSGi EventListener to a Sling
ResourceChangeListener
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.discovery.commons-1.0.14
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-discovery-commons.git
commit 37e545076ed339cf1dd128fb8e6174817f367905
Author: Stefan Egli <st...@apache.org>
AuthorDate: Wed Sep 21 14:46:59 2016 +0000
SLING-5995 : changing IdMapService from being an OSGi EventListener to a Sling ResourceChangeListener
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/discovery/commons@1761756 13f79535-47bb-0310-9956-ffa450edef68
---
pom.xml | 8 ++--
.../commons/providers/spi/base/IdMapService.java | 48 +++++++++-------------
.../commons/providers/spi/base/package-info.java | 4 +-
.../providers/spi/base/MockedResourceResolver.java | 20 +++++++++
4 files changed, 47 insertions(+), 33 deletions(-)
diff --git a/pom.xml b/pom.xml
index b5b8105..ec30556 100644
--- a/pom.xml
+++ b/pom.xml
@@ -119,8 +119,8 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
- <version>2.4.0</version>
- <scope>provided</scope>
+ <version>2.10.0</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
@@ -161,7 +161,9 @@
</dependency>
<dependency>
<groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
+ <artifactId>osgi.core</artifactId>
+ <version>6.0.0</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
diff --git a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/IdMapService.java b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/IdMapService.java
index 7828103..e171738 100644
--- a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/IdMapService.java
+++ b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/IdMapService.java
@@ -22,6 +22,7 @@ import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@@ -31,7 +32,6 @@ import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
@@ -39,6 +39,9 @@ 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.api.resource.ValueMap;
+import org.apache.sling.api.resource.observation.ResourceChangeListener;
+import org.apache.sling.api.resource.observation.ResourceChange;
+import org.apache.sling.api.resource.observation.ResourceChange.ChangeType;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.discovery.commons.providers.util.ResourceHelper;
import org.apache.sling.settings.SlingSettingsService;
@@ -46,8 +49,6 @@ import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.event.Event;
-import org.osgi.service.event.EventConstants;
-import org.osgi.service.event.EventHandler;
/**
* The IdMapService is responsible for storing a slingId-clusterNodeId
@@ -56,7 +57,7 @@ import org.osgi.service.event.EventHandler;
*/
@Component(immediate = false)
@Service(value = { IdMapService.class })
-public class IdMapService extends AbstractServiceWithBackgroundCheck implements EventHandler {
+public class IdMapService extends AbstractServiceWithBackgroundCheck implements ResourceChangeListener {
@Reference
private ResourceResolverFactory resourceResolverFactory;
@@ -80,7 +81,7 @@ public class IdMapService extends AbstractServiceWithBackgroundCheck implements
private BundleContext bundleContext;
- private ServiceRegistration eventHandlerRegistration;
+ private volatile ServiceRegistration<ResourceChangeListener> eventHandlerRegistration;
/** test-only constructor **/
public static IdMapService testConstructor(
@@ -131,18 +132,16 @@ public class IdMapService extends AbstractServiceWithBackgroundCheck implements
}
Dictionary<String,Object> properties = new Hashtable<String,Object>();
properties.put(Constants.SERVICE_DESCRIPTION, "IdMap Change Listener.");
+ properties.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
String[] topics = new String[] {
- SlingConstants.TOPIC_RESOURCE_ADDED,
- SlingConstants.TOPIC_RESOURCE_CHANGED,
- SlingConstants.TOPIC_RESOURCE_REMOVED };
- properties.put(EventConstants.EVENT_TOPIC, topics);
- String path = getIdMapPath();
- if (path.endsWith("/")) {
- path = path.substring(0, path.length()-1);
- }
- properties.put(EventConstants.EVENT_FILTER, "(&(path="+path+"))");
- eventHandlerRegistration = bundleContext.registerService(
- EventHandler.class.getName(), this, properties);
+ ChangeType.ADDED.toString(),
+ ChangeType.CHANGED.toString(),
+ ChangeType.REMOVED.toString()
+ };
+ properties.put(ResourceChangeListener.CHANGES, topics);
+ properties.put(ResourceChangeListener.PATHS, getIdMapPath());
+
+ this.eventHandlerRegistration = bundleContext.registerService(ResourceChangeListener.class, this, properties);
}
/** Get or create a ResourceResolver **/
@@ -318,18 +317,11 @@ public class IdMapService extends AbstractServiceWithBackgroundCheck implements
}
@Override
- public void handleEvent(Event event) {
- final String resourcePath = (String) event.getProperty("path");
- if (resourcePath == null) {
- // not of my business
- return;
- }
-
- if (!resourcePath.startsWith(getIdMapPath())) {
- // not of my business
- return;
- }
- logger.debug("handleEvent: got event for path: {}, event: {}", resourcePath, event);
+ public void onChange(List<ResourceChange> changes) {
+ // the listener is registered on the .../idmap subpath, so any
+ // change it receives should be relevant. hence no further
+ // filtering necessary here.
+ logger.debug("onChange: got notified of changes, clearing cache.");
clearCache();
}
diff --git a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/package-info.java b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/package-info.java
index aa1fe77..8a66f21 100644
--- a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/package-info.java
+++ b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/package-info.java
@@ -20,9 +20,9 @@
/**
* Provides default SPI-implementations used by discovery.commons.providers.impl
*
- * @version 1.0.0
+ * @version 2.0.0
*/
-@Version("1.0.0")
+@Version("2.0.0")
package org.apache.sling.discovery.commons.providers.spi.base;
import aQute.bnd.annotation.Version;
diff --git a/src/test/java/org/apache/sling/discovery/commons/providers/spi/base/MockedResourceResolver.java b/src/test/java/org/apache/sling/discovery/commons/providers/spi/base/MockedResourceResolver.java
index a86a380..16a9c38 100644
--- a/src/test/java/org/apache/sling/discovery/commons/providers/spi/base/MockedResourceResolver.java
+++ b/src/test/java/org/apache/sling/discovery/commons/providers/spi/base/MockedResourceResolver.java
@@ -326,4 +326,24 @@ public class MockedResourceResolver implements ResourceResolver {
}
+ @Override
+ public Resource getParent(Resource child) {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ @Override
+ public boolean hasChildren(Resource resource) {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ @Override
+ public Resource copy(String srcAbsPath, String destAbsPath) throws PersistenceException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ @Override
+ public Resource move(String srcAbsPath, String destAbsPath) throws PersistenceException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.