Groovy Module (Commons)

Last modified by Vincent Massol on 2021/03/17 21:55

cogGroovy Execution API
TypeJAR
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Standard

Description

This module has the following features:

  • Ability to define Groovy Compilation Customizer components to customize execution of Groovy scripts
  • A Timed Interrupt Customizer allowing to stop executing Groovy Scripts after a given timeout has been reached

Timed Interrupt Customizer

This customizer stops execution of Groovy Script after a timeout has been reached, thus preventing long running operations. However this works only for Groovy code. If the Groovy code calls Java methods for example and these methods take a long time to execute the execution will be stopped only when the Groovy code regains control.

Implementing a new Customizer

Do the following:

  • Add a Component implementing the org.xwiki.groovy.GroovyCompilationCustomizer Component Role.
  • Implement the CompilationCustomizer createCustomizer(); method.

For example:

...
@Component
@Named("timedInterrupt")
@Singleton
public class TimedInterruptGroovyCompilationCustomizer implements GroovyCompilationCustomizer
{
   /**
     * Used to get the script timeout configuration parameter value.
     */

   @Inject
   private TimedInterruptCustomizerConfiguration configuration;

   @Override
   public CompilationCustomizer createCustomizer()
   {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("value", this.configuration.getTimeout());
       return new ASTTransformationCustomizer(parameters, TimedInterrupt.class);
   }
}

Prerequisites & Installation Instructions

No Groovy Customizers are defined by default.

To add a Compilation Customizer, edit the xwiki.properties configuration file and add the following property:

groovy.compilationCustomizers=<list of customizer ids here>

For example to enable the Timed Interrupt Customizer, you would write:

groovy.compilationCustomizers=timedinterrupt

You can also configure the Timed Interrupt Customize timeout value by adding/editing the following property, in seconds (60 seconds being the default):

groovy.customizer.timedInterrupt.timeout=60
Tags:
    

Get Connected