概述
Groovy是动态热编译语言,和java结束在低代码产品里能发挥不错的作用,那如何读取存放在数据库里的脚本记录,并且随着脚本的更改和更新?下面就给大家介绍一下。
1.首先我们需要实现一个 URLStreamHandler
package com.breeze.bms.groovy;
import com.breeze.bms.mybatis.domain.GroovyScript;
import com.breeze.bms.service.GroovyScriptService;
import com.breeze.bms.util.SpringUtil;
import com.google.common.base.Splitter;
import org.springframework.util.ObjectUtils;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.List;
/**
* @author breeze
* @since 2022/3/21 11:32
*/
public class GroovyResourceStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL u) throws IOException {
GroovyScript groovyScript = null;//咱们自己定义的groovyScript对象
if (!ObjectUtils.isEmpty(u.getPath())) {
List<String> paths = Splitter.on("/").omitEmptyStrings().splitToList(u.getPath());
if (!ObjectUtils.isEmpty(paths) && paths.size() == 2) {
//从数据库里加载
groovyScript = SpringUtil.getBean(GroovyScriptService.class).loadGroovyScript(paths.get(0), paths.get(1).replaceAll(".groovy",""));
}
}
if (ObjectUtils.isEmpty(groovyScript))
throw new IOException("没有可执行groovy脚本!");
GroovyResourceConnection connection = new GroovyResourceConnection(u, groovyScript);
return connection;
}
}
定义resourceConnection
package com.breeze.bms.groovy;
import com.breeze.bms.mybatis.domain.GroovyScript;
import com.breeze.bms.service.GroovyScriptService;
import com.breeze.bms.util.G;
import com.breeze.bms.util.SpringUtil;
import org.codehaus.groovy.ant.Groovy;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* @author breeze
* @since 2022/3/21 11:29
*/
public class GroovyResourceConnection extends URLConnection {
private final GroovyScript groovyScript;
protected GroovyResourceConnection(URL url, GroovyScript groovyScript) {
super(url);
this.groovyScript = groovyScript;
}
@Override
public void connect() throws IOException {
this.connected = true;
}
@Override
public InputStream getInputStream() throws IOException {
this.connect();
return new ByteArrayInputStream(groovyScript.getScriptText().getBytes());
}
//这个方法是核心,groovy引擎判断是否需要重新加载
public long getLastModified() {
GroovyScript temp = SpringUtil.getBean(GroovyScriptService.class).infoWithoutScriptText(groovyScript.getId());
return G.l(temp.getUpdateTime(),0);
}
}
2.指定groovy协议解析类
/**
* @author breeze
* @since 2022/6/9 14:59
*/
public class GroovyURLStreamHandlerFactory implements URLStreamHandlerFactory {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
return "groovy".equals(protocol) ? new GroovyResourceStreamHandler() : null;
}
}
3.具体使用就可以使用groovy协议
GroovyScriptEngine groovyScriptEngine; = new GroovyScriptEngine(new URL[]{new URL(null, "groovy://db/system/", new GroovyResourceStreamHandler())});
Binding binding = new Binding();
Script script = groovyScriptEngine.createScript(groovyScript.getScriptName() + ".groovy", binding);
if (script != null) {
return script.invokeMethod(methodName, params);
}
更多详细源码请关注 https://gitee.com/wtr/BMS-VUE
最后
以上就是高大大炮为你收集整理的GroovyEngine加载数据库脚本的全部内容,希望文章能够帮你解决GroovyEngine加载数据库脚本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复