1 | |
package org.apache.maven.plugin.invoker; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import groovy.lang.Binding; |
23 | |
import groovy.lang.GroovyShell; |
24 | |
|
25 | |
import java.io.File; |
26 | |
import java.io.PrintStream; |
27 | |
import java.io.PrintWriter; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.List; |
30 | |
import java.util.Map; |
31 | |
|
32 | |
import org.apache.tools.ant.AntClassLoader; |
33 | |
import org.codehaus.groovy.control.CompilerConfiguration; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 2 | class GroovyScriptInterpreter |
42 | |
implements ScriptInterpreter |
43 | |
{ |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public Object evaluateScript( String script, List classPath, Map globalVariables, PrintStream scriptOutput ) |
49 | |
throws ScriptEvaluationException |
50 | |
{ |
51 | 2 | PrintStream origOut = System.out; |
52 | 2 | PrintStream origErr = System.err; |
53 | |
|
54 | |
try |
55 | |
{ |
56 | 2 | CompilerConfiguration config = new CompilerConfiguration( CompilerConfiguration.DEFAULT ); |
57 | |
|
58 | 2 | if ( scriptOutput != null ) |
59 | |
{ |
60 | 2 | System.setErr( scriptOutput ); |
61 | 2 | System.setOut( scriptOutput ); |
62 | 2 | config.setOutput( new PrintWriter( scriptOutput ) ); |
63 | |
} |
64 | |
|
65 | 2 | ClassLoader loader = null; |
66 | 2 | if ( classPath != null && !classPath.isEmpty() ) |
67 | |
{ |
68 | 0 | AntClassLoader childFirstLoader = new AntClassLoader( getClass().getClassLoader(), false ); |
69 | 0 | for ( Iterator it = classPath.iterator(); it.hasNext(); ) |
70 | |
{ |
71 | 0 | String path = (String) it.next(); |
72 | 0 | childFirstLoader.addPathComponent( new File( path ) ); |
73 | |
} |
74 | 0 | loader = childFirstLoader; |
75 | |
} |
76 | |
|
77 | 2 | Binding binding = new Binding( globalVariables ); |
78 | |
|
79 | 2 | GroovyShell interpreter = new GroovyShell( loader, binding, config ); |
80 | |
|
81 | |
try |
82 | |
{ |
83 | 2 | return interpreter.evaluate( script ); |
84 | |
} |
85 | 0 | catch ( ThreadDeath e ) |
86 | |
{ |
87 | 0 | throw e; |
88 | |
} |
89 | 0 | catch ( Throwable e ) |
90 | |
{ |
91 | 0 | throw new ScriptEvaluationException( e ); |
92 | |
} |
93 | |
} |
94 | |
finally |
95 | |
{ |
96 | 2 | System.setErr( origErr ); |
97 | 2 | System.setOut( origOut ); |
98 | |
} |
99 | |
} |
100 | |
|
101 | |
} |