1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-22 01:48:50 +00:00

fix: 修复在MODS服下未加载js引擎的问题

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-11-11 18:04:40 +08:00
parent 6c5d9e26da
commit d3b8226b69

View File

@ -1,6 +1,12 @@
package pw.yumc.YumCore.engine; package pw.yumc.YumCore.engine;
import java.io.File;
import java.io.Reader; import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap; import java.util.HashMap;
import javax.script.Bindings; import javax.script.Bindings;
@ -50,8 +56,24 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
public MiaoScriptEngine(ScriptEngineManager engineManager, final String engineType) { public MiaoScriptEngine(ScriptEngineManager engineManager, final String engineType) {
try { try {
engine = engineManager.getEngineByName(engineType); engine = engineManager.getEngineByName(engineType);
} catch (final NullPointerException ex) { } catch (final NullPointerException ignored) {
engine = engineManager.getEngineByName("javascript"); }
if (engine == null) {
File nashorn = new File(System.getProperty("java.ext.dirs").split(File.pathSeparator)[0], "nashorn.jar");
if (nashorn.exists()) {
try {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
// 设置方法的访问权限
method.setAccessible(true);
// 获取系统类加载器
URL url = nashorn.toURI().toURL();
method.invoke(Thread.currentThread().getContextClassLoader(), url);
engineManager = new ScriptEngineManager();
engine = engineManager.getEngineByName(engineType);
} catch (NoSuchMethodException | MalformedURLException | IllegalAccessException | InvocationTargetException ignored) {
ignored.printStackTrace();
}
}
} }
} }