0

We are setting up a flowable application to use multiple bpmns with reusable java delegates. In application.yml, we have properties set for each bpmn

e.g.

app1.mail.active: true

app2.mail.active: false

ApplicationProperties file is set with @ConfigurationProperties() to get the properties.

Created a Spring bean:

@Bean("applicationProperties")
public ApplicationProperties applicationProperties(){
    return new ApplicationProperties();
}

I am trying to use a script task using groovy and initialize the appropriate properties. It seems Spring beans are not available inside the script.

<scriptTask id="sid-C8B8BE3F-F6CB-4559-B48C-5BC14AB76494" name="Initialize Process" scriptFormat="groovy">
    <script><![CDATA[
       // I want to be able to access applicationProperties bean here
         def _properties = applicationProperties.getApp1()
         execution.setVariable("properties", _properties)
   ]]></script>
</scriptTask>

When initializing bpmn for app1, I want to set a variable for properties related to app1 and similar to app2. I am able to use existing spring beans elsewhere in the same bpmn but not inside groovy script task.

Thank you in advance.

2 Answers2

0

The solution that I came up was to create a service task with java delegate to initialize application properties and common variables based on the bpmn instance and set a properties variable, then as part of the same service task I added an executionListener to set variables that are specific to that instance, using groovy script.

<serviceTask id="initProcess" name="Init Process" flowable:delegateExpression="${initProcessDelegate}">
   <extensionElements>
      <flowable:executionListener event="end" class="org.flowable.engine.impl.bpmn.listener.ScriptExecutionListener">
          <flowable:field name="script">
            <flowable:string><![CDATA[

               import config.ApplicationProperties;
               ApplicationProperties.Props props = (ApplicationProperties.Props) execution.getVariable("properties");
               System.out.println("properties " + properties.getUrl()); 

          ]]></flowable:string>
          </flowable:field>
          <flowable:field name="language">
            <flowable:string><![CDATA[groovy]]></flowable:string>
          </flowable:field>
     </flowable:executionListener>
   </extensionElements>
</serviceTask>


public class ProcessInitDelegate implements JavaDelegate {

    Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    ApplicationProperties applicationProperties;

    public void execute(DelegateExecution execution) {
        log.info("<--------------- ProcessInitDelegate ------------->");
         try {
            // set up properties for current instance
            String instance = (String) execution.getVariable("processInstance");
            Object properties = applicationProperties.getInnerInstance(instance);
            if (instance.equals("<your_instance_name>")) {
              ApplicationProperties.Props instanceProperties = (ApplicationProperties.Props) properties;
              execution.setVariable("properties, instanceProperties);
            }
         } catch (Exception e) {
           log.error("Error setting up process {} ", e.getMessage());
         }
    }
}
0

You can swop over to SecureJavaScript and add the bean to the trusted beans in your custom config.

<dependency>
       <groupId>org.flowable</groupId>
       <artifactId>flowable-secure-javascript</artifactId>
       <version>6.6.0</version>
</dependency>

@Bean
public SecureJavascriptConfigurator secureJavascriptConfigurator() {
    SecureJavascriptConfigurator configurator =
        new SecureJavascriptConfigurator()
            .setMaxStackDepth(10)
            .setMaxScriptExecutionTime(3000L)
            .setMaxMemoryUsed(3145728L)
            .setNrOfInstructionsBeforeStateCheckCallback(10)
            .setEnableAccessToBeans(true);
    
    return configurator;
}

@Bean
@ConditionalOnClass(SpringProcessEngineConfiguration.class)
public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> customProcessEngineConfigurer() {        
    return configuration -> {            
        configuration.addConfigurator(secureJavascriptConfigurator());
        Map<Object, Object> beans = new HashMap<>();
        beans.put("applicationProperties", applicationProperties);
        configuration.setBeans(beans);
    };
}