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