sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r932399 - in /sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core: ScriptHelper.java impl/DefaultSlingScript.java impl/SlingScriptAdapterFactory.java
Date Fri, 09 Apr 2010 13:15:50 GMT
Author: cziegeler
Date: Fri Apr  9 13:15:50 2010
New Revision: 932399

URL: http://svn.apache.org/viewvc?rev=932399&view=rev
Log:
SLING-1478 : Improve SlingScriptHelper service handling

Modified:
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/ScriptHelper.java
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java

Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/ScriptHelper.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/ScriptHelper.java?rev=932399&r1=932398&r2=932399&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/ScriptHelper.java
(original)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/ScriptHelper.java
Fri Apr  9 13:15:50 2010
@@ -54,72 +54,88 @@ import org.slf4j.LoggerFactory;
  */
 public class ScriptHelper implements SlingScriptHelper {
 
+    /** The corresponding script. */
     private final SlingScript script;
 
+    /** The current request or <code>null</code>. */
     private final SlingHttpServletRequest request;
 
+    /** The current response or <code>null</code>. */
     private final SlingHttpServletResponse response;
 
+    /** The bundle context. */
     protected final BundleContext bundleContext;
 
     /**
      * The list of references - we don't need to synchronize this as we are
      * running in one single request.
      */
-    protected final List<ServiceReference> references = new ArrayList<ServiceReference>();
+    protected List<ServiceReference> references;
 
     /** A map of found services. */
-    protected final Map<String, Object> services = new HashMap<String, Object>();
+    protected Map<String, Object> services;
 
-    public ScriptHelper(BundleContext ctx, SlingScript script) {
+    public ScriptHelper(final BundleContext ctx, final SlingScript script) {
         if (ctx == null ) {
             throw new IllegalArgumentException("Bundle context must not be null.");
         }
-        this.bundleContext = ctx;
         this.request = null;
         this.response = null;
         this.script = script;
+        this.bundleContext = ctx;
     }
 
-    public ScriptHelper(BundleContext ctx, SlingScript script, SlingHttpServletRequest request,
-            SlingHttpServletResponse response) {
+    public ScriptHelper(final BundleContext ctx,
+            final SlingScript script,
+            final SlingHttpServletRequest request,
+            final SlingHttpServletResponse response) {
         if (ctx == null ) {
             throw new IllegalArgumentException("Bundle context must not be null.");
         }
-        this.bundleContext = ctx;
         this.script = script;
         this.request = new OnDemandReaderRequest(request);
         this.response = new OnDemandWriterResponse(response);
+        this.bundleContext = ctx;
     }
 
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#getScript()
+     */
     public SlingScript getScript() {
         return script;
     }
 
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#getRequest()
+     */
     public SlingHttpServletRequest getRequest() {
         return request;
     }
 
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#getResponse()
+     */
     public SlingHttpServletResponse getResponse() {
         return response;
     }
 
     /**
-     * @trows SlingIOException Wrapping a <code>IOException</code> thrown
-     *        while handling the include.
-     * @throws SlingServletException Wrapping a <code>ServletException</code>
-     *             thrown while handling the include.
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#include(java.lang.String)
      */
     public void include(String path) {
         include(path, (RequestDispatcherOptions) null);
     }
 
-    /** Include the output of another request, using specified options */
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#include(java.lang.String, java.lang.String)
+     */
     public void include(String path, String options) {
         include(path, new RequestDispatcherOptions(options));
     }
 
-    /** Include the output of another request, using specified options */
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#include(java.lang.String, org.apache.sling.api.request.RequestDispatcherOptions)
+     */
     public void include(String path, RequestDispatcherOptions options) {
         final RequestDispatcher dispatcher = getRequest().getRequestDispatcher(
             path, options);
@@ -135,17 +151,23 @@ public class ScriptHelper implements Sli
         }
     }
 
-    /** Forward the request to another resource, using no options */
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#forward(java.lang.String)
+     */
     public void forward(String path) {
         forward(path, (RequestDispatcherOptions) null);
     }
 
-    /** Forward the request to another resource, using specified options */
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#forward(java.lang.String, java.lang.String)
+     */
     public void forward(String path, String options) {
         forward(path, new RequestDispatcherOptions(options));
     }
 
-    /** Forward the request to another resource, using specified options */
+    /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#forward(java.lang.String, org.apache.sling.api.request.RequestDispatcherOptions)
+     */
     public void forward(String path, RequestDispatcherOptions options) {
         final RequestDispatcher dispatcher = getRequest().getRequestDispatcher(
             path, options);
@@ -162,16 +184,29 @@ public class ScriptHelper implements Sli
     }
 
     /**
+     * @see org.apache.sling.api.scripting.SlingScriptHelper#dispose()
+     */
+    public void dispose() {
+        LoggerFactory.getLogger(this.getClass()).error("ScriptHelper#dispose has been called.
This method is deprecated and should never be called by clients!");
+    }
+
+    /**
      * @see org.apache.sling.api.scripting.SlingScriptHelper#getService(java.lang.Class)
      */
     @SuppressWarnings("unchecked")
     public <ServiceType> ServiceType getService(Class<ServiceType> type) {
-        ServiceType service = (ServiceType) this.services.get(type.getName());
+        ServiceType service = (this.services == null ? null : (ServiceType) this.services.get(type.getName()));
         if (service == null) {
             final ServiceReference ref = this.bundleContext.getServiceReference(type.getName());
             if (ref != null) {
                 service = (ServiceType) this.bundleContext.getService(ref);
                 if ( service != null ) {
+                    if ( this.services == null ) {
+                        this.services = new HashMap<String, Object>();
+                    }
+                    if ( this.references == null ) {
+                        this.references = new ArrayList<ServiceReference>();
+                    }
                     this.references.add(ref);
                     this.services.put(type.getName(), service);
                 }
@@ -194,9 +229,12 @@ public class ScriptHelper implements Sli
             if (refs != null) {
                 final List<ServiceType> objects = new ArrayList<ServiceType>();
                 for (int i = 0; i < refs.length; i++) {
-                    this.references.add(refs[i]);
                     final ServiceType service = (ServiceType) this.bundleContext.getService(refs[i]);
                     if (service != null) {
+                        if ( this.references == null ) {
+                            this.references = new ArrayList<ServiceReference>();
+                        }
+                        this.references.add(refs[i]);
                         objects.add(service);
                     }
                 }
@@ -215,19 +253,16 @@ public class ScriptHelper implements Sli
      * Clean up this instance.
      */
     public void cleanup() {
-        final Iterator<ServiceReference> i = this.references.iterator();
-        while (i.hasNext()) {
-            final ServiceReference ref = i.next();
-            this.bundleContext.ungetService(ref);
+        if ( this.references != null ) {
+            final Iterator<ServiceReference> i = this.references.iterator();
+            while (i.hasNext()) {
+                final ServiceReference ref = i.next();
+                this.bundleContext.ungetService(ref);
+            }
+            this.references.clear();
+        }
+        if ( this.services != null ) {
+            this.services.clear();
         }
-        this.references.clear();
-        this.services.clear();
-    }
-
-    /**
-     * @see org.apache.sling.api.scripting.SlingScriptHelper#dispose()
-     */
-    public void dispose() {
-        LoggerFactory.getLogger(this.getClass()).error("ScriptHelper#dispose has been called.
This method is deprecated and should never be called by clients!");
     }
 }

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=932399&r1=932398&r2=932399&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 Apr  9 13:15:50 2010
@@ -68,7 +68,6 @@ import org.apache.sling.api.scripting.Sl
 import org.apache.sling.api.scripting.SlingScriptConstants;
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.scripting.api.BindingsValuesProvider;
-import org.apache.sling.scripting.core.ScriptHelper;
 import org.apache.sling.scripting.core.impl.helper.ProtectedBindings;
 import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
@@ -106,18 +105,27 @@ class DefaultSlingScript implements Slin
     /** The ScriptBindingsValuesProviders. */
     private final Collection<BindingsValuesProvider> bindingsValuesProviders;
 
+    /** The cache for services. */
+    private final ServiceCache cache;
+
     /**
      * Constructor
      * @param bundleContext The bundle context
      * @param scriptResource The script resource
      * @param scriptEngine The script engine
+     * @param bindingsValuesProviders additional bindings values providers
+     * @param cache serviceCache
      */
-    DefaultSlingScript(BundleContext bundleContext, Resource scriptResource, ScriptEngine
scriptEngine,
-            Collection<BindingsValuesProvider> bindingsValuesProviders) {
+    DefaultSlingScript(final BundleContext bundleContext,
+            final Resource scriptResource,
+            final ScriptEngine scriptEngine,
+            final Collection<BindingsValuesProvider> bindingsValuesProviders,
+            final ServiceCache cache) {
         this.scriptResource = scriptResource;
         this.scriptEngine = scriptEngine;
         this.bundleContext = bundleContext;
         this.bindingsValuesProviders = bindingsValuesProviders;
+        this.cache = cache;
         this.scriptName = this.scriptResource.getPath();
         // Now know how to get the input stream, we still have to decide
         // on the encoding of the stream's data. Primarily we assume it is
@@ -262,7 +270,7 @@ class DefaultSlingScript implements Slin
 
             // dispose of the SlingScriptHelper
             if ( bindings != null && disposeScriptHelper ) {
-                final ScriptHelper helper = (ScriptHelper) bindings.get(SLING);
+                final InternalScriptHelper helper = (InternalScriptHelper) bindings.get(SLING);
                 if ( helper != null ) {
                     helper.cleanup();
                 }
@@ -467,9 +475,9 @@ class DefaultSlingScript implements Slin
         if (slingObject == null) {
 
             if ( request != null ) {
-                slingObject = new ScriptHelper(this.bundleContext, this, request, slingBindings.getResponse());
+                slingObject = new InternalScriptHelper(this.bundleContext, this, request,
slingBindings.getResponse(), this.cache);
             } else {
-                slingObject = new ScriptHelper(this.bundleContext, this);
+                slingObject = new InternalScriptHelper(this.bundleContext, this, this.cache);
             }
         } else if (!(slingObject instanceof SlingScriptHelper) ) {
             throw fail(SLING, "Wrong type");

Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java?rev=932399&r1=932398&r2=932399&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
(original)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
Fri Apr  9 13:15:50 2010
@@ -120,6 +120,11 @@ public class SlingScriptAdapterFactory i
      */
     private Map<String, Map<Object, BindingsValuesProvider>> langBindingsValuesProviders;
 
+    /**
+     * The service cache for script execution.
+     */
+    private ServiceCache serviceCache;
+
     // ---------- AdapterFactory -----------------------------------------------
 
     @SuppressWarnings("unchecked")
@@ -133,7 +138,8 @@ public class SlingScriptAdapterFactory i
         if (engine != null) {
             Collection<BindingsValuesProvider> bindingsValuesProviders = getBindingsValuesProviders(engine.getFactory());
             // unchecked cast
-            return (AdapterType) new DefaultSlingScript(this.bundleContext, resource, engine,
bindingsValuesProviders);
+            return (AdapterType) new DefaultSlingScript(this.bundleContext,
+                    resource, engine, bindingsValuesProviders, this.serviceCache);
         }
 
         return null;
@@ -322,9 +328,12 @@ public class SlingScriptAdapterFactory i
         } catch (Throwable t) {
             // so what ?
         }
+        this.serviceCache = new ServiceCache(this.bundleContext);
     }
 
     protected void deactivate(ComponentContext context) {
+        this.serviceCache.dispose();
+        this.serviceCache = null;
 
         try {
             org.apache.sling.scripting.core.impl.ScriptEngineConsolePlugin.destroyPlugin();



Mime
View raw message