View Javadoc
1   /*
2    * scriptable-dataset (https://github.com/hazendaz/scriptable-dataset)
3    *
4    * Copyright 2011-2024 Hazendaz.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   *
11   * Contributors:
12   *     Gunnar Morling
13   *     Hazendaz (Jeremy Landis).
14   */
15  package de.gmorling.scriptabledataset.handlers;
16  
17  import javax.script.ScriptEngine;
18  
19  /**
20   * Implementations can be registered with a ScriptableDataSet to be called before and after script contained in a data
21   * set field is executed. This can be used to add commonly used import statements for all scripts of a given language or
22   * to post-process the result of a script execution.
23   * <p>
24   * Implementations must define a default constructor, if they shall be used as standard handler for a language.
25   */
26  public interface ScriptInvocationHandler {
27  
28      /**
29       * Must return the name of the scripting language for which this handler can be registered, as expected by the JSR
30       * 223 scripting engine manager, e.g. "jruby".
31       *
32       * @return The name of the scripting language.
33       */
34      String getLanguageName();
35  
36      /**
37       * Will be called before a script contained in a field of a data set is executed.
38       *
39       * @param script
40       *            The script to be executed.
41       *
42       * @return The script to be executed, enriched with common imports for example.
43       */
44      String preInvoke(String script);
45  
46      /**
47       * Will be called after a script contained in a field of a data set is executed.
48       *
49       * @param object
50       *            The result of the script execution.
51       *
52       * @return The result of the script execution, possibly modified by this handler.
53       */
54      Object postInvoke(Object object);
55  
56      /**
57       * Makes the scripting engine available to handler implementations.
58       *
59       * @param engine
60       *            The scripting engine used to execute the current script.
61       */
62      void setScriptEngine(ScriptEngine engine);
63  }