DefaultProcessEngineFactory.java 11.3 KB
Newer Older
J
Merge  
Joram Barrez 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

14 15
package org.activiti.compatibility;

16
import java.util.ArrayList;
17
import java.util.HashMap;
18
import java.util.List;
19
import java.util.Map;
20

21 22
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
23 24
import org.activiti.engine.impl.persistence.deploy.Deployer;
import org.activiti.engine.impl.rules.RulesDeployer;
25
import org.activiti5.engine.ActivitiException;
J
Merge  
Joram Barrez 已提交
26
import org.activiti5.engine.ProcessEngine;
27
import org.activiti5.engine.delegate.event.ActivitiEventListener;
28
import org.activiti5.engine.impl.asyncexecutor.AsyncExecutor;
29 30
import org.activiti5.engine.impl.bpmn.parser.factory.ActivityBehaviorFactory;
import org.activiti5.engine.impl.bpmn.parser.factory.ListenerFactory;
31
import org.activiti5.engine.impl.history.HistoryLevel;
32 33
import org.activiti5.engine.impl.persistence.deploy.DeploymentCache;
import org.activiti5.engine.impl.persistence.entity.ProcessDefinitionEntity;
34
import org.activiti5.engine.parse.BpmnParseHandler;
35 36


37
public class DefaultProcessEngineFactory {
T
Tijs Rademakers 已提交
38

39
  /**
40
   * Takes in an Activiti 6 process engine config, gives back an Activiti 5 Process engine.
41
   */
42
  public ProcessEngine buildProcessEngine(ProcessEngineConfigurationImpl activiti6Configuration) {
43 44 45
    org.activiti5.engine.impl.cfg.ProcessEngineConfigurationImpl activiti5Configuration = null;
    if (activiti6Configuration instanceof StandaloneProcessEngineConfiguration) {
      activiti5Configuration = new org.activiti5.engine.impl.cfg.StandaloneProcessEngineConfiguration();
46 47
      copyConfigItems(activiti6Configuration, activiti5Configuration);
      return activiti5Configuration.buildProcessEngine();
48 49 50
    } else {
      throw new ActivitiException("Unsupported process engine configuration");
    }
51 52 53 54
  }
   
