Experimental Topics

Table of Contents

30.1. Introduction
30.2. Runtime Support for Scripting Languages
30.2.1. Introduction
30.2.2. Supported languages
30.2.3. Running a Script

30.1. Introduction

This chapter is provided to document experimental features that are being introduced in the current development branch (Nuxeo 5.2-SNAPSHOT).

This content is likely to move to other places after some time.

Note

Your feedback is welcome! If you have use cases and want to help with the development of these technologies, please join the ECM mailing list or the forum.

30.2. Runtime Support for Scripting Languages

30.2.1. Introduction

Preliminary, yet powerful, support for scripting in Nuxeo Runtime has been recently added. This makes scripting available from all Nuxeo’s platform. Thanks to this new feature, you can easily uses script from your custom components. This can be very useful for a lot of use cases, like:

  • dynamic rules (scripting language as DSLs)

  • easily modifiable behaviors

  • light and powerful configuration / customization

  • etc.

Scripts have access to the whole API thanks to Java scripting integration (JSR-223).

Moreover, scripts can also be run remotely thanks to the Nuxeo Runtime command line. This allows you to create a script on your administration machine, launch it on the remote platform and get the result back. It makes scripting a killer-feature for administration scripts (ex: expire content, bulk content modification, bulk refactoring of the content repository layout, etc.).

30.2.2. Supported languages

I’ve just integrated scripting support through JSR 223 in nuxeo. This was integrated as a new project nuxeo-runtime-scripting which is in SVN but it is was not yet added in the nuxeo.ear build (neither in runtime SVN module).

For now only these scripting engines have been integrated:

  • Jexl

  • JRuby

  • Jython

  • Groovy

  • JavaScript (Rhino)

If needed more will be added later (like PHP for example).

30.2.3. Running a Script

You can run scripts in nuxeo in 3 different ways:

  • Put the script inside the nuxeo.ear/script directory (you should define this directory through a runtime variable). Then from the Java code you can do:

    Framework.getService(ScriptingService.class).eval("my_script.js");

    where my_script.js is the script path relative to the script directory.

    Or you can use the JSR 322 API:

    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("js");
    engine.eval("absolute/path/to/my_script.js");
  • Let the script inside your .jar and register it under using name as a script component. Then you can run the script as follow:

    Framework.getService(ScriptingService.class).getScript("myScript").eval();

    This method chaches the compiled script so it is only supported for languages that support compilation (currently all the engines that comes in Nuxeo).

  • Run a script from remote :)

    This can be used for debug, testing or administration. You write a script locally then you run it against a remote Nuxeo EP server. The script will be send to the server and executed on the server then the server will return the result (including STDOUT and STDERR) to the client.

    For security reason this feature can be disabled using a runtime property on the server.

    Here is an example on how you can run a remote script:

    ScriptingClient client = new ScriptingClient("localhost", 62474);
    URL src = RemoteTest.class.getClassLoader().getResource("test.js");
    RemoteScript script = client.loadScript(src);
    script.eval();

    For the following JavaScript script:

    importPackage(java.lang);
    importPackage(org.nuxeo.runtime);
    importPackage(org.nuxeo.runtime.api);
    importPackage(org.nuxeo.runtime.model);
    
    runtime = Framework.getRuntime();
    name = runtime.getName();
    version = runtime.getVersion();
    desc = runtime.getDescription();
    println("Remote runtime: "+name+" v."+version);
    println(desc);
    println("---------------------------------------");
    println("Registered components:");
    println("---------------------------------------");
    regs = runtime.getComponentManager().getRegistrations();
    for (var i=0, size=regs.size(); i<size; i++){
        println(regs.get(i).getName());
    }

    The following will be printed on the STDOUT of the client:

    Remote runtime: OSGi NXRuntime v.1.4.0
    OSGi NXRuntime version 1.4.0
    ---------------------------------------
    Registered components:
    ---------------------------------------
    service:org.nuxeo.ecm.core.api.DocumentAdapterService
    service:org.nuxeo.ecm.core.repository.RepositoryService
    service:org.nuxeo.runtime.remoting.RemotingService
    service:org.nuxeo.runtime.EventService
    service:org.nuxeo.ecm.platform.login.LoginConfig
    ...

So, this new feature can be used to write pure script based Nuxeo components. Also in future I will try to configure Tomcat to be able to run scripts inside servlets. This means to be able to write web pages in PHP or other supported language for Nuxeo EP ;-)

If you're willing to leverage these new scripting features in your own applications and explore new ways to use scripting to make balance "enterprise" with "agile" in the ECM field, please join the list or the forum and start contributing.