You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2008/07/31 18:51:37 UTC

svn commit: r681415 - in /incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post: AbstractSlingPostOperation.java SlingPostOperation.java SlingPostProcessor.java impl/SlingPostServlet.java

Author: cziegeler
Date: Thu Jul 31 09:51:35 2008
New Revision: 681415

URL: http://svn.apache.org/viewvc?rev=681415&view=rev
Log:
SLING-594 : Add new SlingPostProcessor interface which is called just before the save.

Added:
    incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostProcessor.java   (with props)
Modified:
    incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractSlingPostOperation.java
    incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostOperation.java
    incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java

Modified: incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractSlingPostOperation.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractSlingPostOperation.java?rev=681415&r1=681414&r2=681415&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractSlingPostOperation.java (original)
+++ incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/AbstractSlingPostOperation.java Thu Jul 31 09:51:35 2008
@@ -62,7 +62,8 @@
      *            progress.
      */
     public void run(SlingHttpServletRequest request,
-            HtmlResponse response) {
+                    HtmlResponse response,
+                    SlingPostProcessor[] processors) {
 
         // calculate the paths
         String path = getItemPath(request);
@@ -83,6 +84,11 @@
 
             doRun(request, response, changes);
 
+            // invoke processors
+            for(int i=0; i<processors.length; i++) {
+                processors[i].process(request, changes);
+            }
+
             // set changes on html response
             for(Modification change : changes) {
                 switch ( change.getType() ) {

Modified: incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostOperation.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostOperation.java?rev=681415&r1=681414&r2=681415&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostOperation.java (original)
+++ incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostOperation.java Thu Jul 31 09:51:35 2008
@@ -72,5 +72,5 @@
      * @throws org.apache.sling.api.SlingException May be thrown if an error
      *             occurrs running the operation.
      */
-    void run(SlingHttpServletRequest request, HtmlResponse response);
+    void run(SlingHttpServletRequest request, HtmlResponse response, SlingPostProcessor[] processors);
 }

Added: incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostProcessor.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostProcessor.java?rev=681415&view=auto
==============================================================================
--- incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostProcessor.java (added)
+++ incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostProcessor.java Thu Jul 31 09:51:35 2008
@@ -0,0 +1,48 @@
+/*
+ * 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.servlets.post;
+
+import java.util.List;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+
+/**
+ * The <code>SlingPostProcessor</code> interface defines a service API to be
+ * implemented by service providers extending the Sling default POST servlet.
+ * Service providers may register OSGi services of this type to be used by the
+ * Sling default POST servlet to handle specific operations.
+ * <p>
+ * During a request the <code>SlingPostOperation</code> service is called
+ * with a list of registered post processors. After the operation has performed
+ * its changes but before the changes are persistet, all post processors
+ * are called.
+ */
+public interface SlingPostProcessor {
+
+    /**
+     * Process the current request.
+     * The post processor can inspect the list of changes and perform additional
+     * changes. If the processor performs a change it should make the change
+     * and add a {@link Modification} object to the changes list.
+     * @param request The current request.
+     * @param changes The list of changes for this request.
+     */
+    void process(SlingHttpServletRequest request, List<Modification> changes)
+    throws Exception;
+}

Propchange: incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostProcessor.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/SlingPostProcessor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java?rev=681415&r1=681414&r2=681415&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java (original)
+++ incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java Thu Jul 31 09:51:35 2008
@@ -17,8 +17,10 @@
 package org.apache.sling.servlets.post.impl;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
@@ -33,29 +35,33 @@
 import org.apache.sling.commons.osgi.OsgiUtil;
 import org.apache.sling.servlets.post.SlingPostConstants;
 import org.apache.sling.servlets.post.SlingPostOperation;
+import org.apache.sling.servlets.post.SlingPostProcessor;
 import org.apache.sling.servlets.post.impl.helper.DateParser;
 import org.apache.sling.servlets.post.impl.helper.NodeNameGenerator;
 import org.apache.sling.servlets.post.impl.operations.CopyOperation;
 import org.apache.sling.servlets.post.impl.operations.DeleteOperation;
 import org.apache.sling.servlets.post.impl.operations.ModifyOperation;
 import org.apache.sling.servlets.post.impl.operations.MoveOperation;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * POST servlet that implements the sling client library "protocol"
- * 
+ *
  * @scr.component immediate="true" label="%servlet.post.name"
  *                description="%servlet.post.description"
  * @scr.service interface="javax.servlet.Servlet"
  * @scr.property name="service.description" value="Sling Post Servlet"
  * @scr.property name="service.vendor" value="The Apache Software Foundation"
- * 
+ *
  * Use this as the default servlet for POST requests for Sling
  * @scr.property name="sling.servlet.resourceTypes"
  *               value="sling/servlet/default" private="true"
  * @scr.property name="sling.servlet.methods" value="POST" private="true"
+ * @scr.reference name="postProcessors" interface="org.apache.sling.servlets.post.SlingPostProcessor" cardinality="0..n" policy="dynamic"
  */
 public class SlingPostServlet extends SlingAllMethodsServlet {
 
@@ -100,6 +106,14 @@
 
     private final Map<String, SlingPostOperation> postOperations = new HashMap<String, SlingPostOperation>();
 
+    private final List<ServiceReference> delayedPostProcessors = new ArrayList<ServiceReference>();
+
+    private final List<ServiceReference> postProcessors = new ArrayList<ServiceReference>();
+
+    private SlingPostProcessor[] cachedPostProcessors = new SlingPostProcessor[0];
+
+    private ComponentContext componentContext;
+
     @Override
     public void init() {
         // default operation: create/modify
@@ -138,8 +152,12 @@
 
         } else {
 
+            final SlingPostProcessor[] processors;
+            synchronized ( this.delayedPostProcessors ) {
+                processors = this.cachedPostProcessors;
+            }
             try {
-                operation.run(request, htmlResponse);
+                operation.run(request, htmlResponse, processors);
             } catch (ResourceNotFoundException rnfe) {
                 htmlResponse.setStatus(HttpServletResponse.SC_NOT_FOUND,
                     rnfe.getMessage());
@@ -176,7 +194,7 @@
 
     /**
      * compute redirect URL (SLING-126)
-     * 
+     *
      * @param ctx the post processor
      * @return the redirect location or <code>null</code>
      */
@@ -251,6 +269,13 @@
     // ---------- SCR Integration ----------------------------------------------
 
     protected void activate(ComponentContext context) {
+        synchronized ( this.delayedPostProcessors ) {
+            this.componentContext = context;
+            for(final ServiceReference ref : this.delayedPostProcessors) {
+                this.registerPostProcessor(ref);
+            }
+            this.delayedPostProcessors.clear();
+        }
         Dictionary<?, ?> props = context.getProperties();
 
         String[] nameHints = OsgiUtil.toStringArray(props.get(PROP_NODE_NAME_HINT_PROPERTIES));
@@ -268,5 +293,44 @@
     protected void deactivate(ComponentContext context) {
         nodeNameGenerator = null;
         dateParser = null;
+        this.componentContext = null;
+    }
+
+    protected void bindPostProcessors(ServiceReference ref) {
+        synchronized ( this.delayedPostProcessors ) {
+            if ( this.componentContext == null ) {
+                this.delayedPostProcessors.add(ref);
+            } else {
+                this.registerPostProcessor(ref);
+            }
+        }
+    }
+
+    protected void unbindPostProcessors(ServiceReference ref) {
+        synchronized ( this.delayedPostProcessors ) {
+            this.delayedPostProcessors.remove(ref);
+            this.postProcessors.remove(ref);
+        }
+    }
+
+    protected void registerPostProcessor(ServiceReference ref) {
+        final int ranking = OsgiUtil.toInteger(ref.getProperty(Constants.SERVICE_RANKING), 0);
+        int index = 0;
+        while ( index < this.postProcessors.size() &&
+                ranking < OsgiUtil.toInteger(this.postProcessors.get(index).getProperty(Constants.SERVICE_RANKING), 0)) {
+            index++;
+        }
+        if ( index == this.postProcessors.size() ) {
+            this.postProcessors.add(ref);
+        } else {
+            this.postProcessors.add(index, ref);
+        }
+        this.cachedPostProcessors = new SlingPostProcessor[this.postProcessors.size()];
+        index = 0;
+        for(final ServiceReference current : this.postProcessors) {
+            final SlingPostProcessor processor = (SlingPostProcessor) this.componentContext.locateService("postProcessor", current);
+            this.cachedPostProcessors[index] = processor;
+            index++;
+        }
     }
 }