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