Skip to content

Commit

Permalink
flowable#3884: Use ScriptCondition instead of ScriptEngineExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmusfaber committed Aug 28, 2024
1 parent 5da58be commit 2bb710a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.flowable.common.engine.impl.javax.el.MapELResolver;
import org.flowable.common.engine.impl.javax.el.ValueExpression;
import org.flowable.common.engine.impl.persistence.deploy.DeploymentCache;
import org.flowable.common.engine.impl.scripting.ScriptingEngines;

/**
* Default {@link ExpressionManager} implementation that contains the logic for creating
Expand All @@ -44,7 +43,6 @@
*/
public class DefaultExpressionManager implements ExpressionManager {

public static final String JUEL_EXPRESSION_LANGUAGE = "juel";
protected ExpressionFactory expressionFactory;
protected List<FlowableFunctionDelegate> functionDelegates;
protected FlowableFunctionResolver functionResolver;
Expand Down Expand Up @@ -72,60 +70,44 @@ public DefaultExpressionManager(Map<Object, Object> beans) {

@Override
public Expression createExpression(String text) {
return createExpression(text, ScriptingEngines.DEFAULT_SCRIPTING_LANGUAGE);
}

@Override
public Expression createExpression(String text, String language) {
String expressionText = text.trim();

if (language == null || ScriptingEngines.DEFAULT_SCRIPTING_LANGUAGE.equals(language)) {
return createJuelExpression(expressionText);
} else {
return createScriptEngineExpression(expressionText, language);
}
}

protected boolean isCacheEnabled(String text) {
return expressionCache != null && (expressionTextLengthCacheLimit < 0 || text.length() <= expressionTextLengthCacheLimit);
}

protected Expression createJuelExpression(String expressionText) {
if (isCacheEnabled(expressionText)) {
Expression cachedExpression = expressionCache.get(expressionText);
if (isCacheEnabled(text)) {
Expression cachedExpression = expressionCache.get(text);
if (cachedExpression != null) {
return cachedExpression;
}
}

if (parsingElContext == null) {
this.parsingElContext = new ParsingElContext(functionResolver);
} else if (parsingElContext.getFunctionMapper() != null && parsingElContext.getFunctionMapper() instanceof FlowableFunctionMapper) {
((FlowableFunctionMapper) parsingElContext.getFunctionMapper()).setFunctionResolver(functionResolver);
}

String expressionText = text.trim();

ValueExpression valueExpression = expressionFactory.createValueExpression(parsingElContext, expressionText, Object.class);
Expression expression = createJuelExpression(expressionText, valueExpression);
Expression expression = createJuelExpression(text, valueExpression);

if (isCacheEnabled(expressionText)) {
expressionCache.add(expressionText, expression);
if (isCacheEnabled(text)) {
expressionCache.add(text, expression);
}

return expression;
}

protected Expression createJuelExpression(String expression, ValueExpression valueExpression) {
return new JuelExpression(this, valueExpression, expression);
protected boolean isCacheEnabled(String text) {
return expressionCache != null && (expressionTextLengthCacheLimit < 0 || text.length() <= expressionTextLengthCacheLimit);
}

private Expression createScriptEngineExpression(String text, String language) {
return new ScriptEngineExpression(text, language);
protected Expression createJuelExpression(String expression, ValueExpression valueExpression) {
return new JuelExpression(this, valueExpression, expression);
}

public void setExpressionFactory(ExpressionFactory expressionFactory) {
this.expressionFactory = expressionFactory;
}


@Override
public ELContext getElContext(VariableContainer variableContainer) {
ELResolver elResolver = getOrCreateStaticElResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ public interface ExpressionManager {
*/
Expression createExpression(String expression);

/**
* Creates an {@link Expression} instance from the given String using the specified scripting language.
* Expression are resolved against a {@link VariableContainer} (e.g. a process Execution, a case instance plan item, etc.)
*/
Expression createExpression(String expression, String expressionLanguage);

/**
* Creates an {@link ELContext} against which {@link Expression} instance can be resolved.
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.debug.ExecutionTreeUtil;
import org.flowable.engine.impl.Condition;
import org.flowable.engine.impl.delegate.ActivityBehavior;
import org.flowable.engine.impl.delegate.TriggerableActivityBehavior;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.engine.impl.scripting.ScriptCondition;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;

Expand Down Expand Up @@ -92,12 +94,9 @@ protected void evaluateEventSubProcesses(List<EventSubProcess> eventSubProcesses
boolean conditionIsTrue = false;
String conditionExpression = conditionalEventDefinition.getConditionExpression();
if (StringUtils.isNotEmpty(conditionExpression)) {
String conditionLanguage = conditionalEventDefinition.getConditionLanguage();
Expression expression = CommandContextUtil.getProcessEngineConfiguration(commandContext).getExpressionManager().createExpression(conditionExpression, conditionLanguage);
Object result = expression.getValue(parentExecution);
if (result instanceof Boolean && (Boolean) result) {
conditionIsTrue = true;
}
String conditionLanguage = conditionalEventDefinition.getConditionLanguage();
Condition condition = new ScriptCondition(conditionExpression, conditionLanguage);
conditionIsTrue = condition.evaluate(startEvent.getId(), parentExecution);

} else {
conditionIsTrue = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.event.impl.FlowableEventBuilder;
import org.flowable.engine.impl.Condition;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.scripting.ScriptCondition;
import org.flowable.engine.impl.util.CommandContextUtil;

/**
Expand Down Expand Up @@ -61,9 +63,9 @@ public void trigger(DelegateExecution execution, String triggerName, Object trig
ExecutionEntity executionEntity = (ExecutionEntity) execution;

ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
Expression expression = processEngineConfiguration.getExpressionManager().createExpression(conditionExpression, conditionLanguage);
Object result = expression.getValue(execution);
if (result instanceof Boolean && (Boolean) result) {
Condition condition = new ScriptCondition(conditionExpression, conditionLanguage);
boolean result = condition.evaluate(executionEntity.getActivityId(), executionEntity);
if (result) {
processEngineConfiguration.getActivityInstanceEntityManager().recordActivityStart(executionEntity);

FlowableEventDispatcher eventDispatcher = processEngineConfiguration.getEventDispatcher();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.flowable.engine.impl.scripting;

import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.common.engine.impl.scripting.ScriptEngineRequest;
import org.flowable.common.engine.impl.scripting.ScriptingEngines;
import org.flowable.engine.delegate.DelegateExecution;
Expand All @@ -34,20 +35,26 @@ public ScriptCondition(String expression, String language) {

@Override
public boolean evaluate(String sequenceFlowId, DelegateExecution execution) {
ScriptingEngines scriptingEngines = CommandContextUtil.getProcessEngineConfiguration().getScriptingEngines();

ScriptEngineRequest.Builder builder = ScriptEngineRequest.builder()
.script(expression)
.language(language)
.variableContainer(execution);
Object result = scriptingEngines.evaluate(builder.build()).getResult();
if (result == null) {
throw new FlowableException("condition script returns null: " + expression + " for " + execution);
}
if (!(result instanceof Boolean)) {
throw new FlowableException("condition script returns non-Boolean: " + result + " (" + result.getClass().getName() + ") for " + execution);
}
return (Boolean) result;
Object result;
if (language == null) {
result = CommandContextUtil.getProcessEngineConfiguration().getExpressionManager().createExpression(expression).getValue(execution);
} else {
ScriptingEngines scriptingEngines = CommandContextUtil.getProcessEngineConfiguration().getScriptingEngines();

ScriptEngineRequest.Builder builder = ScriptEngineRequest.builder()
.script(expression)
.language(language)
.variableContainer(execution);
result = scriptingEngines.evaluate(builder.build()).getResult();
}

if (result == null) {
throw new FlowableException("condition script returns null: " + expression + " for " + execution);
}
if (!(result instanceof Boolean)) {
throw new FlowableException("condition script returns non-Boolean: " + result + " (" + result.getClass().getName() + ") for " + execution);
}
return (Boolean) result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@
*/
package org.flowable.engine.impl.util.condition;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.engine.DynamicBpmnConstants;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.Condition;
import org.flowable.engine.impl.context.BpmnOverrideContext;
import org.flowable.engine.impl.el.UelExpressionCondition;
import org.flowable.engine.impl.scripting.ScriptCondition;
import org.flowable.engine.impl.util.CommandContextUtil;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
* @author Joram Barrez
* @author Tijs Rademakers
Expand All @@ -42,9 +40,7 @@ public static boolean hasTrueCondition(SequenceFlow sequenceFlow, DelegateExecut
}

if (StringUtils.isNotEmpty(conditionExpression)) {

Expression expression = CommandContextUtil.getProcessEngineConfiguration().getExpressionManager().createExpression(conditionExpression, conditionLanguage);
Condition condition = new UelExpressionCondition(expression);
Condition condition = new ScriptCondition(conditionExpression, conditionLanguage);
return condition.evaluate(sequenceFlow.getId(), execution);
} else {
return true;
Expand Down

0 comments on commit 2bb710a

Please sign in to comment.