Java Groovy集成

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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值