incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1179020 - in /sling/trunk/bundles/scripting/core: ./ src/main/java/org/apache/sling/scripting/core/impl/ src/main/java/org/apache/sling/scripting/core/impl/helper/ src/test/java/org/apache/sling/scripting/core/impl/
Date Wed, 05 Oct 2011 00:28:28 GMT
Author: justin
Date: Wed Oct  5 00:28:27 2011
New Revision: 1179020

URL: http://svn.apache.org/viewvc?rev=1179020&view=rev
Log:
SLING-2228 - applying Felix's patch to not reregister ScriptEngineManager when a script engine
is added/removed.

Added:
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/ProxyScriptEngineManager.java
Modified:
    sling/trunk/bundles/scripting/core/pom.xml
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactory.java
    sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptEngineManagerFactoryTest.java

Modified: sling/trunk/bundles/scripting/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/pom.xml?rev=1179020&r1=1179019&r2=1179020&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/pom.xml (original)
+++ sling/trunk/bundles/scripting/core/pom.xml Wed Oct  5 00:28:27 2011
@@ -110,7 +110,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.scripting.api</artifactId>
-            <version>2.1.0</version>
+            <version>2.1.3-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
 

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=1179020&r1=1179019&r2=1179020&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
Wed Oct  5 00:28:27 2011
@@ -41,12 +41,12 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.ReferencePolicy;
 import org.apache.sling.api.scripting.SlingScriptConstants;
+import org.apache.sling.scripting.core.impl.helper.ProxyScriptEngineManager;
 import org.apache.sling.scripting.core.impl.helper.SlingScriptEngineManager;
 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;
@@ -75,7 +75,12 @@ public class ScriptEngineManagerFactory 
      */
     private ServiceTracker eventAdminTracker;
 
-    private volatile ScriptEngineManager scriptEngineManager;
+    /**
+     * The proxy to the actual ScriptEngineManager. This proxy is actually
+     * registered as the ScriptEngineManager service for the lifetime of
+     * this factory.
+     */
+    private final ProxyScriptEngineManager scriptEngineManager = new ProxyScriptEngineManager();
 
     private final Set<Bundle> engineSpiBundles = new HashSet<Bundle>();
 
@@ -85,49 +90,36 @@ public class ScriptEngineManagerFactory 
 
     /**
      * Refresh the script engine manager.
-     * This method is called from within a synchronized block,
-     * no other sync is required!
      */
