# BeanShell interpreter environment

The most common use of BeanShell scripts in the EAC Development Toolkit is to orchestrate the elements defined in the application configuration document.

More precisely, BeanShell scripts are used to orchestrate the execution of methods on the objects that are loaded from the configuration document. In order to enable this, when the toolkit constructs the BeanShell Interpreter environment, it sets internal variables associated with each element defined in the configuration document. While additional variables can be declared at any point in a script, this allows scripts to immediately act on objects defined in the document without declaring any variables.

Take, for example, the following configuration document:

    <app appName="myApp" eacHost="devhost.company.com" eacPort="8888"
        dataPrefix="myApp" sslEnabled="false" lockManager="LockManager" >
      <working-dir>C:\Endeca\apps\myApp</working-dir>
      <log-dir>./logs/baseline</log-dir>
    </app>

    <host id="ITLHost" hostName="itlhost.company.com" port="8888" />

    <copy id="CopyData" src-host-id="ITLHost" dest-host-id="ITLHost"
        recursive="true" >
      <src>./data/incoming/*.txt</src>
      <dest>./data/processing/</dest>
    </copy>

    <backup id="ArchiveState" host-id="ITLHost" move="true" num-backups="5">
      <dir>C:\Endeca\apps\myApp\data\state</dir>
    </backup>

    <forge id="Forge" host-id="ITLHost">
      <properties>
        <property name="numStateBackups" value="10" />
        <property name="numLogBackups" value="10" />
      </properties>
      <directories>
        <directory name="incomingDataDir">./data/incoming</directory>
        <directory name="configDir">./data/processing</directory>
      </directories>
      <args>
        <arg>-vw</arg>
      </args>
      <input-dir>./data/processing</input-dir>
      <output-dir>./data/forge_output</output-dir>
      <state-dir>./data/state</state-dir>
      <temp-dir>./data/temp</temp-dir>
      <num-partitions>1</num-partitions>
      <pipeline-file>./data/processing/pipeline.epx</pipeline-file>
    </forge>

A BeanShell script defined in this document will have five variables immediately available for use: `ITLHost`, `CopyData`, `ArchiveState`, `Forge`, and `log`. Note that there is no variable associated with the `app` element in the document, as this element does not correspond to an object instance. Each of the other elements is instantiated, loaded with data based on its configuration and made available in the BeanShell interpreter. In addition, a special variable called log is always created for each script with a `java.util.Logger` instance.

A simple BeanShell script can then be written without importing a single class or instantiating a single variable.

    <script id="SimpleForgeScript">
      <bean-shell-script>
        <![CDATA[
      log.info("Starting Forge script.");
      CopyData.run();
      Forge.run();
      ArchiveState.setNumBackups(Forge.getProperty("numStateBackups"));
      ArchiveState.run();
      log.info("Finished Forge script.");
        ]]>
      </bean-shell-script>
    </script>

In addition to exposing objects defined in the document, the toolkit imports and executes a default script each time a BeanShell script is invoked. If a file named "`beanshell.imports`" is successfully loaded as a classpath resource, that file is executed each time a BeanShell script is executed. This allows a default set of imports to be defined. For example, the following default file imports all of the classes in the toolkit, exposing them to BeanShell scripts:

    import com.Endeca.soleng.eac.toolkit.*;
    import com.Endeca.soleng.eac.toolkit.application.*;
    import com.Endeca.soleng.eac.toolkit.base.*;
    import com.Endeca.soleng.eac.toolkit.component.*;
    import com.Endeca.soleng.eac.toolkit.component.cluster.*;
    import com.Endeca.soleng.eac.toolkit.exception.*;
    import com.Endeca.soleng.eac.toolkit.host.*;
    import com.Endeca.soleng.eac.toolkit.logging.*;
    import com.Endeca.soleng.eac.toolkit.script.*;
    import com.Endeca.soleng.eac.toolkit.utility.*;
    import com.Endeca.soleng.eac.toolkit.utility.perl.*;
    import com.Endeca.soleng.eac.toolkit.utility.webstudio.*;
    import com.Endeca.soleng.eac.toolkit.utility.wget.*;
    import com.Endeca.soleng.eac.toolkit.utils.*;