cancel
Showing results for 
Search instead for 
Did you mean: 

How to add binding to Script task

elygre
Confirmed Champ
Confirmed Champ
We are considering embedding Activiti into an application, where we want to enable the use of the application-specific APIs in Activiti script tasks:

// This is a groovy task// The object "applicationSpecificApi" is injected into the script binding by us, similar to the object "execution"def approvedAmount = execution.getVariable("amount");def approvedBy  = execution.getVariable("user_id");applicationSpecificApi.authorizeSpending (approvedAmount, approvedBy);‍‍‍‍‍‍

How do we add such a binding (a reference to either documentation or sample code should suffice)?

Thanks,
Eirik
2 REPLIES 2

jbarrez
Star Contributor
Star Contributor
You would have to check how the ScriptTask is implemented. Somewhere in classes below, the task or execution is simply injected in a hashmap of objects that are available.

elygre
Confirmed Champ
Confirmed Champ
For reference, in case other people have the same question. It looks like there are two mechanisms that are more or less accessible.

First, the engine configuration has a list of "ResolverFactory", as shown in the code below (from ProcessEngineConfigurationImpl). By setting your own ResolverFactories, you can inject your own resolver:

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));
   }
}

Second, the "BeansResolverFactory" which is inserted by default in the code above, uses "Map<Object, Object> ProcessEngineConfigurationImpl.getBeans()" to look for beans to inject (presumably the same beans that are available in expressions), so you can just set beans to be injected here:

// While creating the engine
Map<Object, Object> beans = new HashMap<Object, Object>();
beans.put("myservice", new MyService());
((ProcessEngineConfigurationImpl)config).setBeans(beans);

// In a script task
myservice.callMyFunction()

We ended up with the second solution, for now; it looks easy enough to change later, if required.