sling-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] karlpauls commented on a change in pull request #1: SLING-7134 - Script execution order is not deterministic on Java 9
Date Thu, 01 Jan 1970 00:00:00 GMT
karlpauls commented on a change in pull request #1: SLING-7134 - Script execution order is
not deterministic on Java 9
URL: https://github.com/apache/sling-org-apache-sling-scripting-core/pull/1#discussion_r154778051
 
 

 ##########
 File path: src/main/java/org/apache/sling/scripting/core/impl/jsr223/SlingScriptEngineManager.java
 ##########
 @@ -0,0 +1,320 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.jsr223;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+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;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptEngineManager;
+
+import org.apache.sling.api.scripting.SlingScriptConstants;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(
+        service = {ScriptEngineManager.class, SlingScriptEngineManager.class},
+        reference = @Reference(
+                name = "ScriptEngineFactory",
+                bind = "bindScriptEngineFactory",
+                unbind = "unbindScriptEngineFactory",
+                service = ScriptEngineFactory.class,
+                cardinality = ReferenceCardinality.MULTIPLE,
+                policy = ReferencePolicy.DYNAMIC
+        )
+)
+public class SlingScriptEngineManager extends ScriptEngineManager implements BundleListener
{
+
+    public static final String EVENT_TOPIC_SCRIPT_MANAGER_UPDATED =
+            "org/apache/sling/scripting/core/impl/jsr223/SlingScriptEngineManager/UPDATED";
+    public static final String ENGINE_FACTORY_SERVICE = "META-INF/services/" + ScriptEngineFactory.class.getName();
+
+    private static final Logger LOG = LoggerFactory.getLogger(SlingScriptEngineManager.class);
+
+    private final Set<Bundle> engineSpiBundles = new HashSet<>();
+    private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
+    private final Map<ScriptEngineFactory, Map<String, Object>> factoriesProperties
= new HashMap<>();
+    private final Set<ServiceReference<ScriptEngineFactory>> serviceReferences
= new HashSet<>();
+
+    private ScriptEngineManager internalManager = new ScriptEngineManager(SlingScriptEngineManager.class.getClassLoader());
 
 Review comment:
   @raducotescu, it is there: the following makes it work:
   
   `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 8c52960..aba954b 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
   @@ -84,7 +84,18 @@ public class SlingScriptEngineManager extends ScriptEngineManager implements
Bun
        private final Map<ScriptEngineFactory, Map<String, Object>> factoriesProperties
= new HashMap<>();
        private final Set<ServiceReference<ScriptEngineFactory>> serviceReferences
= new HashSet<>();
    
   -    private ScriptEngineManager internalManager = new ScriptEngineManager(SlingScriptEngineManager.class.getClassLoader());
   +    private final ScriptEngineManager internalManager;
   +    {
   +        ClassLoader loader = Thread.currentThread().getContextClassLoader();
   +        try {
   +            Thread.currentThread().setContextClassLoader(null);
   +            internalManager = new ScriptEngineManager(ClassLoader.getSystemClassLoader());
   +        }
   +        finally {
   +            Thread.currentThread().setContextClassLoader(loader);
   +        }
   +    }
   +
        private SortedSet<SortableScriptEngineFactory> factories = new TreeSet<>();
        private ComponentContext componentContext;
    
   @@ -224,7 +235,6 @@ public class SlingScriptEngineManager extends ScriptEngineManager implements
Bun
            readWriteLock.writeLock().lock();
            try {
                factories = new TreeSet<>();
   -            internalManager = new ScriptEngineManager(SlingScriptEngineManager.class.getClassLoader());
                long fakeBundleIdCounter = Long.MIN_VALUE;
                // first add the platform factories
                for (ScriptEngineFactory factory : internalManager.getEngineFactories()) {
   `
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message