Darwino Script and Command Line provides capabilities for customizing Darwino applications through scripting and commands. Darwino Script is an interpreted scripting language similar to JavaScript that can directly call Java classes and is optimized for low resource usage. It can be used by citizen developers for customizations and end user configurations. The Darwino command framework allows executing text commands that are contributed via extensions to perform administration and debugging tasks. A simple CLI and web UI are provided to execute Darwino Scripts and Commands.
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
15 darwino script & command line
1. Darwino Script and Command Line
Add customization capabilities to your Darwino applications
2. 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
3. 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…)
4. 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, …
5. 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)
6. 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 script
DSProgramRuntimeContext ctx = new DSProgramRuntimeContext(env);
// Compile the script or get it from the cache
DSScript expr = ctx.getEnvironment().createScript("1+4");
// Execute and get the result
Object result = expr.execute(ctx);
assertEquals(5, result);
7. Creating a Reusable Library
• A library can export
– Member access (global values, object members…)
– Function calls (global function, object method…)
– Object/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;
}
8. Existing Libraries
• Java bridge
• @function
– Similar to Notes/Domino @functions
– Targets citizen developers
• Java Objects cab implement
– DSObject, DSArray, DSCallable for easy library creationb
9. 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
10. 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
11. 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
12. 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 environment
classpath c:phildevapache-tomcat-8.0.20libsqljdbc42.jar
classpath c:phildevapache-tomcat-8.0.20libpostgresql-9.4-1203-jdbc4.jar
load-config c:phildevapache-tomcat-8.0.20
13. 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 module
dwo-runtime-debug-enable=true
# only admins - this is the default
#dwo-runtime-debug-users=admin