incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r965896 - in /sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl: ./ nodestream/
Date Tue, 20 Jul 2010 16:24:31 GMT
Author: bdelacretaz
Date: Tue Jul 20 16:24:31 2010
New Revision: 965896

URL: http://svn.apache.org/viewvc?rev=965896&view=rev
Log:
SLING-500 - store and replay output of background servlets from repository (still rough -
job path with .bgreplay returns servlet output)

Added:
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/OutputReplayServlet.java
  (with props)
Modified:
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundTestServlet.java
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeInputStream.java
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeOutputStream.java

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java?rev=965896&r1=965895&r2=965896&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
Tue Jul 20 16:24:31 2010
@@ -18,10 +18,11 @@
  */
 package org.apache.sling.bgservlets.impl;
 
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -29,6 +30,7 @@ import org.apache.sling.api.resource.Log
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.bgservlets.JobStatus;
+import org.apache.sling.bgservlets.impl.nodestream.NodeOutputStream;
 import org.apache.sling.commons.auth.spi.AuthenticationInfo;
 import org.apache.sling.engine.SlingServlet;
 import org.slf4j.Logger;
@@ -68,14 +70,31 @@ class BackgroundRequestExecutionJob impl
                     "Missing AuthenticationInfo attribute");
         }
         resourceResolver = rrf.getResourceResolver(aa);
-
-        // TODO write output to the Sling repository. For now: just a temp file
-        final File output = File.createTempFile(getClass().getSimpleName(),
-                ".data");
-        output.deleteOnExit();
-        path = output.getAbsolutePath();
-        stream = new SuspendableOutputStream(new FileOutputStream(output));
-        response = new BackgroundHttpServletResponse(hsr, stream);
+        
+        // Save servlet output to the repository
+        final Session s = resourceResolver.adaptTo(Session.class);
+        if(s == null) {
+            throw new IOException("Unable to get Session from ResourceResolver " + resourceResolver);
+        }
+        
+        // TODO configurable path
+        try {
+            final String outputRootPath = "/" + getClass().getSimpleName();
+            Node outputRoot = null;
+            if(s.itemExists(outputRootPath)) {
+                outputRoot = (Node)s.getItem(outputRootPath);
+            } else {
+                outputRoot = s.getRootNode().addNode(outputRootPath.substring(1));
+            }
+            final Node output = outputRoot.addNode("out_" + System.currentTimeMillis());
+            s.save();
+            final NodeOutputStream nos = new NodeOutputStream(output);
+            path = output.getPath();
+            stream = new SuspendableOutputStream(nos);
+            response = new BackgroundHttpServletResponse(hsr, stream);
+        } catch(RepositoryException re) {
+            throw new IOException("RepositoryException in BackgroundRequestExecutionJob",
re); 
+        }
     }
 
     public String toString() {

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundTestServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundTestServlet.java?rev=965896&r1=965895&r2=965896&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundTestServlet.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundTestServlet.java
Tue Jul 20 16:24:31 2010
@@ -20,6 +20,7 @@ package org.apache.sling.bgservlets.impl
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Date;
 
 import javax.servlet.ServletException;
 
@@ -55,6 +56,7 @@ public class BackgroundTestServlet exten
         final int interval = getIntParam(request, "interval", 1);
         final int flushEvery = getIntParam(request, "flushEvery", 2);
 
+        w.println("Start at " + new Date());
         try {
             for (int i = 1; i <= cycles; i++) {
                 if (i % flushEvery == 0) {

Added: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/OutputReplayServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/OutputReplayServlet.java?rev=965896&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/OutputReplayServlet.java
(added)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/OutputReplayServlet.java
Tue Jul 20 16:24:31 2010
@@ -0,0 +1,74 @@
+/*
+ * 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.bgservlets.impl;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.jcr.Node;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.bgservlets.impl.nodestream.NodeInputStream;
+
+/** Servlet that replays the output of servlets executed in
+ *  the background.
+ */
+@Component
+@Service
+@SuppressWarnings("serial")
+@Properties ( {
+    @Property(name="sling.servlet.resourceTypes", value="sling/servlet/default"),
+    @Property(name="sling.servlet.extensions", value="bgreplay")
+})
+public class OutputReplayServlet extends SlingSafeMethodsServlet {
+
+    @Override
+    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

+    throws ServletException, IOException {
+        final Node n = request.getResource().adaptTo(Node.class);
+        if(n == null) {
+            response.sendError(HttpServletResponse.SC_NOT_FOUND, 
+                    "Resource does not adapt to a Node: " + request.getResource().getPath());
+        }
+        
+        // TODO content-type, length etc
+        final NodeInputStream nis = new NodeInputStream(n);
+        try {
+            final OutputStream os = response.getOutputStream();
+            final byte [] buffer = new byte[32768];
+            int count = 0;
+            while((count = nis.read(buffer, 0, buffer.length)) > 0) {
+                os.write(buffer, 0, count);
+            }
+            os.flush();
+        } finally {
+            if(nis != null) {
+                nis.close();
+            }
+        }
+    }
+}

Propchange: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/OutputReplayServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/OutputReplayServlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeInputStream.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeInputStream.java?rev=965896&r1=965895&r2=965896&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeInputStream.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeInputStream.java
Tue Jul 20 16:24:31 2010
@@ -46,7 +46,7 @@ public class NodeInputStream extends Inp
     /** Current stream that we are reading */
     private InputStream currentStream;
     
-    NodeInputStream(Node n) throws IOException {
+    public NodeInputStream(Node n) throws IOException {
         node = n;
         selectNextStream();
     }
@@ -101,7 +101,7 @@ public class NodeInputStream extends Inp
             return 0;
         }
         int result = currentStream.read(b, off, len);
-        if(result == 0) {
+        if(result <= 0) {
             selectNextStream();
             return read(b, off, len);
         }

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeOutputStream.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeOutputStream.java?rev=965896&r1=965895&r2=965896&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeOutputStream.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/nodestream/NodeOutputStream.java
Tue Jul 20 16:24:31 2010
@@ -82,10 +82,14 @@ public class NodeOutputStream extends Ou
         counter++;
         final String name = NodeOutputStream.STREAM_PROPERTY_NAME_PREFIX + counter;
         try {
-            node.setProperty(name, new ByteArrayInputStream(buffer.toByteArray()));
-            log.debug("Saved {} bytes to Property {}", buffer.size(), node.getProperty(name).getPath());
-            node.save();
-            buffer.reset();
+            if(!node.getSession().isLive()) {
+                log.warn("Session closed, unable to flush stream");
+            } else {
+                node.setProperty(name, new ByteArrayInputStream(buffer.toByteArray()));
+                log.debug("Saved {} bytes to Property {}", buffer.size(), node.getProperty(name).getPath());
+                node.save();
+                buffer.reset();
+            }
         } catch(RepositoryException re) {
             throw new IOException("RepositoryException in flush()", re);
         }



Mime
View raw message