1 package org.apache.maven.shared.scriptinterpreter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.FilenameUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.nio.file.Files;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Locale;
35 import java.util.Map;
36
37
38
39
40
41
42 public class ScriptRunner
43 {
44
45
46 private static final Logger LOG = LoggerFactory.getLogger( ScriptRunner.class );
47
48
49
50
51
52 private Map<String, ScriptInterpreter> scriptInterpreters;
53
54
55
56
57 private Map<String, Object> globalVariables;
58
59
60
61
62 private List<String> classPath;
63
64
65
66
67 private String encoding;
68
69
70
71
72 public ScriptRunner()
73 {
74 scriptInterpreters = new LinkedHashMap<>();
75 scriptInterpreters.put( "bsh", new BeanShellScriptInterpreter() );
76 scriptInterpreters.put( "groovy", new GroovyScriptInterpreter() );
77 globalVariables = new HashMap<>();
78 classPath = new ArrayList<>();
79 }
80
81
82
83
84
85
86
87 public void addScriptInterpreter( String id, ScriptInterpreter scriptInterpreter )
88 {
89 scriptInterpreters.put( id, scriptInterpreter );
90 }
91
92
93
94
95
96
97
98 public void setGlobalVariable( String name, Object value )
99 {
100 this.globalVariables.put( name, value );
101 }
102
103
104
105
106
107
108
109
110
111 public void setClassPath( List<String> classPath )
112 {
113 this.classPath = ( classPath != null ) ? new ArrayList<>( classPath ) : new ArrayList<String>();
114 }
115
116
117
118
119
120
121
122 public void setScriptEncoding( String encoding )
123 {
124 this.encoding = ( encoding != null && encoding.length() > 0 ) ? encoding : null;
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public void run( final String scriptDescription, final File basedir, final String relativeScriptPath,
140 final Map<String, ?> context, final ExecutionLogger logger )
141 throws IOException, ScriptException
142 {
143 if ( relativeScriptPath == null )
144 {
145 LOG.debug( "{}: relativeScriptPath is null, not executing script", scriptDescription );
146 return;
147 }
148
149 final File scriptFile = resolveScript( new File( basedir, relativeScriptPath ) );
150
151 if ( !scriptFile.exists() )
152 {
153 LOG.debug( "{} : no script '{}' found in directory {}", scriptDescription, relativeScriptPath,
154 basedir.getAbsolutePath() );
155 return;
156 }
157
158 LOG.info( "run {} {}.{}",
159 scriptDescription, relativeScriptPath, FilenameUtils.getExtension( scriptFile.getAbsolutePath() ) );
160
161 executeRun( scriptDescription, scriptFile, context, logger );
162 }
163
164
165
166
167
168
169
170
171
172
173
174 public void run( final String scriptDescription, File scriptFile, final Map<String, ?> context,
175 final ExecutionLogger logger )
176 throws IOException, ScriptException
177 {
178
179 if ( !scriptFile.exists() )
180 {
181 LOG.debug( "{} : script file not found in directory {}", scriptDescription, scriptFile.getAbsolutePath() );
182 return;
183 }
184
185 LOG.info( "run {} {}", scriptDescription, scriptFile.getAbsolutePath() );
186
187 executeRun( scriptDescription, scriptFile, context, logger );
188 }
189
190 private void executeRun( final String scriptDescription, File scriptFile,
191 final Map<String, ?> context, final ExecutionLogger logger )
192 throws IOException, ScriptException
193 {
194 ScriptInterpreter interpreter = getInterpreter( scriptFile );
195 if ( LOG.isDebugEnabled() )
196 {
197 String name = interpreter.getClass().getName();
198 name = name.substring( name.lastIndexOf( '.' ) + 1 );
199 LOG.debug( "Running script with {} :{}", name, scriptFile );
200 }
201
202 String script;
203 try
204 {
205 byte[] bytes = Files.readAllBytes( scriptFile.toPath() );
206 if ( encoding != null )
207 {
208 script = new String( bytes, encoding );
209 }
210 else
211 {
212 script = new String( bytes );
213 }
214 }
215 catch ( IOException e )
216 {
217 String errorMessage =
218 "error reading " + scriptDescription + " " + scriptFile.getPath() + ", " + e.getMessage();
219 throw new IOException( errorMessage, e );
220 }
221
222 Object result;
223 try
224 {
225 if ( logger != null )
226 {
227 logger.consumeLine( "Running " + scriptDescription + ": " + scriptFile );
228 }
229
230 PrintStream out = ( logger != null ) ? logger.getPrintStream() : null;
231
232 Map<String, Object> scriptVariables = new HashMap<>( this.globalVariables );
233 scriptVariables.put( "basedir", scriptFile.getParentFile() );
234 scriptVariables.put( "context", context );
235
236 result = interpreter.evaluateScript( script, classPath, scriptVariables, out );
237 if ( logger != null )
238 {
239 logger.consumeLine( "Finished " + scriptDescription + ": " + scriptFile );
240 }
241 }
242 catch ( ScriptEvaluationException e )
243 {
244 Throwable t = ( e.getCause() != null ) ? e.getCause() : e;
245 if ( logger != null )
246 {
247 t.printStackTrace( logger.getPrintStream() );
248 }
249 throw e;
250 }
251
252 if ( !( result == null || Boolean.parseBoolean( String.valueOf( result ) ) ) )
253 {
254 throw new ScriptReturnException( "The " + scriptDescription + " returned " + result + ".", result );
255 }
256 }
257
258
259
260
261
262
263
264
265 private File resolveScript( File scriptFile )
266 {
267 if ( scriptFile != null && !scriptFile.exists() )
268 {
269 for ( String ext : this.scriptInterpreters.keySet() )
270 {
271 File candidateFile = new File( scriptFile.getPath() + '.' + ext );
272 if ( candidateFile.exists() )
273 {
274 scriptFile = candidateFile;
275 break;
276 }
277 }
278 }
279 return scriptFile;
280 }
281
282
283
284
285
286
287
288
289
290 private ScriptInterpreter getInterpreter( File scriptFile )
291 {
292 String ext = FilenameUtils.getExtension( scriptFile.getName() ).toLowerCase( Locale.ENGLISH );
293 ScriptInterpreter interpreter = scriptInterpreters.get( ext );
294 if ( interpreter == null )
295 {
296 interpreter = scriptInterpreters.get( "bsh" );
297 }
298 return interpreter;
299 }
300
301 }