/
CamundaProcessAutodeployment.java
93 lines (80 loc) · 3.97 KB
/
CamundaProcessAutodeployment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.camunda.community.rest.client.springboot;
import org.apache.commons.io.IOUtils;
import org.camunda.community.rest.client.api.DeploymentApi;
import org.camunda.community.rest.client.invoker.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.ResourceUtils;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
/**
* Auto deploys all Camunda resources found on classpath during startup of the application
*/
@Configuration
public class CamundaProcessAutodeployment {
private Logger logger = LoggerFactory.getLogger(CamundaProcessAutodeployment.class);
@Autowired
private DeploymentApi deploymentApi;
@Autowired
private ResourcePatternResolver patternResolver;
// TODO Possible extension: Provide a @Deployment annotation like Spring Zeebe
@Value("${camunda.autoDeploy.bpmnResources:}")
private String bpmnResourcesPattern;
@Value("${camunda.autoDeploy.dmnResources:}")
private String dmnResourcesPattern;
@Value("${camunda.autoDeploy.formResources:}")
private String formResourcesPattern;
@Value("${spring.application.name:spring-app}")
private String applicationName;
@PostConstruct
public void deployCamundaResources() throws IOException, ApiException {
if (bpmnResourcesPattern==null || bpmnResourcesPattern.length()==0) {
bpmnResourcesPattern = "classpath*:**/*.bpmn"; // Not sure why the default mechanism in @Value makes problems - but this works!
}
if (dmnResourcesPattern==null || dmnResourcesPattern.length()==0) {
dmnResourcesPattern = "classpath*:**/*.dmn";
}
if (formResourcesPattern==null || formResourcesPattern.length()==0) {
formResourcesPattern = "classpath*:**/*.form";
}
deployResources(Arrays.asList(patternResolver.getResources(bpmnResourcesPattern)), "bpmn");
deployResources(Arrays.asList(patternResolver.getResources(dmnResourcesPattern)), "dmn");
deployResources(Arrays.asList(patternResolver.getResources(formResourcesPattern)), "form");
}
private void deployResources(List<Resource> resourcesToDeploy, String type) throws IOException, ApiException {
logger.info("Found resources for deployment of type "+ type +": " + resourcesToDeploy);
for (Resource camundaResource: resourcesToDeploy) {
// We have to create a tmpFile because we need to read the files via InputStream to work also in a jar-packed environment
// but the OpenAPI will need a File.
// We still have to set the file ending correct in the temp file
// (because otherwise the deployer will not pick it up as e.g. BPMN file)
final File tempFile = File.createTempFile(UUID.randomUUID().toString(),"." + type);
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(camundaResource.getInputStream(), out);
}
logger.info(" - Now deploying: " + camundaResource);
deploymentApi.createDeployment(
null,
null,
true, // changedOnly
true, // duplicateFiltering
applicationName + "-" + camundaResource.getFilename(), // deploymentName
null,
tempFile);
// deploying the files one by one because of limitation of OpenAPI at the moment
// see https://jira.camunda.com/browse/CAM-13105
}
}
}