Author: cziegeler
Date: Mon Oct 17 09:49:30 2016
New Revision: 1765234

URL: http://svn.apache.org/viewvc?rev=1765234&view=rev
Log:
SLING-6138 : Use JcrResourceListener for observation

Modified:
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
    
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
    
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java?rev=1765234&r1=1765233&r2=1765234&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
 Mon Oct 17 09:49:30 2016
@@ -48,10 +48,11 @@ import org.apache.jackrabbit.api.observa
 import org.apache.sling.api.resource.observation.ResourceChange;
 import org.apache.sling.api.resource.observation.ResourceChange.ChangeType;
 import org.apache.sling.api.resource.path.Path;
+import org.apache.sling.api.resource.path.PathSet;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.jcr.resource.internal.helper.jcr.PathMapper;
+import org.apache.sling.spi.resource.provider.ObservationReporter;
 import org.apache.sling.spi.resource.provider.ObserverConfiguration;
-import org.apache.sling.spi.resource.provider.ProviderContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -75,21 +76,26 @@ public class JcrResourceListener impleme
 
     private final PathMapper pathMapper;
 
-    private final ProviderContext ctx;
-
     private final boolean includeExternal;
 
+    private final PathSet excludedPaths;
+
+    private final ObservationReporter reporter;
+
     @SuppressWarnings("deprecation")
     public JcrResourceListener(
-                    final ProviderContext ctx,
+                    final ObservationReporter reporter,
+                    final List<ObserverConfiguration> configs,
+                    final PathSet excludedPaths,
                     final String mountPrefix,
                     final PathMapper pathMapper,
                     final SlingRepository repository)
     throws RepositoryException {
-        this.includeExternal = isIncludeExternal(ctx);
+        this.includeExternal = isIncludeExternal(configs);
+        this.excludedPaths = excludedPaths;
         this.pathMapper = pathMapper;
         this.mountPrefix = mountPrefix;
-        this.ctx = ctx;
+        this.reporter = reporter;
         boolean foundClass = false;
         try {
             
this.getClass().getClassLoader().loadClass(JackrabbitEvent.class.getName());
@@ -99,13 +105,15 @@ public class JcrResourceListener impleme
         }
         this.hasJackrabbitEventClass = foundClass;
         this.session = 
repository.loginAdministrative(repository.getDefaultWorkspace());
-        final String absPath = getAbsPath(pathMapper, ctx);
-        final int types = getTypes(ctx);
+        // TODO - as paths can have glob patterns, we need to update the 
algorithm
+        // final String absPath = getAbsPath(pathMapper, configs, 
excludedPaths);
+        final String absPath = "/";
+        final int types = getTypes(configs);
         
this.session.getWorkspace().getObservationManager().addEventListener(this, 
types, absPath, true, null, null, false);
     }
 
