cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing my own Groovy API from ScriptTask

sorinello
Champ in-the-making
Champ in-the-making
Hello.

I am creating an integration between a product and Activiti. Everything works fine so now, I am using my own IdentityService implementation, etc.

My product supports natively Groovy, as Activiti does. My product also exposes and API which can be used from Groovy.
My goal is to make my product Groovy API accessible from ScriptTask services.
I am not sure about how this can be achieved, I have searched the forums and also bought Tijs' book Activiti in Action, but it seems I am unable to find useful information.
I don't think it's a good idea to insert my API as a variable when starting a process instance, since some things from the API may change at runtime (context, etc)

As a typical use case I have: When my process reaches the ScriptTask, I would like to use my API to do some external things, like create a page automatically, etc.

ANY advice is more than appreciated.
Best regards,
Sorin B.
1 REPLY 1

frederikherema1
Star Contributor
Star Contributor
Look at the ExpressionManager if you want to use your own API from within  expressions used in activiti. For example, when using spring, you can use Spring-beans in your expressions to call method on.

For scripts, there is a similar approach to get "objects" in the scope of all script-tasks, with our using a process-variable: ScriptBindingsFactory. The ScriptBindingsFactory is responsible for creating ScriptBindings that are exposed in any script (js, groovy, ..). In the procesEngineConfiguration, you can add a custom factory OR add custom resolvers that are called by the dealt bindings:


  protected void initScriptingEngines() {
    if (resolverFactories==null) {
      resolverFactories = new ArrayList<ResolverFactory>();
      resolverFactories.add(new VariableScopeResolverFactory());
      resolverFactories.add(new BeansResolverFactory());
    }
    if (scriptingEngines==null) {
      scriptingEngines = new ScriptingEngines(new ScriptBindingsFactory(resolverFactories));
    }
  }

Using a custom org.activiti.engine.impl.scripting.ResolverFactory, you can make certain keywords resolve to your object. You can, for example create a resolver that always returns a reference to you product services, using "services". In scripts, you can use "service.createPage(execution.getVariable("pageName");".

Hope this helps you…