You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by to...@apache.org on 2014/08/26 12:07:58 UTC

svn commit: r1620549 - in /sling/trunk/contrib/extensions/replication/core/src/main: java/org/apache/sling/replication/rule/ java/org/apache/sling/replication/rule/impl/ java/org/apache/sling/replication/servlet/ resources/SLING-CONTENT/libs/sling/repl...

Author: tommaso
Date: Tue Aug 26 10:07:57 2014
New Revision: 1620549

URL: http://svn.apache.org/r1620549
Log:
SLING-3872 - applied Marius Petria's patch for triggers, added some javadoc and missing AL headers, adjusted configuration for event based reverse repl

Added:
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/ReplicationTrigger.java   (with props)
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/RuleBasedReplicationTrigger.java   (with props)
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/servlet/ReplicationTriggerServlet.java   (with props)
    sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-remote.json
    sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.publish/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-content-event.json
    sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-triggers.json
Removed:
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/servlet/ReplicationAgentQueueEventServlet.java
Modified:
    sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/TriggerPathReplicationRule.java
    sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.agent.impl.SimpleReplicationAgentFactory-publish-reverse.json
    sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-agents.json

Added: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/ReplicationTrigger.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/ReplicationTrigger.java?rev=1620549&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/ReplicationTrigger.java (added)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/ReplicationTrigger.java Tue Aug 26 10:07:57 2014
@@ -0,0 +1,38 @@
+/*
+ * 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.rule;
+
+/**
+ * a replication trigger
+ */
+public interface ReplicationTrigger {
+
+    /**
+     * register a replication trigger
+     * @param handlerId id of the given handler
+     * @param requestHandler handler
+     */
+    void register(String handlerId, ReplicationRequestHandler requestHandler);
+
+    /**
+     * unregister the handler with the given id, if existing
+     * @param handlerId id of the handler to unregister
+     */
+    void unregister(String handlerId);
+}

Propchange: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/ReplicationTrigger.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/RuleBasedReplicationTrigger.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/RuleBasedReplicationTrigger.java?rev=1620549&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/RuleBasedReplicationTrigger.java (added)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/RuleBasedReplicationTrigger.java Tue Aug 26 10:07:57 2014
@@ -0,0 +1,71 @@
+/*
+ * 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.rule.impl;
+
+import org.apache.felix.scr.annotations.*;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.replication.queue.ReplicationQueueDistributionStrategy;
+import org.apache.sling.replication.rule.ReplicationRequestHandler;
+import org.apache.sling.replication.rule.ReplicationRule;
+import org.apache.sling.replication.rule.ReplicationRuleEngine;
+import org.apache.sling.replication.rule.ReplicationTrigger;
+import org.osgi.framework.BundleContext;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * Trigger based on rules
+ */
+@Component(immediate = true, label = "Rule for Chain Replication", configurationFactory = true)
+@Service(value = ReplicationTrigger.class)
+public class RuleBasedReplicationTrigger implements ReplicationTrigger {
+
+    @Property(label = "Name")
+    private static final String NAME = "name";
+
+    @Property(label = "Rule String")
+    private static final String RULE_STRING = "ruleString";
+
+    @Property(label = "Target ReplicationRule", name = "ReplicationRule.target")
+    @Reference(name = "ReplicationRule", policy = ReferencePolicy.DYNAMIC)
+    private ReplicationRule replicationRule;
+
+
+    String ruleString;
+
+    @Activate
+    public void activate(BundleContext context, Map<String, ?> config) throws Exception {
+        String name = PropertiesUtil
+                .toString(config.get(NAME), String.valueOf(new Random().nextInt(1000)));
+
+        ruleString = PropertiesUtil.toString(config.get(RULE_STRING), null);
+    }
+
+
+    public void register(String handlerId, ReplicationRequestHandler requestHandler) {
+        replicationRule.apply(handlerId, requestHandler, ruleString);
+    }
+
+    public void unregister(String handlerId) {
+        replicationRule.undo(handlerId);
+    }
+}

Propchange: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/RuleBasedReplicationTrigger.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/TriggerPathReplicationRule.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/TriggerPathReplicationRule.java?rev=1620549&r1=1620548&r2=1620549&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/TriggerPathReplicationRule.java (original)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/rule/impl/TriggerPathReplicationRule.java Tue Aug 26 10:07:57 2014
@@ -22,10 +22,8 @@ import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Map;
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Service;
+
+import org.apache.felix.scr.annotations.*;
 import org.apache.sling.api.SlingConstants;
 import org.apache.sling.replication.agent.AgentReplicationException;
 import org.apache.sling.replication.agent.ReplicationAgent;
