Skip to main content
Jump to: navigation, search

SMILA/Documentation/Scripting

Scripting SMILA using Javascript

Work In Progress

Service Description

  • Bundles: org.eclipse.smila.scripting(.test)
  • OSGi service interface: org.eclipse.smila.scripting.ScriptingEngine
  • Service implementation: org.eclipse.smila.scripting.internal.JavascriptEngine

The ScriptingEngine provides an alternative for describing "synchronous workflows" by using Javascript functions instead of BPEL processes. This approach is easier, more flexible and more maintainable (e.g. debugable), so one day the BPEL approach might be removed completely.

Scripts Basics

A javascript function for SMILA scripting takes one record (including attachments) as an argument, and can return one record (other return types are supported, too and wrapped in a record automatically). For example, a file helloWorld.js (the suffix must be ".js") could look like this:

function greetings(record) {
  record.metadata.greetings = "Hello " + record.metadata.name + "!";
  return record;
}
<pre>

Script files to execute are added by default to <tt>SMILA/configuration/org.eclipse.smila.scripting/js</tt>. They are currently loaded "on-demand" and not stored in the service for reuse, so changes in the files will be effective for the next execution.

A script is invoked using the <tt>ScriptingEngine.callScript()</tt> methods. The first argument of both methods is a "scriptName" string in format "<file>.<function>" where the <file> part is the name of the script file (without path and ".js" suffix) and the <function> part is the name of a function defined in this file.

==== Exposing script functions ====

The script directory can contain "script catalog" files. They can be used to expose and describe available scripts in the ReST API so that a client can detect available scripts. Such a file must be named <tt><prefix>ScriptCatalog.js</tt>, e.g. <tt>smilaScriptCatalog.js</tt> and must have this format:

<pre>
[
  {
    name: "helloWorld.greetings",
    description: "Get a Hello from SMILA!"
  },
  // ... more function descriptions
]
<pre>

A catalog file does not define functions, it just produces an array of script function descriptions. A description object must contain a "name" property, we recommend to include a "description" property. Other properties can be added as you like (e.g. a structured description of expected parameters in the passed record).

The <tt>ScriptingEngine.listScripts()</tt> method merges the arrays produced by all catalog scripts into one array (elements that are not objects or do not have a "name" property are ignored) and sorts them by name.

The name property must be in format <file>.<function>, as described above for the scriptName parameter of the callScripts() functions. 

==== Configuration ====

* The script directory can be changed on startup using a system property: <tt>SMILA -Dsmila.scripting.dir=/home/smila/js ...</tt>. The system property can also be added to SMILA.ini, of course.


=== Scripting Features ("SDK") ===

==== Working with Records ====

'''TODO'''

==== Accessing OSGi services ====

'''TODO'''

==== Using Pipelets ====

'''TODO'''

==== Loading other scripts ====

'''TODO'''

==== Logging ====

'''TODO'''

=== HTTP REST API ===

==== Manage Scripts ====

__URL:__ <tt>http://<hostname>:8080/smila/script</tt>

Methods:
* GET: list exposed scripts - show the result of <tt>ScriptingEngine.listScripts()</tt>. No parameters, no request body. 

Response: result of <tt>ScriptingEngine.listScripts()</tt>, wrapped as a JSON object with property "scripts" containing the array of script descriptions:
<pre>
{
    "scripts": [
        {
            "name": "helloWorld.greetings",
            "description": "Get a Hello from SMILA!",
            "url": "http://localhost:8080/smila/script/helloWorld.greetings/"
        }
    ]
}
<pre>


__URL:__ <tt>http://<hostname>:8080/smila/script/<scriptfile>.<function></tt>

Methods:
* GET: show script description. No parameters, no request body.

Response-Codes
* 200 OK: Success
* 404 Not Found: Function is not exposed in any ScriptCatalog file.

Example: <tt>GET /smila/script/helloWorld.greetings</tt> yields:

<pre>
{
    "name": "helloWorld.greetings",
    "description": "Get a Hello from SMILA!",
    "url": "http://localhost:8080/smila/script/helloWorld.greetings/"
}
<pre>

==== Execute a script ====

__URL:__ <tt>http://<hostname>:8080/smila/script/<script-file>.<function></tt>

Methods:
* POST: execute script with record in request body. Attachments are supported, too.

Response-Codes
* 200 OK: Script executed successfully.
* 400 Bad Request: Last URL part does not have <script-file>.<function> format, or error in Script execution
* 404 Not Found: Script file does not exist or does not contain the function

Example request:
<pre>
POST http://localhost:8080/smila/script/helloWorld.greetings
{
  "name": "Juergen"
}
<pre>

Response:
<pre>
{
    "name": "Juergen",
    "greetings": "Hello Juergen!"
}
<pre>

Back to the top