  @SuppressWarnings("unchecked")
  protected void copyConfigItems(ProcessEngineConfigurationImpl activiti6Configuration, org.activiti5.engine.impl.cfg.ProcessEngineConfigurationImpl activiti5Configuration) {
55 56
    activiti5Configuration.setActiviti5CompatibilityHandler(activiti6Configuration.getActiviti5CompatibilityHandler());
    
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 94 95
    if (activiti6Configuration.getIdGeneratorDataSource() != null) {
      activiti5Configuration.setIdGeneratorDataSource(activiti6Configuration.getIdGeneratorDataSource());
    } else if (activiti6Configuration.getIdGeneratorDataSourceJndiName() != null) {
      activiti5Configuration.setIdGeneratorDataSourceJndiName(activiti6Configuration.getIdGeneratorDataSourceJndiName());
    } else {
      activiti5Configuration.setDataSource(activiti6Configuration.getDataSource());
    }
    
    if (activiti6Configuration.getJdbcDriver() != null) {
      activiti5Configuration.setJdbcDriver(activiti6Configuration.getJdbcDriver());
    }
    if (activiti6Configuration.getJdbcUrl() != null) {
      activiti5Configuration.setJdbcUrl(activiti6Configuration.getJdbcUrl());
    }
    if (activiti6Configuration.getJdbcUsername() != null) {
      activiti5Configuration.setJdbcUsername(activiti6Configuration.getJdbcUsername());
    }
    if (activiti6Configuration.getJdbcPassword() != null) {
      activiti5Configuration.setJdbcPassword(activiti6Configuration.getJdbcPassword());
    }
    
    if (activiti6Configuration.getIdBlockSize() > 0) {
      activiti5Configuration.setIdBlockSize(activiti6Configuration.getIdBlockSize());
    }
    
    if (activiti6Configuration.getJdbcMaxActiveConnections() > 0) {
      activiti5Configuration.setJdbcMaxActiveConnections(activiti6Configuration.getJdbcMaxActiveConnections());
    }
    
    activiti5Configuration.setHistoryLevel(HistoryLevel.getHistoryLevelForKey(activiti6Configuration.getHistoryLevel().getKey()));
    
    activiti5Configuration.setMailServerDefaultFrom(activiti6Configuration.getMailServerDefaultFrom());
    activiti5Configuration.setMailServerHost(activiti6Configuration.getMailServerHost());
    activiti5Configuration.setMailServerPassword(activiti6Configuration.getMailServerPassword());
    activiti5Configuration.setMailServerPort(activiti6Configuration.getMailServerPort());
    activiti5Configuration.setMailServerUsername(activiti6Configuration.getMailServerUsername());
    activiti5Configuration.setMailServerUseSSL(activiti6Configuration.getMailServerUseSSL());
    activiti5Configuration.setMailServerUseTLS(activiti6Configuration.getMailServerUseTLS());
    if (activiti6Configuration.getMailServers() != null && activiti6Configuration.getMailServers().size() > 0) {
96
      activiti5Configuration.getMailServers().putAll(activiti6Configuration.getMailServers());
97 98
    }
    
99 100 101 102
    if (activiti6Configuration.getMailSessionJndi() != null) {
      activiti5Configuration.setMailSessionJndi(activiti6Configuration.getMailSessionJndi());
    }
    
103 104 105 106 107 108 109
    activiti5Configuration.setCreateDiagramOnDeploy(activiti6Configuration.isCreateDiagramOnDeploy());
    activiti5Configuration.setProcessDefinitionCacheLimit(activiti6Configuration.getProcessDefinitionCacheLimit());
    
    if (activiti6Configuration.isAsyncExecutorEnabled()) {
      activiti5Configuration.setAsyncExecutorEnabled(true);
      if (activiti6Configuration.isAsyncExecutorActivate()) {
        activiti5Configuration.setAsyncExecutorActivate(true);
110
      }
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    }
    if (activiti6Configuration.getActiviti5AsyncExecutor() != null) {
      AsyncExecutor activiti5AsyncExecutor = (AsyncExecutor) activiti6Configuration.getActiviti5AsyncExecutor();
      activiti5Configuration.setAsyncExecutor(activiti5AsyncExecutor);
    }
    
    activiti5Configuration.setJpaCloseEntityManager(activiti6Configuration.isJpaCloseEntityManager());
    activiti5Configuration.setJpaHandleTransaction(activiti6Configuration.isJpaHandleTransaction());
    activiti5Configuration.setJpaPersistenceUnitName(activiti6Configuration.getJpaPersistenceUnitName());
    
    if (activiti6Configuration.getBeans() != null) {
      activiti5Configuration.setBeans(activiti6Configuration.getBeans());
    }
    
    if (activiti6Configuration.getActiviti5ProcessDefinitionCache() != null) {
      activiti5Configuration.setProcessDefinitionCache((DeploymentCache<ProcessDefinitionEntity>) activiti6Configuration.getActiviti5ProcessDefinitionCache());
    }
    activiti5Configuration.setProcessDefinitionCacheLimit(activiti6Configuration.getProcessDefinitionCacheLimit());
    
    if (activiti6Configuration.getActiviti5KnowledgeBaseCache() != null) {
      activiti5Configuration.setKnowledgeBaseCache((DeploymentCache<Object>) activiti6Configuration.getActiviti5KnowledgeBaseCache());
    }
    activiti5Configuration.setKnowledgeBaseCacheLimit(activiti6Configuration.getKnowledgeBaseCacheLimit());
    
    if (activiti6Configuration.getActiviti5ActivityBehaviorFactory() != null) {
      activiti5Configuration.setActivityBehaviorFactory((ActivityBehaviorFactory) activiti6Configuration.getActiviti5ActivityBehaviorFactory());
    }
    if (activiti6Configuration.getActiviti5ListenerFactory() != null) {
      activiti5Configuration.setListenerFactory((ListenerFactory) activiti6Configuration.getActiviti5ListenerFactory());
    }
    
    convertParseHandlers(activiti6Configuration, activiti5Configuration);
    
    if (activiti6Configuration.getActiviti5CustomMybatisMappers() != null) {
      activiti5Configuration.setCustomMybatisMappers(activiti6Configuration.getActiviti5CustomMybatisMappers());
    }
    
    if (activiti6Configuration.getActiviti5CustomMybatisXMLMappers() != null) {
      activiti5Configuration.setCustomMybatisXMLMappers(activiti6Configuration.getActiviti5CustomMybatisXMLMappers());
    }
    
    convertEventListeners(activiti6Configuration, activiti5Configuration);
    
    // check if we need to enable rules deployment for Activiti 5
    if (activiti6Configuration.getCustomPostDeployers() != null) {
      List<org.activiti5.engine.impl.persistence.deploy.Deployer> activiti5Deployers = new ArrayList<org.activiti5.engine.impl.persistence.deploy.Deployer>();
      for (Deployer deployer : activiti6Configuration.getCustomPostDeployers()) {
        if (deployer instanceof RulesDeployer) {
          activiti5Deployers.add(new org.activiti5.engine.impl.rules.RulesDeployer());
          break;
161 162
        }
      }
163
      
164 165 166 167 168
      if (activiti5Deployers.size() > 0) {
        if (activiti5Configuration.getCustomPostDeployers() != null) {
          activiti5Configuration.getCustomPostDeployers().addAll(activiti5Deployers);
        } else {
          activiti5Configuration.setCustomPostDeployers(activiti5Deployers);
169 170
        }
      }
171 172
    }
  }
J
Merge  
Joram Barrez 已提交
173

174 175 176 177 178 179
  protected void convertParseHandlers(ProcessEngineConfigurationImpl activiti6Configuration, org.activiti5.engine.impl.cfg.ProcessEngineConfigurationImpl activiti5Configuration) {
    activiti5Configuration.setPreBpmnParseHandlers(convert(activiti6Configuration.getActiviti5PreBpmnParseHandlers()));
    activiti5Configuration.setPostBpmnParseHandlers(convert(activiti6Configuration.getActiviti5PostBpmnParseHandlers()));
    activiti5Configuration.setCustomDefaultBpmnParseHandlers(convert(activiti6Configuration.getActiviti5CustomDefaultBpmnParseHandlers()));
  }
  
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  protected void convertEventListeners(ProcessEngineConfigurationImpl activiti6Configuration, org.activiti5.engine.impl.cfg.ProcessEngineConfigurationImpl activiti5Configuration) {
    if (activiti6Configuration.getActiviti5EventListeners() != null) {
      List<ActivitiEventListener> eventListeners = new ArrayList<ActivitiEventListener>();
      for (Object eventObject : activiti6Configuration.getActiviti5EventListeners()) {
        ActivitiEventListener eventListener = (ActivitiEventListener) eventObject;
        eventListeners.add(eventListener);
      }
      activiti5Configuration.setEventListeners(eventListeners);
    }
    
    if (activiti6Configuration.getActiviti5TypedEventListeners() != null) {
      Map<String, List<ActivitiEventListener>> eventListenerMap = new HashMap<String, List<ActivitiEventListener>>();
      for (String eventKey : activiti6Configuration.getActiviti5TypedEventListeners().keySet()) {
        List<ActivitiEventListener> eventListeners = new ArrayList<ActivitiEventListener>();
        for (Object eventObject : activiti6Configuration.getActiviti5TypedEventListeners().get(eventKey)) {
          ActivitiEventListener eventListener = (ActivitiEventListener) eventObject;
          eventListeners.add(eventListener);
        }
        eventListenerMap.put(eventKey, eventListeners);
      }
      activiti5Configuration.setTypedEventListeners(eventListenerMap);
    }
  }
  
204 205 206 207 208 209 210 211 212 213 214 215
  protected List<BpmnParseHandler> convert(List<Object> activiti5BpmnParseHandlers) {
    if (activiti5BpmnParseHandlers == null) {
      return null;
    }
      
    List<BpmnParseHandler> parseHandlers = new ArrayList<BpmnParseHandler>(activiti5BpmnParseHandlers.size());
    for (Object activiti6BpmnParseHandler : activiti5BpmnParseHandlers) {
      parseHandlers.add((BpmnParseHandler) activiti6BpmnParseHandler);
    }
    return parseHandlers;
  }
  
J
Merge  
Joram Barrez 已提交
216
}