当前位置:网站首页>Floweable source code annotation (40) class delegation
Floweable source code annotation (40) class delegation
2022-07-01 05:22:00 【jinyangjie0】
Flowable Source code address :https://github.com/flowable/flowable-engine
Flowable-6.7.2 Source code comment address :https://github.com/solojin/flowable-6.7.2-annotated
Package path :org.activiti.engine.impl.bpmn.helper
ClassDelegate Class delegation
Inherit TaskListener、ExecutionListener Such as the interface
/** * Used to allow class delegation bpmn Constructed helper class . * * When needed at runtime , This class will lazily instantiate the referenced class . * * @author Joram Barrez * @author Falko Menge * @author Saeid Mirzaei */
public class ClassDelegate extends AbstractBpmnActivityBehavior implements TaskListener, ExecutionListener, SubProcessActivityBehavior {
protected String className;
protected List<FieldDeclaration> fieldDeclarations;
protected ExecutionListener executionListenerInstance;
protected TaskListener taskListenerInstance;
protected ActivityBehavior activityBehaviorInstance;
protected Expression skipExpression;
protected List<MapExceptionEntry> mapExceptions;
protected String serviceTaskId;
public ClassDelegate(String className, List<FieldDeclaration> fieldDeclarations, Expression skipExpression) {
this.className = className;
this.fieldDeclarations = fieldDeclarations;
this.skipExpression = skipExpression;
}
public ClassDelegate(String id, String className, List<FieldDeclaration> fieldDeclarations, Expression skipExpression, List<MapExceptionEntry> mapExceptions) {
this(className, fieldDeclarations, skipExpression);
this.serviceTaskId = id;
this.mapExceptions = mapExceptions;
}
public ClassDelegate(String className, List<FieldDeclaration> fieldDeclarations) {
this(className, fieldDeclarations, null);
}
public ClassDelegate(Class<?> clazz, List<FieldDeclaration> fieldDeclarations) {
this(clazz.getName(), fieldDeclarations, null);
}
public ClassDelegate(Class<?> clazz, List<FieldDeclaration> fieldDeclarations, Expression skipExpression) {
this(clazz.getName(), fieldDeclarations, skipExpression);
}
// perform Monitor
@Override
public void notify(DelegateExecution execution) {
if (executionListenerInstance == null) {
executionListenerInstance = getExecutionListenerInstance();
}
Context.getProcessEngineConfiguration()
.getDelegateInterceptor()
.handleInvocation(new ExecutionListenerInvocation(executionListenerInstance, execution));
}
protected ExecutionListener getExecutionListenerInstance() {
Object delegateInstance = instantiateDelegate(className, fieldDeclarations);
if (delegateInstance instanceof ExecutionListener) {
return (ExecutionListener) delegateInstance;
} else if (delegateInstance instanceof JavaDelegate) {
return new ServiceTaskJavaDelegateActivityBehavior((JavaDelegate) delegateInstance, skipExpression);
} else {
throw new ActivitiIllegalArgumentException(delegateInstance.getClass().getName() + " doesn't implement " + ExecutionListener.class + " nor " + JavaDelegate.class);
}
}
// Mission Monitor
@Override
public void notify(DelegateTask delegateTask) {
if (taskListenerInstance == null) {
taskListenerInstance = getTaskListenerInstance();
}
try {
Context.getProcessEngineConfiguration()
.getDelegateInterceptor()
.handleInvocation(new TaskListenerInvocation(taskListenerInstance, delegateTask));
} catch (Exception e) {
throw new ActivitiException("Exception while invoking TaskListener: " + e.getMessage(), e);
}
}
protected TaskListener getTaskListenerInstance() {
Object delegateInstance = instantiateDelegate(className, fieldDeclarations);
if (delegateInstance instanceof TaskListener) {
return (TaskListener) delegateInstance;
} else {
throw new ActivitiIllegalArgumentException(delegateInstance.getClass().getName() + " doesn't implement " + TaskListener.class);
}
}
// Activity behavior
@Override
public void execute(DelegateExecution execution) {
ActivityExecution activityExecution = (ActivityExecution) execution;
if (Context.getProcessEngineConfiguration().isEnableProcessDefinitionInfoCache()) {
ObjectNode taskElementProperties = Context.getBpmnOverrideElementProperties(serviceTaskId, execution.getProcessDefinitionId());
if (taskElementProperties != null && taskElementProperties.has(DynamicBpmnConstants.SERVICE_TASK_CLASS_NAME)) {
String overrideClassName = taskElementProperties.get(DynamicBpmnConstants.SERVICE_TASK_CLASS_NAME).asText();
if (StringUtils.isNotEmpty(overrideClassName) && !overrideClassName.equals(className)) {
className = overrideClassName;
activityBehaviorInstance = null;
}
}
}
if (activityBehaviorInstance == null) {
activityBehaviorInstance = getActivityBehaviorInstance(activityExecution);
}
try {
activityBehaviorInstance.execute(execution);
} catch (BpmnError error) {
ErrorPropagation.propagateError(error, activityExecution);
} catch (RuntimeException e) {
if (!ErrorPropagation.mapException(e, activityExecution, mapExceptions))
throw e;
}
}
// Signal activity behavior
@Override
public void signal(ActivityExecution execution, String signalName, Object signalData) throws Exception {
if (activityBehaviorInstance == null) {
activityBehaviorInstance = getActivityBehaviorInstance(execution);
}
if (activityBehaviorInstance instanceof SignallableActivityBehavior) {
((SignallableActivityBehavior) activityBehaviorInstance).signal(execution, signalName, signalData);
} else {
throw new ActivitiException("signal() can only be called on a " + SignallableActivityBehavior.class.getName() + " instance");
}
}
// Subprocess activity behavior
@Override
public void completing(DelegateExecution execution, DelegateExecution subProcessInstance) throws Exception {
if (activityBehaviorInstance == null) {
activityBehaviorInstance = getActivityBehaviorInstance((ActivityExecution) execution);
}
if (activityBehaviorInstance instanceof SubProcessActivityBehavior) {
((SubProcessActivityBehavior) activityBehaviorInstance).completing(execution, subProcessInstance);
} else {
throw new ActivitiException("completing() can only be called on a " + SubProcessActivityBehavior.class.getName() + " instance");
}
}
@Override
public void completed(ActivityExecution execution) throws Exception {
if (activityBehaviorInstance == null) {
activityBehaviorInstance = getActivityBehaviorInstance(execution);
}
if (activityBehaviorInstance instanceof SubProcessActivityBehavior) {
((SubProcessActivityBehavior) activityBehaviorInstance).completed(execution);
} else {
throw new ActivitiException("completed() can only be called on a " + SubProcessActivityBehavior.class.getName() + " instance");
}
}
protected ActivityBehavior getActivityBehaviorInstance(ActivityExecution execution) {
Object delegateInstance = instantiateDelegate(className, fieldDeclarations);
if (delegateInstance instanceof ActivityBehavior) {
return determineBehaviour((ActivityBehavior) delegateInstance, execution);
} else if (delegateInstance instanceof JavaDelegate) {
return determineBehaviour(new ServiceTaskJavaDelegateActivityBehavior((JavaDelegate) delegateInstance, skipExpression), execution);
} else {
throw new ActivitiIllegalArgumentException(delegateInstance.getClass().getName() + " doesn't implement " + JavaDelegate.class.getName() + " nor " + ActivityBehavior.class.getName());
}
}
// if necessary , Add properties to a given delegate instance ( For example, multiple instances )
protected ActivityBehavior determineBehaviour(ActivityBehavior delegateInstance, ActivityExecution execution) {
if (hasMultiInstanceCharacteristics()) {
multiInstanceActivityBehavior.setInnerActivityBehavior((AbstractBpmnActivityBehavior) delegateInstance);
return multiInstanceActivityBehavior;
}
return delegateInstance;
}
protected Object instantiateDelegate(String className, List<FieldDeclaration> fieldDeclarations) {
return ClassDelegate.defaultInstantiateDelegate(className, fieldDeclarations);
}
// -- Assistant method ( It can also be used by external classes ) ----------------------------------------
public static Object defaultInstantiateDelegate(Class<?> clazz, List<FieldDeclaration> fieldDeclarations) {
return defaultInstantiateDelegate(clazz.getName(), fieldDeclarations);
}
public static Object defaultInstantiateDelegate(String className, List<FieldDeclaration> fieldDeclarations) {
Object object = ReflectUtil.instantiate(className);
applyFieldDeclaration(fieldDeclarations, object);
return object;
}
public static void applyFieldDeclaration(List<FieldDeclaration> fieldDeclarations, Object target) {
applyFieldDeclaration(fieldDeclarations, target, true);
}
public static void applyFieldDeclaration(List<FieldDeclaration> fieldDeclarations, Object target, boolean throwExceptionOnMissingField) {
if (fieldDeclarations != null) {
for (FieldDeclaration declaration : fieldDeclarations) {
applyFieldDeclaration(declaration, target, throwExceptionOnMissingField);
}
}
}
public static void applyFieldDeclaration(FieldDeclaration declaration, Object target) {
applyFieldDeclaration(declaration, target, true);
}
public static void applyFieldDeclaration(FieldDeclaration declaration, Object target, boolean throwExceptionOnMissingField) {
Method setterMethod = ReflectUtil.getSetter(declaration.getName(),
target.getClass(), declaration.getValue().getClass());
if (setterMethod != null) {
try {
setterMethod.invoke(target, declaration.getValue());
} catch (IllegalArgumentException e) {
throw new ActivitiException("Error while invoking '" + declaration.getName() + "' on class " + target.getClass().getName(), e);
} catch (IllegalAccessException e) {
throw new ActivitiException("Illegal access when calling '" + declaration.getName() + "' on class " + target.getClass().getName(), e);
} catch (InvocationTargetException e) {
throw new ActivitiException("Exception while invoking '" + declaration.getName() + "' on class " + target.getClass().getName(), e);
}
} else {
Field field = ReflectUtil.getField(declaration.getName(), target);
if (field == null) {
if (throwExceptionOnMissingField) {
throw new ActivitiIllegalArgumentException("Field definition uses unexisting field '" + declaration.getName() + "' on class " + target.getClass().getName());
} else {
return;
}
}
// Check whether the type of the delegate field is correct
if (!fieldTypeCompatible(declaration, field)) {
throw new ActivitiIllegalArgumentException("Incompatible type set on field declaration '" + declaration.getName()
+ "' for class " + target.getClass().getName()
+ ". Declared value has type " + declaration.getValue().getClass().getName()
+ ", while expecting " + field.getType().getName());
}
ReflectUtil.setField(field, target, declaration.getValue());
}
}
public static boolean fieldTypeCompatible(FieldDeclaration declaration, Field field) {
if (declaration.getValue() != null) {
return field.getType().isAssignableFrom(declaration.getValue().getClass());
} else {
// Null Indicates that it can be set to any field type
return true;
}
}
/** * Return this delegate class {@link ClassDelegate} Configured class name . If you want to check which delegate classes you already have , For example, in the listener list , It's very useful */
public String getClassName() {
return className;
}
}
边栏推荐
- Global and Chinese market of 3D design and modeling software 2022-2028: Research Report on technology, participants, trends, market size and share
- Rainbow combines neuvector to practice container safety management
- Global and Chinese markets of Ethernet communication modules 2022-2028: Research Report on technology, participants, trends, market size and share
- Things generated by busybox
- 0xc000007b the application cannot start the solution normally (the pro test is valid)
- Rust基础入门之变量绑定与解构
- [daily question in summer] Luogu p1568 race
- Design and application of immutable classes
- Dynamic verification of new form items in El form; El form verifies that the dynamic form V-IF does not take effect;
- Single page application
猜你喜欢

