incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1169804 - in /sling/trunk: bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/ bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ bundles/scripting/core/src/main/java/org/a...
Date Mon, 12 Sep 2011 15:38:17 GMT
Author: justin
Date: Mon Sep 12 15:38:17 2011
New Revision: 1169804

URL: http://svn.apache.org/viewvc?rev=1169804&view=rev
Log:
SLING-2210 - adding support for service property compatible.javax.script.name and including
example in GSP script engine

Added:
    sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.gsp
      - copied, changed from r1169802, sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.groovy
Modified:
    sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactory.java
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/SlingScriptAdapterFactory.java
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java
    sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactoryTest.java
    sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ScriptBindingsValuesProviderTest.java
    sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.groovy

Modified: sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java?rev=1169804&r1=1169803&r2=1169804&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java
(original)
+++ sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/scripting/internal/GSPScriptEngineFactory.java
Mon Sep 12 15:38:17 2011
@@ -36,7 +36,8 @@ import org.apache.sling.scripting.api.Ab
 @Service
 @Properties({
     @Property(name="service.vendor", value="The Apache Software Foundation"),
-    @Property(name="service.description", value="GSP Script Engine")
+    @Property(name="service.description", value="GSP Script Engine"),
+    @Property(name="compatible.javax.script.name", value="groovy")
 })
 public class GSPScriptEngineFactory extends AbstractScriptEngineFactory {
     

Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactory.java?rev=1169804&r1=1169803&r2=1169804&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactory.java
(original)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactory.java
Mon Sep 12 15:38:17 2011
@@ -23,9 +23,11 @@ import java.io.InputStreamReader;
 import java.net.URL;
 import java.util.Collection;
 import java.util.Dictionary;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
@@ -44,6 +46,7 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.framework.BundleListener;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.event.Event;
@@ -76,7 +79,7 @@ public class ScriptEngineManagerFactory 
 
     private final Set<Bundle> engineSpiBundles = new HashSet<Bundle>();
 
-    private final Set<ScriptEngineFactory> engineSpiServices = new HashSet<ScriptEngineFactory>();
+    private final Map<ScriptEngineFactory, Map<Object, Object>> engineSpiServices
= new HashMap<ScriptEngineFactory, Map<Object, Object>>();
 
     private ServiceRegistration scriptEngineManagerRegistration;
 
@@ -106,15 +109,16 @@ public class ScriptEngineManagerFactory 
         }
 
         // register script engines from registered services
