jvm-sanbox 使用
官方文档
https://github.com/alibaba/jvm-sandbox/wiki
安装
- 下载压缩包,解压
- 初始化:运行 install-local.sh;返回了sandbox home路径
- 在home路径下新建sandbox-module文件夹,用于存储自定义module
自定义module实现
- 新建maven工程
- 模块实现
package module;
import analyser.CodeAnalyser;
import com.alibaba.jvm.sandbox.api.Information;
import com.alibaba.jvm.sandbox.api.ProcessControlException;
import com.alibaba.jvm.sandbox.api.ProcessController;
import com.alibaba.jvm.sandbox.api.annotation.Command;
import com.alibaba.jvm.sandbox.api.listener.ext.Advice;
import com.alibaba.jvm.sandbox.api.listener.ext.AdviceListener;
import com.alibaba.jvm.sandbox.api.listener.ext.EventWatchBuilder;
import org.kohsuke.MetaInfServices;
import com.alibaba.jvm.sandbox.api.Module;
import com.alibaba.jvm.sandbox.api.resource.ModuleEventWatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
@MetaInfServices({Module.class})
@Information(id="static-anlysis-plugin",version = "1.0.0",author = "")
public class StaticAnalysisModule implements Module{
@Resource
private ModuleEventWatcher moduleEventWatcher;
protected static Logger log = LoggerFactory.getLogger(StaticAnalysisModule.class);
@Command("groovyAnalysis")
public void groovyAnalysis(){
new EventWatchBuilder(moduleEventWatcher)
.onClass("com.alibaba.freelogin.secret.service.impl.XXXXXX1")
.onBehavior("XXXXXX2")
.onWatch(new AdviceListener(){
@Override
protected void before(Advice advice) throws Throwable {
log.info("sandbox uniface-module start");
CodeAnalyser codeAnalyser=new CodeAnalyser();
Object[] params= advice.getParameterArray();
String analyserReault=codeAnalyser.checkGroovyCode(params[0].toString(),"feature","testModule123");
if(analyserReault==null){
ProcessController.noneImmediately();
}else {
ProcessController.throwsImmediately(new Exception(analyserReault));
}
}
});
}
}
- 打包 mvn clean package
自定义模块生效
- package放到sandbox-module文件夹下
- 查看监听端口的进程号 lsof -i:PORT_NUM
- 加载自定义module ./sandbox.sh -p 进程号
- 查看所加载的module ./sandbox.sh -p 进程号 -l
- 如果没看到自己的module,查看下日志 ~logs/sandbox/sandbox.log
- 指定模块生效 ./sandbox.sh -p 2140 -d @information_id/@command_name
####至关重要,不然module不生效