You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2019/05/22 14:37:57 UTC

[sling-org-apache-sling-scripting-core] branch master updated: SLING-8431 - On Java 11 the platform provided script engine factories might be registered more than once

This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-core.git


The following commit(s) were added to refs/heads/master by this push:
     new cdec524  SLING-8431 - On Java 11 the platform provided script engine factories might be registered more than once
cdec524 is described below

commit cdec52401b182a9af5feb553f927cea523acd7e8
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Wed May 22 16:37:48 2019 +0200

    SLING-8431 - On Java 11 the platform provided script engine factories might be registered more than once
    
    * the set of factories from the SlingScriptEngineManager will now contain only unique
    script engine factories; SortableScriptEngineFactories are now considered equal if they are based
    on the same delegate
    * updated the Pax test support for Java 11 and above
---
 pom.xml                                            |  4 ++--
 .../core/impl/jsr223/SlingScriptEngineManager.java |  2 +-
 .../impl/jsr223/SortableScriptEngineFactory.java   | 23 +++++++++++++++++--
 .../jsr223/SortableScriptEngineFactoryTest.java    |  2 +-
 .../core/it/ScriptingCoreTestSupport.java          | 26 ++++++++++++++++------
 5 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0ddb1df..9cb3466 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,8 +22,8 @@
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.apache.sling</groupId>
-        <artifactId>sling</artifactId>
-        <version>34</version>
+        <artifactId>sling-bundle-parent</artifactId>
+        <version>35</version>
         <relativePath />
     </parent>
 