-        for (final ScriptEngineFactory factory : this.engineSpiServices) {
-            extensions.addAll(registerFactory(tmp, factory));
+        for (final Map.Entry<ScriptEngineFactory, Map<Object, Object>> factory
: this.engineSpiServices.entrySet()) {
+            extensions.addAll(registerFactory(tmp, factory.getKey(), factory.getValue()));
         }
 
         scriptEngineManager = tmp;
 
         if (bundleContext != null) {
             scriptEngineManagerRegistration = bundleContext.registerService(
-                ScriptEngineManager.class.getName(), scriptEngineManager,
+                new String[] { ScriptEngineManager.class.getName(), SlingScriptEngineManager.class.getName()
},
+                scriptEngineManager,
                 new Hashtable<String, Object>());
         }
 
@@ -149,7 +153,7 @@ public class ScriptEngineManagerFactory 
 	                try {
 	                    Class<ScriptEngineFactory> clazz = bundle.loadClass(line);
 	                    ScriptEngineFactory spi = clazz.newInstance();
-	                    registerFactory(mgr, spi);
+	                    registerFactory(mgr, spi, null);
 	                    extensions.addAll(spi.getExtensions());
 	                } catch (Throwable t) {
 	                    log.error("Cannot register ScriptEngineFactory " + line, t);
@@ -170,11 +174,11 @@ public class ScriptEngineManagerFactory 
         return extensions;
     }
 
-    private Collection<?> registerFactory(final SlingScriptEngineManager mgr, final
ScriptEngineFactory factory) {
+    private Collection<?> registerFactory(final SlingScriptEngineManager mgr, final
ScriptEngineFactory factory, final Map<Object, Object> props) {
         log.info("Adding ScriptEngine {}, {} for language {}, {}", new Object[] { factory.getEngineName(),
factory.getEngineVersion(),
                 factory.getLanguageName(), factory.getLanguageVersion() });
 
-        mgr.registerScriptEngineFactory(factory);
+        mgr.registerScriptEngineFactory(factory, props);
 
         return factory.getExtensions();
     }
@@ -251,21 +255,24 @@ public class ScriptEngineManagerFactory 
 
 
 
-    protected void bindScriptEngineFactory(ScriptEngineFactory scriptEngineFactory) {
-        synchronized ( this ) {
-            this.engineSpiServices.add(scriptEngineFactory);
-            if ( this.scriptEngineManager != null ) {
-                this.scriptEngineManager = null;
-                this.refreshScriptEngineManager();
+    protected void bindScriptEngineFactory(ServiceReference ref) {
+        final ScriptEngineFactory scriptEngineFactory = (ScriptEngineFactory) bundleContext.getService(ref);
+        if (scriptEngineFactory != null) {
+            synchronized ( this ) {
+                this.engineSpiServices.put(scriptEngineFactory, getProperties(ref));
+                if ( this.scriptEngineManager != null ) {
+                    this.scriptEngineManager = null;
+                    this.refreshScriptEngineManager();
+                }
             }
+            // send event
+            postEvent(SlingScriptConstants.TOPIC_SCRIPT_ENGINE_FACTORY_ADDED, scriptEngineFactory);
         }
-        // send event
-        postEvent(SlingScriptConstants.TOPIC_SCRIPT_ENGINE_FACTORY_ADDED, scriptEngineFactory);
     }
 
     protected void unbindScriptEngineFactory(ScriptEngineFactory scriptEngineFactory) {
         synchronized ( this ) {
-            if ( this.engineSpiServices.remove(scriptEngineFactory) ) {
+            if ( this.engineSpiServices.remove(scriptEngineFactory) != null ) {
                 if ( this.scriptEngineManager != null ) {
                     this.scriptEngineManager = null;
                     this.refreshScriptEngineManager();
@@ -276,6 +283,14 @@ public class ScriptEngineManagerFactory 
         postEvent(SlingScriptConstants.TOPIC_SCRIPT_ENGINE_FACTORY_REMOVED, scriptEngineFactory);
     }
 
+    private Map<Object, Object> getProperties(final ServiceReference ref) {
+        final Map<Object, Object> props = new HashMap<Object, Object>();
+        for (final String key : ref.getPropertyKeys()) {
+            props.put(key, ref.getProperty(key));
+        }
+        return props;
+    }
+
     /**
      * Get the event admin.
      *

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=1169804&r1=1169803&r2=1169804&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
Mon Sep 12 15:38:17 2011
@@ -26,7 +26,6 @@ import java.util.concurrent.ConcurrentHa
 import javax.script.Bindings;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineFactory;
-import javax.script.ScriptEngineManager;
 
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Properties;
@@ -38,6 +37,7 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.commons.mime.MimeTypeProvider;
 import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.scripting.api.BindingsValuesProvider;
+import org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.Filter;
@@ -100,7 +100,7 @@ public class SlingScriptAdapterFactory i
      * The script engine manager.
      */
     @Reference
-    private ScriptEngineManager scriptEngineManager;
+    private SlingScriptEngineManager scriptEngineManager;
 
     // ---------- AdapterFactory -----------------------------------------------
 
@@ -213,12 +213,27 @@ public class SlingScriptAdapterFactory i
     private Collection<BindingsValuesProvider> getBindingsValuesProviders(ScriptEngineFactory
scriptEngineFactory) {
         final List<BindingsValuesProvider> results = new ArrayList<BindingsValuesProvider>();
         results.addAll(genericBindingsValuesProviders.values());
+
+        // we load the compatible language ones first so that the most specific
+        // overrides these
+        Map<Object, Object> factoryProps = scriptEngineManager.getProperties(scriptEngineFactory);
+        if (factoryProps != null) {
+            String[] compatibleLangs = PropertiesUtil.toStringArray(factoryProps.get("compatible.javax.script.name"),
new String[0]);
+            for (final String name : compatibleLangs) {
+                final Map<Object, BindingsValuesProvider> langProviders = langBindingsValuesProviders.get(name);
+                if (langProviders != null) {
+                    results.addAll(langProviders.values());
+                }
+            }
+        }
+
         for (final String name : scriptEngineFactory.getNames()) {
             final Map<Object, BindingsValuesProvider> langProviders = langBindingsValuesProviders.get(name);
             if (langProviders != null) {
                 results.addAll(langProviders.values());
             }
         }
+
         return results;
     }
 

Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java?rev=1169804&r1=1169803&r2=1169804&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java
(original)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/SlingScriptEngineManager.java
Mon Sep 12 15:38:17 2011
@@ -19,7 +19,9 @@
 package org.apache.sling.scripting.core.impl.helper;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.script.ScriptEngineFactory;
 import javax.script.ScriptEngineManager;
@@ -28,6 +30,7 @@ import javax.script.ScriptEngineManager;
 public class SlingScriptEngineManager extends ScriptEngineManager {
 
     private final List<ScriptEngineFactory> factories = new ArrayList<ScriptEngineFactory>();
+    private final Map<ScriptEngineFactory, Map<Object, Object>> factoryProperties
= new HashMap<ScriptEngineFactory, Map<Object, Object>>();
 
     public SlingScriptEngineManager(ClassLoader classLoader) {
         super(classLoader);
@@ -47,7 +50,11 @@ public class SlingScriptEngineManager ex
         return result;
     }
 
-    public void registerScriptEngineFactory(ScriptEngineFactory factory) {
+    public Map<Object, Object> getProperties(ScriptEngineFactory factory) {
+        return factoryProperties.get(factory);
+    }
+
+    public void registerScriptEngineFactory(ScriptEngineFactory factory, Map<Object, Object>
props) {
         for (Object ext : factory.getExtensions()) {
             registerEngineExtension((String) ext, factory);
         }
@@ -61,6 +68,10 @@ public class SlingScriptEngineManager ex
         }
 
         factories.add(factory);
+
+        if (props != null) {
+            factoryProperties.put(factory, props);
+        }
     }
 
 }

Modified: sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactoryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactoryTest.java?rev=1169804&r1=1169803&r2=1169804&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactoryTest.java
(original)
+++ sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactoryTest.java
Mon Sep 12 15:38:17 2011
@@ -88,7 +88,7 @@ public class ScriptEngineManagerFactoryT
     @Test
     public void checkNonNullManagerAfterActivate() throws Exception  {
         context.checking(new Expectations(){{
-            one(bundleCtx).registerService(with(equal("javax.script.ScriptEngineManager")),
with(any(Object.class)), with(any(Dictionary.class)));
+            one(bundleCtx).registerService(with(equal(new String[] {"javax.script.ScriptEngineManager",
"org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager"})), with(any(Object.class)),
with(any(Dictionary.class)));
             will(returnValue(new MockServiceRegistration()));
         }});
 
@@ -101,7 +101,7 @@ public class ScriptEngineManagerFactoryT
     @Test
     public void checkAddingScriptBundle() throws Exception {
         context.checking(new Expectations(){{
-            exactly(2).of(bundleCtx).registerService(with(equal("javax.script.ScriptEngineManager")),
with(any(Object.class)), with(any(Dictionary.class)));
+            exactly(2).of(bundleCtx).registerService(with(equal(new String[] {"javax.script.ScriptEngineManager",
"org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager"})), with(any(Object.class)),
with(any(Dictionary.class)));
             will(returnValue(new MockServiceRegistration()));
         }});
 

Modified: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ScriptBindingsValuesProviderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ScriptBindingsValuesProviderTest.java?rev=1169804&r1=1169803&r2=1169804&view=diff
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ScriptBindingsValuesProviderTest.java
(original)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/ScriptBindingsValuesProviderTest.java
Mon Sep 12 15:38:17 2011
@@ -62,7 +62,19 @@ public class ScriptBindingsValuesProvide
         final String toDelete = uploadTestScript("bindingsprovided.groovy","html.groovy");
         try {
             final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
-            assertTrue("Content includes ESP marker (" + content + ")",content.contains("ESP
template"));
+            assertTrue("Content includes Groovy marker (" + content + ")",content.contains("Groovy
template"));
+            assertTrue("Content includes test text (" + content + ")", content.contains("Hello
World!"));
+            assertTrue("Content includes Groovy-specific test text (" + content + ")", content.contains("Hello
World from Groovy!"));
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+
+    public void testGSPProvider() throws IOException {
+        final String toDelete = uploadTestScript("bindingsprovided.gsp","html.gsp");
+        try {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertTrue("Content includes GSP marker (" + content + ")",content.contains("GSP
template"));
             assertTrue("Content includes test text (" + content + ")", content.contains("Hello
World!"));
             assertTrue("Content includes Groovy-specific test text (" + content + ")", content.contains("Hello
World from Groovy!"));
         } finally {

Modified: sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.groovy
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.groovy?rev=1169804&r1=1169803&r2=1169804&view=diff
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.groovy
(original)
+++ sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.groovy
Mon Sep 12 15:38:17 2011
@@ -19,7 +19,7 @@
 
 out.write """<html>
     <body>
-        <h1>ESP template</h1>
+        <h1>Groovy template</h1>
         <p>helloWorld:${helloWorld}</p>
         <p>groovyHelloWorld:${groovyHelloWorld}</p>
     </body>

Copied: sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.gsp
(from r1169802, sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.groovy)
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.gsp?p2=sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.gsp&p1=sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.groovy&r1=1169802&r2=1169804&rev=1169804&view=diff
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.groovy
(original)
+++ sling/trunk/launchpad/integration-tests/src/main/resources/integration-test/bindingsprovided.gsp
Mon Sep 12 15:38:17 2011
@@ -17,10 +17,10 @@
  * under the License.
  */
 
-out.write """<html>
+<html>
     <body>
-        <h1>ESP template</h1>
+        <h1>GSP template</h1>
         <p>helloWorld:${helloWorld}</p>
         <p>groovyHelloWorld:${groovyHelloWorld}</p>
     </body>
-</html>"""
\ No newline at end of file
+</html>
\ No newline at end of file



Mime
View raw message