incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r587474 - in /incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting: SlingScriptResolver.java helpers/OnDemandOutputStream.java
Date Tue, 23 Oct 2007 12:26:04 GMT
Author: bdelacretaz
Date: Tue Oct 23 05:26:04 2007
New Revision: 587474

URL: http://svn.apache.org/viewvc?rev=587474&view=rev
Log:
SLING-71 - work in progress, 'out' variable is available to scripts

Added:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/OnDemandOutputStream.java
  (with props)
Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java?rev=587474&r1=587473&r2=587474&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java
Tue Oct 23 05:26:04 2007
@@ -20,6 +20,9 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.io.Reader;
 import java.util.HashMap;
 import java.util.Map;
@@ -40,6 +43,7 @@
 import org.apache.sling.microsling.api.exceptions.SlingException;
 import org.apache.sling.microsling.scripting.engines.RhinoJavasSriptEngine;
 import org.apache.sling.microsling.scripting.engines.VelocityTemplatesScriptEngine;
+import org.apache.sling.microsling.scripting.helpers.OnDemandOutputStream;
 import org.apache.sling.microsling.scripting.helpers.ScriptFilenameBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -85,7 +89,7 @@
      * @throws ServletException
      * @throws IOException
      */
-    public boolean evaluateScript(HttpServletRequest req,HttpServletResponse resp) throws
ServletException, IOException {
+    public boolean evaluateScript(final HttpServletRequest req,final HttpServletResponse
resp) throws ServletException, IOException {
         try {
             final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(req);
 
@@ -101,14 +105,21 @@
             props.put(ScriptEngine.RESPONSE, resp);
             props.put(ScriptEngine.REQUEST_CONTEXT, ctx);
             props.put(ScriptEngine.RESOURCE, ctx.getResource());
+            
+            // out is an OutputStream that's acquired on demand (SLING-71)
+            final OutputStream onDemand = new OnDemandOutputStream() {
+                @Override
+                protected OutputStream getOut() throws IOException {
+                    return resp.getOutputStream();
+                }
+            };
+            final PrintWriter pw = new PrintWriter(resp.getWriter());
+            props.put(ScriptEngine.OUT, pw);
 
             // evaluate the script now using the ScriptEngine
             script.getScriptEngine().eval(script.getScriptSource(), props);
-
-            resp.getWriter().println("<!-- This is the end -->");
+            pw.flush();
             resp.getWriter().flush();
-
-
             return true;
 
         } catch (IOException ioe) {
@@ -157,6 +168,10 @@
         // this is the location of the trailing asterisk
         final int scriptExtensionOffset = scriptFilename.length() - 1;
 
+        if(log.isDebugEnabled()) {
+            log.debug("Looking for script with filename=" + scriptFilename + " under " +
scriptPath);
+        }
+        
         if(s.itemExists(scriptPath)) {
 
             // get the item and ensure it is a node

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/OnDemandOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/OnDemandOutputStream.java?rev=587474&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/OnDemandOutputStream.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/OnDemandOutputStream.java
Tue Oct 23 05:26:04 2007
@@ -0,0 +1,70 @@
+/*
+ * 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.microsling.scripting.helpers;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/** OutputStream that acquires its output only when needed,
+ *  i.e. when a call that requires is is made.
+ *  This class is NOT thread-safe, due to getOut not
+ *  synchronizing. 
+ */
+public abstract class OnDemandOutputStream extends OutputStream {
+
+    private OutputStream out;
+    
+    @Override
+    /** close only if output stream was acquired */
+    public void close() throws IOException {
+        if(out!=null) {
+            out.close();
+        }
+    }
+
+    @Override
+    /** flush only if the output stream was acquired */
+    public void flush() throws IOException {
+        if(out!=null) {
+            out.flush();
+        }
+    }
+
+    @Override
+    public void write(byte[] arg0, int arg1, int arg2) throws IOException {
+        doGetOut().write(arg0, arg1, arg2);
+    }
+
+    @Override
+    public void write(byte[] arg0) throws IOException {
+        doGetOut().write(arg0);
+    }
+
+    @Override
+    public void write(int arg0) throws IOException {
+        doGetOut().write(arg0);
+    }
+    
+    private OutputStream doGetOut() throws IOException {
+        if(out==null) {
+            out = getOut();
+        }
+        return out;
+    }
+    
+    protected abstract OutputStream getOut() throws IOException;
+}

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/OnDemandOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/OnDemandOutputStream.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message