1 package org.apache.maven.plugins.scripting;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.UncheckedIOException;
26
27 import javax.script.ScriptContext;
28 import javax.script.ScriptEngine;
29 import javax.script.ScriptEngineManager;
30 import javax.script.ScriptException;
31
32 /**
33 * Evaluates a script held in a file. Use the engine name to override the engine if the file name does not refer/decode
34 * to a valid engine name or not define any at all.
35 * @author Rusi Popov
36 */
37 public class FileScriptEvaluator extends AbstractScriptEvaluator
38 {
39
40 /**
41 * Not null, existing readable file with the script
42 */
43 private final File scriptFile;
44
45 /**
46 * Possibly null engine name
47 */
48 private final String engineName;
49
50 /**
51 * @param engineName optional engine name, used to override the engine selection from the file extension
52 * @param scriptFile not null
53 */
54 public FileScriptEvaluator( String engineName, File scriptFile )
55 {
56 this.scriptFile = scriptFile;
57
58 this.engineName = engineName;
59 }
60
61 /**
62 * @param engine the script engine.
63 * @param context the script context.
64 * @return the result of the scriptFile.
65 * @throws ScriptException if an error occurs in script.
66 * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#eval(javax.script.ScriptEngine, javax.script.ScriptContext)
67 */
68 protected Object eval( ScriptEngine engine, ScriptContext context ) throws ScriptException
69 {
70 try ( FileReader reader = new FileReader( scriptFile ) )
71 {
72 return engine.eval( reader, context );
73 }
74 catch ( IOException ex )
75 {
76 throw new UncheckedIOException( scriptFile + " caused:", ex );
77 }
78 }
79
80 /**
81 * Gets the script engine by engineName, otherwise by extension of the sciptFile
82 *
83 * @param manager the script engine manager
84 * @throws UnsupportedScriptEngineException if specified engine is not available
85 * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#getEngine(javax.script.ScriptEngineManager)
86 */
87 protected ScriptEngine getEngine( ScriptEngineManager manager ) throws UnsupportedScriptEngineException
88 {
89 ScriptEngine result;
90
91 if ( engineName != null && !engineName.isEmpty() )
92 {
93 result = manager.getEngineByName( engineName );
94
95 if ( result == null )
96 {
97 throw new UnsupportedScriptEngineException( "No engine found by name \"" + engineName + "\n" );
98 }
99 }
100 else
101 {
102 String extension = scriptFile.getName();
103 int position = extension.indexOf( "." );
104
105 if ( position >= 0 )
106 {
107 extension = extension.substring( position + 1 );
108 }
109 result = manager.getEngineByExtension( extension );
110
111 if ( result == null )
112 {
113 throw new UnsupportedScriptEngineException( "No engine found by extension \"" + extension + "\n" );
114 }
115 }
116 return result;
117 }
118 }