1 package org.apache.maven.shared.scriptinterpreter;
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 import org.codehaus.groovy.control.CompilerConfiguration;
25 import org.codehaus.groovy.runtime.powerassert.PowerAssertionError;
26 import org.codehaus.groovy.tools.RootLoader;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.net.URL;
32 import java.util.List;
33 import java.util.Map;
34
35
36
37
38
39
40 class GroovyScriptInterpreter
41 implements ScriptInterpreter
42 {
43
44
45 @Override
46 public Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables,
47 PrintStream scriptOutput )
48 throws ScriptEvaluationException
49 {
50 PrintStream origOut = System.out;
51 PrintStream origErr = System.err;
52
53 try ( RootLoader childFirstLoader = new RootLoader( new URL[] {}, getClass().getClassLoader() ) )
54 {
55
56 if ( scriptOutput != null )
57 {
58 System.setErr( scriptOutput );
59 System.setOut( scriptOutput );
60 }
61
62 if ( classPath != null && !classPath.isEmpty() )
63 {
64 for ( String path : classPath )
65 {
66 childFirstLoader.addURL( new File( path ).toURI().toURL() );
67 }
68 }
69
70 GroovyShell interpreter = new GroovyShell( childFirstLoader,
71 new Binding( globalVariables ),
72 new CompilerConfiguration( CompilerConfiguration.DEFAULT ) );
73
74 try
75 {
76 return interpreter.evaluate( script );
77 }
78 catch ( ThreadDeath e )
79 {
80 throw e;
81 }
82 catch ( PowerAssertionError e )
83 {
84 throw new ScriptEvaluationException( "Assertion Error", e );
85 }
86 catch ( Throwable e )
87 {
88 throw new ScriptEvaluationException( e );
89 }
90 }
91 catch ( IOException e )
92 {
93 throw new ScriptEvaluationException( e );
94 }
95 finally
96 {
97 System.setErr( origErr );
98 System.setOut( origOut );
99 }
100 }
101
102 }