15 darwino script & command line
TRANSCRIPT
Darwino Script and Command Line
Add customization capabilities to your Darwino applications
Darwino Script Introduction
• Darwino Script is a scripting language, very similar to JavaScript– Follows ECMA-Script 262-3 for a lot of behaviors– Designed as a “formula language” rather than a language for writing programs– Extended for ease of use: new operators like ?. or ?[, @functions…– Uses the Java primitives as core language primitives, include BigDecimal– Objects are Java objects and don’t have prototypes
• Although a custom library can easily provide this feature• Ex: JsonArray & JsonObject
– Tailored to script Java classes/objects• Can directly call any Java classes/objects, if security permits• Can easily be extended through java extension points
Darwino Script Introduction
• Low resource consumption, even on mobile devices– Fast compiler with caching capability
• Dynamic execution: interprets an AST tree in memory– No class file is generated, so it does not require a JVM (AOT compiler works)
• Can be transpiled to other languages (Java, JavaScript, AngularJS expressions…)
Darwino Script Usage
• Script language for citizen developers– Can be interpreted dynamically or transpiled (see: UI generation)
• End user customization– Workflow engine conditions, …– Other customizations done by a user
• Database events, …
Darwino Script Main Objects
• DSEnvironment– Maintains the global scripting options, libraries and variables– Shared across script executions
• DSContext– The execution context for a script execution– Contains the local variables, and execution options
• DSScript– An in-memory compiled script, ready to be executed
• DSLibrary– Define a set of functions, classes or value members (including constants)
Executing a Piece of Script// Create the execution environment// This environment holds the libraries to use// - standard (print, println..)// - JavaJre (access to Java Object using reflection)DSEnvironment env = new DSEnvironment();env.registerLibrary(new StandardLibrary());env.registerLibrary(new JavaJreLibrary(env));
// The context can also take extra parameters:// this// library to use for this context// Note that the context contains the global variables being created by the script// and can also be reused, with these values, while executing another scriptDSProgramRuntimeContext ctx = new DSProgramRuntimeContext(env);
// Compile the script or get it from the cacheDSScript expr = ctx.getEnvironment().createScript("1+4");
// Execute and get the resultObject result = expr.execute(ctx);
assertEquals(5, result);
Creating a Reusable Library
• A library can export– Member access (global values, object members…)– Function calls (global function, object method…)– O bject/Array constructors
public interface DSLibrary {
public Object getMember(DSRuntimeContext context, Object instance, String member) throws DSRuntimeException;
public Object getMember(DSRuntimeContext context, Object instance, int index) throws DSRuntimeException;
public boolean putMember(DSRuntimeContext context, Object instance, String member, Object value) throws DSRuntimeException;
public boolean putMember(DSRuntimeContext context, Object instance, int index, Object value) throws DSRuntimeException;
public Object call(DSRuntimeContext context, Object instance, Object[] parameters) throws DSRuntimeException;
public Object constructObject(DSRuntimeContext context, String type, Object[] parameters) throws DSRuntimeException;
public Object constructArray(DSRuntimeContext context, String type, int dimensions, int size) throws DSRuntimeException;}
Existing Libraries
• Java bridge• @function
– Similar to Notes/Domino @functions– Targets citizen developers
• Java Objects cab implement– DSObject, DSArray, DSCallable for easy library creationb
A Word About Security
• If the script is going to be exposed to end users– You should control what library is exposed
• Use reflection to access Java object?– You should fine control what object can be exposed/called from the script
• Can also depend on the user profile
• All the features are exposed through libraries, so it is easy to control– But Darwino will make it even easier with a new security interface
Darwino Command Framework
• The Darwino command framework allows the execution of text based commands
• The lists of commands is contributed via extensions• Generally used to exposed administration/debugging capabilities
Some Available Commands
• Built-in commands libraries are provided by the runtime– Default: echo, set, call, profiler, classpath…– JSON Store: manage databases, access data…– Runtime: access to user
• And more to come over time– Access to log files– Java logging API settings
Darwino Command CLI
• Commands can be executed from a simple console shell– Provided as a Java class in the dwo-apps-jstore-cli project
• Configuration file is executed when the console is launched~/.darwino/console-init.dsh
• Example:
# Initialize with TOMCAT environmentclasspath c:\phildev\apache-tomcat-8.0.20\lib\sqljdbc42.jarclasspath c:\phildev\apache-tomcat-8.0.20\lib\postgresql-9.4-1203-jdbc4.jarload-config c:\phildev\apache-tomcat-8.0.20
Darwino Debug Web UI
• A simple WebUI can be added to any Darwino Web application– Access to Commands, Profiler, Script…– Just add the maven dependency to the web project
• For security reasons, it requires a property to be explicitly set– Commands can execute code that inspect, create or delete sensitive data
# Enabled the debug moduledwo-runtime-debug-enable=true# only admins - this is the default#dwo-runtime-debug-users=admin
Darwino Debug Web UI - Demo
Thank you for your attention!