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.logic.init;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.List;
25  import javax.sql.DataSource;
26  import org.apache.commons.io.IOUtils;
27  import org.apache.syncope.common.lib.types.EntitlementsHolder;
28  import org.apache.syncope.common.lib.types.FlowableEntitlement;
29  import org.apache.syncope.core.flowable.impl.FlowableDeployUtils;
30  import org.apache.syncope.core.flowable.impl.FlowableRuntimeUtils;
31  import org.apache.syncope.core.flowable.support.DomainProcessEngine;
32  import org.apache.syncope.core.persistence.api.SyncopeCoreLoader;
33  import org.flowable.engine.ProcessEngine;
34  import org.flowable.engine.impl.db.DbIdGenerator;
35  import org.flowable.engine.repository.ProcessDefinition;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.springframework.core.io.Resource;
39  
40  public class FlowableLoader implements SyncopeCoreLoader {
41  
42      protected static final Logger LOG = LoggerFactory.getLogger(FlowableLoader.class);
43  
44      protected final Resource userWorkflowDef;
45  
46      protected final DomainProcessEngine dpEngine;
47  
48      public FlowableLoader(final Resource userWorkflowDef, final DomainProcessEngine dpEngine) {
49          this.userWorkflowDef = userWorkflowDef;
50          this.dpEngine = dpEngine;
51      }
52  
53      @Override
54      public int getOrder() {
55          return 310;
56      }
57  
58      @Override
59      public void load() {
60          EntitlementsHolder.getInstance().addAll(FlowableEntitlement.values());
61      }
62  
63      @Override
64      public void load(final String domain, final DataSource datasource) {
65          byte[] wfDef = new byte[0];
66  
67          try (InputStream wfIn = userWorkflowDef.getInputStream()) {
68              wfDef = IOUtils.toByteArray(wfIn);
69          } catch (IOException e) {
70              LOG.error("While loading " + userWorkflowDef.getFilename(), e);
71          }
72  
73          ProcessEngine processEngine = dpEngine.getEngines().get(domain);
74          if (processEngine == null) {
75              LOG.error("Could not find the configured ProcessEngine for domain {}", domain);
76          } else {
77              List<ProcessDefinition> processes = processEngine.getRepositoryService().
78                      createProcessDefinitionQuery().processDefinitionKey(FlowableRuntimeUtils.WF_PROCESS_ID).
79                      list();
80              LOG.debug(FlowableRuntimeUtils.WF_PROCESS_ID + " Flowable processes in repository: {}", processes);
81  
82              // Only loads process definition from file if not found in repository
83              if (processes.isEmpty()) {
84                  processEngine.getRepositoryService().createDeployment().addInputStream(
85                          userWorkflowDef.getFilename(), new ByteArrayInputStream(wfDef)).deploy();
86  
87                  ProcessDefinition procDef = processEngine.getRepositoryService().createProcessDefinitionQuery().
88                          processDefinitionKey(FlowableRuntimeUtils.WF_PROCESS_ID).latestVersion().
89                          singleResult();
90  
91                  FlowableDeployUtils.deployModel(processEngine, procDef);
92  
93                  LOG.debug("Flowable Workflow definition loaded for domain {}", domain);
94  
95                  if (processEngine.getProcessEngineConfiguration().getIdGenerator() instanceof DbIdGenerator) {
96                      // jump to the next ID block
97                      for (int i = 0; i < processEngine.getProcessEngineConfiguration().getIdBlockSize(); i++) {
98                          processEngine.getProcessEngineConfiguration().getIdGenerator().getNextId();
99                      }
100                 }
101             }
102         }
103     }
104 }