Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

ScriptWrapper



To be able to integrate the Script in you java application, you will need to create:
  • A wrapper around the interface which the script must implement: ScriptWrapper
  • A wrapper around the interface methods you want to use: ScriptMethodProxy

ScriptWrapper

The ScriptWrapper is a wrapper around a script seen as implementing an interface. A ScriptWrapper must be a subclass of the ScriptWrapper of the scripting language you want to use:

The code you must implement is very simple. For example, for the following interface in Groovy:
      public interface Script {
        public int computeResult(int value) {
        }
      }
The Groovy wrapper:
      public class MyGroovyScriptWrapper extends GroovyScriptWrapper<Script> {
      }
with Script being the interface you want to implement in the scripting language.

ScriptWrapper configuration


The ScriptWrapper has several methods allowing to configure it. For example:
  • Set the Script context which can be used by the scripts
  • Add a Exception listener to handle exceptions thrown during the execution of the scripts
  • Add a logger to log messages echoed during the execution of the scripts
  • Add custom default imports to the scripts
  • Set the ClassLoader to use with the wrapper
  • Configure the optimization level of the compilation for the wrappers which allow it

Script proxy

The most simple way to use the wrapper is to simply get a proxy object which will act as an implementation of the interface for the script. For example:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      Script Script = wrapper.getScript();
After installing the script, you will be able to use it exactly as the interface:
      File file = new File(<our script file>);
      wrapper.installScript(file);

      int value = script.computeResult(10);

Using a tagging interface

Main Article: Tagging interface

It is possible to use an interface without any method when creating yout wrapper. For example this one is perfectly valid:
      public interface Script {
      }

Combining wrappers

Main Article: ScriptWrapperCombiner

A ScriptWrapper only allows your script to implement one interface.

A ScriptWrapperCombiner allows the script to implement one interface among several. The combiner will get the most appropriate one for your script.

ScriptMethodWrapper

Main Article: ScriptMethodWrapper

The ScriptMethodProxy is a proxy around a method declared in the interface. Note that the ScriptMethodWrapper does not depend on the the scripting language you want to use.

There are two ScriptMethodWrapper implementations depending on the method you want to use: You should only implement the executeScriptImpl() method. For example, for the computeResult(int value) method of the following interface:
      public interface Script {
        public int computeResult(int value);
      }
you will have:
      public class MethodWrapperImpl extends AbstractScriptMethodWrapper<Script, Integer> {

        public MethodWrapperImpl(ScriptWrapper<Script> wrapper) {
          super(wrapper);
        }

        protected Integer executeScriptImpl(Object... arguments) {
          Integer i = (Integer)arguments[0];
          return script.computeResult(i);
        }
      }

See also


Categories: api

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence