The 'scripted' Creator

The scripted creator is declared by default by DWR as follows: <creator id="script" class="org.directwebremoting.create.ScriptedCreator"/>

This creator uses BSF to execute scripts that return some bean, for example:

<allow>
  ...
  <create creator="script" javascript="EmailValidator">
    <param name="class" value="org.apache.commons.validator.EmailValidator"/>
    <param name="language" value="beanshell"/>
    <param name="script">
      import org.apache.commons.validator.EmailValidator;
      return EmailValidator.getInstance();
    </param>
  </create>
  ...
</allow>

The script creator accepts the following parameters:

Parameter DWR Version Description
language 1.0 The language that we are scripting in. This string is passed to BSF. For example 'beanshell'. (required)
class 1.0 The type of object that is created. This is required for security reasons.
script 1.0 The script to execute which will return a Java Object to remote. (required unless scriptPath is used)
scriptPath 1.1 The path to a script stored in a file. (required unless script is used)
reloadable 1.1 Should the ScriptedCreator check for changes to scripts loaded using scriptPath? (optional, defaults to true)

Issues to be aware of

In order to use this creator, you will need to place into your WEB-INF/lib folder: a download of the BSF JAR file along with a jar file for whatever scripting language you wish to use.

If there is a class that the script creates where the scope is session (or application), and you change the script, The class in the session is no longer going to match the class generated by the script and errors will occur. The container doesn't have to be restarted but the user has to log out (or clear the session somehow) and log back in.

Also when the class param is not null and is used to create a new instance, it simply calls the class.newInstance() method. This is ok unless the script is creating a new class with certain parameters or calling certain functions to configure the class. Unfortunately, re-evaluating the script each time causes the issue above to happen every request.