sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tomm...@apache.org
Subject svn commit: r1636311 - in /sling/trunk/contrib/extensions/replication: core/src/main/java/org/apache/sling/replication/component/ core/src/main/java/org/apache/sling/replication/component/impl/ core/src/main/java/org/apache/sling/replication/trigger/im...
Date Mon, 03 Nov 2014 10:35:31 GMT
Author: tommaso
Date: Mon Nov  3 10:35:30 2014
New Revision: 1636311

URL: http://svn.apache.org/r1636311
Log:
SLING-4128 - persisting event trigger automatically creates nuggets path

Added:
    sling/trunk/contrib/extensions/replication/it/src/main/resources/SLING-CONTENT/libs/test/install.author/org.apache.sling.replication.component.impl.GenericReplicationComponentFactory-trigger-test-persisting-event.json
Modified:
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/ReplicationComponentFactory.java
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactory.java
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTrigger.java
    sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTriggerTest.java
    sling/trunk/contrib/extensions/replication/it/src/test/java/org/apache/sling/replication/it/triggers/ReplicationTriggerResourcesTest.java

Modified: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/ReplicationComponentFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/ReplicationComponentFactory.java?rev=1636311&r1=1636310&r2=1636311&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/ReplicationComponentFactory.java
(original)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/ReplicationComponentFactory.java
Mon Nov  3 10:35:30 2014
@@ -257,7 +257,7 @@ public interface ReplicationComponentFac
     /**
      * jcr persisting event trigger nuggets path property
      */
-    String TRIGGER_PERSISTED_JCR_EVENT_PROPERTY_NUGGETS_PATH = "nuggets.path";
+    String TRIGGER_PERSISTED_JCR_EVENT_PROPERTY_NUGGETS_PATH = "nuggetsPath";
 
     /**
      * privilege request authorization strategy type

Modified: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactory.java?rev=1636311&r1=1636310&r2=1636311&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactory.java
(original)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactory.java
Mon Nov  3 10:35:30 2014
@@ -130,7 +130,7 @@ public class DefaultReplicationComponent
                 return (ComponentType) createExporter(properties, componentProvider);
             }
         } catch (IllegalArgumentException e) {
-            log.warn("Cannot create component of type {} with properties {}", new Object[]{type,
properties}, e);
+            log.warn("Cannot create component {}", new Object[]{type, properties}, e);
         }
 
         return null;

Modified: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTrigger.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTrigger.java?rev=1636311&r1=1636310&r2=1636311&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTrigger.java
(original)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTrigger.java
Mon Nov  3 10:35:30 2014
@@ -29,6 +29,7 @@ import java.util.Set;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.replication.communication.ReplicationActionType;
 import org.apache.sling.replication.communication.ReplicationRequest;
+import org.apache.sling.replication.component.ManagedReplicationComponent;
 import org.apache.sling.replication.trigger.ReplicationTrigger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -37,7 +38,7 @@ import org.slf4j.LoggerFactory;
  * {@link org.apache.sling.replication.trigger.ReplicationTrigger} that listens for certain
events and persists them
  * under a specific path in the repo
  */
