View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.flowable.impl;
20  
21  import com.fasterxml.jackson.databind.json.JsonMapper;
22  import com.fasterxml.jackson.databind.node.ObjectNode;
23  import java.io.ByteArrayInputStream;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import javax.xml.stream.XMLInputFactory;
27  import javax.xml.stream.XMLStreamException;
28  import javax.xml.stream.XMLStreamReader;
29  import org.apache.syncope.core.workflow.api.WorkflowException;
30  import org.flowable.bpmn.converter.BpmnXMLConverter;
31  import org.flowable.bpmn.model.BpmnModel;
32  import org.flowable.common.engine.api.FlowableException;
33  import org.flowable.editor.constants.ModelDataJsonConstants;
34  import org.flowable.editor.language.json.converter.BpmnJsonConverter;
35  import org.flowable.engine.ProcessEngine;
36  import org.flowable.engine.repository.Deployment;
37  import org.flowable.engine.repository.Model;
38  import org.flowable.engine.repository.ProcessDefinition;
39  
40  public final class FlowableDeployUtils {
41  
42      private static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
43  
44      private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory.newInstance();
45  
46      static {
47          XML_INPUT_FACTORY.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
48          XML_INPUT_FACTORY.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
49      }
50  
51      public static Deployment deployDefinition(
52              final ProcessEngine engine, final String resourceName, final byte[] definition) {
53  
54          try {
55              return engine.getRepositoryService().createDeployment().
56                      addInputStream(resourceName, new ByteArrayInputStream(definition)).deploy();
57          } catch (FlowableException e) {
58              throw new WorkflowException("While importing " + resourceName, e);
59          }
60      }
61  
62      public static void deployModel(final ProcessEngine engine, final ProcessDefinition procDef) {
63          XMLStreamReader xtr = null;
64          try (InputStream bpmnStream = engine.getRepositoryService().
65                  getResourceAsStream(procDef.getDeploymentId(), procDef.getResourceName());
66               InputStreamReader isr = new InputStreamReader(bpmnStream);) {
67  
68              xtr = XML_INPUT_FACTORY.createXMLStreamReader(isr);
69              BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
70  
71              Model model = engine.getRepositoryService().newModel();
72              ObjectNode modelObjectNode = MAPPER.createObjectNode();
73              modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, procDef.getName());
74              model.setMetaInfo(modelObjectNode.toString());
75              model.setName(procDef.getName());
76              model.setDeploymentId(procDef.getDeploymentId());
77              model.setVersion(procDef.getVersion());
78  
79              engine.getRepositoryService().saveModel(model);
80              engine.getRepositoryService().addModelEditorSource(
81                      model.getId(),
82                      new BpmnJsonConverter().convertToJson(bpmnModel).toString().getBytes());
83          } catch (Exception e) {
84              throw new WorkflowException("While importing " + procDef.getResourceName(), e);
85          } finally {
86              if (xtr != null) {
87                  try {
88                      xtr.close();
89                  } catch (XMLStreamException e) {
90                      // ignore
91                  }
92              }
93          }
94      }
95  
96      private FlowableDeployUtils() {
97          // private constructor for static utility class
98      }
99  }