incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1077929 - in /sling/trunk/bundles/servlets/post/src: main/java/org/apache/sling/servlets/post/impl/ main/java/org/apache/sling/servlets/post/impl/helper/ main/java/org/apache/sling/servlets/post/impl/operations/ test/java/org/apache/sling/...
Date Fri, 04 Mar 2011 13:09:23 GMT
Author: fmeschbe
Date: Fri Mar  4 13:09:22 2011
New Revision: 1077929

URL: http://svn.apache.org/viewvc?rev=1077929&view=rev
Log:
SLING-1816 Support dynamic configurability of the Sling POST Servlet (instead of having to
deactivate and reactivate the servlet for configuration).

Modified:
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/DefaultNodeNameGenerator.java
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
    sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java?rev=1077929&r1=1077928&r2=1077929&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
(original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
Fri Mar  4 13:09:22 2011
@@ -18,7 +18,6 @@ package org.apache.sling.servlets.post.i
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -30,6 +29,7 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Modified;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
@@ -74,7 +74,7 @@ import org.slf4j.LoggerFactory;
 /**
  * POST servlet that implements the sling client library "protocol"
  */
-@Component(immediate = true, metatype = true, label = "%servlet.post.name", description =
"%servlet.post.description")
+@Component(immediate = true, specVersion = "1.1", metatype = true, label = "%servlet.post.name",
description = "%servlet.post.description")
 @Service(value = Servlet.class)
 @org.apache.felix.scr.annotations.Properties({
     @Property(name = "service.description", value = "Sling Post Servlet"),
@@ -129,11 +129,6 @@ public class SlingPostServlet extends Sl
 
     private static final String PARAM_AUTO_CHECKIN = ":autoCheckin";
 
-    /**
-     * utility class for parsing date strings
-     */
-    private DateParser dateParser;
-
     private ModifyOperation modifyOperation;
 
     private ServiceRegistration[] internalOperations;
@@ -156,8 +151,6 @@ public class SlingPostServlet extends Sl
 
     private ComponentContext componentContext;
 
-    private NodeNameGenerator defaultNodeNameGenerator;
-
     private ImportOperation importOperation;
 
     /**
@@ -327,7 +320,8 @@ public class SlingPostServlet extends Sl
 
     // ---------- SCR Integration ----------------------------------------------
 
-    protected void activate(ComponentContext context) {
+    protected void activate(final ComponentContext context,
+            final Map<String, Object> configuration) {
         synchronized ( this.delayedPostProcessors ) {
             this.componentContext = context;
             for(final ServiceReference ref : this.delayedPostProcessors) {
@@ -341,24 +335,8 @@ public class SlingPostServlet extends Sl
             }
             this.delayedPostOperations.clear();
         }
-        Dictionary<?, ?> props = context.getProperties();
 
-        String[] nameHints = OsgiUtil.toStringArray(props.get(PROP_NODE_NAME_HINT_PROPERTIES));
-        int nameMax = (int) OsgiUtil.toLong(
-            props.get(PROP_NODE_NAME_MAX_LENGTH), -1);
-        defaultNodeNameGenerator = new DefaultNodeNameGenerator(nameHints, nameMax);
-
-        dateParser = new DateParser();
-        String[] dateFormats = OsgiUtil.toStringArray(props.get(PROP_DATE_FORMAT));
-        for (String dateFormat : dateFormats) {
-            try {
-                dateParser.register(dateFormat);
-            } catch (Throwable t) {
-                log.warn(
-                    "activate: Ignoring format {} because it is invalid: {}",
-                    dateFormat, t);
-            }
-        }
+        // Dictionary<?, ?> props = context.getProperties();
 
         synchronized ( this.delayedNodeNameGenerators ) {
             for(final ServiceReference ref : this.delayedNodeNameGenerators) {
@@ -367,17 +345,16 @@ public class SlingPostServlet extends Sl
             this.delayedNodeNameGenerators.clear();
         }
 
-        this.baseVersioningConfiguration = createBaseVersioningConfiguration(props);
-
         // default operation: create/modify
-        modifyOperation = new ModifyOperation(defaultNodeNameGenerator,
-            dateParser);
+        modifyOperation = new ModifyOperation();
         modifyOperation.setExtraNodeNameGenerators(cachedNodeNameGenerators);
 
-        importOperation = new ImportOperation(defaultNodeNameGenerator,
-            contentImporter);
+        importOperation = new ImportOperation(contentImporter);
         importOperation.setExtraNodeNameGenerators(cachedNodeNameGenerators);
 
+        // configure now
+        configure(configuration);
+
         // other predefined operations
         final ArrayList<ServiceRegistration> providedServices = new ArrayList<ServiceRegistration>();
         final BundleContext bundleContext = componentContext.getBundleContext();
@@ -406,6 +383,33 @@ public class SlingPostServlet extends Sl
         modifyOperation.setServletContext(getServletContext());
     }
 
+    @Modified
+    private void configure(Map<String, Object> configuration) {
+        this.baseVersioningConfiguration = createBaseVersioningConfiguration(configuration);
+
+        final DateParser dateParser = new DateParser();
+        String[] dateFormats = OsgiUtil.toStringArray(configuration.get(PROP_DATE_FORMAT));
+        for (String dateFormat : dateFormats) {
+            try {
+                dateParser.register(dateFormat);
+            } catch (Throwable t) {
+                log.warn(
+                    "configure: Ignoring DateParser format {} because it is invalid: {}",
+                    dateFormat, t);
+            }
+        }
+
+        String[] nameHints = OsgiUtil.toStringArray(configuration.get(PROP_NODE_NAME_HINT_PROPERTIES));
+        int nameMax = (int) OsgiUtil.toLong(
+            configuration.get(PROP_NODE_NAME_MAX_LENGTH), -1);
+        NodeNameGenerator nodeNameGenerator = new DefaultNodeNameGenerator(
+            nameHints, nameMax);
+
+        this.modifyOperation.setDateParser(dateParser);
+        this.modifyOperation.setDefaultNodeNameGenerator(nodeNameGenerator);
+        this.importOperation.setDefaultNodeNameGenerator(nodeNameGenerator);
+    }
+
     @Override
     public void destroy() {
         modifyOperation.setServletContext(null);
@@ -419,8 +423,6 @@ public class SlingPostServlet extends Sl
             internalOperations = null;
         }
         modifyOperation = null;
-        defaultNodeNameGenerator = null;
-        dateParser = null;
         this.componentContext = null;
     }
 
@@ -579,7 +581,7 @@ public class SlingPostServlet extends Sl
         }
     }
 
-    private VersioningConfiguration createBaseVersioningConfiguration(Dictionary<?, ?>
props) {
+    private VersioningConfiguration createBaseVersioningConfiguration(Map<?, ?> props)
{
         VersioningConfiguration cfg = new VersioningConfiguration();
         cfg.setCheckinOnNewVersionableNode(OsgiUtil.toBoolean(
                 props.get(PROP_CHECKIN_ON_CREATE), DEFAULT_CHECKIN_ON_CREATE));

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/DefaultNodeNameGenerator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/DefaultNodeNameGenerator.java?rev=1077929&r1=1077928&r2=1077929&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/DefaultNodeNameGenerator.java
(original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/DefaultNodeNameGenerator.java
Fri Mar  4 13:09:22 2011
@@ -37,6 +37,10 @@ public class DefaultNodeNameGenerator im
     private int maxLength = DEFAULT_MAX_NAME_LENGTH;
     private int counter;
 
+    public DefaultNodeNameGenerator() {
+        this(null, -1);
+    }
+
     public DefaultNodeNameGenerator(String[] parameterNames, int maxNameLength) {
         if (parameterNames == null) {
             this.parameterNames = new String[0];

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java?rev=1077929&r1=1077928&r2=1077929&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
(original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
Fri Mar  4 13:09:22 2011
@@ -45,24 +45,31 @@ import org.apache.sling.servlets.post.No
 import org.apache.sling.servlets.post.PostResponse;
 import org.apache.sling.servlets.post.SlingPostConstants;
 import org.apache.sling.servlets.post.VersioningConfiguration;
+import org.apache.sling.servlets.post.impl.helper.DefaultNodeNameGenerator;
 import org.apache.sling.servlets.post.impl.helper.RequestProperty;
 
 abstract class AbstractCreateOperation extends AbstractPostOperation {
     /**
      * The default node name generator
      */
-    private final NodeNameGenerator defaultNodeNameGenerator;
+    private NodeNameGenerator defaultNodeNameGenerator;
 
     /**
      * utility class for generating node names
      */
     private NodeNameGenerator[] extraNodeNameGenerators;
 
-    public AbstractCreateOperation(NodeNameGenerator defaultNodeNameGenerator) {
-		this.defaultNodeNameGenerator = defaultNodeNameGenerator;
-	}
+    protected AbstractCreateOperation() {
+		this.defaultNodeNameGenerator = new DefaultNodeNameGenerator();
+    }
+
+    public void setDefaultNodeNameGenerator(
+            NodeNameGenerator defaultNodeNameGenerator) {
+        this.defaultNodeNameGenerator = defaultNodeNameGenerator;
+    }
 
-	public void setExtraNodeNameGenerators(NodeNameGenerator[] extraNodeNameGenerators) {
+    public void setExtraNodeNameGenerators(
+            NodeNameGenerator[] extraNodeNameGenerators) {
         this.extraNodeNameGenerators = extraNodeNameGenerators;
     }
 

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java?rev=1077929&r1=1077928&r2=1077929&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
(original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
Fri Mar  4 13:09:22 2011
@@ -34,7 +34,6 @@ import org.apache.sling.jcr.contentloade
 import org.apache.sling.jcr.contentloader.ImportOptions;
 import org.apache.sling.servlets.post.Modification;
 import org.apache.sling.servlets.post.ModificationType;
-import org.apache.sling.servlets.post.NodeNameGenerator;
 import org.apache.sling.servlets.post.PostResponse;
 import org.apache.sling.servlets.post.SlingPostConstants;
 import org.apache.sling.servlets.post.VersioningConfiguration;
@@ -52,9 +51,7 @@ public class ImportOperation extends Abs
      */
 	private ContentImporter contentImporter;
 
-    public ImportOperation(NodeNameGenerator defaultNodeNameGenerator,
-            ContentImporter contentImporter) {
-    	super(defaultNodeNameGenerator);
+    public ImportOperation(ContentImporter contentImporter) {
         this.contentImporter = contentImporter;
     }
 

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java?rev=1077929&r1=1077928&r2=1077929&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
(original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
Fri Mar  4 13:09:22 2011
@@ -51,17 +51,15 @@ import org.apache.sling.servlets.post.im
  */
 public class ModifyOperation extends AbstractCreateOperation {
 
-    private final DateParser dateParser;
+    private DateParser dateParser;
 
     /**
      * handler that deals with file upload
      */
     private final SlingFileUploadHandler uploadHandler;
 
-    public ModifyOperation(NodeNameGenerator defaultNodeNameGenerator,
-            DateParser dateParser) {
-        super(defaultNodeNameGenerator);
-        this.dateParser = dateParser;
+    public ModifyOperation() {
+        this.dateParser = new DateParser();
         this.uploadHandler = new SlingFileUploadHandler();
     }
 
@@ -69,6 +67,10 @@ public class ModifyOperation extends Abs
         this.uploadHandler.setServletContext(servletContext);
     }
 
+    public void setDateParser(final DateParser dateParser) {
+        this.dateParser = dateParser;
+    }
+
     @Override
     protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification>
changes)
             throws RepositoryException {

Modified: sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java?rev=1077929&r1=1077928&r2=1077929&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java
(original)
+++ sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/RequestPropertyTest.java
Fri Mar  4 13:09:22 2011
@@ -281,7 +281,7 @@ public class RequestPropertyTest {
         response.setPath("/test/path");
 
         Map<String, RequestProperty> props = (Map<String, RequestProperty>) PrivateAccessor.invoke(
-            new ModifyOperation(null, null), "collectContent", COLLECT_CLASSES,
+            new ModifyOperation(), "collectContent", COLLECT_CLASSES,
             new Object[] { request, response });
         return props;
     }



Mime
View raw message