|
@@ -2,11 +2,13 @@ package org.bfkj.utils;
|
|
|
|
|
|
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
|
|
import org.openjdk.nashorn.api.scripting.ScriptObjectMirror;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.script.*;
|
|
|
import java.io.File;
|
|
|
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.*;
|
|
@@ -131,32 +133,20 @@ public class ScriptEnginePro {
|
|
|
if (Objects.isNull(className) || Objects.isNull(computing_expression)) {
|
|
|
return "java执行参数不全,类名:".concat(Objects.isNull(className) ? "" : className.toString()).concat("; 方法名:").concat(computing_expression);
|
|
|
}
|
|
|
- if (Objects.nonNull(filePath)) {
|
|
|
- File file = new File(System.getProperty("user.dir").concat(File.separator).concat("plugins").concat(File.separator).concat(filePath.toString()));
|
|
|
- if (!file.exists()) {
|
|
|
- return "外部文件加载不存在:".concat(System.getProperty("user.dir")).concat(File.separator).concat("plugins").concat(File.separator).concat(filePath.toString());
|
|
|
- }
|
|
|
- Method addURLMethod = null;
|
|
|
- boolean accessible = false;
|
|
|
- try {
|
|
|
- addURLMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
|
|
- URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
|
|
|
- accessible = addURLMethod.canAccess(null);
|
|
|
- if (!accessible) {
|
|
|
- addURLMethod.setAccessible(true);
|
|
|
- }
|
|
|
- addURLMethod.invoke(classLoader, file.toURI().toURL());
|
|
|
- } catch (Exception e) {
|
|
|
- return "JAR加载到环境失败".concat(LogUtils.getException(e));
|
|
|
- } finally {
|
|
|
- if (addURLMethod != null && !accessible) {
|
|
|
- addURLMethod.setAccessible(false);
|
|
|
+ try {
|
|
|
+ ClassLoader classLoader;
|
|
|
+ if (Objects.nonNull(filePath) && StringUtils.hasText(filePath.toString())) {
|
|
|
+ File file = new File(System.getProperty("user.dir").concat(File.separator).concat("plugins").concat(File.separator).concat(filePath.toString()));
|
|
|
+ if (!file.exists()) {
|
|
|
+ return "外部文件加载不存在:".concat(System.getProperty("user.dir")).concat(File.separator).concat("plugins").concat(File.separator).concat(filePath.toString());
|
|
|
}
|
|
|
+ URL url = file.toURI().toURL();
|
|
|
+ classLoader = new URLClassLoader(new URL[]{url}, Thread.currentThread().getContextClassLoader());
|
|
|
+ } else {
|
|
|
+ classLoader = Thread.currentThread().getContextClassLoader();
|
|
|
}
|
|
|
- }
|
|
|
- try {
|
|
|
initParam();
|
|
|
- Class<?> classExample = Class.forName(className.toString());
|
|
|
+ Class<?> classExample = classLoader.loadClass(className.toString());
|
|
|
classInstance = classExample.getConstructor().newInstance();
|
|
|
for (Method currentMethod : classExample.getMethods()) {
|
|
|
String methodName = currentMethod.getName();
|