Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.security.Policy;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.codehaus.groovy.control.CompilerConfiguration;
- import org.codehaus.groovy.control.customizers.SecureASTCustomizer;
- import org.codehaus.groovy.runtime.InvokerHelper;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import groovy.lang.Binding;
- import groovy.lang.GroovyShell;
- import groovy.lang.Script;
- import groovy.util.Eval;
- public class GroovyExecutor implements ApplicationContextAware,InitializingBean{
- private static final Log log = LogFactory.getLog(GroovyExecutor.class);
- private ApplicationContext applicationContext;
- private GroovyShell groovyShell;
- public ConcurrentHashMap<String,Class<?>> classCache = new ConcurrentHashMap<String,Class<?>>();
- public static List<String> blackList = new ArrayList<String>();
- static {
- blackList.add(System.class.getName());
- blackList.add(Script.class.getName());
- blackList.add(GroovyShell.class.getName());
- blackList.add(Eval.class.getName());
- blackList.add(Runtime.class.getName());
- blackList.add(ProcessBuilder.class.getName());
- }
- @Override
- public void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException {
- this.applicationContext = applicationContext;
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- try {
- CompilerConfiguration conf = new CompilerConfiguration();
- SecureASTCustomizer customizer = new SecureASTCustomizer();
- customizer.setReceiversBlackList(blackList);
- conf.addCompilationCustomizers(customizer);
- Map<String,Object> springBeanMap = this.applicationContext.getBeansOfType(Object.class, true, false);
- Binding springBinding = new Binding(springBeanMap);
- groovyShell = new GroovyShell(springBinding,conf);
- Policy.setPolicy(new ScriptPolicy("/groovy/script"));
- if (System.getSecurityManager() == null) {
- System.setSecurityManager(new SecurityManager());
- }
- }catch(Throwable t) {
- log.error("groovy shell error",t);
- }
- }
- public Object run(String scriptText,Map<String,Object> params){
- Class<?> scriptClass = null;
- Object result = null;
- if(classCache.containsKey(scriptText)){
- scriptClass = classCache.get(scriptText);
- }else{
- //if slow ,parse all at init
- scriptClass = groovyShell.getClassLoader().parseClass(scriptText);
- classCache.put(scriptText, scriptClass);
- }
- Script script = InvokerHelper.createScript(scriptClass, buildScriptContext(params));
- try{
- result = script.run();
- }catch(Throwable t){
- log.error("run error for->"+scriptText, t);
- }
- return result;
- }
- @SuppressWarnings({"unchecked" })
- private Binding buildScriptContext(Map<String,Object> params){
- Map<String,Object> var = new HashMap<String,Object>();
- if(groovyShell.getContext().getVariables() != null && !groovyShell.getContext().getVariables().isEmpty()){
- var.putAll(groovyShell.getContext().getVariables());
- }
- if(params != null && !params.isEmpty()){
- var.putAll(params);
- }
- Binding context = new Binding(var);
- return context;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement