Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Combining wrappers



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.

Use case

Suppose that you want to have a script implementing one of these two interfaces:
      public interface Script {
        public int computeResult(int value);
      }
and:
      public interface Script2 {
        public String getMessage();
      }
Normally you must define two different ScriptWrappers, one for each interface, and use the right one for each script. The combiner allows you to define only one wrapper and let the wrapper choose the right interface for you.

Creation of the ScriptWrapperCombiner

The creation of the combiner begins with creating the wrappers for each interface, as usual. In the above example, we would do for example:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      ScriptWrapper<Script> wrapper2 = new GroovyScriptWrapper<Script2>() {
      };
Then we will create a combiner and add the two wrappers to it:
      ScriptWrapperCombiner combiner = new ScriptWrapperCombiner();
      combiner.addScriptWrapper(wrapper);
      combiner.addScriptWrapper(wrapper2);

Usage

The combiner is a regular ScriptWrapper. Now we can install a script as for any ScriptWrapper. For example:
      File file = new File(<our script file>);
      wrapper.installScript(file);
The only difference from the regular wrappers is that the ScriptWrapper.getScript() will only return a non null result after the script has been installed, because the effective interface depends on the script signature. For example, suppose the following script:
      public getMessage() {
        return "Hello World!";
      }
Then after installing the script, the following code will print "Hello World!" on the console:
      Script2 script = (Script2)wrapper.getProxy();
      String message = script.getMessage();
      System.out.println(message);

Wrapper detection details

The detection of the appropriate wrapper is performed in the order of the list provided to the combiner. It means that if the script implements several interfaces, the first one found will be chosen.

It no appropriate wrapper is found, an exception will be fired exactly as for regular wrappers, for the last one which was not found in the list.

See also


Categories: api

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