All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vanillabp.springboot.adapter.ModuleAwareBpmnDeployment Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
package io.vanillabp.springboot.adapter;

import io.vanillabp.springboot.modules.WorkflowModuleProperties;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;

public abstract class ModuleAwareBpmnDeployment {

    protected abstract Logger getLogger();
    
    protected abstract String getAdapterId();
    
    private final VanillaBpProperties properties;

    private final String applicationName;

    @Autowired(required = false)
    private List moduleProperties;
    
    public ModuleAwareBpmnDeployment(
            final VanillaBpProperties properties,
            final String applicationName) {
        
        this.properties = properties;
        this.applicationName = applicationName;
        
    }

    protected void deployAllWorkflowModules() {

        final var hasExplicitDefinedWorkflowModules = (moduleProperties != null)
                && !moduleProperties.isEmpty();

        if (hasExplicitDefinedWorkflowModules) {
            moduleProperties.forEach(this::deployWorkflowModule);
            return;
        }

        if (!StringUtils.hasText(applicationName)) {
            throw new RuntimeException(
                    "No workflow-module configurations found (see "
                    + "https://github.com/vanillabp/spring-boot-support?tab=readme-ov-file#configuration)\n"
                    + "and need to use property 'spring.application.name' as the workflow-module-id instead but it is not defined!");
        } else {
            getLogger().info(
                    "No workflow-module configurations found (see "
                    + "https://github.com/vanillabp/spring-boot-support?tab=readme-ov-file#configuration),\n"
                    + "will use property 'spring.application.name' as the workflow-module-id instead: {}",
                    applicationName);
        }
        deployWorkflowModule(applicationName);
    	
    }
    
    private void deployWorkflowModule(
    		final WorkflowModuleProperties propertySpecification) {
    	
        final var workflowModuleId = propertySpecification.getWorkflowModuleId();

        deployWorkflowModule(
                workflowModuleId);
    	
    }

    protected abstract void doDeployment(
    		String workflowModuleId,
    		Resource[] bpmns,
    		Resource[] dmns,
    		Resource[] cmms) throws Exception;

    private void deployWorkflowModule(
    		final String workflowModuleId) {

        final var resourcesLocation = properties
                .getAdapterResourcesLocationFor(workflowModuleId, getAdapterId());

        try {

            final var bpmns = findResources(workflowModuleId, resourcesLocation, "*.bpmn");
            final var cmms = findResources(workflowModuleId, resourcesLocation, "*.cmmn");
            final var dmns = findResources(workflowModuleId, resourcesLocation, "*.dmn");

            doDeployment(
                    workflowModuleId,
                    bpmns,
                    dmns,
                    cmms);

            getLogger()
                    .info("Deployed resources for process archive <{}>",
                            workflowModuleId == null ? "default" : workflowModuleId);

        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
        	throw new RuntimeException(e);
        }

    }

    private Resource[] findResources(
            final String workflowModuleId,
            final String adapterResourcesLocation,
            final String fileNamePattern) throws IOException {

        // test for multi-jar
        if ((moduleProperties != null)
                && (!adapterResourcesLocation.contains(":") // e.g. file://, classpath://, etc.
                || adapterResourcesLocation.startsWith(ResourcePatternResolver.CLASSPATH_URL_PREFIX))) {
            getLogger()
                    .warn("On using workflow modules you should define resource-path using '{}' to ensure BPMN resources are found!\nCurrent resource-path of module '{}' for adapater '{}':\n{}",
                            ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX,
                            workflowModuleId,
                            getAdapterId(),
                            adapterResourcesLocation);
        }

        final var resourcesLocation = adapterResourcesLocation.endsWith("/")
                ? adapterResourcesLocation + fileNamePattern
                : adapterResourcesLocation + "/" + fileNamePattern;

        getLogger()
                .debug("Scanning process archive <{}> for {}",
                        workflowModuleId == null ? "default" : workflowModuleId,
                        resourcesLocation);

        return new PathMatchingResourcePatternResolver().getResources(resourcesLocation);

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy