提交 41d8d22d 编写于 作者: J Joram Barrez

ACT-1553: allow to override the default bpmn parser with a custom one

上级 3612d3bf
......@@ -21,6 +21,7 @@ import org.activiti.bpmn.model.CallActivity;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.ErrorEventDefinition;
import org.activiti.bpmn.model.EventGateway;
import org.activiti.bpmn.model.EventSubProcess;
import org.activiti.bpmn.model.ExclusiveGateway;
import org.activiti.bpmn.model.InclusiveGateway;
import org.activiti.bpmn.model.ManualTask;
......@@ -70,6 +71,7 @@ public class CdiEventSupportBpmnParseHandler implements BpmnParseHandler {
supportedTypes.add(UserTask.class);
supportedTypes.add(EndEvent.class);
supportedTypes.add(SubProcess.class);
supportedTypes.add(EventSubProcess.class);
supportedTypes.add(CallActivity.class);
supportedTypes.add(SendTask.class);
supportedTypes.add(ReceiveTask.class);
......
/* 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.
*/
package org.activiti.engine.impl.bpmn.parser.handler;
import org.activiti.bpmn.model.BaseElement;
import org.activiti.bpmn.model.EventSubProcess;
/**
* @author Joram Barrez
*/
public class EventSubProcessParseHandler extends SubProcessParseHandler {
protected Class< ? extends BaseElement> getHandledType() {
return EventSubProcess.class;
}
}
......@@ -12,9 +12,6 @@
*/
package org.activiti.engine.impl.bpmn.parser.handler;
import java.util.HashSet;
import java.util.Set;
import org.activiti.bpmn.constants.BpmnXMLConstants;
import org.activiti.bpmn.model.BaseElement;
import org.activiti.bpmn.model.EventSubProcess;
......@@ -29,15 +26,8 @@ import org.activiti.engine.impl.pvm.process.ActivityImpl;
*/
public class SubProcessParseHandler extends AbstractActivityBpmnParseHandler<SubProcess> {
protected static Set<Class<? extends BaseElement>> supportedTypes = new HashSet<Class<? extends BaseElement>>();
static {
supportedTypes.add(SubProcess.class);
supportedTypes.add(EventSubProcess.class);
}
public Set<Class< ? extends BaseElement>> getHandledTypes() {
return supportedTypes;
protected Class< ? extends BaseElement> getHandledType() {
return SubProcess.class;
}
protected void executeParse(BpmnParse bpmnParse, SubProcess subProcess) {
......
......@@ -287,6 +287,7 @@ public abstract class ProcessEngineConfigurationImpl extends ProcessEngineConfig
// Bpmn parser
protected List<BpmnParseHandler> preBpmnParseHandlers;
protected List<BpmnParseHandler> postBpmnParseHandlers;
protected List<BpmnParseHandler> customDefaultBpmnParseHandlers;
protected ActivityBehaviorFactory activityBehaviorFactory;
protected ListenerFactory listenerFactory;
protected BpmnParseFactory bpmnParseFactory;
......@@ -785,6 +786,7 @@ public abstract class ProcessEngineConfigurationImpl extends ProcessEngineConfig
}
protected List<BpmnParseHandler> getDefaultBpmnParseHandlers() {
// Alpabetic list of default parse handler classes
List<BpmnParseHandler> bpmnParserHandlers = new ArrayList<BpmnParseHandler>();
bpmnParserHandlers.add(new BoundaryEventParseHandler());
......@@ -816,6 +818,37 @@ public abstract class ProcessEngineConfigurationImpl extends ProcessEngineConfig
bpmnParserHandlers.add(new TransactionParseHandler());
bpmnParserHandlers.add(new UserTaskParseHandler());
// Replace any default handler if the user wants to replace them
if (customDefaultBpmnParseHandlers != null) {
Map<Class<?>, BpmnParseHandler> customParseHandlerMap = new HashMap<Class<?>, BpmnParseHandler>();
for (BpmnParseHandler bpmnParseHandler : customDefaultBpmnParseHandlers) {
for (Class<?> handledType : bpmnParseHandler.getHandledTypes()) {
customParseHandlerMap.put(handledType, bpmnParseHandler);
}
}
for (int i=0; i<bpmnParserHandlers.size(); i++) {
// All the default handlers support only one type
BpmnParseHandler defaultBpmnParseHandler = bpmnParserHandlers.get(i);
if (defaultBpmnParseHandler.getHandledTypes().size() != 1) {
StringBuilder supportedTypes = new StringBuilder();
for (Class<?> type : defaultBpmnParseHandler.getHandledTypes()) {
supportedTypes.append(" " + type.getCanonicalName() + " ");
}
throw new ActivitiException("The default BPMN parse handlers should only support one type, but " + defaultBpmnParseHandler.getClass()
+ " supports " + supportedTypes.toString() + ". This is likely a programmatic error");
} else {
Class<?> handledType = defaultBpmnParseHandler.getHandledTypes().iterator().next();
if (customParseHandlerMap.containsKey(handledType)) {
BpmnParseHandler newBpmnParseHandler = customParseHandlerMap.get(handledType);
log.info("Replacing default BpmnParseHandler " + defaultBpmnParseHandler.getClass().getName() + " with " + newBpmnParseHandler.getClass().getName());
bpmnParserHandlers.set(i, newBpmnParseHandler);
}
}
}
}
// History
for (BpmnParseHandler handler : getDefaultHistoryParseHandlers()) {
bpmnParserHandlers.add(handler);
......@@ -1481,6 +1514,14 @@ public abstract class ProcessEngineConfigurationImpl extends ProcessEngineConfig
public void setPreBpmnParseHandlers(List<BpmnParseHandler> preBpmnParseHandlers) {
this.preBpmnParseHandlers = preBpmnParseHandlers;
}
public List<BpmnParseHandler> getCustomDefaultBpmnParseHandlers() {
return customDefaultBpmnParseHandlers;
}
public void setCustomDefaultBpmnParseHandlers(List<BpmnParseHandler> customDefaultBpmnParseHandlers) {
this.customDefaultBpmnParseHandlers = customDefaultBpmnParseHandlers;
}
public List<BpmnParseHandler> getPostBpmnParseHandlers() {
return postBpmnParseHandlers;
......@@ -1871,7 +1912,6 @@ public abstract class ProcessEngineConfigurationImpl extends ProcessEngineConfig
public void setKnowledgeBaseCacheLimit(int knowledgeBaseCacheLimit) {
this.knowledgeBaseCacheLimit = knowledgeBaseCacheLimit;
}
public DeploymentCache<Object> getKnowledgeBaseCache() {
return knowledgeBaseCache;
......
......@@ -20,6 +20,7 @@ import org.activiti.bpmn.model.BusinessRuleTask;
import org.activiti.bpmn.model.CallActivity;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.EventGateway;
import org.activiti.bpmn.model.EventSubProcess;
import org.activiti.bpmn.model.ExclusiveGateway;
import org.activiti.bpmn.model.InclusiveGateway;
import org.activiti.bpmn.model.IntermediateCatchEvent;
......@@ -71,6 +72,7 @@ public class FlowNodeHistoryParseHandler implements BpmnParseHandler {
supportedElementClasses.add(CallActivity.class);
supportedElementClasses.add(SubProcess.class);
supportedElementClasses.add(EventSubProcess.class);
}
public Set<Class< ? extends BaseElement>> getHandledTypes() {
......
......@@ -20,9 +20,9 @@ import org.activiti.engine.test.Deployment;
* @author Frederik Heremans
* @author Joram Barrez
*/
public class BPMNParseListenerTest extends ResourceActivitiTestCase {
public class BPMNParseHandlerTest extends ResourceActivitiTestCase {
public BPMNParseListenerTest() {
public BPMNParseHandlerTest() {
super("org/activiti/standalone/parsing/bpmn.parse.listener.activiti.cfg.xml");
}
......
/* 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.
*/
package org.activiti.standalone.parsing;
import org.activiti.engine.impl.test.ResourceActivitiTestCase;
import org.activiti.engine.test.Deployment;
/**
* @author Frederik Heremans
* @author Joram Barrez
*/
public class CustomDefaultBpmnParseHandlerTest extends ResourceActivitiTestCase {
public CustomDefaultBpmnParseHandlerTest() {
super("org/activiti/standalone/parsing/custom.default.parse.handler.activiti.cfg.xml");
}
@Deployment
public void testCustomDefaultUserTaskParsing() throws Exception {
// The task which is created after process instance start should be async
runtimeService.startProcessInstanceByKey("customDefaultBpmnParseHandler");
assertEquals(0, taskService.createTaskQuery().count());
assertEquals(1, managementService.createJobQuery().count());
managementService.executeJob(managementService.createJobQuery().singleResult().getId());
assertEquals(1, taskService.createTaskQuery().count());
}
}
/* 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.
*/
package org.activiti.standalone.parsing;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.impl.bpmn.parser.BpmnParse;
import org.activiti.engine.impl.bpmn.parser.handler.UserTaskParseHandler;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
/**
* @author Joram Barrez
*/
public class CustomUserTaskBpmnParseHandler extends UserTaskParseHandler {
protected void executeParse(BpmnParse bpmnParse, UserTask userTask) {
// Do the regular stuff
super.executeParse(bpmnParse, userTask);
// Make user tasks always async
ActivityImpl activity = findActivity(bpmnParse, userTask.getId());
activity.setAsync(true);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="customDefaultBpmnParseHandler">
<startEvent id="theStart" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
<userTask id="theTask" name="my task" />
<sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
<endEvent id="theEnd" />
</process>
</definitions>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<!-- Database configurations -->
<property name="history" value="audit" />
<property name="databaseSchemaUpdate" value="true" />
<!-- job executor configurations -->
<property name="jobExecutorActivate" value="false" />
<property name="customDefaultBpmnParseHandlers">
<list>
<bean class="org.activiti.standalone.parsing.CustomUserTaskBpmnParseHandler" />
</list>
</property>
</bean>
</beans>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册