Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "SMILA/Documentation/Scripting"

Line 20: Line 20:
 
   return record;
 
   return record;
 
}
 
}
<pre>
+
</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.
 
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.
Line 38: Line 38:
 
   // ... more function descriptions
 
   // ... more function descriptions
 
]
 
]
<pre>
+
</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).
 
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).
Line 93: Line 93:
 
     ]
 
     ]
 
}
 
}
<pre>
+
</pre>
  
  
Line 113: Line 113:
 
     "url": "http://localhost:8080/smila/script/helloWorld.greetings/"
 
     "url": "http://localhost:8080/smila/script/helloWorld.greetings/"
 
}
 
}
<pre>
+
</pre>
  
 
==== Execute a script ====
 
==== Execute a script ====
Line 133: Line 133:
 
   "name": "Juergen"
 
   "name": "Juergen"
 
}
 
}
<pre>
+
</pre>
  
 
Response:
 
Response:
Line 141: Line 141:
 
     "greetings": "Hello Juergen!"
 
     "greetings": "Hello Juergen!"
 
}
 
}
<pre>
+
</pre>

Revision as of 11:27, 21 October 2014

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;
}

Script files to execute are added by default to SMILA/configuration/org.eclipse.smila.scripting/js. 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 ScriptingEngine.callScript() 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 <prefix>ScriptCatalog.js, e.g. smilaScriptCatalog.js and must have this format:

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

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 ScriptingEngine.listScripts() 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: SMILA -Dsmila.scripting.dir=/home/smila/js .... 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:__ http://<hostname>:8080/smila/script

Methods:

  • GET: list exposed scripts - show the result of ScriptingEngine.listScripts(). No parameters, no request body.

Response: result of ScriptingEngine.listScripts(), wrapped as a JSON object with property "scripts" containing the array of script descriptions:

{
    "scripts": [
        {
            "name": "helloWorld.greetings",
            "description": "Get a Hello from SMILA!",
            "url": "http://localhost:8080/smila/script/helloWorld.greetings/"
        }
    ]
}


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

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: GET /smila/script/helloWorld.greetings yields:

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

Execute a script

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

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:

POST http://localhost:8080/smila/script/helloWorld.greetings
{
  "name": "Juergen"
}

Response:

{
    "name": "Juergen",
    "greetings": "Hello Juergen!"
}

Copyright © Eclipse Foundation, Inc. All Rights Reserved.