I have created a service task that retrieves a groovy script from the deployment resources. I am hoping someone can have a look and tell me if I am going to run into trouble
This is just a first draft so the caching is rudimentary for now…
public class GroovyDeploymentResourceDelegate extends TaskActivityBehavior {
private static final long serialVersionUID = 1L;
private static final String LANGUAGE= "groovy";
private static ConcurrentHashMap<String, String> cachedScripts = new ConcurrentHashMap<>();
@Override
public void execute(DelegateExecution execution) {
String key = execution.getProcessDefinitionId() + "::" + execution.getCurrentActivityId();
String script = cachedScripts.get(key);
if (script == null) {
try {
RepositoryService repositoryService = CommandContextUtil.getProcessEngineConfiguration().getRepositoryService();
ProcessDefinition processDefinition = repositoryService.getProcessDefinition(execution.getProcessDefinitionId());
String className = execution.getCurrentFlowElement().getName();
String resourceName = className + "." + LANGUAGE;
InputStream in = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
script = IOUtils.toString(in, StandardCharsets.UTF_8);
// Execute the script
script += "\nnew " + className + "().execute(execution)";
cachedScripts.put(key, script);
} catch (Exception e) {
throw new RuntimeException("Could not load script for " + execution.getCurrentFlowElement().getName(), e);
}
}
new ScriptTaskActivityBehavior(script, LANGUAGE, null).execute(execution);
}
}
We’d then define a script like this:
class DoSomething implements JavaDelegate {
def static logger = Logger.getLogger(DoSomething.class)
def void execute(DelegateExecution execution) {
logger.info("Doing something")
}
def doSomethingMore() {
logger.info("I get to call methods")
}
}
And just package that in DoSomething.groovy inside the zip file with the processes.
I should probably say why I am doing this. It is to improve the development process of scripts. We get to write a groovy script in the IDE and not have to copy the contents into a script task. It is also a class and is nice for unit testing in isolation. If we wanted to we can use a compiled version as well, but sticking to scripts for now for ease and flexibility of deployment.