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.ArrayList;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Map;
32
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.logging.Log;
35 import org.codehaus.plexus.util.FileUtils;
36 import org.codehaus.plexus.util.StringUtils;
37
38
39
40
41
42
43
44 class ScriptRunner
45 {
46
47
48
49
50 private Log log;
51
52
53
54
55
56 private Map scriptInterpreters;
57
58
59
60
61 private Map globalVariables;
62
63
64
65
66 private List classPath;
67
68
69
70
71 private String encoding;
72
73
74
75
76
77
78 public ScriptRunner( Log log )
79 {
80 if ( log == null )
81 {
82 throw new IllegalArgumentException( "missing logger" );
83 }
84 this.log = log;
85 scriptInterpreters = new LinkedHashMap();
86 scriptInterpreters.put( "bsh", new BeanShellScriptInterpreter() );
87 scriptInterpreters.put( "groovy", new GroovyScriptInterpreter() );
88 globalVariables = new HashMap();
89 classPath = new ArrayList();
90 }
91
92
93
94
95
96
97 private Log getLog()
98 {
99 return log;
100 }
101
102
103
104
105
106
107
108 public void setGlobalVariable( String name, Object value )
109 {
110 this.globalVariables.put( name, value );
111 }
112
113
114
115
116
117
118
119
120
121 public void setClassPath( List classPath )
122 {
123 this.classPath = ( classPath != null ) ? new ArrayList( classPath ) : new ArrayList();
124 }
125
126
127
128
129
130
131
132 public void setScriptEncoding( String encoding )
133 {
134 this.encoding = StringUtils.isNotEmpty( encoding ) ? encoding : null;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 public void run( final String scriptDescription, final File basedir, final String relativeScriptPath,
153 final Map context, final FileLogger logger, String stage, boolean failOnException )
154 throws MojoExecutionException, BuildFailureException
155 {
156 if ( relativeScriptPath == null )
157 {
158 return;
159 }
160
161 final File scriptFile = resolveScript( new File( basedir, relativeScriptPath ) );
162
163 if ( !scriptFile.exists() )
164 {
165 return;
166 }
167
168 Map globalVariables = new HashMap( this.globalVariables );
169 globalVariables.put( "basedir", basedir );
170 globalVariables.put( "context", context );
171
172 PrintStream out = ( logger != null ) ? logger.getPrintStream() : null;
173
174 ScriptInterpreter interpreter = getInterpreter( scriptFile );
175 if ( getLog().isDebugEnabled() )
176 {
177 String name = interpreter.getClass().getName();
178 name = name.substring( name.lastIndexOf( '.' ) + 1 );
179 getLog().debug( "Running script with " + name + ": " + scriptFile );
180 }
181
182 String script;
183 try
184 {
185 script = FileUtils.fileRead( scriptFile, encoding );
186 }
187 catch ( IOException e )
188 {
189 String errorMessage =
190 "error reading " + scriptDescription + " " + scriptFile.getPath() + ", " + e.getMessage();
191 throw new MojoExecutionException( errorMessage, e );
192 }
193
194 Object result;
195 try
196 {
197 if ( logger != null )
198 {
199 logger.consumeLine( "Running " + scriptDescription + ": " + scriptFile );
200 }
201 result = interpreter.evaluateScript( script, classPath, globalVariables, out );
202 if ( logger != null )
203 {
204 logger.consumeLine( "Finished " + scriptDescription + ": " + scriptFile );
205 }
206 }
207 catch ( ScriptEvaluationException e )
208 {
209 Throwable t = ( e.getCause() != null ) ? e.getCause() : e;
210 String msg = ( t.getMessage() != null ) ? t.getMessage() : t.toString();
211 if ( getLog().isDebugEnabled() )
212 {
213 String errorMessage = "Error evaluating " + scriptDescription + " " + scriptFile.getPath() + ", " + t;
214 getLog().debug( errorMessage, t );
215 }
216 if ( logger != null )
217 {
218 t.printStackTrace( logger.getPrintStream() );
219 }
220 if ( failOnException )
221 {
222 throw new BuildFailureException( "The " + scriptDescription + " did not succeed. " + msg, stage );
223 }
224 else
225 {
226 throw new BuildErrorException( "The " + scriptDescription + " did not succeed. " + msg, stage, t );
227 }
228 }
229
230 if ( !( result == null || Boolean.TRUE.equals( result ) || "true".equals( result ) ) )
231 {
232 throw new BuildFailureException( "The " + scriptDescription + " returned " + result + ".", stage );
233 }
234 }
235
236
237
238
239
240
241
242
243 private File resolveScript( File scriptFile )
244 {
245 if ( scriptFile != null && !scriptFile.exists() )
246 {
247 for ( Iterator it = this.scriptInterpreters.keySet().iterator(); it.hasNext(); )
248 {
249 String ext = (String) it.next();
250 File candidateFile = new File( scriptFile.getPath() + '.' + ext );
251 if ( candidateFile.exists() )
252 {
253 scriptFile = candidateFile;
254 break;
255 }
256 }
257 }
258 return scriptFile;
259 }
260
261
262
263
264
265
266
267
268
269 private ScriptInterpreter getInterpreter( File scriptFile )
270 {
271 String ext = FileUtils.extension( scriptFile.getName() ).toLowerCase( Locale.ENGLISH );
272 ScriptInterpreter interpreter = (ScriptInterpreter) scriptInterpreters.get( ext );
273 if ( interpreter == null )
274 {
275 interpreter = (ScriptInterpreter) scriptInterpreters.get( "bsh" );
276 }
277 return interpreter;
278 }
279
280 }