import java.lang.reflect.Method;
import java.util.Map;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
/**
* Groovy脚本工具类
*
*/
public class GroovyUtils {
/**
* 执行Groovy脚本,创建类实例执行
*
* @param code Groovy脚本编码
* @param method 方法名
* @return Groovy脚本执行结果
* @throws Exception
*/
public static Object
invokeByInstance(Object obj, String method, Object... args)
throws Exception {
GroovyObject groovyObject = (GroovyObject) obj;
Object result = groovyObject.invokeMethod(method, args);
return result;
}
/**
* 执行Groovy脚本,以静态方式执行
*
* @param code Groovy脚本编码
* @param method 方法名
* @return Groovy脚本执行结果
* @throws Exception
*/
public static Object
invoke(Class<?> cls, String method, Object... args)
throws Exception {
Class<?>[] parameterTypes =
null;
if (args !=
null && args.length >
0) {
parameterTypes =
new Class<?>[args.length];
for (
int i =
0; i < args.length; i++) {
if (args[i] ==
null) {
parameterTypes[i] = Object.class;
}
else {
parameterTypes[i] = args[i].getClass();
}
}
}
Method methodObject = cls.getDeclaredMethod(method, parameterTypes);
Object result = methodObject.invoke(
null, args);
return result;
}
/**
* 初始化类,并存入缓存
*
* @param content Groovy脚本脚本内容
* @return 编译好的类
* @throws Exception 找不到或无法加载Groovy脚本类时,抛出异常
*/
public static Class<?>
initCls(String content)
throws Exception {
ClassLoader cl = GroovyUtils.class.getClassLoader();
GroovyClassLoader gcl =
new GroovyClassLoader(cl);
try {
Class<?> groovyCls = gcl.parseClass(content);
if (groovyCls ==
null) {
throw new IllegalAccessException(
"Rule content can not be parsed as a Class, ensure the content is correct.\r\n" + content);
}
return groovyCls;
}
finally {
gcl.close();
}
}
/**
* 编译脚本
*
* @param scriptText 脚本内容
* @return Script对象
*/
public static Script
initScript(
final String scriptText) {
return new GroovyShell().parse(scriptText);
}
/**
* 执行脚本
*
* @param script Script对象
* @return 执行结果
*/
public static Object
invokeScript(Script script) {
return script.run();
}
/**
* 执行脚本
*
* @param script Script对象
* @param binding Binding对象,用于绑定脚本所需外部变量
* @return 执行结果
*/
public static Object
invokeScript(Script script, Binding binding) {
script.setBinding(binding);
return script.run();
}
/**
* 执行脚本
*
* @param script Script对象
* @param variables 脚本所需外部变量Map
* @return 执行结果
*/
public static Object
invokeScript(Script script, Map<?, ?> variables) {
Binding binding =
new Binding(variables);
return invokeScript(script, binding);
}
/**
* 执行脚本
*
* @param scriptText 脚本内容
* @return 执行结果
*/
public static Object
invokeScript(String scriptText) {
return invokeScript(initScript(scriptText));
}
/**
* 执行脚本
*
* @param scriptText 脚本内容
* @param binding Binding对象,用于绑定脚本所需外部变量
* @return
*/
public static Object
invokeScript(String scriptText, Binding binding) {
return invokeScript(initScript(scriptText), binding);
}
/**
* 执行脚本
*
* @param scriptText 脚本内容
* @param variables 脚本所需外部变量Map
* @return
*/
public static Object
invokeScript(String scriptText, Map<String, Object> variables) {
Binding binding =
new Binding(variables);
return invokeScript(scriptText, binding);
}
}
public String
executeTemplate(
final String template,
final Map<String, Object> variables) {
securityChecks(template);
return executeTemplate(
new SimpleTemplateEngine(), template, variables);
}
public String
executeTemplate(
final TemplateEngine templateEngine,
final String template,
final Map<String, Object> variables) {
securityChecks(template);
if (template ==
null) {
return null;
}
try {
final Template templateObject = templateEngine
.createTemplate(template);
final Writable writable = templateObject.make(variables);
final StringWriter writer =
new StringWriter();
writable.writeTo(writer);
writer.flush();
if (log.isDebugEnabled() ==
true) {
log.debug(writer.toString());
}
return writer.toString();
}
catch (CompilationFailedException ex) {
log.error(ex.getMessage() +
" while executing template: "
+ template, ex);
}
catch (FileNotFoundException ex) {
log.error(ex.getMessage() +
" while executing template: "
+ template, ex);
}
catch (ClassNotFoundException ex) {
log.error(ex.getMessage() +
" while executing template: "
+ template, ex);
}
catch (IOException ex) {
log.error(ex.getMessage() +
" while executing template: "
+ template, ex);
}
return null;
}
转载请注明原文地址: https://ju.6miu.com/read-7710.html