Things generated by busybox

导电滑环短路的原因以及应对措施
![Is there any good website or software for learning programming? [introduction to programming]?](/img/ae/68a5880f313c307880ac80bd200530.jpg)
Is there any good website or software for learning programming? [introduction to programming]?

C WPF uses dockpanel to realize screenshot box

智慧运维:基于 BIM 技术的可视化管理系统

Leetcode1497- check whether array pairs can be divided by K - array - hash table - count

Redis数据库的部署及常用命令

Use and principle of reentrantlock

Usage and principle of synchronized
![[NLP Li Hongyi] notes](/img/8e/a51ca5eee638facd54270fb28d2fce.jpg)
[NLP Li Hongyi] notes
随机推荐
Numeric amount plus comma; JS two methods of adding three digits and a comma to numbers; JS data formatting
el-cascader回显失败;el-cascader回显不出来
Solution: drag the Xib control to the code file, and an error setvalue:forundefined key:this class is not key value coding compliant for the key is reported
Leetcode1497- check whether array pairs can be divided by K - array - hash table - count
Youqitong [vip] v3.7.2022.0106 official January 22 Edition
Global and Chinese market of high-end home theater 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of 3D CAD 2022-2028: Research Report on technology, participants, trends, market size and share
AcWing 886. Finding combinatorial number II (pretreatment factorial)
Is there any good website or software for learning programming? [introduction to programming]?
Manually implement a simple stack
LRU cache for leveldb source code analysis
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Use of STM32 expansion board temperature sensor and temperature humidity sensor
QDataStream的简单读写验证
Variable binding and deconstruction for rudimentary rust
液压滑环的特点讲解
[daily question in summer] Luogu p7222 [rc-04] informatics competition
数字金额加逗号;js给数字加三位一逗号间隔的两种方法;js数据格式化
Spanner 论文小结
每日一题-LeetCode1175-质数排列-数学