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 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 PrintStream origOut = System.out;
52 PrintStream origErr = System.err;
53
54 try
55 {
56 CompilerConfiguration config = new CompilerConfiguration( CompilerConfiguration.DEFAULT );
57
58 if ( scriptOutput != null )
59 {
60 System.setErr( scriptOutput );
61 System.setOut( scriptOutput );
62 config.setOutput( new PrintWriter( scriptOutput ) );
63 }
64
65 ClassLoader loader = null;
66 if ( classPath != null && !classPath.isEmpty() )
67 {
68 AntClassLoader childFirstLoader = new AntClassLoader( getClass().getClassLoader(), false );
69 for ( Iterator it = classPath.iterator(); it.hasNext(); )
70 {
71 String path = (String) it.next();
72 childFirstLoader.addPathComponent( new File( path ) );
73 }
74 loader = childFirstLoader;
75 }
76
77 Binding binding = new Binding( globalVariables );
78
79 GroovyShell interpreter = new GroovyShell( loader, binding, config );
80
81 try
82 {
83 return interpreter.evaluate( script );
84 }
85 catch ( Exception e )
86 {
87 throw new ScriptEvaluationException( e );
88 }
89 }
90 finally
91 {
92 System.setErr( origErr );
93 System.setOut( origOut );
94 }
95 }
96
97 }