@@ -56,6 +54,9 @@ public class TriggerPathReplicationRule 
     private BundleContext bundleContext;
     private final Map<String, ServiceRegistration> registrations = new HashMap<String, ServiceRegistration>();
 
+    @Property(label = "Name", value = "trigger", propertyPrivate = true)
+    private static final String NAME = "name";
+
     @Activate
     protected void activate(BundleContext bundleContext) {
         this.bundleContext = bundleContext;

Added: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/servlet/ReplicationTriggerServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/servlet/ReplicationTriggerServlet.java?rev=1620549&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/servlet/ReplicationTriggerServlet.java (added)
+++ sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/servlet/ReplicationTriggerServlet.java Tue Aug 26 10:07:57 2014
@@ -0,0 +1,132 @@
+/*
+ * 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.servlet;
+
+import org.apache.felix.scr.annotations.*;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingAllMethodsServlet;
+import org.apache.sling.replication.agent.ReplicationAgent;
+import org.apache.sling.replication.communication.ReplicationRequest;
+import org.apache.sling.replication.event.ReplicationEvent;
+import org.apache.sling.replication.event.ReplicationEventType;
+import org.apache.sling.replication.resources.ReplicationConstants;
+import org.apache.sling.replication.rule.ReplicationRequestHandler;
+import org.apache.sling.replication.rule.ReplicationTrigger;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+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;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Triggers Server Sent Events servlet
+ */
+@SuppressWarnings("serial")
+@Component(metatype = false)
+@Service(value = Servlet.class)
+@Properties({
+        @Property(name = "sling.servlet.resourceTypes", value = "sling/replication/service/trigger"),
+        @Property(name = "sling.servlet.methods", value = "GET")
+})
+public class ReplicationTriggerServlet extends SlingAllMethodsServlet {
+
+    private final int MAX_NUMBER_OF_SECONDS = 3600;
+    private final int DEFAULT_NUMBER_OF_SECONDS = 60;
+    private final String HANDLER_NAME = "ReplicationTriggerServlet";
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Override
+    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
+            throws ServletException, IOException {
+
+        int seconds = DEFAULT_NUMBER_OF_SECONDS;
+
+        try{
+            seconds = Integer.parseInt(request.getParameter("sec"));
+
+        } catch(Throwable e){
+
+        }
+
+        if (seconds > MAX_NUMBER_OF_SECONDS) {
+            seconds = MAX_NUMBER_OF_SECONDS;
+        }
+        else if (seconds < 0) {
+            seconds = DEFAULT_NUMBER_OF_SECONDS;
+        }
+
+        ReplicationTrigger replicationTrigger = request.getResource().adaptTo(ReplicationTrigger.class);
+
+        // setup SSE headers
+        response.setContentType("text/event-stream");
+        response.setCharacterEncoding("UTF-8");
+        response.setHeader("Cache-Control", "no-cache");
+        response.setHeader("Connection", "keep-alive");
+        // needed to allow the JavaScript EventSource API to make a call from author to this server and listen for the events
+        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
+        response.setHeader("Access-Control-Allow-Credentials", "true");
+
+        final PrintWriter writer = response.getWriter();
+
+        String handlerId = HANDLER_NAME + UUID.randomUUID().toString();
+
+        replicationTrigger.register(handlerId, new ReplicationRequestHandler() {
+            public void execute(ReplicationRequest request) {
+                writeEvent(writer, request);
+            }
+        });
+
+        try {
+            Thread.sleep(seconds * 1000);
+        } catch (InterruptedException e) {
+            log.error("thread interrupted", e);
+        }
+
+        replicationTrigger.unregister(handlerId);
+
+    }
+
+    /* Write a single server-sent event to the response stream for the given event and message */
+    private void writeEvent(PrintWriter writer, ReplicationRequest replicationRequest) {
+
+        // write the event type (make sure to include the double newline)
+        writer.write("id: " + replicationRequest.getTime() + "\n");
+
+        // write the actual data
+        // this could be simple text or could be JSON-encoded text that the
+        // client then decodes
+        writer.write("data: " + replicationRequest.getAction() + "\n\n");
+
+        // flush the buffers to make sure the container sends the bytes
+        writer.flush();
+        log.debug("SSE event {}: {}", replicationRequest.getTime(), replicationRequest.getAction());
+    }
+}

Propchange: sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/servlet/ReplicationTriggerServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.agent.impl.SimpleReplicationAgentFactory-publish-reverse.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.agent.impl.SimpleReplicationAgentFactory-publish-reverse.json?rev=1620549&r1=1620548&r2=1620549&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.agent.impl.SimpleReplicationAgentFactory-publish-reverse.json (original)
+++ sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.agent.impl.SimpleReplicationAgentFactory-publish-reverse.json Tue Aug 26 10:07:57 2014
@@ -1,10 +1,11 @@
 {
-    "jcr:primaryType" : "sling:OsgiConfig",
-    "name" : "agents/publish-reverse",
-    "ReplicationPackageExporter.target" : "(name=exporters/remote/publish)",
-    "ReplicationPackageImporter.target" : "(name=importers/local/default)",
-    "ReplicationQueueProvider.target" : "(name=sjh)",
-    "ReplicationQueueDistributionStrategy.target" : "(name=error)",
-    "rules" : ["scheduled poll every 30 sec"],
-    "runModes" : ["author"]
+    "jcr:primaryType": "sling:OsgiConfig",
+    "name": "agents/publish-reverse",
+    "ReplicationPackageExporter.target": "(name=exporters/remote/publish)",
+    "ReplicationPackageImporter.target": "(name=importers/local/default)",
+    "ReplicationQueueProvider.target": "(name=sjh)",
+    "ReplicationQueueDistributionStrategy.target": "(name=error)",
+    "rules": ["scheduled poll every 30 sec",
+        "remote trigger on http://localhost:4503/libs/sling/replication/services/triggers/content-event with user admin and password admin"],
+    "runModes": ["author"]
 }

Added: sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-remote.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-remote.json?rev=1620549&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-remote.json (added)
+++ sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.author/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-remote.json Tue Aug 26 10:07:57 2014
@@ -0,0 +1,7 @@
+{
+    "jcr:primaryType" : "sling:OsgiConfig",
+    "name": "triggers/rule/remote",
+
+    "ReplicationRule.target" : "(name=remote)",
+    "ruleString" : "remote trigger on http://localhost:4503/libs/sling/replication/services/triggers/content-event with user admin and password admin"
+}
\ No newline at end of file

Added: sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.publish/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-content-event.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.publish/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-content-event.json?rev=1620549&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.publish/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-content-event.json (added)
+++ sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install.publish/org.apache.sling.replication.rule.impl.RuleBasedReplicationTrigger-content-event.json Tue Aug 26 10:07:57 2014
@@ -0,0 +1,7 @@
+{
+    "jcr:primaryType" : "sling:OsgiConfig",
+    "name": "triggers/rule/content-event",
+
+    "ReplicationRule.target" : "(name=trigger)",
+    "ruleString" : "trigger on path: /content/usergenerated"
+}
\ No newline at end of file

Modified: sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-agents.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-agents.json?rev=1620549&r1=1620548&r2=1620549&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-agents.json (original)
+++ sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-agents.json Tue Aug 26 10:07:57 2014
@@ -8,6 +8,6 @@
         "sling:resourceSuperType=sling/replication/service",
         "name={name}",
 
-        "queue/sling:resourceType=replication/agent/queue"
+        "queue/sling:resourceType=sling/replication/service/agent/queue"
     ]
 }
\ No newline at end of file

Added: sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-triggers.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-triggers.json?rev=1620549&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-triggers.json (added)
+++ sling/trunk/contrib/extensions/replication/core/src/main/resources/SLING-CONTENT/libs/sling/replication/install/resources/services/org.apache.sling.replication.resources.impl.OsgiPropertiesResourceProviderFactory-triggers.json Tue Aug 26 10:07:57 2014
@@ -0,0 +1,12 @@
+{
+    "jcr:primaryType" : "sling:OsgiConfig",
+    "name" : "replicationTriggers",
+    "provider.roots" : [ "/libs/sling/replication/services/triggers" ],
+    "serviceType" : "org.apache.sling.replication.rule.ReplicationTrigger",
+    "resourceProperties" : [
+        "sling:resourceType=sling/replication/service/trigger",
+        "sling:resourceSuperType=sling/replication/service",
+
+        "name={name}"
+    ]
+}
\ No newline at end of file