-public class PersistingJcrEventReplicationTrigger extends AbstractJcrEventTrigger implements
ReplicationTrigger {
+public class PersistingJcrEventReplicationTrigger extends AbstractJcrEventTrigger implements
ReplicationTrigger, ManagedReplicationComponent {
 
     private static final String DEFAULT_NUGGETS_PATH = "/var/nuggets";
 
@@ -58,7 +59,11 @@ public class PersistingJcrEventReplicati
 
         Session session = getSession();
 
-        if (session != null && session.hasPermission(nuggetsPath, Privilege.JCR_ADD_CHILD_NODES))
{
+        if (!session.nodeExists(nuggetsPath)) {
+            initializeNuggetsPath(session);
+        }
+
+        if (session.hasPermission(nuggetsPath, Privilege.JCR_ADD_CHILD_NODES)) {
             log.debug("persisting event under {}", nuggetsPath);
             Node nuggetsNode = session.getNode(nuggetsPath);
             if (nuggetsNode != null) {
@@ -92,4 +97,38 @@ public class PersistingJcrEventReplicati
         return replicationRequest;
     }
 
+    private void initializeNuggetsPath(Session session) throws RepositoryException {
+        if (session != null) {
+            Node parent = session.getRootNode();
+            if (session.hasPermission(parent.getPath(), Privilege.JCR_ADD_CHILD_NODES)) {
+                for (String nodeName : nuggetsPath.split("/")) {
+                    if (!parent.hasNode(nodeName)) {
+                        parent = parent.addNode(nodeName, "sling:Folder");
+                    } else {
+                        parent = parent.getNode(nodeName);
+                    }
+                }
+            }
+        }
+    }
+
+    public void enable() {
+        Session session = null;
+        try {
+            session = getSession();
+            if (!session.nodeExists(nuggetsPath)) {
+                initializeNuggetsPath(session);
+            }
+        } catch (RepositoryException e) {
+            log.warn("could not create nuggets path " + nuggetsPath, e);
+        } finally {
+            if (session != null) {
+                session.logout();
+            }
+        }
+    }
+
+    public void disable() {
+
+    }
 }

Modified: sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTriggerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTriggerTest.java?rev=1636311&r1=1636310&r2=1636311&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTriggerTest.java
(original)
+++ sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/trigger/impl/PersistingJcrEventReplicationTriggerTest.java
Mon Nov  3 10:35:30 2014
@@ -33,6 +33,7 @@ import org.junit.Test;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -45,6 +46,32 @@ public class PersistingJcrEventReplicati
     public void testProcessEventWithoutPrivileges() throws Exception {
         String serviceName = "serviceId";
         Session session = mock(Session.class);
+        when(session.nodeExists("/var/nuggets")).thenReturn(true);
+        SlingRepository repository = mock(SlingRepository.class);
+        when(repository.loginService(serviceName, null)).thenReturn(session);
+        String path = "/some/path";
+        String nuggetsPath = "/var/nuggets";
+        PersistingJcrEventReplicationTrigger persistingJcrEventReplicationTrigger = new PersistingJcrEventReplicationTrigger(
+                repository, path, serviceName, nuggetsPath);
+        Event event = mock(Event.class);
+        ReplicationRequest replicationRequest = persistingJcrEventReplicationTrigger.processEvent(event);
+        assertNull(replicationRequest);
+    }
+
+    @Test
+    public void testProcessEventWithoutPrivilegesAndNuggetsPath() throws Exception {
+        String serviceName = "serviceId";
+        Session session = mock(Session.class);
+        when(session.hasPermission(eq("var"), eq(Privilege.JCR_ADD_CHILD_NODES))).thenReturn(true);
+        when(session.hasPermission(eq("nuggets"), eq(Privilege.JCR_ADD_CHILD_NODES))).thenReturn(true);
+        when(session.nodeExists("/var/nuggets")).thenReturn(false); // first time it doesn't
exist and should be created
+        when(session.nodeExists("/var/nuggets")).thenReturn(true); // second time it should
exist
+        Node rootNode = mock(Node.class);
+        Node varNode = mock(Node.class);
+        Node nuggetsNode = mock(Node.class);
+        when(varNode.addNode("nuggets", "sling:Folder")).thenReturn(nuggetsNode);
+        when(rootNode.addNode("var", "sling:Folder")).thenReturn(varNode);
+        when(session.getRootNode()).thenReturn(rootNode);
         SlingRepository repository = mock(SlingRepository.class);
         when(repository.loginService(serviceName, null)).thenReturn(session);
         String path = "/some/path";
@@ -61,6 +88,7 @@ public class PersistingJcrEventReplicati
         String nuggetsPath = "/var/nuggets";
         String serviceName = "serviceId";
         Session session = mock(Session.class);
+        when(session.nodeExists("/var/nuggets")).thenReturn(true);
         Workspace workspace = mock(Workspace.class);
         ObservationManager observationManager = mock(ObservationManager.class);
         when(workspace.getObservationManager()).thenReturn(observationManager);
@@ -85,4 +113,41 @@ public class PersistingJcrEventReplicati
         ReplicationRequest replicationRequest = persistingJcrEventReplicationTrigger.processEvent(event);
         assertNotNull(replicationRequest);
     }
+
+    @Test
+    public void testProcessEventWithPrivilegesAndNoNuggetsNode() throws Exception {
+        String nuggetsPath = "/var/nuggets";
+        String serviceName = "serviceId";
+        Session session = mock(Session.class);
+        when(session.nodeExists("/var/nuggets")).thenReturn(false); // first time it doesn't
exist and should be created
+        when(session.nodeExists("/var/nuggets")).thenReturn(true); // second time it should
exist
+        Node rootNode = mock(Node.class);
+        Node varNode = mock(Node.class);
+        Node nuggetsNode = mock(Node.class);
+        when(varNode.addNode("nuggets", "sling:Folder")).thenReturn(nuggetsNode);
+        when(rootNode.addNode("var", "sling:Folder")).thenReturn(varNode);
+        when(session.getRootNode()).thenReturn(rootNode);
+        Workspace workspace = mock(Workspace.class);
+        ObservationManager observationManager = mock(ObservationManager.class);
+        when(workspace.getObservationManager()).thenReturn(observationManager);
+        when(session.getWorkspace()).thenReturn(workspace);
+        when(session.hasPermission(any(String.class), eq(Privilege.JCR_ADD_CHILD_NODES))).thenReturn(true);
+
+        SlingRepository repository = mock(SlingRepository.class);
+        when(repository.loginService(serviceName, null)).thenReturn(session);
+
+        String path = "/some/path";
+        PersistingJcrEventReplicationTrigger persistingJcrEventReplicationTrigger = new PersistingJcrEventReplicationTrigger(
+                repository, path, serviceName, nuggetsPath);
+        ReplicationRequestHandler handler = mock(ReplicationRequestHandler.class);
+        persistingJcrEventReplicationTrigger.register(handler);
+
+        Node eventNode = mock(Node.class);
+        when(nuggetsNode.addNode(any(String.class))).thenReturn(eventNode);
+        when(session.getNode(nuggetsPath)).thenReturn(nuggetsNode);
+        Event event = mock(Event.class);
+        when(event.getPath()).thenReturn("/some/path/generating/event");
+        ReplicationRequest replicationRequest = persistingJcrEventReplicationTrigger.processEvent(event);
+        assertNotNull(replicationRequest);
+    }
 }
\ No newline at end of file

Added: sling/trunk/contrib/extensions/replication/it/src/main/resources/SLING-CONTENT/libs/test/install.author/org.apache.sling.replication.component.impl.GenericReplicationComponentFactory-trigger-test-persisting-event.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/it/src/main/resources/SLING-CONTENT/libs/test/install.author/org.apache.sling.replication.component.impl.GenericReplicationComponentFactory-trigger-test-persisting-event.json?rev=1636311&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/replication/it/src/main/resources/SLING-CONTENT/libs/test/install.author/org.apache.sling.replication.component.impl.GenericReplicationComponentFactory-trigger-test-persisting-event.json
(added)
+++ sling/trunk/contrib/extensions/replication/it/src/main/resources/SLING-CONTENT/libs/test/install.author/org.apache.sling.replication.component.impl.GenericReplicationComponentFactory-trigger-test-persisting-event.json
Mon Nov  3 10:35:30 2014
@@ -0,0 +1,13 @@
+{
+    "jcr:primaryType": "sling:OsgiConfig",
+    "name": "test-persisting-event",
+
+    "componentType": "trigger",
+
+    "properties": [
+        "type=persistedJcrEvent",
+        "path=/content",
+        "servicename=replicationService"
+    ]
+
+}
\ No newline at end of file

Modified: sling/trunk/contrib/extensions/replication/it/src/test/java/org/apache/sling/replication/it/triggers/ReplicationTriggerResourcesTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/it/src/test/java/org/apache/sling/replication/it/triggers/ReplicationTriggerResourcesTest.java?rev=1636311&r1=1636310&r2=1636311&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/it/src/test/java/org/apache/sling/replication/it/triggers/ReplicationTriggerResourcesTest.java
(original)
+++ sling/trunk/contrib/extensions/replication/it/src/test/java/org/apache/sling/replication/it/triggers/ReplicationTriggerResourcesTest.java
Mon Nov  3 10:35:30 2014
@@ -34,7 +34,8 @@ public class ReplicationTriggerResources
                 "test-content-event",
                 "test-remote-event",
                 "test-replicate-event",
-                "test-scheduled-event"
+                "test-scheduled-event",
+                "test-persisting-event"
         };
         for (String name : names) {
             assertExists(authorClient, triggerUrl(name));



Mime
View raw message