incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r982920 - in /sling/trunk/contrib/extensions/bgservlets/src/main: java/org/apache/sling/bgservlets/impl/ java/org/apache/sling/bgservlets/impl/storage/ resources/OSGI-INF/ resources/OSGI-INF/metatype/
Date Fri, 06 Aug 2010 10:08:54 GMT
Author: bdelacretaz
Date: Fri Aug  6 10:08:54 2010
New Revision: 982920

URL: http://svn.apache.org/viewvc?rev=982920&view=rev
Log:
SLING-550 - make important parameters configurable

Added:
    sling/trunk/contrib/extensions/bgservlets/src/main/resources/OSGI-INF/
    sling/trunk/contrib/extensions/bgservlets/src/main/resources/OSGI-INF/metatype/
    sling/trunk/contrib/extensions/bgservlets/src/main/resources/OSGI-INF/metatype/metatype.properties
  (with props)
Modified:
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ExecutionEngineImpl.java
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/storage/JobStorageImpl.java

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java?rev=982920&r1=982919&r2=982920&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java
Fri Aug  6 10:08:54 2010
@@ -39,19 +39,22 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.bgservlets.ExecutionEngine;
 import org.apache.sling.bgservlets.JobStorage;
 import org.apache.sling.engine.SlingServlet;
+import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Filter that runs the current request in the background if specific request
- * parameters are set. TODO: define the position of this filter in the chain,
- * and how do we enforce it?
+ * parameters are set. Must be placed early in the filter chain.
  */
