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 java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.io.PrintStream; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
import bsh.EvalError; |
30 | |
import bsh.Interpreter; |
31 | |
import bsh.TargetError; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 2 | class BeanShellScriptInterpreter |
40 | |
implements ScriptInterpreter |
41 | |
{ |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public Object evaluateScript( String script, List classPath, Map globalVariables, PrintStream scriptOutput ) |
47 | |
throws ScriptEvaluationException |
48 | |
{ |
49 | 2 | PrintStream origOut = System.out; |
50 | 2 | PrintStream origErr = System.err; |
51 | |
|
52 | |
try |
53 | |
{ |
54 | 2 | Interpreter engine = new Interpreter(); |
55 | |
|
56 | 2 | if ( scriptOutput != null ) |
57 | |
{ |
58 | 2 | System.setErr( scriptOutput ); |
59 | 2 | System.setOut( scriptOutput ); |
60 | 2 | engine.setErr( scriptOutput ); |
61 | 2 | engine.setOut( scriptOutput ); |
62 | |
} |
63 | |
|
64 | 2 | if ( classPath != null && !classPath.isEmpty() ) |
65 | |
{ |
66 | 0 | for ( Iterator it = classPath.iterator(); it.hasNext(); ) |
67 | |
{ |
68 | 0 | String path = (String) it.next(); |
69 | |
try |
70 | |
{ |
71 | 0 | engine.getClassManager().addClassPath( new File( path ).toURI().toURL() ); |
72 | |
} |
73 | 0 | catch ( IOException e ) |
74 | |
{ |
75 | 0 | throw new RuntimeException( "bad class path: " + path, e ); |
76 | 0 | } |
77 | |
} |
78 | |
} |
79 | |
|
80 | 2 | if ( globalVariables != null ) |
81 | |
{ |
82 | 1 | for ( Iterator it = globalVariables.keySet().iterator(); it.hasNext(); ) |
83 | |
{ |
84 | 1 | String variable = (String) it.next(); |
85 | 1 | Object value = globalVariables.get( variable ); |
86 | |
try |
87 | |
{ |
88 | 1 | engine.set( variable, value ); |
89 | |
} |
90 | 0 | catch ( EvalError e ) |
91 | |
{ |
92 | 0 | throw new RuntimeException( e ); |
93 | 2 | } |
94 | |
} |
95 | |
} |
96 | |
|
97 | |
try |
98 | |
{ |
99 | 2 | return engine.eval( script ); |
100 | |
} |
101 | 0 | catch ( TargetError e ) |
102 | |
{ |
103 | 0 | throw new ScriptEvaluationException( e.getTarget() ); |
104 | |
} |
105 | 0 | catch ( Exception e ) |
106 | |
{ |
107 | 0 | throw new ScriptEvaluationException( e ); |
108 | |
} |
109 | |
} |
110 | |
finally |
111 | |
{ |
112 | 2 | System.setErr( origErr ); |
113 | 2 | System.setOut( origOut ); |
114 | |
} |
115 | |
} |
116 | |
|
117 | |
} |