Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import java.security.Policy;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.concurrent.ConcurrentHashMap;
  8.  
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.codehaus.groovy.control.CompilerConfiguration;
  12. import org.codehaus.groovy.control.customizers.SecureASTCustomizer;
  13. import org.codehaus.groovy.runtime.InvokerHelper;
  14. import org.springframework.beans.BeansException;
  15. import org.springframework.beans.factory.InitializingBean;
  16. import org.springframework.context.ApplicationContext;
  17. import org.springframework.context.ApplicationContextAware;
  18.  
  19. import groovy.lang.Binding;
  20. import groovy.lang.GroovyShell;
  21. import groovy.lang.Script;
  22. import groovy.util.Eval;
  23.  
  24. public class GroovyExecutor implements ApplicationContextAware,InitializingBean{
  25.  
  26. private static final Log log = LogFactory.getLog(GroovyExecutor.class);
  27.  
  28. private ApplicationContext applicationContext;
  29.  
  30. private GroovyShell groovyShell;
  31.  
  32. public ConcurrentHashMap<String,Class<?>> classCache = new ConcurrentHashMap<String,Class<?>>();
  33.  
  34. public static List<String> blackList = new ArrayList<String>();
  35.  
  36. static {
  37. blackList.add(System.class.getName());
  38. blackList.add(Script.class.getName());
  39. blackList.add(GroovyShell.class.getName());
  40. blackList.add(Eval.class.getName());
  41. blackList.add(Runtime.class.getName());
  42. blackList.add(ProcessBuilder.class.getName());
  43. }
  44.  
  45. @Override
  46. public void setApplicationContext(ApplicationContext applicationContext)
  47. throws BeansException {
  48. this.applicationContext = applicationContext;
  49. }
  50.  
  51. @Override
  52. public void afterPropertiesSet() throws Exception {
  53. try {
  54.  
  55.  
  56. CompilerConfiguration conf = new CompilerConfiguration();
  57. SecureASTCustomizer customizer = new SecureASTCustomizer();
  58. customizer.setReceiversBlackList(blackList);
  59. conf.addCompilationCustomizers(customizer);
  60.  
  61.  
  62.  
  63.  
  64. Map<String,Object> springBeanMap = this.applicationContext.getBeansOfType(Object.class, true, false);
  65. Binding springBinding = new Binding(springBeanMap);
  66. groovyShell = new GroovyShell(springBinding,conf);
  67.  
  68. Policy.setPolicy(new ScriptPolicy("/groovy/script"));
  69. if (System.getSecurityManager() == null) {
  70. System.setSecurityManager(new SecurityManager());
  71. }
  72. }catch(Throwable t) {
  73. log.error("groovy shell error",t);
  74. }
  75. }
  76.  
  77. public Object run(String scriptText,Map<String,Object> params){
  78. Class<?> scriptClass = null;
  79. Object result = null;
  80. if(classCache.containsKey(scriptText)){
  81. scriptClass = classCache.get(scriptText);
  82. }else{
  83. //if slow ,parse all at init
  84. scriptClass = groovyShell.getClassLoader().parseClass(scriptText);
  85. classCache.put(scriptText, scriptClass);
  86. }
  87. Script script = InvokerHelper.createScript(scriptClass, buildScriptContext(params));
  88. try{
  89. result = script.run();
  90. }catch(Throwable t){
  91. log.error("run error for->"+scriptText, t);
  92. }
  93. return result;
  94. }
  95.  
  96.  
  97.  
  98. @SuppressWarnings({"unchecked" })
  99. private Binding buildScriptContext(Map<String,Object> params){
  100. Map<String,Object> var = new HashMap<String,Object>();
  101. if(groovyShell.getContext().getVariables() != null && !groovyShell.getContext().getVariables().isEmpty()){
  102. var.putAll(groovyShell.getContext().getVariables());
  103. }
  104. if(params != null && !params.isEmpty()){
  105. var.putAll(params);
  106. }
  107. Binding context = new Binding(var);
  108. return context;
  109. }
  110.  
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement