1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.gmaven.adapter.impl;
17
18 import java.util.Map;
19
20 import javax.annotation.Nullable;
21
22 import groovy.lang.Binding;
23 import groovy.lang.GroovyClassLoader;
24 import groovy.lang.GroovyCodeSource;
25 import groovy.lang.GroovyShell;
26 import org.codehaus.gmaven.adapter.ClassSource;
27 import org.codehaus.gmaven.adapter.ResourceLoader;
28 import org.codehaus.gmaven.adapter.ScriptExecutor;
29 import org.codehaus.groovy.control.CompilerConfiguration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import static com.google.common.base.Preconditions.checkNotNull;
34
35
36
37
38
39
40 public class ScriptExecutorImpl
41 implements ScriptExecutor
42 {
43 private final Logger log = LoggerFactory.getLogger(getClass());
44
45 private final GroovyRuntimeImpl runtime;
46
47 ScriptExecutorImpl(final GroovyRuntimeImpl runtime) {
48 this.runtime = checkNotNull(runtime);
49 }
50
51 @Override
52 @Nullable
53 public Object execute(final ClassSource classSource,
54 final ClassLoader classLoader,
55 final ResourceLoader resourceLoader,
56 final Map<String, Object> context,
57 final @Nullable Map<String, Object> options)
58 throws Exception
59 {
60 checkNotNull(classSource);
61 checkNotNull(classLoader);
62 checkNotNull(resourceLoader);
63 checkNotNull(context);
64
65 log.trace("Execute; class-source: {}, class-loader: {}, resource-loader: {}, context: {}",
66 classSource, classLoader, resourceLoader, context);
67
68 GroovyClassLoader gcl = runtime.createGroovyClassLoader(classLoader, resourceLoader);
69
70 CompilerConfiguration cc = new CompilerConfiguration();
71
72
73
74 Binding binding = runtime.createBinding(context);
75 GroovyShell shell = new GroovyShell(gcl, binding, cc);
76
77 if (options != null) {
78 configureOptions(shell, options);
79 }
80
81 GroovyCodeSource codeSource = runtime.createGroovyCodeSource(classSource);
82 try {
83 return shell.evaluate(codeSource);
84 }
85 finally {
86 gcl.clearCache();
87 }
88 }
89
90 private void configureOptions(final GroovyShell shell, final Map<String, Object> options) {
91
92 }
93 }