fix: 加 PluginContext 桩类+自动导包,Groovy脚本编译不再报错
This commit is contained in:
@@ -5,6 +5,8 @@ import groovy.lang.Binding;
|
||||
import groovy.lang.GroovyClassLoader;
|
||||
import groovy.lang.GroovyShell;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.codehaus.groovy.control.CompilerConfiguration;
|
||||
import org.codehaus.groovy.control.customizers.ImportCustomizer;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -14,14 +16,25 @@ import java.util.*;
|
||||
* Groovy 脚本解析引擎。
|
||||
* 共享 GroovyClassLoader 避免频繁创建 ClassLoader,每次执行复用同一个。
|
||||
* 脚本可访问内置变量:content, url, jsoup, json, utils
|
||||
* 自动导入 Groovy 内置包和 PAR 桩类,使脚本编译通过。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class GroovyScriptEngine {
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
/** 全局单例 ClassLoader,避免每次 new GroovyShell 产生新 ClassLoader */
|
||||
private static final GroovyClassLoader classLoader = new GroovyClassLoader();
|
||||
private static final GroovyClassLoader classLoader;
|
||||
|
||||
static {
|
||||
ImportCustomizer imports = new ImportCustomizer();
|
||||
imports.addStarImports("groovy.json");
|
||||
imports.addStarImports("com.par.core.adapter");
|
||||
|
||||
CompilerConfiguration config = new CompilerConfiguration();
|
||||
config.addCompilationCustomizers(imports);
|
||||
|
||||
classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> execute(String groovyScript, String content, String url) {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.par.core.adapter;
|
||||
|
||||
/**
|
||||
* PluginContext 桩类,仅供 Groovy 脚本编译通过。
|
||||
* PAR 只调用脚本中的 parse() 方法,不会使用此类。
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class PluginContext {
|
||||
public Site site;
|
||||
public Object httpClient;
|
||||
|
||||
public static class Site {
|
||||
public String authorization;
|
||||
public String siteUrl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user