【代码修复】IoT:网络组件相关
This commit is contained in:
@@ -44,6 +44,7 @@
|
||||
</dependency>
|
||||
|
||||
<!-- JavaScript 引擎 - 使用 GraalJS 替代 Nashorn -->
|
||||
<!-- TODO @haohao:得考虑下,jdk8 可能不支持 graalvm,后续哈;【优先级:低】 -->
|
||||
<dependency>
|
||||
<groupId>org.graalvm.sdk</groupId>
|
||||
<artifactId>graal-sdk</artifactId>
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
// TODO @haohao:挪到 test 目录下
|
||||
/**
|
||||
* 脚本使用示例类
|
||||
*/
|
||||
|
||||
@@ -63,6 +63,7 @@ public class JsScriptEngine extends AbstractScriptEngine implements AutoCloseabl
|
||||
.build();
|
||||
|
||||
// 创建隔离的临时目录路径
|
||||
// TODO @haohao:貌似没用到?
|
||||
Path tempDirectory = Path.of(System.getProperty("java.io.tmpdir"), "graaljs-" + IdUtil.fastSimpleUUID());
|
||||
|
||||
// 初始化 GraalJS 上下文
|
||||
@@ -94,6 +95,7 @@ public class JsScriptEngine extends AbstractScriptEngine implements AutoCloseabl
|
||||
Source source = getOrCreateSource(script);
|
||||
|
||||
// 执行脚本并捕获结果,添加超时控制
|
||||
// TODO @haohao:通过线程池 + future 会好点?
|
||||
Value result;
|
||||
Thread executionThread = Thread.currentThread();
|
||||
Thread watchdogThread = new Thread(() -> {
|
||||
@@ -236,11 +238,14 @@ public class JsScriptEngine extends AbstractScriptEngine implements AutoCloseabl
|
||||
if (result.isNumber()) {
|
||||
if (result.fitsInInt()) {
|
||||
return result.asInt();
|
||||
} else if (result.fitsInLong()) {
|
||||
}
|
||||
if (result.fitsInLong()) {
|
||||
return result.asLong();
|
||||
} else if (result.fitsInFloat()) {
|
||||
}
|
||||
if (result.fitsInFloat()) {
|
||||
return result.asFloat();
|
||||
} else if (result.fitsInDouble()) {
|
||||
}
|
||||
if (result.fitsInDouble()) {
|
||||
return result.asDouble();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,6 @@ public class ScriptEngineFactory implements DisposableBean {
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
// 应用关闭时,释放所有引擎资源
|
||||
log.info("应用关闭,释放所有脚本引擎资源...");
|
||||
releaseAllEngines();
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
// TODO @haohao:搞到 test 里面哈;
|
||||
/**
|
||||
* GraalJS 脚本引擎示例
|
||||
* <p>
|
||||
|
||||
@@ -56,6 +56,7 @@ public class JsSandbox implements ScriptSandbox {
|
||||
*/
|
||||
public JsSandbox() {
|
||||
// 初始化 Java 相关的不安全关键字
|
||||
// TODO @haohao:可以使用 addAll 哈。
|
||||
Arrays.asList(
|
||||
"java.lang.System",
|
||||
"java.io",
|
||||
|
||||
@@ -40,6 +40,7 @@ public class ScriptServiceImpl implements ScriptService {
|
||||
try {
|
||||
return engine.execute(script, context);
|
||||
} catch (Exception e) {
|
||||
// TODO @haohao:最好打印一些参数;下面类似的也是
|
||||
log.error("执行脚本失败: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("执行脚本失败: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ public class ScriptUtils {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
// TODO @haohao:使用 lombok 简化掉
|
||||
private ScriptUtils() {
|
||||
// 私有构造函数
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user