diff --git a/src/main/java/org/apache/sling/scripting/core/impl/jsr223/SlingScriptEngineManager.java b/src/main/java/org/apache/sling/scripting/core/impl/jsr223/SlingScriptEngineManager.java
index 14cb13a..85750da 100644
--- a/src/main/java/org/apache/sling/scripting/core/impl/jsr223/SlingScriptEngineManager.java
+++ b/src/main/java/org/apache/sling/scripting/core/impl/jsr223/SlingScriptEngineManager.java
@@ -242,7 +242,7 @@ public class SlingScriptEngineManager extends ScriptEngineManager implements Bun
                     try {
                         ScriptEngineManager manager = new ScriptEngineManager(bundle.adapt(BundleWiring.class).getClassLoader());
                         for (ScriptEngineFactory factory : manager.getEngineFactories()) {
-                            factories.add(new SortableScriptEngineFactory(factory, fakeBundleIdCounter++, 0));
+                            factories.add(new SortableScriptEngineFactory(factory, bundle.getBundleId(), 0));
                         }
                     } catch (Exception ex) {
                         LOG.error("Unable to process bundle " + bundle.getSymbolicName(), ex);
diff --git a/src/main/java/org/apache/sling/scripting/core/impl/jsr223/SortableScriptEngineFactory.java b/src/main/java/org/apache/sling/scripting/core/impl/jsr223/SortableScriptEngineFactory.java
index b04573f..e62b601 100644
--- a/src/main/java/org/apache/sling/scripting/core/impl/jsr223/SortableScriptEngineFactory.java
+++ b/src/main/java/org/apache/sling/scripting/core/impl/jsr223/SortableScriptEngineFactory.java
@@ -23,6 +23,8 @@ import java.util.List;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineFactory;
 
+import org.jetbrains.annotations.NotNull;
+
 public class SortableScriptEngineFactory implements ScriptEngineFactory, Comparable {
 
     private final ScriptEngineFactory delegate;
@@ -47,7 +49,7 @@ public class SortableScriptEngineFactory implements ScriptEngineFactory, Compara
      * @param bundleId       the bundle id of the bundle registering the {@link ScriptEngineFactory}
      * @param serviceRanking the service ranking of the {@link ScriptEngineFactory}
      */
-    SortableScriptEngineFactory(ScriptEngineFactory delegate, long bundleId, int serviceRanking) {
+    SortableScriptEngineFactory(@NotNull ScriptEngineFactory delegate, long bundleId, int serviceRanking) {
         this.delegate = delegate;
         this.bundleId = bundleId;
         this.serviceRanking = serviceRanking;
@@ -114,7 +116,7 @@ public class SortableScriptEngineFactory implements ScriptEngineFactory, Compara
     }
 
     @Override
-    public int compareTo(Object o) {
+    public int compareTo(@NotNull Object o) {
         SortableScriptEngineFactory other = (SortableScriptEngineFactory) o;
         if (equals(other)) {
             return 0;
@@ -131,4 +133,21 @@ public class SortableScriptEngineFactory implements ScriptEngineFactory, Compara
         }
         return -1;
     }
+
+    @Override
+    public int hashCode() {
+        return delegate.hashCode();
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof SortableScriptEngineFactory) {
+            SortableScriptEngineFactory other = (SortableScriptEngineFactory) obj;
+            return this.delegate.equals(other.delegate);
+        }
+        return false;
+    }
 }
diff --git a/src/test/java/org/apache/sling/scripting/core/impl/jsr223/SortableScriptEngineFactoryTest.java b/src/test/java/org/apache/sling/scripting/core/impl/jsr223/SortableScriptEngineFactoryTest.java
index e220bcf..52b73ff 100644
--- a/src/test/java/org/apache/sling/scripting/core/impl/jsr223/SortableScriptEngineFactoryTest.java
+++ b/src/test/java/org/apache/sling/scripting/core/impl/jsr223/SortableScriptEngineFactoryTest.java
@@ -169,6 +169,6 @@ public class SortableScriptEngineFactoryTest {
     }
 
     private SortableScriptEngineFactory getCompareFactory(long bundleId, int serviceRanking) {
-        return new SortableScriptEngineFactory(null, bundleId, serviceRanking);
+        return new SortableScriptEngineFactory(mock(ScriptEngineFactory.class), bundleId, serviceRanking);
     }
 }
diff --git a/src/test/java/org/apache/sling/scripting/core/it/ScriptingCoreTestSupport.java b/src/test/java/org/apache/sling/scripting/core/it/ScriptingCoreTestSupport.java
index 7f35c79..c5c9fd7 100644
--- a/src/test/java/org/apache/sling/scripting/core/it/ScriptingCoreTestSupport.java
+++ b/src/test/java/org/apache/sling/scripting/core/it/ScriptingCoreTestSupport.java
@@ -28,6 +28,7 @@ import static org.apache.sling.testing.paxexam.SlingOptions.sling;
 import static org.apache.sling.testing.paxexam.SlingOptions.versionResolver;
 import static org.apache.sling.testing.paxexam.SlingOptions.webconsole;
 import static org.ops4j.pax.exam.CoreOptions.composite;
+import static org.ops4j.pax.exam.CoreOptions.frameworkProperty;
 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
 import static org.ops4j.pax.exam.CoreOptions.vmOption;
@@ -56,19 +57,30 @@ public class ScriptingCoreTestSupport extends TestSupport {
             junitBundles()
         };
         try {
-            Integer javaVersion = Integer.parseInt(System.getProperty("java.specification.version"));
-            if (javaVersion >= 9) {
-                Option[] java9AndBeyondConfiguration = Arrays.copyOf(configuration, configuration.length + 1);
-                java9AndBeyondConfiguration[java9AndBeyondConfiguration.length - 1] = vmOption("--add-modules=java.se.ee");
-                configuration = java9AndBeyondConfiguration;
+            int javaVersion = Integer.parseInt(System.getProperty("java.specification.version"));
+            if (javaVersion >= 9 && javaVersion < 11) {
+                configuration = Arrays.copyOf(configuration, configuration.length + 1);
+                configuration[configuration.length - 1] = vmOption("--add-modules=java.se.ee");
+            }
+            if (javaVersion >= 11) {
+                configuration = Arrays.copyOf(configuration, configuration.length + 1);
+                configuration[configuration.length -1] = composite(
+                        frameworkProperty("org.osgi.framework.system.packages.extra")
+                                .value("javax.xml.stream;version=\"1.1.0\",javax.xml.stream.events;" +
+                                        "version=\"1.1.0\""),
+                        mavenBundle("org.apache.geronimo.specs", "geronimo-annotation_1.3_spec", "1.1"),
+                        mavenBundle("org.apache.geronimo.specs", "geronimo-activation_1.1_spec", "1.1"),
+                        mavenBundle("org.apache.servicemix.specs", "org.apache.servicemix.specs.jaxb-api-2.2", "2.9.0"),
+                        mavenBundle("org.apache.servicemix.bundles", "org.apache.servicemix.bundles.jaxb-impl", "2.2.11_1")
+                );
             }
         } catch (NumberFormatException e) {
-            // ignore
+            // do nothing
         }
         return configuration;
     }
 
-    protected Option launchpad() {
+    private Option launchpad() {
         versionResolver.setVersion("org.apache.felix", "org.apache.felix.http.jetty", "3.1.6"); // Java 7
         versionResolver.setVersion("org.apache.felix", "org.apache.felix.http.whiteboard", "2.3.2"); // Java 7
         final int httpPort = findFreePort();