-    private boolean isIncludeExternal(ProviderContext ctx) {
-        for (ObserverConfiguration c : 
ctx.getObservationReporter().getObserverConfigurations()) {
+    private boolean isIncludeExternal(final List<ObserverConfiguration> 
configs) {
+        for (ObserverConfiguration c : configs) {
             if (c.includeExternal()) {
                 return true;
             }
@@ -155,12 +163,12 @@ public class JcrResourceListener impleme
                     if ( !addedEvents.containsKey(rsrcPath)
                       && !removedEvents.containsKey(rsrcPath)
                       && !changedEvents.containsKey(rsrcPath)
-                      && ctx.getExcludedPaths().matches(rsrcPath) == null) {
+                      && this.excludedPaths.matches(rsrcPath) == null) {
 
                         changedEvents.put(rsrcPath, 
createResourceChange(event, rsrcPath, ChangeType.CHANGED));
                     }
                 } else {
-                    if ( ctx.getExcludedPaths().matches(eventPath) == null ) {
+                    if ( this.excludedPaths.matches(eventPath) == null ) {
                         if ( type == NODE_ADDED ) {
                             // add is stronger than update
                             changedEvents.remove(eventPath);
@@ -182,7 +190,7 @@ public class JcrResourceListener impleme
         changes.addAll(addedEvents.values());
         changes.addAll(removedEvents.values());
         changes.addAll(changedEvents.values());
-        ctx.getObservationReporter().reportChanges(changes, false);
+        this.reporter.reportChanges(changes, false);
 
     }
 
@@ -209,9 +217,11 @@ public class JcrResourceListener impleme
         return false;
     }
 
-    static String getAbsPath(PathMapper pathMapper, ProviderContext ctx) {
+    static String getAbsPath(PathMapper pathMapper,
+            final List<ObserverConfiguration> configs,
+            final PathSet excludedPaths) {
         final Set<String> paths = new HashSet<String>();
-        for (ObserverConfiguration c : 
ctx.getObservationReporter().getObserverConfigurations()) {
+        for (final ObserverConfiguration c : configs) {
             final Set<String> includePaths = new HashSet<String>();
             final Set<String> excludePaths = new HashSet<String>();
             for (Path p : c.getExcludedPaths()) {
@@ -223,7 +233,7 @@ public class JcrResourceListener impleme
             includePaths.removeAll(excludePaths);
             paths.addAll(includePaths);
         }
-        for (Path p : ctx.getExcludedPaths()) {
+        for (Path p : excludedPaths) {
             paths.remove(pathMapper.mapResourcePathToJCRPath(p.getPath()));
         }
         return getLongestCommonPrefix(paths);
@@ -254,9 +264,9 @@ public class JcrResourceListener impleme
         return prefix.toString();
     }
 
-    private int getTypes(ProviderContext ctx) {
+    private int getTypes(final List<ObserverConfiguration> configs) {
         int result = 0;
-        for (ObserverConfiguration c : 
ctx.getObservationReporter().getObserverConfigurations()) {
+        for (ObserverConfiguration c : configs) {
             for (ChangeType t : c.getChangeTypes()) {
                 switch (t) {
                 case ADDED:
@@ -295,9 +305,8 @@ public class JcrResourceListener impleme
             return path;
         }
         try {
-            if ( !refreshedSession.get() ) {
+            if ( refreshedSession.compareAndSet(false, true) ) {
                 session.refresh(false);
-                refreshedSession.set(true);
             }
             final Node node = session.getNode(path);
             final Node parent = node.getParent();

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java?rev=1765234&r1=1765233&r2=1765234&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
 Mon Oct 17 09:49:30 2016
@@ -24,6 +24,7 @@ import java.security.Principal;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.Executor;
@@ -63,6 +64,7 @@ import org.apache.sling.jcr.resource.api
 import org.apache.sling.jcr.resource.internal.JcrModifiableValueMap;
 import org.apache.sling.jcr.resource.internal.JcrResourceListener;
 import org.apache.sling.jcr.resource.internal.NodeUtil;
+import org.apache.sling.spi.resource.provider.ObserverConfiguration;
 import org.apache.sling.spi.resource.provider.ProviderContext;
 import org.apache.sling.spi.resource.provider.QueryLanguageProvider;
 import org.apache.sling.spi.resource.provider.ResolveContext;
@@ -192,10 +194,16 @@ public class JcrResourceProvider extends
 
     private void registerListener(final ProviderContext ctx) {
         if ( this.repository != null ) {
-            try {
-                this.listener = new JcrResourceListener(ctx, root, pathMapper, 
repository);
-           } catch (RepositoryException e) {
-                throw new SlingException("Can't create the listener", e);
+            final List<ObserverConfiguration> configs = 
ctx.getObservationReporter().getObserverConfigurations();
+            if ( !configs.isEmpty() ) {
+                try {
+                    this.listener = new 
JcrResourceListener(ctx.getObservationReporter(),
+                            configs,
+                            ctx.getExcludedPaths(),
+                            root, pathMapper, repository);
+                } catch (RepositoryException e) {
+                    throw new SlingException("Can't create the listener", e);
+                }
             }
         }
     }

Modified: 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java?rev=1765234&r1=1765233&r2=1765234&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerScalabilityTest.java
 Mon Oct 17 09:49:30 2016
@@ -67,7 +67,11 @@ public class JcrResourceListenerScalabil
         SlingRepository repository = mock(SlingRepository.class);
         when(repository.loginAdministrative(null)).thenReturn(session);
 
-        jcrResourceListener = new JcrResourceListener(new 
SimpleProviderContext(), "/", new PathMapperImpl(), 
RepositoryUtil.getRepository());
+        final ProviderContext ctx = new SimpleProviderContext();
+        jcrResourceListener = new 
JcrResourceListener(ctx.getObservationReporter(),
+                ctx.getObservationReporter().getObserverConfigurations(),
+                ctx.getExcludedPaths(),
+                "/", new PathMapperImpl(), RepositoryUtil.getRepository());
 
         Event event = mock(MockEvent.class);
         events = mock(EventIterator.class);

Modified: 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java?rev=1765234&r1=1765233&r2=1765234&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceListenerTest.java
 Mon Oct 17 09:49:30 2016
@@ -21,8 +21,6 @@ import javax.jcr.Session;
 import org.apache.sling.api.resource.path.PathSet;
 import org.apache.sling.commons.testing.jcr.RepositoryUtil;
 import org.apache.sling.jcr.api.SlingRepository;
-import org.apache.sling.spi.resource.provider.ObservationReporter;
-import org.apache.sling.spi.resource.provider.ProviderContext;
 import org.junit.After;
 import org.junit.Before;
 
@@ -54,17 +52,11 @@ public class JcrResourceListenerTest ext
         RepositoryUtil.startRepository();
         this.adminSession = 
RepositoryUtil.getRepository().loginAdministrative(null);
         RepositoryUtil.registerSlingNodeTypes(adminSession);
-        this.listener = new JcrResourceListener(new ProviderContext() {
-            @Override
-            public ObservationReporter getObservationReporter() {
-                return JcrResourceListenerTest.this.getObservationReporter();
-            }
-
-            @Override
-            public PathSet getExcludedPaths() {
-                return PathSet.fromPaths();
-            }
-        }, "/", new PathMapperImpl(), RepositoryUtil.getRepository());
+        this.listener = new JcrResourceListener(
+                JcrResourceListenerTest.this.getObservationReporter(),
+                
JcrResourceListenerTest.this.getObservationReporter().getObserverConfigurations(),
+                PathSet.fromPaths(),
+                "/", new PathMapperImpl(), RepositoryUtil.getRepository());
     }
 
     @Override


Reply via email to