-@Component
+@Component(
+        metatype=true, 
+        label="%BackgroundServletStarterFilter.label", 
+        description="%BackgroundServletStarterFilter.description")
 @Service
 @Properties( {
-        @Property(name = "filter.scope", value = "request"),
-        @Property(name = "filter.order", intValue = java.lang.Integer.MIN_VALUE) })
+        @Property(name = "filter.scope", value = "request", propertyPrivate=true),
+        @Property(name = "filter.order", intValue = java.lang.Integer.MIN_VALUE, propertyPrivate=true
)})
 public class BackgroundServletStarterFilter implements Filter {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
@@ -67,14 +70,26 @@ public class BackgroundServletStarterFil
     
     @Reference
     private JobStorage jobStorage;
+    
+    /** Name of the property that defines the request parameter name to 
+     *  use to start a servlet in the background.
+     */
+    @Property(value="sling:bg")
+    public static final String PROP_BG_PARAM = "background.parameter.name";
 
     /**
-     * Request runs in the background if this request parameter is present TODO
-     * should be configurable, and maybe use other decision methods
+     * Request runs in the background if this request parameter is present
      */
-    public static final String BG_PARAM = "sling:bg";
-    private static final String[] PARAM_TO_REMOVE = { BG_PARAM };
+    private String bgParamName;
 
+    protected void activate(ComponentContext ctx) {
+        bgParamName = (String)ctx.getProperties().get(PROP_BG_PARAM);
+        if(bgParamName == null || bgParamName.length() == 0) {
+            throw new IllegalStateException("Missing " + PROP_BG_PARAM + " in ComponentContext");
+        }
+        log.info("Request parameter {} will run servlets in the background", bgParamName);
+    }
+    
     public void doFilter(final ServletRequest sreq,
             final ServletResponse sresp, final FilterChain chain)
             throws IOException, ServletException {
@@ -91,17 +106,17 @@ public class BackgroundServletStarterFil
         final SlingHttpServletRequest slingRequest = 
             (request instanceof SlingHttpServletRequest ? (SlingHttpServletRequest) request
: null);
         final HttpServletResponse response = (HttpServletResponse) sresp;
-        final String bgParam = sreq.getParameter(BG_PARAM);
+        final String bgParam = sreq.getParameter(bgParamName);
         if (Boolean.valueOf(bgParam)) {
             try {
                 final BackgroundRequestExecutionJob job = new BackgroundRequestExecutionJob(
                         slingServlet, resourceResolverFactory, jobStorage,
-                        slingRequest, response, PARAM_TO_REMOVE);
+                        slingRequest, response, new String[] { bgParamName });
                 log.debug("{} parameter true, running request in the background ({})",
-                        BG_PARAM, job);
+                        bgParamName, job);
                 if (slingRequest != null) {
                     slingRequest.getRequestProgressTracker().log(
-                            BG_PARAM
+                            bgParamName
                             + " parameter true, running request in background ("
                             + job + ")");
                 }

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ExecutionEngineImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ExecutionEngineImpl.java?rev=982920&r1=982919&r2=982920&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ExecutionEngineImpl.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ExecutionEngineImpl.java
Fri Aug  6 10:08:54 2010
@@ -30,6 +30,7 @@ import java.util.concurrent.ThreadPoolEx
 import java.util.concurrent.TimeUnit;
 
 import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.SlingException;
 import org.apache.sling.bgservlets.ExecutionEngine;
@@ -43,13 +44,28 @@ import org.slf4j.LoggerFactory;
  * Simple ExecutionEngine TODO should use Sling's thread pool, and check
  * synergies with scheduler services
  */
-@Component
+@Component(
+        metatype=true, 
+        label="%ExecutionEngineImpl.label", 
+        description="%ExecutionEngineImpl.description")
 @Service
 public class ExecutionEngineImpl implements ExecutionEngine {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
     private Executor executor;
     private final Map<String, JobStatus> jobs = Collections.synchronizedMap(new HashMap<String,
JobStatus>());
+    
+    @Property(intValue=10)
+    public static final String PROP_CORE_POOL_SIZE = "core.pool.size";
+    private int corePoolSize;
+
+    @Property(intValue=20)
+    public static final String PROP_MAX_POOL_SIZE = "max.pool.size";
+    private int maximumPoolSize;
+    
+    @Property(intValue=30)
+    public static final String PROP_KEEP_ALIVE_TIME = "keep.alive.time.seconds";
+    private int keepAliveTimeSeconds;
 
     private class RunnableWrapper implements Runnable {
         private final Runnable inputJob;
@@ -90,12 +106,19 @@ public class ExecutionEngineImpl impleme
             super("Execution queue is full, cannot execute " + r);
         }
     }
+    
+    private int getIntegerProperty(ComponentContext ctx, String name) {
+        final Integer value = (Integer)ctx.getProperties().get(name);
+        if(value == null) {
+            throw new IllegalStateException("Missing ComponentContext property: " + name);
+        }
+        return value.intValue();
+    }
 
     protected void activate(ComponentContext context) {
-        // TODO configurable!
-        final int corePoolSize = 2;
-        int maximumPoolSize = 2;
-        long keepAliveTime = 30;
+        corePoolSize = getIntegerProperty(context, PROP_CORE_POOL_SIZE);
+        maximumPoolSize = getIntegerProperty(context, PROP_MAX_POOL_SIZE);
+        keepAliveTimeSeconds = getIntegerProperty(context, PROP_KEEP_ALIVE_TIME);
         TimeUnit unit = TimeUnit.SECONDS;
         BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(4);
         RejectedExecutionHandler handler = new RejectedExecutionHandler() {
@@ -104,8 +127,10 @@ public class ExecutionEngineImpl impleme
                 onJobRejected(r);
             }
         };
+        log.info("ThreadPoolExecutor configuration: corePoolSize = {}, maxPoolSize={}, keepAliveTimeSeconds={}",
+                new Object[] { corePoolSize, maximumPoolSize, keepAliveTimeSeconds });
         executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
-                keepAliveTime, unit, workQueue, handler);
+                keepAliveTimeSeconds, unit, workQueue, handler);
     }
 
     protected void deactivate(ComponentContext context) {

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/storage/JobStorageImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/storage/JobStorageImpl.java?rev=982920&r1=982919&r2=982920&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/storage/JobStorageImpl.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/storage/JobStorageImpl.java
Fri Aug  6 10:08:54 2010
@@ -28,29 +28,51 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
 import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.bgservlets.BackgroundServletConstants;
 import org.apache.sling.bgservlets.JobData;
 import org.apache.sling.bgservlets.JobStorage;
 import org.apache.sling.bgservlets.impl.DeepNodeCreator;
+import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /** Default JobStorage implementation */
-@Component
+@Component(
+        metatype=true, 
+        label="%JobStorage.label", 
+        description="%JobStorage.description")
 @Service
 public class JobStorageImpl implements JobStorage {
 
     private Logger log = LoggerFactory.getLogger(getClass());
     
-    /** TODO configurable */
-    public static final String JOBS_PATH = "/var/bg/jobs"; 
+    /** Configurable base path for storing job data */
+    @Property(value="/var/bg/jobs")
+    public static final String PROP_JOB_STORAGE_PATH = "job.storage.path";
+    
     public static final String PATH_FORMAT = "/yyyy/MM/dd/HH/mm";
     public static final String JOB_NODETYPE = "nt:unstructured";
     
+    private String jobStoragePath;
 	private int counter;
 	private static final DateFormat pathFormat = new SimpleDateFormat(PATH_FORMAT);
 	
+    protected void activate(ComponentContext ctx) {
+        jobStoragePath = (String)ctx.getProperties().get(PROP_JOB_STORAGE_PATH);
+        if(jobStoragePath == null || jobStoragePath.length() == 0) {
+            throw new IllegalStateException("Missing " + PROP_JOB_STORAGE_PATH + " in ComponentContext");
+        }
+        if(!jobStoragePath.startsWith("/")) {
+            jobStoragePath = "/" + jobStoragePath;
+        }
+        if(jobStoragePath.endsWith("/")) {
+            jobStoragePath = jobStoragePath.substring(0, jobStoragePath.length() - 1);
+        }
+        log.info("Jobs will be stored under {}", jobStoragePath);
+    }
+    
 	public JobData createJobData(Session s) {
         try {
             return getJobData(createNewJobNode(s));
@@ -71,7 +93,7 @@ public class JobStorageImpl implements J
 	    String path = null;
 	    synchronized (this) {
 	        counter++;
-	        path = JOBS_PATH + pathFormat.format(new Date()) + "/" + counter;
+	        path = jobStoragePath + pathFormat.format(new Date()) + "/" + counter;
         }
 	    final Node result = new DeepNodeCreator().deepCreateNode(path, s, JOB_NODETYPE);
 	    result.addMixin(JobData.JOB_DATA_MIXIN);

Added: sling/trunk/contrib/extensions/bgservlets/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=982920&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/resources/OSGI-INF/metatype/metatype.properties
(added)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/resources/OSGI-INF/metatype/metatype.properties
Fri Aug  6 10:08:54 2010
@@ -0,0 +1,53 @@
+#
+#  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.
+#
+
+
+#
+# This file contains localization strings for configuration labels and
+# descriptions as used in the metatype.xml descriptor generated by the
+# the SCR plugin
+
+BackgroundServletStarterFilter.label = Background Requests Filter
+BackgroundServletStarterFilter.description = ServletFilter that runs \
+	requests in the background if a specific request parameter is set \
+	to true.
+
+background.parameter.name.name = Background request parameter name 
+background.parameter.name.description = Requests run in the background \
+	if this request parameter is set to true.
+	
+JobStorage.label = Background Requests Job Storage Service
+JobStorage.description = Stores job state and output for requests running \
+	in the background.
+
+job.storage.path.name = Job Storage Base Path
+job.storage.path.description = Path under which job state is stored in \
+	the repository.
+	
+ExecutionEngineImpl.label = Execution Engine for Background Requests
+ExecutionEngineImpl.description = Used to run requests in the background
+
+core.pool.size.name = Core ThreadPoolExecutor size
+core.pool.size.description = See ThreadPoolExecutor documentation for more info.
+
+max.pool.size.name = Maximum ThreadPoolExecutor size
+max.pool.size.description = See ThreadPoolExecutor documentation for more info.
+
+keep.alive.time.seconds.name = Keep alive time for ThreadPoolExecutor (seconds)
+keep.alive.time.seconds.description = See ThreadPoolExecutor documentation for more info.
\ No newline at end of file

Propchange: sling/trunk/contrib/extensions/bgservlets/src/main/resources/OSGI-INF/metatype/metatype.properties
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message