incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r795055 - in /sling/trunk: bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/ contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/
Date Fri, 17 Jul 2009 11:48:35 GMT
Author: cziegeler
Date: Fri Jul 17 11:48:35 2009
New Revision: 795055

URL: http://svn.apache.org/viewvc?rev=795055&view=rev
Log:
Store script resource resolver in script context, return script resource with request resource
resolver.

Modified:
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java
    sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java
    sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java

Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java?rev=795055&r1=795054&r2=795055&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java
(original)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java
Fri Jul 17 11:48:35 2009
@@ -59,9 +59,12 @@
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.SyntheticResource;
 import org.apache.sling.api.scripting.ScriptEvaluationException;
 import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.api.scripting.SlingScriptConstants;
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.scripting.core.ScriptHelper;
 import org.osgi.framework.BundleContext;
@@ -74,6 +77,8 @@
 	// resource adapts (null if the resource does not adapt to a node
     private static final String NODE = "currentNode";
 
+    private static ThreadLocal<ResourceResolver> requestResourceResolver = new ThreadLocal<ResourceResolver>();
+
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
     private Resource scriptResource;
@@ -95,7 +100,16 @@
     // ---------- SlingScript interface ----------------------------------------
 
     public Resource getScriptResource() {
-        return scriptResource;
+        final ResourceResolver resolver = requestResourceResolver.get();
+        if ( resolver == null ) {
+            // if we don't have a request resolver we directly return the script resource
+            return scriptResource;
+        }
+        Resource rsrc = resolver.getResource(this.scriptResource.getPath());
+        if ( rsrc == null ) {
+            rsrc = new SyntheticResource(resolver, this.scriptResource.getPath(), this.scriptResource.getResourceType());
+        }
+        return rsrc;
     }
 
     /**
@@ -126,6 +140,13 @@
             ctx.setWriter((Writer) bindings.get(OUT));
             ctx.setErrorWriter(new LogWriter((Logger) bindings.get(LOG)));
 
+            // set the current resource resolver
+            requestResourceResolver.set(props.getRequest().getResourceResolver());
+
+            // set the script resource resolver as an attribute
+            ctx.setAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER,
+                    this.scriptResource.getResourceResolver(), ScriptContext.ENGINE_SCOPE);
+
             reader = getScriptReader();
             if ( method != null && !(this.scriptEngine instanceof Invocable)) {
                 reader = getWrapperReader(reader, method, args);
@@ -179,6 +200,7 @@
                     // don't care
                 }
             }
+            requestResourceResolver.remove();
         }
     }
 

Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java?rev=795055&r1=795054&r2=795055&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java
(original)
+++ sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java
Fri Jul 17 11:48:35 2009
@@ -31,8 +31,10 @@
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingIOException;
 import org.apache.sling.api.SlingServletException;
+import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.api.scripting.SlingScriptConstants;
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.commons.classloader.ClassLoaderWriter;
 import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
@@ -122,11 +124,18 @@
      * @throws SlingIOException
      */
     @SuppressWarnings("unchecked")
-    private void callJsp(Bindings bindings, SlingScriptHelper scriptHelper) {
-
-        ioProvider.setRequestResourceResolver(scriptHelper.getScript().getScriptResource().getResourceResolver());
+    private void callJsp(final Bindings bindings,
+                         final SlingScriptHelper scriptHelper,
+                         final ScriptContext context) {
+
+        ResourceResolver resolver = (ResourceResolver) context.getAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER,
+                ScriptContext.ENGINE_SCOPE);
+        if ( resolver == null ) {
+            resolver = scriptHelper.getScript().getScriptResource().getResourceResolver();
+        }
+        ioProvider.setRequestResourceResolver(resolver);
         try {
-            JspServletWrapperAdapter jsp = getJspWrapperAdapter(scriptHelper);
+            final JspServletWrapperAdapter jsp = getJspWrapperAdapter(scriptHelper);
             // create a SlingBindings object
             final SlingBindings slingBindings = new SlingBindings();
             slingBindings.putAll(bindings);
@@ -332,7 +341,7 @@
                 Thread.currentThread().setContextClassLoader(jspClassLoader);
 
                 try {
-                    callJsp(props, scriptHelper);
+                    callJsp(props, scriptHelper, context);
                 } catch (SlingServletException e) {
                     // ServletExceptions use getRootCause() instead of getCause(),
                     // so we have to extract the actual root cause and pass it as

Modified: sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java?rev=795055&r1=795054&r2=795055&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
(original)
+++ sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
Fri Jul 17 11:48:35 2009
@@ -30,8 +30,10 @@
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingIOException;
 import org.apache.sling.api.SlingServletException;
+import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScript;
+import org.apache.sling.api.scripting.SlingScriptConstants;
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.commons.classloader.ClassLoaderWriter;
 import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
@@ -156,16 +158,24 @@
 
     /**
      * Call the servlet.
-     * @param scriptHelper
+     * @param binding The bindings for the script invocation
+     * @param scriptHelper The script helper.
+     * @param context The script context.
      * @throws SlingServletException
      * @throws SlingIOException
      */
     @SuppressWarnings("unchecked")
-    private void callServlet(Bindings bindings, SlingScriptHelper scriptHelper) {
-
-        ioProvider.setRequestResourceResolver(scriptHelper.getScript().getScriptResource().getResourceResolver());
+    private void callServlet(final Bindings bindings,
+                             final SlingScriptHelper scriptHelper,
+                             final ScriptContext context) {
+        ResourceResolver resolver = (ResourceResolver) context.getAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER,
+                ScriptContext.ENGINE_SCOPE);
+        if ( resolver == null ) {
+            resolver = scriptHelper.getScript().getScriptResource().getResourceResolver();
+        }
+        ioProvider.setRequestResourceResolver(resolver);
         try {
-            ServletWrapper servlet = getWrapperAdapter(scriptHelper);
+            final ServletWrapper servlet = getWrapperAdapter(scriptHelper);
             // create a SlingBindings object
             final SlingBindings slingBindings = new SlingBindings();
             slingBindings.putAll(bindings);
@@ -251,7 +261,7 @@
             final Bindings props = context.getBindings(ScriptContext.ENGINE_SCOPE);
             final SlingScriptHelper scriptHelper = (SlingScriptHelper) props.get(SLING);
             if (scriptHelper != null) {
-                ((JavaScriptEngineFactory)this.getFactory()).callServlet(props, scriptHelper);
+                ((JavaScriptEngineFactory)this.getFactory()).callServlet(props, scriptHelper,
context);
             }
             return null;
         }



Mime
View raw message