Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Method wrappers usage



The API will consider a script as a class implementing a Java interface. The basic usage article explained how to use the library by creating a proxy which could be used exacly as you would do with the interface in ava code.

This article explain a more specific usage where you will define a wrapper for each method you want to implement.

Use case

Suppose that you want to allow users to implement a scripted implementation to compute a value, such as:
      public interface Script {
        public int computeResult(int value) {
        }
      }

API concepts


The API is based on two concepts:
  • The ScriptWrapper is a wrapper around a script implementing an interface
  • The ScriptMethodProxy is a wrapper around a method declared in the interface

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: Creating the ScriptWrapper is identical as for the basic usage. The code you must implement is very simple. For example with Groovy:
      public class MyGroovyScriptWrapper extends GroovyScriptWrapper<Script> {
      }
with Script being the interface you want to implement in the scripting language.

ScriptMethodProxy

The ScriptMethodProxy which is a wrapper 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 script defined in the use case:
      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);
        }
      }

Checking and converting the arguments

To check the validity of the arguments of the Script, you must declare the valid arguments for the method you want to use by calling the AbstractMethodProxy.checkDeclaredArguments(String, Class...) method.

For example, for the method declared previously, we can have:
      public class MethodWrapperImpl extends AbstractScriptMethodWrapper<Script, Integer> {

        public MethodWrapperImpl(ScriptWrapper<Script> wrapper) {
          super(wrapper);
          checkDeclaredArguments("computeResult", Integer.class);
        }

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

Runtime usage

Main Article: Script runtime usage

After you have created the two wrappers classes, you can now use them with:
      ScriptWrapper<Script> wrapper = new MyGroovyScriptWrapper();
      ScriptMethodWrapper<Script, Integer> methodWrapper = new MethodWrapperImpl(wrapper);
And install the script:
      File file = new File(<our script file>);
      wrapper.installScript(file);
and:
      int value = methodWrapper.executeMethod(10);

Coding the script

In our example,we could for example code the following groovy script:
      public int computeResult(int value) {
        return value * 10;
      }
You don't need to specify that this groovy script implements the Script interface. The framework will take care of that.

Using a new Script

You don't need to create a new ScriptWrapper if you want to change to script you use. Just call again the ScriptWrapper.installScript(File) method. For example this will work:
      wrapper = new MyGroovyScriptWrapper();
      ScriptMethodProxy<Script, Integer> methodProxy = new MethodWrapperImpl(wrapper);

      File file = new File(<our first script file>);
      wrapper.installScript(file);
      int value = methodWrapper.executeMethod(10);

      file = new File(<our second script file>);
      wrapper.installScript(file);
      int value = methodWrapper.executeMethod(10);

See also


Categories: api | general

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