/home/projects/magnet/reference/core/script

script

Overview

The <script> element can be used to define any type of processing Magnet has to do before starting the process(es) it contains. The content of this element holds a snippet to be interpreted by a scripting engine - Magnet can potentially support any type of scripting language that can run in the JVM: Python (rather, Jython), Javascript (Rhino), Ruby (JRuby)... Since Magnet is a Java application, it was only natural to have built-in support for BeanShell - a scripting language that can be considered an interpreted Java.

To execute a script, you need to specify in the <script> element what type of scripting engine should be used to process the content. To use the BeanShell engine, you need to use the bsh type. The <script> element is profile-aware. Using that element's profile attribute, you can specify for which profile a given <script> element must be rendered and executed. If the profile attribute is not assigned, then the script will be executed if there is no other <script> element for the specified profile at runtime. Finally, you can control the behavior of the script when an error occurs during its interpretation by the scripting engine. Using the isAbortingOnError boolean attribute, you can stop Magnet's execution (the attribute's value must then be true), or you can tell Magnet to simply log a warning message and resume its execution (by specifying false as the value).

Attributes

Name Description Required Interpolation
type The type of scripting engine to use to process the script. Use bsh for BeanShell,or any other type of value for which a script handler was registered. yes no
profile The name of the profile for which the script must be executed. If the attribute is not provided, the script becomes the "default" script that is executed only if no other script exists that matches the execution profile. no no
isAbortingOnError This attribute defines how Magnet behaves when an error occurs executing the script. If the value is false, Magnet logs an error and stops the execution. If the value is false Magnet logs a warning message and resumes its execution. By default the value is false. no yes

Rendering Operations

When a <script> element is rendered, it performs the following operations in order:

  1. Renders the isAbortingOnError attribute (using variable interpolation).
  2. Renders the text of the <script> element (using variable interpolation). This allows one to put variables within the script itself and let Magnet render these variables before the scripting engine encounters them.
  3. Executes the script using the appropriate script handler (based on the type).

Examples

This first example uses BeanShell to print a simple message to standard output:

<magnet xmlns:magnet="http://schemas.sapia-oss.org/magnet/"
        name="ScriptExample1"
        description="This is a first script example.">
...
    <script type="bsh">
        System.out.println("A message from BeanShell");
    </script>
...
</magnet>

This second example uses BeanShell to create a new directory at startup. It shows how you can use the profile attribute to perform conditionnal execution. If the "test" profile is executed, then the second script element will create a new directory using the current time with the prefix "test", otherwise the first script element will create a new directory using the current time. In both cases, Magnet will stop its execution if an error occurs creating the new directory beacause the attribute isAbortingOnError is set to true.

<magnet xmlns:magnet="http://schemas.sapia-oss.org/magnet/"
        name="ScriptExample2"
        description="This is a second script example.">
...
    <script type="bsh" isAbortingOnError="true">
        File aFile = new File(System.getProperty("user.dir"),
             String.valueOf(System.currentTimeMillis()));
        aFile.mkdirs();
    </script>

    <script type="bsh" profile="test" isAbortingOnError="true">
        File aFile = new File(System.getProperty("user.dir"),
             "test" + System.currentTimeMillis());
        aFile.mkdirs();
    </script>
...
</magnet>

This third example shows how you can use the variable replacement feature of magnet within a script. It's a variation of the previous example were the explicit calls to System.getProperty() are replaced by variables.

<magnet xmlns:magnet="http://schemas.sapia-oss.org/magnet/"
        name="ScriptExample2"
        description="This is a second script example.">
...
    <script type="bsh" isAbortingOnError="true">
        File aFile = new File("${user.dir}"), String.valueOf(System.currentTimeMillis()));
        aFile.mkdirs();
    </script>

    <script type="bsh" profile="test" isAbortingOnError="true">
        File aFile = new File("${user.dir}"), "test" + System.currentTimeMillis());
        aFile.mkdirs();
    </script>
...
</magnet>