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 javax.script.ScriptContext;
23 import javax.script.ScriptEngine;
24 import javax.script.ScriptEngineManager;
25 import javax.script.ScriptException;
26
27 /**
28 * Evaluates a script held in a string
29 * @author Rusi Popov
30 */
31 public class StringScriptEvaluator extends AbstractScriptEvaluator
32 {
33
34 /**
35 * Not null name of the engine to execute the script
36 */
37 private final String engineName;
38
39 /**
40 * The non-null script itself
41 */
42 private final String script;
43
44 /**
45 * @param engineName the engine name
46 * @param script the script
47 * @throws IllegalArgumentException if either engineName or script is null
48 */
49 public StringScriptEvaluator( String engineName, String script )
50 {
51 if ( engineName == null || engineName.isEmpty() )
52 {
53 throw new IllegalArgumentException( "Expected a non-empty engine name provided" );
54 }
55 this.engineName = engineName;
56
57 if ( script == null || script.trim().isEmpty() )
58 {
59 throw new IllegalArgumentException( "Expected a non-empty script provided" );
60 }
61 this.script = script;
62 }
63
64 /**
65 * @param manager the script engine manager.
66 * @throws UnsupportedScriptEngineException if the engineName is not supported
67 * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#getEngine(javax.script.ScriptEngineManager)
68 */
69 protected ScriptEngine getEngine( ScriptEngineManager manager ) throws UnsupportedScriptEngineException
70 {
71 ScriptEngine result = manager.getEngineByName( engineName );
72
73 if ( result == null )
74 {
75 throw new UnsupportedScriptEngineException( "Unknown engine specified with name \"" + engineName + "\"" );
76 }
77 return result;
78 }
79
80 /**
81 * @param engine the script engine.
82 * @param context the script context.
83 * @throws ScriptException if an error occurs in script.
84 * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#eval(javax.script.ScriptEngine, javax.script.ScriptContext)
85 */
86 protected Object eval( ScriptEngine engine, ScriptContext context ) throws ScriptException
87 {
88 return engine.eval( script, context );
89 }
90 }