1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-12-26 07:18:52 +00:00

feat: 循环判断ext目录寻找js引擎文件并且加载

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-11-11 18:13:37 +08:00
parent d3b8226b69
commit e74c8275e2
2 changed files with 20 additions and 14 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId> <artifactId>YumCore</artifactId>
<version>1.8.6</version> <version>1.8.7</version>
<build> <build>
<finalName>${project.artifactId}</finalName> <finalName>${project.artifactId}</finalName>
<plugins> <plugins>

View File

@ -18,6 +18,8 @@ import javax.script.ScriptEngineManager;
import javax.script.ScriptException; import javax.script.ScriptException;
import javax.script.SimpleBindings; import javax.script.SimpleBindings;
import lombok.val;
/** /**
* 喵式脚本引擎 * 喵式脚本引擎
* *
@ -59,21 +61,25 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable {
} catch (final NullPointerException ignored) { } catch (final NullPointerException ignored) {
} }
if (engine == null) { if (engine == null) {
File nashorn = new File(System.getProperty("java.ext.dirs").split(File.pathSeparator)[0], "nashorn.jar"); val dirs = System.getProperty("java.ext.dirs").split(File.pathSeparator);
if (nashorn.exists()) { for (String dir : dirs) {
try { File nashorn = new File(dir, "nashorn.jar");
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); if (nashorn.exists()) {
// 设置方法的访问权限 try {
method.setAccessible(true); Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
// 获取系统类加载器 // 设置方法的访问权限
URL url = nashorn.toURI().toURL(); method.setAccessible(true);
method.invoke(Thread.currentThread().getContextClassLoader(), url); // 获取系统类加载器
engineManager = new ScriptEngineManager(); URL url = nashorn.toURI().toURL();
engine = engineManager.getEngineByName(engineType); method.invoke(Thread.currentThread().getContextClassLoader(), url);
} catch (NoSuchMethodException | MalformedURLException | IllegalAccessException | InvocationTargetException ignored) { engineManager = new ScriptEngineManager();
ignored.printStackTrace(); engine = engineManager.getEngineByName(engineType);
} catch (NoSuchMethodException | MalformedURLException | IllegalAccessException | InvocationTargetException | NullPointerException ignored) {
}
return;
} }
} }
throw new UnsupportedOperationException("当前环境不支持 " + engineType + " 脚本类型!");
} }
} }