sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tomm...@apache.org
Subject svn commit: r1636636 - in /sling/trunk/contrib/extensions/replication: core/src/main/java/org/apache/sling/replication/component/impl/ core/src/main/java/org/apache/sling/replication/queue/ core/src/main/java/org/apache/sling/replication/trigger/impl/ ...
Date Tue, 04 Nov 2014 16:22:04 GMT
Author: tommaso
Date: Tue Nov  4 16:22:04 2014
New Revision: 1636636

URL: http://svn.apache.org/r1636636
Log:
SLING-4132 - fixed event listeners registration

Added:
    sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/component/
    sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/component/impl/
    sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactoryTest.java
  (with props)
Removed:
    sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/agent/impl/DefaultReplicationComponentFactoryTest.java
Modified:
    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/queue/ReplicationQueueDistributionStrategy.java
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/queue/ReplicationQueueProvider.java
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/AbstractJcrEventTrigger.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/sample/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/settings/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-simpleAgents.json

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=1636636&r1=1636635&r2=1636636&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
Tue Nov  4 16:22:04 2014
@@ -175,7 +175,6 @@ public class DefaultReplicationComponent
 
             boolean isPassive = PropertiesUtil.toBoolean(properties.get(ReplicationComponentFactory.AGENT_SIMPLE_PROPERTY_IS_PASSIVE),
false);
 
-
             return new SimpleReplicationAgent(name, isPassive, serviceName,
                     packageImporter, packageExporter, packageExporterStrategy,
                     queueProvider, queueDistributionStrategy, replicationEventFactory, resourceResolverFactory,
triggers);
@@ -336,7 +335,7 @@ public class DefaultReplicationComponent
     }
 
 
