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 public void run( final String scriptDescription, final File basedir, final String relativeScriptPath,
151 final Map context, final FileLogger logger, String stage )
152 throws MojoExecutionException, BuildFailureException
153 {
154 if ( relativeScriptPath == null )
155 {
156 return;
157 }
158
159 final File scriptFile = resolveScript( new File( basedir, relativeScriptPath ) );
160
161 if ( !scriptFile.exists() )
162 {
163 return;
164 }
165
166 Map globalVariables = new HashMap( this.globalVariables );
167 globalVariables.put( "basedir", basedir );
168 globalVariables.put( "context", context );
169
170 PrintStream out = ( logger != null ) ? logger.getPrintStream() : null;
171
172 ScriptInterpreter interpreter = getInterpreter( scriptFile );
173 if ( getLog().isDebugEnabled() )
174 {
175 String name = interpreter.getClass().getName();
176 name = name.substring( name.lastIndexOf( '.' ) + 1 );
177 getLog().debug( "Running script with " + name + ": " + scriptFile );
178 }
179
180 String script;
181 try
182 {
183 script = FileUtils.fileRead( scriptFile, encoding );
184 }
185 catch ( IOException e )
186 {
187 String errorMessage =
188 "error reading " + scriptDescription + " " + scriptFile.getPath() + ", " + e.getMessage();
189 throw new MojoExecutionException( errorMessage, e );
190 }
191
192 Object result;
193 try
194 {
195 if ( logger != null )
196 {
197 logger.consumeLine( "Running " + scriptDescription + ": " + scriptFile );
198 }
199 result = interpreter.evaluateScript( script, classPath, globalVariables, out );
200 if ( logger != null )
201 {
202 logger.consumeLine( "Finished " + scriptDescription + ": " + scriptFile );
203 }
204 }
205 catch ( ScriptEvaluationException e )
206 {
207 Throwable t = ( e.getCause() != null ) ? e.getCause() : e;
208 String msg = ( t.getMessage() != null ) ? t.getMessage() : t.toString();
209 if ( getLog().isDebugEnabled() )
210 {
211 String errorMessage = "Error evaluating " + scriptDescription + " " + scriptFile.getPath() + ", " + t;
212 getLog().debug( errorMessage, t );
213 }
214 if ( logger != null )
215 {
216 t.printStackTrace( logger.getPrintStream() );
217 }
218 throw new BuildFailureException( "The " + scriptDescription + " did not succeed. " + msg, stage );
219 }
220
221 if ( !( result == null || Boolean.TRUE.equals( result ) || "true".equals( result ) ) )
222 {
223 throw new BuildFailureException( "The " + scriptDescription + " returned " + result + ".", stage );
224 }
225 }
226
227
228
229
230
231
232
233
234 private File resolveScript( File scriptFile )
235 {
236 if ( scriptFile != null && !scriptFile.exists() )
237 {
238 for ( Iterator it = this.scriptInterpreters.keySet().iterator(); it.hasNext(); )
239 {
240 String ext = (String) it.next();
241 File candidateFile = new File( scriptFile.getPath() + '.' + ext );
242 if ( candidateFile.exists() )
243 {
244 scriptFile = candidateFile;
245 break;
246 }
247 }
248 }
249 return scriptFile;
250 }
251
252
253
254
255
256
257
258
259
260 private ScriptInterpreter getInterpreter( File scriptFile )
261 {
262 String ext = FileUtils.extension( scriptFile.getName() ).toLowerCase( Locale.ENGLISH );
263 ScriptInterpreter interpreter = (ScriptInterpreter) scriptInterpreters.get( ext );
264 if ( interpreter == null )
265 {
266 interpreter = (ScriptInterpreter) scriptInterpreters.get( "bsh" );
267 }
268 return interpreter;
269 }
270
271 }