How to add binding to Script task
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2012 04:03 PM
We are considering embedding Activiti into an application, where we want to enable the use of the application-specific APIs in Activiti script tasks:
How do we add such a binding (a reference to either documentation or sample code should suffice)?
Thanks,
Eirik
// 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
Labels:
- Labels:
-
Archive
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2012 03:48 AM
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.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2012 07:41 AM
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:
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:
We ended up with the second solution, for now; it looks easy enough to change later, if required.
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.
