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.IOException;
27  import java.io.InputStream;
28  import java.io.InputStreamReader;
29  import java.io.Reader;
30  import java.io.UncheckedIOException;
31  
32  /**
33   * Evaluates a script held in a resource. Use the engine name to override the engine if the resource name does not
34   * refer/decode to a valid engine name or not define any at all.
35   *
36   * @author Robert Scholte
37   */
38  public class ResourceScriptEvaluator extends AbstractScriptEvaluator {
39  
40      /**
41       * Not null, existing readable file with the script
42       */
43      private final String resourceName;
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 resourceName not null
53       */
54      public ResourceScriptEvaluator(String engineName, String resourceName) {
55          this.resourceName = resourceName;
56  
57          this.engineName = engineName;
58      }
59  
60      /**
61       * @param engine the script engine.
62       * @param context the script context.
63       * @return the result of the scriptFile.
64       * @throws ScriptException if an error occurs in script.
65       * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#eval(javax.script.ScriptEngine, javax.script.ScriptContext)
66       */
67      protected Object eval(ScriptEngine engine, ScriptContext context) throws ScriptException {
68  
69          try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(resourceName);
70                  Reader reader = new InputStreamReader(is)) {
71              return engine.eval(reader, context);
72          } catch (IOException ex) {
73              throw new UncheckedIOException(resourceName + " caused:", ex);
74          }
75      }
76  
77      /**
78       * Gets the script engine by engineName, otherwise by extension of the sciptFile
79       *
80       * @param manager the script engine manager
81       * @throws UnsupportedScriptEngineException if specified engine is not available
82       * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#getEngine(javax.script.ScriptEngineManager)
83       */
84      protected ScriptEngine getEngine(ScriptEngineManager manager) throws UnsupportedScriptEngineException {
85          ScriptEngine result;
86  
87          if (engineName != null && !engineName.isEmpty()) {
88              result = manager.getEngineByName(engineName);
89  
90              if (result == null) {
91                  throw new UnsupportedScriptEngineException("No engine found by name \"" + engineName + "\n");
92              }
93          } else {
94              String name = resourceName;
95              int fileSepIndex = name.lastIndexOf('/');
96              if (fileSepIndex >= 0) {
97                  name = name.substring(fileSepIndex + 1);
98              }
99  
100             String extension = name;
101             int position = name.indexOf(".");
102             if (position >= 0) {
103                 extension = name.substring(position + 1);
104             }
105             result = manager.getEngineByExtension(extension);
106 
107             if (result == null) {
108                 throw new UnsupportedScriptEngineException("No engine found by extension \"" + extension + "\n");
109             }
110         }
111         return result;
112     }
113 }