Resolving issue #1270, unexpected exception with simulation scripting
This commit is contained in:
parent
54f8276c53
commit
aceaa328fc
BIN
core/lib/graal-sdk-22.1.0.1.jar
Normal file
BIN
core/lib/graal-sdk-22.1.0.1.jar
Normal file
Binary file not shown.
BIN
core/lib/icu4j-71.1.jar
Normal file
BIN
core/lib/icu4j-71.1.jar
Normal file
Binary file not shown.
BIN
core/lib/js-22.1.0.1.jar
Normal file
BIN
core/lib/js-22.1.0.1.jar
Normal file
Binary file not shown.
BIN
core/lib/js-scriptengine-22.1.0.1.jar
Normal file
BIN
core/lib/js-scriptengine-22.1.0.1.jar
Normal file
Binary file not shown.
BIN
core/lib/truffle-api-22.1.0.1.jar
Normal file
BIN
core/lib/truffle-api-22.1.0.1.jar
Normal file
Binary file not shown.
BIN
core/lib/yasson-1.0.2.jar
Normal file
BIN
core/lib/yasson-1.0.2.jar
Normal file
Binary file not shown.
@ -0,0 +1,430 @@
|
|||||||
|
/*
|
||||||
|
* This is a replacement for the ScriptEngineManager which gets around and issue with the sun.misc.ServiceConfigurationError
|
||||||
|
* which has been removed in Java 9+. If using the ScriptEngineManager from the script-api*.jar then the sun.misc throws
|
||||||
|
* a ClassNotFoundException.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation. Oracle designates this
|
||||||
|
* particular file as subject to the "Classpath" exception as provided
|
||||||
|
* by Oracle in the LICENSE file that accompanied this code.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sf.openrocket.scripting;
|
||||||
|
|
||||||
|
import javax.script.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.security.*;
|
||||||
|
import java.util.ServiceLoader;
|
||||||
|
import java.util.ServiceConfigurationError;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <code>ScriptEngineManager</code> implements a discovery and instantiation
|
||||||
|
* mechanism for <code>ScriptEngine</code> classes and also maintains a
|
||||||
|
* collection of key/value pairs storing state shared by all engines created
|
||||||
|
* by the Manager. This class uses the service provider mechanism described in the
|
||||||
|
* {@link java.util.ServiceLoader} class to enumerate all the
|
||||||
|
* implementations of <code>ScriptEngineFactory</code>. <br><br>
|
||||||
|
* The <code>ScriptEngineManager</code> provides a method to return a list of all these factories
|
||||||
|
* as well as utility methods which look up factories on the basis of language name, file extension
|
||||||
|
* and mime type.
|
||||||
|
* <p>
|
||||||
|
* The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope" maintained
|
||||||
|
* by the manager is available to all instances of <code>ScriptEngine</code> created
|
||||||
|
* by the <code>ScriptEngineManager</code>. The values in the <code>Bindings</code> are
|
||||||
|
* generally exposed in all scripts.
|
||||||
|
*
|
||||||
|
* @author Mike Grogan
|
||||||
|
* @author A. Sundararajan
|
||||||
|
* @since 1.6
|
||||||
|
*/
|
||||||
|
public class ScriptEngineManagerRedux {
|
||||||
|
private static final boolean DEBUG = false;
|
||||||
|
/**
|
||||||
|
* The effect of calling this constructor is the same as calling
|
||||||
|
* <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
|
||||||
|
*
|
||||||
|
* @see java.lang.Thread#getContextClassLoader
|
||||||
|
*/
|
||||||
|
public ScriptEngineManagerRedux() {
|
||||||
|
ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
|
||||||
|
init(ctxtLoader);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This constructor loads the implementations of
|
||||||
|
* <code>ScriptEngineFactory</code> visible to the given
|
||||||
|
* <code>ClassLoader</code> using the service provider mechanism.<br><br>
|
||||||
|
* If loader is <code>null</code>, the script engine factories that are
|
||||||
|
* bundled with the platform are loaded. <br>
|
||||||
|
*
|
||||||
|
* @param loader ClassLoader used to discover script engine factories.
|
||||||
|
*/
|
||||||
|
public ScriptEngineManagerRedux(ClassLoader loader) {
|
||||||
|
init(loader);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init(final ClassLoader loader) {
|
||||||
|
globalScope = new SimpleBindings();
|
||||||
|
engineSpis = new TreeSet<ScriptEngineFactory>(Comparator.comparing(
|
||||||
|
ScriptEngineFactory::getEngineName,
|
||||||
|
Comparator.nullsLast(Comparator.naturalOrder()))
|
||||||
|
);
|
||||||
|
nameAssociations = new HashMap<String, ScriptEngineFactory>();
|
||||||
|
extensionAssociations = new HashMap<String, ScriptEngineFactory>();
|
||||||
|
mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
|
||||||
|
initEngines(loader);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
|
||||||
|
if (loader != null) {
|
||||||
|
return ServiceLoader.load(ScriptEngineFactory.class, loader);
|
||||||
|
} else {
|
||||||
|
return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initEngines(final ClassLoader loader) {
|
||||||
|
Iterator<ScriptEngineFactory> itr = null;
|
||||||
|
try {
|
||||||
|
ServiceLoader<ScriptEngineFactory> sl = AccessController.doPrivileged(
|
||||||
|
new PrivilegedAction<ServiceLoader<ScriptEngineFactory>>() {
|
||||||
|
@Override
|
||||||
|
public ServiceLoader<ScriptEngineFactory> run() {
|
||||||
|
return getServiceLoader(loader);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
itr = sl.iterator();
|
||||||
|
} catch (ServiceConfigurationError err) {
|
||||||
|
// } catch (Exception err) {
|
||||||
|
System.err.println("Can't find ScriptEngineFactory providers: " +
|
||||||
|
err.getMessage());
|
||||||
|
if (DEBUG) {
|
||||||
|
err.printStackTrace();
|
||||||
|
}
|
||||||
|
// do not throw any exception here. user may want to
|
||||||
|
// manage his/her own factories using this manager
|
||||||
|
// by explicit registratation (by registerXXX) methods.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (itr.hasNext()) {
|
||||||
|
try {
|
||||||
|
ScriptEngineFactory fact = itr.next();
|
||||||
|
engineSpis.add(fact);
|
||||||
|
} catch (ServiceConfigurationError err) {
|
||||||
|
// } catch (Exception err) {
|
||||||
|
System.err.println("ScriptEngineManager providers.next(): "
|
||||||
|
+ err.getMessage());
|
||||||
|
if (DEBUG) {
|
||||||
|
err.printStackTrace();
|
||||||
|
}
|
||||||
|
// one factory failed, but check other factories...
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (ServiceConfigurationError err) {
|
||||||
|
// } catch (Exception err) {
|
||||||
|
System.err.println("ScriptEngineManager providers.hasNext(): "
|
||||||
|
+ err.getMessage());
|
||||||
|
if (DEBUG) {
|
||||||
|
err.printStackTrace();
|
||||||
|
}
|
||||||
|
// do not throw any exception here. user may want to
|
||||||
|
// manage his/her own factories using this manager
|
||||||
|
// by explicit registratation (by registerXXX) methods.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <code>setBindings</code> stores the specified <code>Bindings</code>
|
||||||
|
* in the <code>globalScope</code> field. ScriptEngineManager sets this
|
||||||
|
* <code>Bindings</code> as global bindings for <code>ScriptEngine</code>
|
||||||
|
* objects created by it.
|
||||||
|
*
|
||||||
|
* @param bindings The specified <code>Bindings</code>
|
||||||
|
* @throws IllegalArgumentException if bindings is null.
|
||||||
|
*/
|
||||||
|
public void setBindings(Bindings bindings) {
|
||||||
|
if (bindings == null) {
|
||||||
|
throw new IllegalArgumentException("Global scope cannot be null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
globalScope = bindings;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <code>getBindings</code> returns the value of the <code>globalScope</code> field.
|
||||||
|
* ScriptEngineManager sets this <code>Bindings</code> as global bindings for
|
||||||
|
* <code>ScriptEngine</code> objects created by it.
|
||||||
|
*
|
||||||
|
* @return The globalScope field.
|
||||||
|
*/
|
||||||
|
public Bindings getBindings() {
|
||||||
|
return globalScope;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the specified key/value pair in the Global Scope.
|
||||||
|
* @param key Key to set
|
||||||
|
* @param value Value to set.
|
||||||
|
* @throws NullPointerException if key is null.
|
||||||
|
* @throws IllegalArgumentException if key is empty string.
|
||||||
|
*/
|
||||||
|
public void put(String key, Object value) {
|
||||||
|
globalScope.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value for the specified key in the Global Scope
|
||||||
|
* @param key The key whose value is to be returned.
|
||||||
|
* @return The value for the specified key.
|
||||||
|
*/
|
||||||
|
public Object get(String key) {
|
||||||
|
return globalScope.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Looks up and creates a <code>ScriptEngine</code> for a given name.
|
||||||
|
* The algorithm first searches for a <code>ScriptEngineFactory</code> that has been
|
||||||
|
* registered as a handler for the specified name using the <code>registerEngineName</code>
|
||||||
|
* method.
|
||||||
|
* <br><br> If one is not found, it searches the set of <code>ScriptEngineFactory</code> instances
|
||||||
|
* stored by the constructor for one with the specified name. If a <code>ScriptEngineFactory</code>
|
||||||
|
* is found by either method, it is used to create instance of <code>ScriptEngine</code>.
|
||||||
|
* @param shortName The short name of the <code>ScriptEngine</code> implementation.
|
||||||
|
* returned by the <code>getNames</code> method of its <code>ScriptEngineFactory</code>.
|
||||||
|
* @return A <code>ScriptEngine</code> created by the factory located in the search. Returns null
|
||||||
|
* if no such factory was found. The <code>ScriptEngineManager</code> sets its own <code>globalScope</code>
|
||||||
|
* <code>Bindings</code> as the <code>GLOBAL_SCOPE</code> <code>Bindings</code> of the newly
|
||||||
|
* created <code>ScriptEngine</code>.
|
||||||
|
* @throws NullPointerException if shortName is null.
|
||||||
|
*/
|
||||||
|
public ScriptEngine getEngineByName(String shortName) {
|
||||||
|
if (shortName == null) throw new NullPointerException();
|
||||||
|
//look for registered name first
|
||||||
|
Object obj;
|
||||||
|
if (null != (obj = nameAssociations.get(shortName))) {
|
||||||
|
ScriptEngineFactory spi = (ScriptEngineFactory)obj;
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = spi.getScriptEngine();
|
||||||
|
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||||
|
return engine;
|
||||||
|
} catch (Exception exp) {
|
||||||
|
if (DEBUG) exp.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ScriptEngineFactory spi : engineSpis) {
|
||||||
|
List<String> names = null;
|
||||||
|
try {
|
||||||
|
names = spi.getNames();
|
||||||
|
} catch (Exception exp) {
|
||||||
|
if (DEBUG) exp.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (names != null) {
|
||||||
|
for (String name : names) {
|
||||||
|
if (shortName.equals(name)) {
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = spi.getScriptEngine();
|
||||||
|
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||||
|
return engine;
|
||||||
|
} catch (Exception exp) {
|
||||||
|
if (DEBUG) exp.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look up and create a <code>ScriptEngine</code> for a given extension. The algorithm
|
||||||
|
* used by <code>getEngineByName</code> is used except that the search starts
|
||||||
|
* by looking for a <code>ScriptEngineFactory</code> registered to handle the
|
||||||
|
* given extension using <code>registerEngineExtension</code>.
|
||||||
|
* @param extension The given extension
|
||||||
|
* @return The engine to handle scripts with this extension. Returns <code>null</code>
|
||||||
|
* if not found.
|
||||||
|
* @throws NullPointerException if extension is null.
|
||||||
|
*/
|
||||||
|
public ScriptEngine getEngineByExtension(String extension) {
|
||||||
|
if (extension == null) throw new NullPointerException();
|
||||||
|
//look for registered extension first
|
||||||
|
Object obj;
|
||||||
|
if (null != (obj = extensionAssociations.get(extension))) {
|
||||||
|
ScriptEngineFactory spi = (ScriptEngineFactory)obj;
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = spi.getScriptEngine();
|
||||||
|
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||||
|
return engine;
|
||||||
|
} catch (Exception exp) {
|
||||||
|
if (DEBUG) exp.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ScriptEngineFactory spi : engineSpis) {
|
||||||
|
List<String> exts = null;
|
||||||
|
try {
|
||||||
|
exts = spi.getExtensions();
|
||||||
|
} catch (Exception exp) {
|
||||||
|
if (DEBUG) exp.printStackTrace();
|
||||||
|
}
|
||||||
|
if (exts == null) continue;
|
||||||
|
for (String ext : exts) {
|
||||||
|
if (extension.equals(ext)) {
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = spi.getScriptEngine();
|
||||||
|
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||||
|
return engine;
|
||||||
|
} catch (Exception exp) {
|
||||||
|
if (DEBUG) exp.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look up and create a <code>ScriptEngine</code> for a given mime type. The algorithm
|
||||||
|
* used by <code>getEngineByName</code> is used except that the search starts
|
||||||
|
* by looking for a <code>ScriptEngineFactory</code> registered to handle the
|
||||||
|
* given mime type using <code>registerEngineMimeType</code>.
|
||||||
|
* @param mimeType The given mime type
|
||||||
|
* @return The engine to handle scripts with this mime type. Returns <code>null</code>
|
||||||
|
* if not found.
|
||||||
|
* @throws NullPointerException if mimeType is null.
|
||||||
|
*/
|
||||||
|
public ScriptEngine getEngineByMimeType(String mimeType) {
|
||||||
|
if (mimeType == null) throw new NullPointerException();
|
||||||
|
//look for registered types first
|
||||||
|
Object obj;
|
||||||
|
if (null != (obj = mimeTypeAssociations.get(mimeType))) {
|
||||||
|
ScriptEngineFactory spi = (ScriptEngineFactory)obj;
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = spi.getScriptEngine();
|
||||||
|
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||||
|
return engine;
|
||||||
|
} catch (Exception exp) {
|
||||||
|
if (DEBUG) exp.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ScriptEngineFactory spi : engineSpis) {
|
||||||
|
List<String> types = null;
|
||||||
|
try {
|
||||||
|
types = spi.getMimeTypes();
|
||||||
|
} catch (Exception exp) {
|
||||||
|
if (DEBUG) exp.printStackTrace();
|
||||||
|
}
|
||||||
|
if (types == null) continue;
|
||||||
|
for (String type : types) {
|
||||||
|
if (mimeType.equals(type)) {
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = spi.getScriptEngine();
|
||||||
|
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||||
|
return engine;
|
||||||
|
} catch (Exception exp) {
|
||||||
|
if (DEBUG) exp.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list whose elements are instances of all the <code>ScriptEngineFactory</code> classes
|
||||||
|
* found by the discovery mechanism.
|
||||||
|
* @return List of all discovered <code>ScriptEngineFactory</code>s.
|
||||||
|
*/
|
||||||
|
public List<ScriptEngineFactory> getEngineFactories() {
|
||||||
|
List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size());
|
||||||
|
for (ScriptEngineFactory spi : engineSpis) {
|
||||||
|
res.add(spi);
|
||||||
|
}
|
||||||
|
return Collections.unmodifiableList(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a <code>ScriptEngineFactory</code> to handle a language
|
||||||
|
* name. Overrides any such association found using the Discovery mechanism.
|
||||||
|
* @param name The name to be associated with the <code>ScriptEngineFactory</code>.
|
||||||
|
* @param factory The class to associate with the given name.
|
||||||
|
* @throws NullPointerException if any of the parameters is null.
|
||||||
|
*/
|
||||||
|
public void registerEngineName(String name, ScriptEngineFactory factory) {
|
||||||
|
if (name == null || factory == null) throw new NullPointerException();
|
||||||
|
nameAssociations.put(name, factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a <code>ScriptEngineFactory</code> to handle a mime type.
|
||||||
|
* Overrides any such association found using the Discovery mechanism.
|
||||||
|
*
|
||||||
|
* @param type The mime type to be associated with the
|
||||||
|
* <code>ScriptEngineFactory</code>.
|
||||||
|
*
|
||||||
|
* @param factory The class to associate with the given mime type.
|
||||||
|
* @throws NullPointerException if any of the parameters is null.
|
||||||
|
*/
|
||||||
|
public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
|
||||||
|
if (type == null || factory == null) throw new NullPointerException();
|
||||||
|
mimeTypeAssociations.put(type, factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a <code>ScriptEngineFactory</code> to handle an extension.
|
||||||
|
* Overrides any such association found using the Discovery mechanism.
|
||||||
|
*
|
||||||
|
* @param extension The extension type to be associated with the
|
||||||
|
* <code>ScriptEngineFactory</code>.
|
||||||
|
* @param factory The class to associate with the given extension.
|
||||||
|
* @throws NullPointerException if any of the parameters is null.
|
||||||
|
*/
|
||||||
|
public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
|
||||||
|
if (extension == null || factory == null) throw new NullPointerException();
|
||||||
|
extensionAssociations.put(extension, factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set of script engine factories discovered. */
|
||||||
|
private TreeSet<ScriptEngineFactory> engineSpis;
|
||||||
|
|
||||||
|
/** Map of engine name to script engine factory. */
|
||||||
|
private HashMap<String, ScriptEngineFactory> nameAssociations;
|
||||||
|
|
||||||
|
/** Map of script file extension to script engine factory. */
|
||||||
|
private HashMap<String, ScriptEngineFactory> extensionAssociations;
|
||||||
|
|
||||||
|
/** Map of script MIME type to script engine factory. */
|
||||||
|
private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
|
||||||
|
|
||||||
|
/** Global bindings associated with script engines created by this manager. */
|
||||||
|
private Bindings globalScope;
|
||||||
|
}
|
||||||
@ -2,7 +2,6 @@ package net.sf.openrocket.simulation.extension.impl;
|
|||||||
|
|
||||||
import javax.script.Invocable;
|
import javax.script.Invocable;
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineManager;
|
|
||||||
import javax.script.ScriptException;
|
import javax.script.ScriptException;
|
||||||
|
|
||||||
import net.sf.openrocket.aerodynamics.Warning;
|
import net.sf.openrocket.aerodynamics.Warning;
|
||||||
@ -91,8 +90,7 @@ public class ScriptingExtension extends AbstractSimulationExtension {
|
|||||||
|
|
||||||
|
|
||||||
SimulationListener getListener() throws SimulationException {
|
SimulationListener getListener() throws SimulationException {
|
||||||
ScriptEngineManager manager = new ScriptEngineManager();
|
ScriptEngine engine = util.getEngineByName(getLanguage());
|
||||||
ScriptEngine engine = manager.getEngineByName(getLanguage());
|
|
||||||
if (engine == null) {
|
if (engine == null) {
|
||||||
throw new SimulationException("Your JRE does not support the scripting language '" + getLanguage() + "'");
|
throw new SimulationException("Your JRE does not support the scripting language '" + getLanguage() + "'");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,8 +10,8 @@ import java.util.prefs.BackingStoreException;
|
|||||||
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineFactory;
|
import javax.script.ScriptEngineFactory;
|
||||||
import javax.script.ScriptEngineManager;
|
|
||||||
|
|
||||||
|
import net.sf.openrocket.scripting.ScriptEngineManagerRedux;
|
||||||
import net.sf.openrocket.startup.Preferences;
|
import net.sf.openrocket.startup.Preferences;
|
||||||
import net.sf.openrocket.util.ArrayList;
|
import net.sf.openrocket.util.ArrayList;
|
||||||
import net.sf.openrocket.util.BugException;
|
import net.sf.openrocket.util.BugException;
|
||||||
@ -22,22 +22,28 @@ import com.google.inject.Inject;
|
|||||||
* Utility class used by the scripting extension and its configurator.
|
* Utility class used by the scripting extension and its configurator.
|
||||||
*/
|
*/
|
||||||
public class ScriptingUtil {
|
public class ScriptingUtil {
|
||||||
|
|
||||||
static final String NODE_ID = ScriptingExtension.class.getCanonicalName();
|
static final String NODE_ID = ScriptingExtension.class.getCanonicalName();
|
||||||
|
|
||||||
private static final List<String> DEFAULT_TRUSTED_HASHES = Arrays.asList(
|
private static final List<String> DEFAULT_TRUSTED_HASHES = List.of(
|
||||||
// Roll control script in roll control example file:
|
// Roll control script in roll control example file:
|
||||||
"SHA-256:9bf364ce4d4a75f09b29178bf9d6872b232084f73dae20dc7b5b073e54e95a42"
|
"SHA-256:9bf364ce4d4a75f09b29178bf9d6872b232084f73dae20dc7b5b073e54e95a42"
|
||||||
);
|
);
|
||||||
|
|
||||||
/** The name to be chosen from a list of alternatives. If not found, will use the default name. */
|
/** The name to be chosen from a list of alternatives. If not found, will use the default name. */
|
||||||
private static final List<String> PREFERRED_LANGUAGE_NAMES = Arrays.asList("JavaScript");
|
private static final List<String> PREFERRED_LANGUAGE_NAMES = List.of("JavaScript");
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
Preferences prefs;
|
Preferences prefs;
|
||||||
|
|
||||||
|
private static ScriptEngineManagerRedux manager;
|
||||||
|
|
||||||
|
public ScriptingUtil() {
|
||||||
|
if (manager == null) {
|
||||||
|
// using the ScriptEngineManger from javax.script package pulls in the sun.misc.ServiceConfigurationError
|
||||||
|
// which is removed in Java 9+ which causes a ClassNotFoundException to be thrown.
|
||||||
|
manager = new ScriptEngineManagerRedux();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the preferred internal language name based on a script language name.
|
* Return the preferred internal language name based on a script language name.
|
||||||
@ -49,7 +55,6 @@ public class ScriptingUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptEngineManager manager = new ScriptEngineManager();
|
|
||||||
ScriptEngine engine = manager.getEngineByName(language);
|
ScriptEngine engine = manager.getEngineByName(language);
|
||||||
if (engine == null) {
|
if (engine == null) {
|
||||||
return null;
|
return null;
|
||||||
@ -57,17 +62,18 @@ public class ScriptingUtil {
|
|||||||
return getLanguage(engine.getFactory());
|
return getLanguage(engine.getFactory());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ScriptEngine getEngineByName(String shortName) {
|
||||||
|
return manager.getEngineByName(shortName);
|
||||||
|
}
|
||||||
|
|
||||||
public List<String> getLanguages() {
|
public List<String> getLanguages() {
|
||||||
List<String> langs = new ArrayList<String>();
|
List<String> langs = new ArrayList<>();
|
||||||
ScriptEngineManager manager = new ScriptEngineManager();
|
|
||||||
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
|
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
|
||||||
langs.add(getLanguage(factory));
|
langs.add(getLanguage(factory));
|
||||||
}
|
}
|
||||||
return langs;
|
return langs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String getLanguage(ScriptEngineFactory factory) {
|
private String getLanguage(ScriptEngineFactory factory) {
|
||||||
for (String name : factory.getNames()) {
|
for (String name : factory.getNames()) {
|
||||||
if (PREFERRED_LANGUAGE_NAMES.contains(name)) {
|
if (PREFERRED_LANGUAGE_NAMES.contains(name)) {
|
||||||
@ -78,8 +84,6 @@ public class ScriptingUtil {
|
|||||||
return factory.getLanguageName();
|
return factory.getLanguageName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test whether the user has indicated this script to be trusted,
|
* Test whether the user has indicated this script to be trusted,
|
||||||
* or if it is an internally trusted script.
|
* or if it is an internally trusted script.
|
||||||
@ -123,7 +127,6 @@ public class ScriptingUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static String normalize(String script) {
|
static String normalize(String script) {
|
||||||
return script.replaceAll("\r", "").trim();
|
return script.replaceAll("\r", "").trim();
|
||||||
}
|
}
|
||||||
@ -132,10 +135,8 @@ public class ScriptingUtil {
|
|||||||
/*
|
/*
|
||||||
* NOTE: Hash length must be max 80 chars, the max length of a key in a Properties object.
|
* NOTE: Hash length must be max 80 chars, the max length of a key in a Properties object.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
String output;
|
String output;
|
||||||
MessageDigest digest;
|
MessageDigest digest;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
digest = MessageDigest.getInstance("SHA-256");
|
digest = MessageDigest.getInstance("SHA-256");
|
||||||
digest.update(language.getBytes(StandardCharsets.UTF_8));
|
digest.update(language.getBytes(StandardCharsets.UTF_8));
|
||||||
@ -152,5 +153,4 @@ public class ScriptingUtil {
|
|||||||
|
|
||||||
return digest.getAlgorithm() + ":" + output;
|
return digest.getAlgorithm() + ":" + output;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -99,6 +99,11 @@
|
|||||||
<zipfileset src="${core.dir}/lib/guice-4.2.3-no_aop.jar" />
|
<zipfileset src="${core.dir}/lib/guice-4.2.3-no_aop.jar" />
|
||||||
<zipfileset src="${core.dir}/lib/aopalliance.jar"/>
|
<zipfileset src="${core.dir}/lib/aopalliance.jar"/>
|
||||||
<zipfileset src="${core.dir}/lib/commonmark-0.19.0.jar"/>
|
<zipfileset src="${core.dir}/lib/commonmark-0.19.0.jar"/>
|
||||||
|
<zipfileset src="${core.dir}/lib/icu4j-71.1.jar"/>
|
||||||
|
<zipfileset src="${core.dir}/lib/js-22.1.0.1.jar"/>
|
||||||
|
<zipfileset src="${core.dir}/lib/graal-sdk-22.1.0.1.jar"/>
|
||||||
|
<zipfileset src="${core.dir}/lib/js-scriptengine-22.1.0.1.jar"/>
|
||||||
|
<zipfileset src="${core.dir}/lib/truffle-api-22.1.0.1.jar"/>
|
||||||
<zipfileset src="${core.dir}/lib/script-api-1.0.jar"/>
|
<zipfileset src="${core.dir}/lib/script-api-1.0.jar"/>
|
||||||
<zipfileset src="${lib.dir}/iText-5.0.2.jar"/>
|
<zipfileset src="${lib.dir}/iText-5.0.2.jar"/>
|
||||||
<zipfileset src="${core.dir}/lib/istack-commons-runtime.jar"/>
|
<zipfileset src="${core.dir}/lib/istack-commons-runtime.jar"/>
|
||||||
|
|||||||
@ -8,7 +8,6 @@ import java.awt.event.FocusListener;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineManager;
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JCheckBox;
|
import javax.swing.JCheckBox;
|
||||||
import javax.swing.JComboBox;
|
import javax.swing.JComboBox;
|
||||||
@ -33,7 +32,6 @@ import com.google.inject.Inject;
|
|||||||
|
|
||||||
@Plugin
|
@Plugin
|
||||||
public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfigurator<ScriptingExtension> {
|
public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfigurator<ScriptingExtension> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ScriptingUtil util;
|
private ScriptingUtil util;
|
||||||
|
|
||||||
@ -66,7 +64,6 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
|||||||
});
|
});
|
||||||
panel.add(languageSelector, "wrap para");
|
panel.add(languageSelector, "wrap para");
|
||||||
|
|
||||||
|
|
||||||
text = new RSyntaxTextArea(extension.getScript(), 20, 80);
|
text = new RSyntaxTextArea(extension.getScript(), 20, 80);
|
||||||
text.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT);
|
text.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT);
|
||||||
text.setCodeFoldingEnabled(true);
|
text.setCodeFoldingEnabled(true);
|
||||||
@ -91,7 +88,6 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
|||||||
RTextScrollPane scroll = new RTextScrollPane(text);
|
RTextScrollPane scroll = new RTextScrollPane(text);
|
||||||
panel.add(scroll, "spanx, grow, wrap para");
|
panel.add(scroll, "spanx, grow, wrap para");
|
||||||
|
|
||||||
|
|
||||||
BooleanModel enabled = new BooleanModel(extension, "Enabled");
|
BooleanModel enabled = new BooleanModel(extension, "Enabled");
|
||||||
JCheckBox check = new JCheckBox(enabled);
|
JCheckBox check = new JCheckBox(enabled);
|
||||||
check.setText(trans.get("SimulationExtension.scripting.text.enabled"));
|
check.setText(trans.get("SimulationExtension.scripting.text.enabled"));
|
||||||
@ -116,7 +112,6 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
|||||||
});
|
});
|
||||||
panel.add(button, "wrap rel");
|
panel.add(button, "wrap rel");
|
||||||
|
|
||||||
|
|
||||||
StyledLabel label = new StyledLabel(trans.get("SimulationExtension.scripting.text.trusted.msg"), -1, Style.ITALIC);
|
StyledLabel label = new StyledLabel(trans.get("SimulationExtension.scripting.text.trusted.msg"), -1, Style.ITALIC);
|
||||||
panel.add(label);
|
panel.add(label);
|
||||||
|
|
||||||
@ -130,7 +125,6 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
|||||||
util.setTrustedScript(extension.getLanguage(), extension.getScript(), trusted.isSelected());
|
util.setTrustedScript(extension.getLanguage(), extension.getScript(), trusted.isSelected());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void setLanguage(String language) {
|
private void setLanguage(String language) {
|
||||||
if (language == null) {
|
if (language == null) {
|
||||||
language = "";
|
language = "";
|
||||||
@ -144,8 +138,7 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String findSyntaxLanguage(String language) {
|
private String findSyntaxLanguage(String language) {
|
||||||
ScriptEngineManager manager = new ScriptEngineManager();
|
ScriptEngine engine = util.getEngineByName(language);
|
||||||
ScriptEngine engine = manager.getEngineByName(language);
|
|
||||||
|
|
||||||
if (engine != null) {
|
if (engine != null) {
|
||||||
Set<String> supported = TokenMakerFactory.getDefaultInstance().keySet();
|
Set<String> supported = TokenMakerFactory.getDefaultInstance().keySet();
|
||||||
@ -163,5 +156,4 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
|||||||
|
|
||||||
return SyntaxConstants.SYNTAX_STYLE_NONE;
|
return SyntaxConstants.SYNTAX_STYLE_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,16 +5,17 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.script.ScriptEngineFactory;
|
import javax.script.ScriptEngineFactory;
|
||||||
import javax.script.ScriptEngineManager;
|
|
||||||
|
|
||||||
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
|
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
|
||||||
|
|
||||||
|
import net.sf.openrocket.scripting.ScriptEngineManagerRedux;
|
||||||
|
|
||||||
public class Scripting {
|
public class Scripting {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Scripting APIs:");
|
System.out.println("Scripting APIs:");
|
||||||
|
|
||||||
ScriptEngineManager manager = new ScriptEngineManager();
|
ScriptEngineManagerRedux manager = new ScriptEngineManagerRedux();
|
||||||
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
|
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
|
||||||
System.out.println(" engineName=" + factory.getEngineName() +
|
System.out.println(" engineName=" + factory.getEngineName() +
|
||||||
" engineVersion=" + factory.getEngineVersion() +
|
" engineVersion=" + factory.getEngineVersion() +
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user