-    private ReplicationTrigger createTrigger(Map<String, Object> properties, ReplicationComponentProvider
componentProvider) {
+    protected ReplicationTrigger createTrigger(Map<String, Object> properties, ReplicationComponentProvider
componentProvider) {
         String factory = PropertiesUtil.toString(properties.get(COMPONENT_TYPE), COMPONENT_TYPE_SERVICE);
 
         if (COMPONENT_TYPE_SERVICE.equals(factory)) {

Modified: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/queue/ReplicationQueueDistributionStrategy.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/queue/ReplicationQueueDistributionStrategy.java?rev=1636636&r1=1636635&r2=1636636&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/queue/ReplicationQueueDistributionStrategy.java
(original)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/queue/ReplicationQueueDistributionStrategy.java
Tue Nov  4 16:22:04 2014
@@ -41,10 +41,9 @@ public interface ReplicationQueueDistrib
      *
      * @param replicationPackage          a {@link org.apache.sling.replication.packaging.ReplicationPackage}
to distribute
      * @param queueProvider the {@link ReplicationQueueProvider} used to provide the queues
to be used for the given package
-     * @return a {@link ReplicationQueueItemState} representing the state of the package
in the queue after its distribution
-     * @throws ReplicationQueueException if distribution fails
+     * @return <code>true</code> if addition was successful, <code>false</code>
otherwise
+     * @throws ReplicationQueueException if any internal error happens during distribution
      */
-    @Nonnull
     boolean add(@Nonnull ReplicationPackage replicationPackage, @Nonnull ReplicationQueueProvider
queueProvider) throws ReplicationQueueException;
 
 
@@ -52,6 +51,7 @@ public interface ReplicationQueueDistrib
      * Returns the queue names available for this strategy.
      * @return a list of queue names
      */
+    @Nonnull
     List<String> getQueueNames();
 
 }

Modified: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/queue/ReplicationQueueProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/queue/ReplicationQueueProvider.java?rev=1636636&r1=1636635&r2=1636636&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/queue/ReplicationQueueProvider.java
(original)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/queue/ReplicationQueueProvider.java
Tue Nov  4 16:22:04 2014
@@ -19,7 +19,6 @@
 package org.apache.sling.replication.queue;
 
 import javax.annotation.Nonnull;
-import java.util.Collection;
 
 import aQute.bnd.annotation.ConsumerType;
 import org.apache.sling.replication.component.ReplicationComponent;
@@ -33,15 +32,13 @@ public interface ReplicationQueueProvide
     /**
      * provide a named queue for the given agent
      *
-     * @param queueName      the name of the queue to retrieve
-     * @return a replication queue to be used for the given parameters
-     * @throws ReplicationQueueException
+     * @param queueName the name of the queue to retrieve
+     * @return a {@link org.apache.sling.replication.queue.ReplicationQueue}
+     * @throws ReplicationQueueException if any error happens
      */
     @Nonnull
     ReplicationQueue getQueue(@Nonnull String queueName) throws ReplicationQueueException;
 
-
-
     /**
      * enables queue driven processing for an agent
      *

Modified: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/AbstractJcrEventTrigger.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/AbstractJcrEventTrigger.java?rev=1636636&r1=1636635&r2=1636636&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/AbstractJcrEventTrigger.java
(original)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/trigger/impl/AbstractJcrEventTrigger.java
Tue Nov  4 16:22:04 2014
@@ -51,6 +51,8 @@ public abstract class AbstractJcrEventTr
 
     protected final SlingRepository repository;
 
+    private Session cachedSession;
+
     public AbstractJcrEventTrigger(SlingRepository repository, String path, String serviceUser)
{
         this.repository = repository;
         this.path = path;
@@ -58,7 +60,7 @@ public abstract class AbstractJcrEventTr
     }
 
     public void register(@Nonnull ReplicationRequestHandler requestHandler) throws ReplicationTriggerException
{
-        Session session = null;
+        Session session;
         try {
             session = getSession();
             JcrEventReplicationTriggerListener listener = new JcrEventReplicationTriggerListener(requestHandler);
@@ -67,26 +69,18 @@ public abstract class AbstractJcrEventTr
                     listener, getEventTypes(), path, true, null, null, false);
         } catch (RepositoryException e) {
             throw new ReplicationTriggerException("unable to register handler " + requestHandler,
e);
-        } finally {
-            if (session != null) {
-                session.logout();
-            }
         }
     }
 
     public void unregister(@Nonnull ReplicationRequestHandler requestHandler) throws ReplicationTriggerException
{
         JcrEventReplicationTriggerListener listener = registeredListeners.get(requestHandler.toString());
         if (listener != null) {
-            Session session = null;
+            Session session;
             try {
                 session = getSession();
                 session.getWorkspace().getObservationManager().removeEventListener(listener);
             } catch (RepositoryException e) {
                 throw new ReplicationTriggerException("unable to unregister handler " + requestHandler,
e);
-            } finally {
-                if (session != null) {
-                    session.logout();
-                }
             }
         }
     }
@@ -143,7 +137,7 @@ public abstract class AbstractJcrEventTr
      * @throws RepositoryException
      */
     protected Session getSession() throws RepositoryException {
-        return repository.loginService(serviceUser, null);
+        return cachedSession != null ? cachedSession : (cachedSession = repository.loginService(serviceUser,
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=1636636&r1=1636635&r2=1636636&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
Tue Nov  4 16:22:04 2014
@@ -22,6 +22,8 @@ import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.observation.Event;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Map;
 import java.util.Set;
 
@@ -66,20 +68,23 @@ public class PersistingJcrEventReplicati
             log.info("persisting event under {}", nuggetsPath);
             Node nuggetsNode = session.getNode(nuggetsPath);
             if (nuggetsNode != null) {
-                String nodeName = event.getIdentifier() != null ? event.getIdentifier() :
String.valueOf(System.nanoTime());
-                Node createdNode = nuggetsNode.addNode(nodeName);
+                String nodeName = String.valueOf(System.nanoTime());
+                Node createdNode = nuggetsNode.addNode(nodeName, "nt:unstructured");
                 if (createdNode != null) {
                     String path = createdNode.getPath();
-                    nuggetsNode.setProperty("path", event.getPath());
-                    nuggetsNode.setProperty("date", event.getDate());
-                    nuggetsNode.setProperty("type", event.getType());
-                    nuggetsNode.setProperty("userData", event.getUserData());
-                    nuggetsNode.setProperty("userID", event.getUserID());
+                    createdNode.setProperty("identifier", event.getIdentifier());
+                    createdNode.setProperty("path", event.getPath());
+                    createdNode.setProperty("date", event.getDate());
+                    createdNode.setProperty("type", event.getType());
+                    createdNode.setProperty("userData", event.getUserData());
+                    createdNode.setProperty("userID", event.getUserID());
 
                     Set<Map.Entry> set = event.getInfo().entrySet();
+                    Collection<String> values = new ArrayList<String>();
                     for (Map.Entry entry : set) {
-                        nuggetsNode.setProperty("info." + entry.getKey(), String.valueOf(entry.getValue()));
+                        values.add(String.valueOf(entry.getKey()) + ":" + String.valueOf(entry.getValue()));
                     }
+                    createdNode.setProperty("info", values.toArray(new String[values.size()]));
                     session.save();
                     log.info("event persisted at {}", path);
                     replicationRequest = new ReplicationRequest(System.currentTimeMillis(),
ReplicationActionType.ADD, path);
@@ -127,10 +132,6 @@ public class PersistingJcrEventReplicati
             }
         } catch (RepositoryException e) {
             log.warn("could not create nuggets path " + nuggetsPath, e);
-        } finally {
-            if (session != null) {
-                session.logout();
-            }
         }
     }
 

Added: sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactoryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactoryTest.java?rev=1636636&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactoryTest.java
(added)
+++ sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactoryTest.java
Tue Nov  4 16:22:04 2014
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.replication.component.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.sling.replication.agent.ReplicationAgent;
+import org.apache.sling.replication.component.ReplicationComponentProvider;
+import org.apache.sling.replication.packaging.ReplicationPackageExporter;
+import org.apache.sling.replication.packaging.ReplicationPackageImporter;
+import org.apache.sling.replication.transport.authentication.TransportAuthenticationProvider;
+import org.apache.sling.replication.trigger.ReplicationTrigger;
+import org.apache.sling.replication.trigger.impl.PersistingJcrEventReplicationTrigger;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Testcase for {@link org.apache.sling.replication.component.impl.DefaultReplicationComponentFactory}
+ */
+public class DefaultReplicationComponentFactoryTest {
+
+    @Test
+    public void testDefaultCreateComponentForAgentByService() throws Exception {
+        DefaultReplicationComponentFactory defaultReplicationComponentFactory = new DefaultReplicationComponentFactory();
+        String name = "sample-agent";
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("name", name);
+        ReplicationComponentProvider provider = mock(ReplicationComponentProvider.class);
+        ReplicationAgent agent = mock(ReplicationAgent.class);
+        when(provider.getComponent(ReplicationAgent.class, name)).thenReturn(agent);
+        ReplicationAgent component = defaultReplicationComponentFactory.createComponent(ReplicationAgent.class,
properties, provider);
+        assertNull(component); // agents cannot be referenced by service name using the factory
+    }
+
+    @Test
+    public void testDefaultCreateComponentForTriggerByService() throws Exception {
+        DefaultReplicationComponentFactory defaultReplicationComponentFactory = new DefaultReplicationComponentFactory();
+        String name = "sample-trigger";
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("name", name);
+        ReplicationComponentProvider provider = mock(ReplicationComponentProvider.class);
+        ReplicationTrigger trigger = mock(ReplicationTrigger.class);
+        when(provider.getComponent(ReplicationTrigger.class, name)).thenReturn(trigger);
+        ReplicationTrigger component = defaultReplicationComponentFactory.createComponent(ReplicationTrigger.class,
properties, provider);
+        assertNotNull(component);
+    }
+
+    @Test
+    public void testDefaultCreateComponentForTransportAuthenticationProviderByService() throws
Exception {
+        DefaultReplicationComponentFactory defaultReplicationComponentFactory = new DefaultReplicationComponentFactory();
+        String name = "sample-auth";
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("name", name);
+        ReplicationComponentProvider provider = mock(ReplicationComponentProvider.class);
+        TransportAuthenticationProvider authenticationProvider = mock(TransportAuthenticationProvider.class);
+        when(provider.getComponent(TransportAuthenticationProvider.class, name)).thenReturn(authenticationProvider);
+        TransportAuthenticationProvider component = defaultReplicationComponentFactory.createComponent(TransportAuthenticationProvider.class,
properties, provider);
+        assertNotNull(component);
+    }
+
+    @Test
+    public void testDefaultCreateComponentForImporterByService() throws Exception {
+        DefaultReplicationComponentFactory defaultReplicationComponentFactory = new DefaultReplicationComponentFactory();
+        String name = "sample-importer";
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("name", name);
+        ReplicationComponentProvider provider = mock(ReplicationComponentProvider.class);
+        ReplicationPackageImporter importer = mock(ReplicationPackageImporter.class);
+        when(provider.getComponent(ReplicationPackageImporter.class, name)).thenReturn(importer);
+        ReplicationPackageImporter component = defaultReplicationComponentFactory.createComponent(ReplicationPackageImporter.class,
properties, provider);
+        assertNotNull(component);
+    }
+
+    @Test
+    public void testDefaultCreateComponentForExporterByService() throws Exception {
+        DefaultReplicationComponentFactory defaultReplicationComponentFactory = new DefaultReplicationComponentFactory();
+        String name = "sample-exporter";
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("name", name);
+        ReplicationComponentProvider provider = mock(ReplicationComponentProvider.class);
+        ReplicationPackageExporter exporter = mock(ReplicationPackageExporter.class);
+        when(provider.getComponent(ReplicationPackageExporter.class, name)).thenReturn(exporter);
+        ReplicationPackageExporter component = defaultReplicationComponentFactory.createComponent(ReplicationPackageExporter.class,
properties, provider);
+        assertNotNull(component);
+    }
+
+    @Test
+    public void testPersistingJcrEventTriggerCreation() throws Exception {
+        DefaultReplicationComponentFactory defaultReplicationComponentFactory = new DefaultReplicationComponentFactory();
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("type", "persistedJcrEvent");
+        properties.put("path", "/content/persistedEvents");
+        properties.put("servicename", "replicationService");
+        ReplicationComponentProvider componentProvider = mock(ReplicationComponentProvider.class);
+        ReplicationTrigger trigger = defaultReplicationComponentFactory.createTrigger(properties,
componentProvider);
+        assertNotNull(trigger);
+        assertEquals(PersistingJcrEventReplicationTrigger.class, trigger.getClass());
+    }
+}
\ No newline at end of file

Propchange: sling/trunk/contrib/extensions/replication/core/src/test/java/org/apache/sling/replication/component/impl/DefaultReplicationComponentFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1636636&r1=1636635&r2=1636636&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
Tue Nov  4 16:22:04 2014
@@ -23,7 +23,6 @@ import javax.jcr.Session;
 import javax.jcr.Workspace;
 import javax.jcr.observation.Event;
 import javax.jcr.observation.ObservationManager;
-import javax.jcr.security.Privilege;
 
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.replication.communication.ReplicationRequest;
@@ -106,7 +105,7 @@ public class PersistingJcrEventReplicati
 
         Node nuggetsNode = mock(Node.class);
         Node eventNode = mock(Node.class);
-        when(nuggetsNode.addNode(any(String.class))).thenReturn(eventNode);
+        when(nuggetsNode.addNode(any(String.class), any(String.class))).thenReturn(eventNode);
         when(session.getNode(nuggetsPath)).thenReturn(nuggetsNode);
         Event event = mock(Event.class);
         when(event.getPath()).thenReturn("/some/path/generating/event");
@@ -143,7 +142,7 @@ public class PersistingJcrEventReplicati
         persistingJcrEventReplicationTrigger.register(handler);
 
         Node eventNode = mock(Node.class);
-        when(nuggetsNode.addNode(any(String.class))).thenReturn(eventNode);
+        when(nuggetsNode.addNode(any(String.class), any(String.class))).thenReturn(eventNode);
         when(session.getNode(nuggetsPath)).thenReturn(nuggetsNode);
         Event event = mock(Event.class);
         when(event.getPath()).thenReturn("/some/path/generating/event");

Modified: sling/trunk/contrib/extensions/replication/sample/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/settings/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-simpleAgents.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/sample/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/settings/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-simpleAgents.json?rev=1636636&r1=1636635&r2=1636636&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/sample/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/settings/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-simpleAgents.json
(original)
+++ sling/trunk/contrib/extensions/replication/sample/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/settings/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-simpleAgents.json
Tue Nov  4 16:22:04 2014
@@ -6,7 +6,6 @@
     "providerType": "osgiConfig",
     "groupPrefix": "agents",
 
-
     "resourceProperties": [
         "sling:resourceType=sling/replication/setting/agent",
         "sling:resourceSuperType=sling/replication/setting",



Mime
View raw message