-    private ScriptEngineManager refreshScriptEngineManager() {
-        if ( this.scriptEngineManager != null ) {
-            return this.scriptEngineManager;
-        }
-
-        if (scriptEngineManagerRegistration != null) {
-            scriptEngineManagerRegistration.unregister();
-            scriptEngineManagerRegistration = null;
-        }
-
+    private void refreshScriptEngineManager() {
         // create (empty) script engine manager
         final ClassLoader loader = getClass().getClassLoader();
         final SlingScriptEngineManager tmp = new SlingScriptEngineManager(loader);
 
         // register script engines from bundles
         final SortedSet<Object> extensions = new TreeSet<Object>();
-        for (final Bundle bundle : this.engineSpiBundles) {
-            extensions.addAll(registerFactories(tmp, bundle));
+        synchronized (this.engineSpiBundles) {
+            for (final Bundle bundle : this.engineSpiBundles) {
+                extensions.addAll(registerFactories(tmp, bundle));
+            }
         }
 
         // register script engines from registered services
-        for (final Map.Entry<ScriptEngineFactory, Map<Object, Object>> factory
: this.engineSpiServices.entrySet()) {
-            extensions.addAll(registerFactory(tmp, factory.getKey(), factory.getValue()));
+        synchronized (this.engineSpiServices) {
+            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(
-                new String[] { ScriptEngineManager.class.getName(), SlingScriptEngineManager.class.getName()
},
-                scriptEngineManager,
-                new Hashtable<String, Object>());
-        }
+        scriptEngineManager.setDelegatee(tmp);
 
         // Log messages to verify which ScriptEngine is actually used
         // for our registered extensions
         if (log.isInfoEnabled()) {
             for (Object o : extensions) {
                 final String ext = o.toString();
-                final ScriptEngine e = scriptEngineManager.getEngineByExtension(ext);
+                final ScriptEngine e = tmp.getEngineByExtension(ext);
                 if (e == null) {
                     log.warn("No ScriptEngine found for extension '{}' that was just registered",
ext);
                 } else {
@@ -136,7 +128,6 @@ public class ScriptEngineManagerFactory 
                 }
             }
         }
-        return tmp;
     }
 
     @SuppressWarnings("unchecked")
@@ -186,20 +177,19 @@ public class ScriptEngineManagerFactory 
     // ---------- BundleListener interface -------------------------------------
 
     public void bundleChanged(BundleEvent event) {
-        if (event.getType() == BundleEvent.STARTED && event.getBundle().getEntry(ENGINE_FACTORY_SERVICE)
!= null) {
-
-            synchronized ( this ) {
+        if (event.getType() == BundleEvent.STARTED
+            && event.getBundle().getEntry(ENGINE_FACTORY_SERVICE) != null) {
+            synchronized (this.engineSpiBundles) {
                 this.engineSpiBundles.add(event.getBundle());
-                this.scriptEngineManager = null;
-                this.refreshScriptEngineManager();
             }
-
-        } else if (event.getType() == BundleEvent.STOPPED  ) {
-            synchronized ( this ) {
-                if ( this.engineSpiBundles.remove(event.getBundle()) ) {
-                    this.scriptEngineManager = null;
-                    this.refreshScriptEngineManager();
-                }
+            this.refreshScriptEngineManager();
+        } else if (event.getType() == BundleEvent.STOPPED) {
+            boolean refresh;
+            synchronized (this.engineSpiBundles) {
+                refresh = this.engineSpiBundles.remove(event.getBundle());
+            }
+            if (refresh) {
+                this.refreshScriptEngineManager();
             }
         }
     }
@@ -216,19 +206,23 @@ public class ScriptEngineManagerFactory 
         this.bundleContext.addBundleListener(this);
 
         Bundle[] bundles = this.bundleContext.getBundles();
-        for (Bundle bundle : bundles) {
-            if (bundle.getState() == Bundle.ACTIVE && bundle.getEntry(ENGINE_FACTORY_SERVICE)
!= null) {
-                synchronized ( this ) {
+        synchronized (this.engineSpiBundles) {
+            for (Bundle bundle : bundles) {
+                if (bundle.getState() == Bundle.ACTIVE
+                    && bundle.getEntry(ENGINE_FACTORY_SERVICE) != null) {
                     this.engineSpiBundles.add(bundle);
-                    this.scriptEngineManager = null;
                 }
             }
         }
 
         // create a script engine manager
-        synchronized ( this ) {
-            this.refreshScriptEngineManager();
-        }
+        this.refreshScriptEngineManager();
+
+        scriptEngineManagerRegistration = this.bundleContext.registerService(
+            new String[] { ScriptEngineManager.class.getName(),
+                SlingScriptEngineManager.class.getName() },
+            scriptEngineManager, new Hashtable<String, Object>());
+
         org.apache.sling.scripting.core.impl.ScriptEngineConsolePlugin.initPlugin(context.getBundleContext(),
this);
     }
 
@@ -241,41 +235,45 @@ public class ScriptEngineManagerFactory 
             scriptEngineManagerRegistration.unregister();
             scriptEngineManagerRegistration = null;
         }
+
         synchronized ( this ) {
             this.engineSpiBundles.clear();
             this.engineSpiServices.clear();
         }
-        scriptEngineManager = null;
+
+        scriptEngineManager.setDelegatee(null);
+
         if (this.eventAdminTracker != null) {
             this.eventAdminTracker.close();
             this.eventAdminTracker = null;
         }
+
         this.bundleContext = null;
     }
 
     protected void bindScriptEngineFactory(final ScriptEngineFactory scriptEngineFactory,
final Map<Object, Object> props) {
         if (scriptEngineFactory != null) {
-            synchronized ( this ) {
+            synchronized ( this.engineSpiServices) {
                 this.engineSpiServices.put(scriptEngineFactory, props);
-                if ( this.scriptEngineManager != null ) {
-                    this.scriptEngineManager = null;
-                    this.refreshScriptEngineManager();
-                }
             }
+
+            this.refreshScriptEngineManager();
+
             // send event
             postEvent(SlingScriptConstants.TOPIC_SCRIPT_ENGINE_FACTORY_ADDED, scriptEngineFactory);
         }
     }
 
     protected void unbindScriptEngineFactory(final ScriptEngineFactory scriptEngineFactory)
{
-        synchronized ( this ) {
-            if ( this.engineSpiServices.remove(scriptEngineFactory) != null ) {
-                if ( this.scriptEngineManager != null ) {
-                    this.scriptEngineManager = null;
-                    this.refreshScriptEngineManager();
-                }
-            }
+        boolean refresh;
+        synchronized (this.engineSpiServices) {
+            refresh = this.engineSpiServices.remove(scriptEngineFactory) != null;
+        }
+
+        if (refresh) {
+            this.refreshScriptEngineManager();
         }
+
         // send event
         postEvent(SlingScriptConstants.TOPIC_SCRIPT_ENGINE_FACTORY_REMOVED, scriptEngineFactory);
     }
@@ -315,12 +313,6 @@ public class ScriptEngineManagerFactory 
      * Refresh the manager if changes occured.
      */
     ScriptEngineManager getScriptEngineManager() {
-        ScriptEngineManager sem = this.scriptEngineManager;
-        if ( sem == null ) {
-            synchronized ( this ) {
-                sem = this.refreshScriptEngineManager();
-            }
-        }
-        return sem;
+        return this.scriptEngineManager;
     }
 }

Added: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/ProxyScriptEngineManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/ProxyScriptEngineManager.java?rev=1179020&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/ProxyScriptEngineManager.java
(added)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/helper/ProxyScriptEngineManager.java
Wed Oct  5 00:28:27 2011
@@ -0,0 +1,107 @@
+/*
+ * 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.scripting.core.impl.helper;
+
+import java.util.List;
+import java.util.Map;
+
+import javax.script.Bindings;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+
+public class ProxyScriptEngineManager extends SlingScriptEngineManager {
+
+    private SlingScriptEngineManager delegatee;
+
+    public ProxyScriptEngineManager() {
+        setDelegatee(null);
+    }
+
+    public void setDelegatee(SlingScriptEngineManager delegatee) {
+        this.delegatee = (delegatee == null)
+                ? new SlingScriptEngineManager()
+                : delegatee;
+    }
+
+    public Object get(String key) {
+        return delegatee.get(key);
+    }
+
+    public ScriptEngine getEngineByExtension(String extension) {
+        return delegatee.getEngineByExtension(extension);
+    }
+
+    public ScriptEngine getEngineByMimeType(String mimeType) {
+        return delegatee.getEngineByMimeType(mimeType);
+    }
+
+    public ScriptEngine getEngineByName(String shortName) {
+        return delegatee.getEngineByName(shortName);
+    }
+
+    public List<ScriptEngineFactory> getEngineFactories() {
+        return delegatee.getEngineFactories();
+    }
+
+    public Map<Object, Object> getProperties(ScriptEngineFactory factory) {
+        return delegatee.getProperties(factory);
+    }
+
+    public void put(String key, Object value) {
+        delegatee.put(key, value);
+    }
+
+    public void registerEngineExtension(String extension,
+            ScriptEngineFactory factory) {
+        delegatee.registerEngineExtension(extension, factory);
+    }
+
+    public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
+        delegatee.registerEngineMimeType(type, factory);
+    }
+
+    public void registerEngineName(String name, ScriptEngineFactory factory) {
+        delegatee.registerEngineName(name, factory);
+    }
+
+    public void registerScriptEngineFactory(ScriptEngineFactory factory,
+            Map<Object, Object> props) {
+        delegatee.registerScriptEngineFactory(factory, props);
+    }
+
+    // Livetribe JSR-223 2.0.6 API
+    public Bindings getGlobalScope()
+    {
+        return getBindings();
+    }
+
+    // Livetribe JSR-223 2.0.6 API
+    public void setGlobalScope(Bindings globalScope)
+    {
+        setBindings(globalScope);
+    }
+
+    public Bindings getBindings() {
+        return delegatee.getBindings();
+    }
+
+    public void setBindings(Bindings bindings) {
+        delegatee.setBindings(bindings);
+    }
+}

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=1179020&r1=1179019&r2=1179020&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
Wed Oct  5 00:28:27 2011
@@ -17,7 +17,6 @@
 package org.apache.sling.scripting.core.impl;
 
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 
 import java.io.File;
@@ -101,7 +100,7 @@ public class ScriptEngineManagerFactoryT
     @Test
     public void checkAddingScriptBundle() throws Exception {
         context.checking(new Expectations(){{
-            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)));
+            exactly(1).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()));
         }});
 
@@ -128,7 +127,6 @@ public class ScriptEngineManagerFactoryT
         factory.bundleChanged(new BundleEvent(BundleEvent.STARTED, bundle));
 
         ScriptEngineManager second = factory.getScriptEngineManager();
-        assertNotSame(first, second);
 
         assertNotNull(second.getEngineByName("dummy"));
     }



Mime
View raw message