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.Bindings;
22  import javax.script.ScriptContext;
23  import javax.script.ScriptEngine;
24  import javax.script.ScriptEngineManager;
25  import javax.script.ScriptException;
26  
27  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.plugins.scripting.engine.ContextAwareEngine;
29  
30  /**
31   * Evaluates a script in the appropriate context and return its possibly null result
32   * @author Rusi Popov
33   */
34  abstract class AbstractScriptEvaluator {
35  
36      /**
37       * @param bindings not null bindings to provide to the script to execute
38       * @param log engine logger if context aware
39       * @return the possibly null result the script produced
40       * @throws UnsupportedScriptEngineException when the engine is not configured correctly
41       * @throws ScriptException  if an error occurs in script.
42       */
43      protected final Object eval(Bindings bindings, Log log) throws ScriptException, UnsupportedScriptEngineException {
44          ScriptEngineManager manager = new ScriptEngineManager();
45          ScriptEngine engine = getEngine(manager);
46          if (engine instanceof ContextAwareEngine) {
47              ((ContextAwareEngine) engine).setLog(log);
48          }
49          ScriptContext context = engine.getContext();
50  
51          context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
52  
53          return eval(engine, context);
54      }
55  
56      /**
57       * AbstractScriptEvaluator the script
58       * @param engine not null
59       * @param context not null, initialized
60       * @return possibly null result of the script
61       * @throws ScriptException  if an error occurs in script.
62       */
63      protected abstract Object eval(ScriptEngine engine, ScriptContext context) throws ScriptException;
64  
65      /**
66       * @param manager not null
67       * @return non-null engine to execute the script
68       * @throws UnsupportedScriptEngineException when no engine could be identified
69       */
70      protected abstract ScriptEngine getEngine(ScriptEngineManager manager) throws UnsupportedScriptEngineException;
71  }