Skip to content

Commit 5c5f494

Browse files
committed
Disable the JUEL script engine lang:import function by default and allow configuring the script output writer
* The lang:import function in the JUEL script engine is no longer registered by default, it can be enabled through JuelScriptEngineFactory#setImportFunctionEnabled * The JUEL out:print function now writes to the writer of the script context instead of always using System.out * Add JSR223FlowableScriptEngine#setScriptOutputWriter to configure the writer used for script output (e.g. to discard it or keep it in memory) (cherry picked from commit 51a9c65)
1 parent 4bd2816 commit 5c5f494

8 files changed

Lines changed: 263 additions & 9 deletions

File tree

‎modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/scripting/JSR223FlowableScriptEngine.java‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.flowable.common.engine.impl.scripting;
1414

15+
import java.io.Writer;
1516
import java.util.Collections;
1617
import java.util.HashMap;
1718
import java.util.List;
@@ -23,6 +24,7 @@
2324
import javax.script.ScriptEngineFactory;
2425
import javax.script.ScriptEngineManager;
2526
import javax.script.ScriptException;
27+
import javax.script.SimpleScriptContext;
2628

2729
import org.apache.commons.lang3.StringUtils;
2830
import org.flowable.common.engine.api.FlowableException;
@@ -47,6 +49,12 @@ public class JSR223FlowableScriptEngine implements FlowableScriptEngine {
4749
protected boolean cacheScriptingEngines = true;
4850
protected Map<String, ScriptEngine> cachedEngines;
4951

52+
/**
53+
* The writer used by the scripts for their output (e.g. {@code println} in Groovy, {@code out:print} in JUEL).
54+
* When not set, the writer of the script engine is used, which is usually {@code System.out}.
55+
*/
56+
protected Writer scriptOutputWriter;
57+
5058
public JSR223FlowableScriptEngine() {
5159
this(new ScriptEngineManager());
5260
}
@@ -132,6 +140,18 @@ public ScriptEngineManager getScriptEngineManager() {
132140
return scriptEngineManager;
133141
}
134142

143+
public Writer getScriptOutputWriter() {
144+
return scriptOutputWriter;
145+
}
146+
147+
/**
148+
* Set the writer used by the scripts for their output.
149+
* Use {@link Writer#nullWriter()} to discard the output, or e.g. a {@link java.io.StringWriter} to keep it in memory.
150+
*/
151+
public void setScriptOutputWriter(Writer scriptOutputWriter) {
152+
this.scriptOutputWriter = scriptOutputWriter;
153+
}
154+
135155

136156
protected class JavaScriptingFlowableScriptEvaluationRequest implements FlowableScriptEvaluationRequest {
137157

@@ -197,7 +217,12 @@ public ScriptEvaluation evaluate() throws FlowableScriptException {
197217
ScriptEngine scriptEngine = getEngineByName(language);
198218
Bindings bindings = createBindings();
199219
try {
200-
Object result = scriptEngine.eval(script, bindings);
220+
Object result;
221+
if (scriptOutputWriter != null) {
222+
result = scriptEngine.eval(script, createScriptContext(scriptEngine, bindings));
223+
} else {
224+
result = scriptEngine.eval(script, bindings);
225+
}
201226
return new ScriptEvaluationImpl(resolver, result);
202227
} catch (ScriptException e) {
203228
throw new FlowableScriptException(e.getMessage(), e);
@@ -207,5 +232,19 @@ public ScriptEvaluation evaluate() throws FlowableScriptException {
207232
protected Bindings createBindings() {
208233
return new ScriptBindings(Collections.singletonList(resolver), scopeContainer, inputVariableContainer, storeScriptVariables);
209234
}
235+
236+
/**
237+
* Creates the same script context as {@link javax.script.AbstractScriptEngine#eval(String, Bindings)} would, but with the configured script output writer.
238+
*/
239+
protected ScriptContext createScriptContext(ScriptEngine scriptEngine, Bindings bindings) {
240+
ScriptContext engineContext = scriptEngine.getContext();
241+
ScriptContext scriptContext = new SimpleScriptContext();
242+
scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
243+
scriptContext.setBindings(engineContext.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE);
244+
scriptContext.setReader(engineContext.getReader());
245+
scriptContext.setWriter(scriptOutputWriter);
246+
scriptContext.setErrorWriter(engineContext.getErrorWriter());
247+
return scriptContext;
248+
}
210249
}
211250
}

‎modules/flowable-engine/src/main/java/org/flowable/engine/impl/scripting/JuelScriptEngine.java‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.io.IOException;
1717
import java.io.Reader;
18+
import java.io.Writer;
1819
import java.lang.reflect.Method;
1920
import java.lang.reflect.Modifier;
2021

@@ -59,11 +60,28 @@
5960
*/
6061
public class JuelScriptEngine extends AbstractScriptEngine implements Compilable {
6162

63+
/**
64+
* The script context of the expression that is currently being evaluated on this thread.
65+
* EL functions are static methods, so this is how {@link #print(Object)} gets access to the writer of the script context.
66+
*/
67+
protected static final ThreadLocal<ScriptContext> CURRENT_SCRIPT_CONTEXT = new ThreadLocal<>();
68+
6269
private ScriptEngineFactory scriptEngineFactory;
6370
private ExpressionFactory expressionFactory;
6471

72+
/**
73+
* Whether the {@code lang:import} function is available in scripts.
74+
* It allows loading any class and calling any of its public static methods, so it is disabled by default.
75+
*/
76+
protected boolean importFunctionEnabled;
77+
6578
public JuelScriptEngine(ScriptEngineFactory scriptEngineFactory) {
79+
this(scriptEngineFactory, false);
80+
}
81+
82+
public JuelScriptEngine(ScriptEngineFactory scriptEngineFactory, boolean importFunctionEnabled) {
6683
this.scriptEngineFactory = scriptEngineFactory;
84+
this.importFunctionEnabled = importFunctionEnabled;
6785
// Resolve the ExpressionFactory
6886
expressionFactory = ExpressionFactoryResolver.resolveExpressionFactory();
6987
}
@@ -111,10 +129,18 @@ public Bindings createBindings() {
111129
}
112130

113131
private Object evaluateExpression(ValueExpression expr, ScriptContext ctx) throws ScriptException {
132+
ScriptContext previousScriptContext = CURRENT_SCRIPT_CONTEXT.get();
133+
CURRENT_SCRIPT_CONTEXT.set(ctx);
114134
try {
115135
return expr.getValue(createElContext(ctx));
116136
} catch (ELException elexp) {
117137
throw new ScriptException(elexp);
138+
} finally {
139+
if (previousScriptContext != null) {
140+
CURRENT_SCRIPT_CONTEXT.set(previousScriptContext);
141+
} else {
142+
CURRENT_SCRIPT_CONTEXT.remove();
143+
}
118144
}
119145
}
120146

@@ -165,8 +191,7 @@ private ELContext createElContext(final ScriptContext scriptCtx) {
165191
// Built-in function are added to ScriptCtx
166192
scriptCtx.setAttribute("out:print", getPrintMethod(), ScriptContext.ENGINE_SCOPE);
167193

168-
SecurityManager securityManager = System.getSecurityManager();
169-
if (securityManager == null) {
194+
if (importFunctionEnabled) {
170195
scriptCtx.setAttribute("lang:import", getImportMethod(), ScriptContext.ENGINE_SCOPE);
171196
}
172197

@@ -206,7 +231,19 @@ private static Method getPrintMethod() {
206231
}
207232

208233
public static void print(Object object) {
209-
System.out.print(object);
234+
ScriptContext scriptContext = CURRENT_SCRIPT_CONTEXT.get();
235+
Writer writer = scriptContext != null ? scriptContext.getWriter() : null;
236+
if (writer == null) {
237+
System.out.print(object);
238+
return;
239+
}
240+
241+
try {
242+
writer.write(String.valueOf(object));
243+
writer.flush();
244+
} catch (IOException e) {
245+
throw new ELException(e);
246+
}
210247
}
211248

212249
private static Method getImportMethod() {

‎modules/flowable-engine/src/main/java/org/flowable/engine/impl/scripting/JuelScriptEngineFactory.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public class JuelScriptEngineFactory implements ScriptEngineFactory {
3131
private static List<String> extensions;
3232
private static List<String> mimeTypes;
3333

34+
/**
35+
* Whether the {@code lang:import} function is available in the created engines.
36+
* It allows loading any class and calling any of its public static methods, so it is disabled by default.
37+
*/
38+
protected boolean importFunctionEnabled;
39+
3440
static {
3541
names = Collections.unmodifiableList(Collections.singletonList("juel"));
3642
extensions = names;
@@ -137,7 +143,15 @@ public String getProgram(String... statements) {
137143

138144
@Override
139145
public ScriptEngine getScriptEngine() {
140-
return new JuelScriptEngine(this);
146+
return new JuelScriptEngine(this, importFunctionEnabled);
147+
}
148+
149+
public boolean isImportFunctionEnabled() {
150+
return importFunctionEnabled;
151+
}
152+
153+
public void setImportFunctionEnabled(boolean importFunctionEnabled) {
154+
this.importFunctionEnabled = importFunctionEnabled;
141155
}
142156

143157
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.engine.impl.scripting;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
17+
18+
import java.io.StringWriter;
19+
20+
import javax.script.ScriptContext;
21+
import javax.script.ScriptEngine;
22+
import javax.script.ScriptException;
23+
import javax.script.SimpleScriptContext;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
/**
28+
* @author Filip Hrisafov
29+
*/
30+
class JuelScriptEngineTest {
31+
32+
@Test
33+
void importFunctionIsDisabledByDefault() {
34+
ScriptEngine scriptEngine = new JuelScriptEngineFactory().getScriptEngine();
35+
ScriptContext scriptContext = new SimpleScriptContext();
36+
37+
assertThatThrownBy(() -> scriptEngine.eval("${lang:import(context, 'math', 'java.lang.Math')}", scriptContext))
38+
.isInstanceOf(ScriptException.class)
39+
.hasMessageContaining("lang:import");
40+
assertThat(scriptContext.getAttribute("lang:import")).isNull();
41+
}
42+
43+
@Test
44+
void importFunctionCanBeEnabled() throws ScriptException {
45+
JuelScriptEngineFactory scriptEngineFactory = new JuelScriptEngineFactory();
46+
scriptEngineFactory.setImportFunctionEnabled(true);
47+
ScriptEngine scriptEngine = scriptEngineFactory.getScriptEngine();
48+
ScriptContext scriptContext = new SimpleScriptContext();
49+
50+
scriptEngine.eval("${lang:import(context, 'integer', 'java.lang.Integer')}", scriptContext);
51+
52+
assertThat(scriptEngine.eval("${integer:toHexString(255)}", scriptContext)).isEqualTo("ff");
53+
}
54+
55+
@Test
56+
void printWritesToScriptContextWriter() throws ScriptException {
57+
ScriptEngine scriptEngine = new JuelScriptEngineFactory().getScriptEngine();
58+
ScriptContext scriptContext = new SimpleScriptContext();
59+
StringWriter writer = new StringWriter();
60+
scriptContext.setWriter(writer);
61+
scriptContext.setAttribute("name", "Kermit", ScriptContext.ENGINE_SCOPE);
62+
63+
scriptEngine.eval("${out:print('Hello ')}${out:print(name)}", scriptContext);
64+
65+
assertThat(writer).hasToString("Hello Kermit");
66+
}
67+
68+
}

‎modules/flowable-engine/src/test/java/org/flowable/examples/bpmn/scripttask/ScriptTaskTest.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1818
import static org.assertj.core.api.Assertions.entry;
1919

20+
import java.io.StringWriter;
21+
import java.util.Collections;
2022
import java.util.HashMap;
2123
import java.util.Map;
2224

2325
import org.flowable.common.engine.api.FlowableException;
2426
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
2527
import org.flowable.common.engine.impl.scripting.FlowableScriptEvaluationException;
28+
import org.flowable.common.engine.impl.scripting.JSR223FlowableScriptEngine;
2629
import org.flowable.common.engine.impl.util.CollectionUtil;
2730
import org.flowable.engine.impl.test.PluggableFlowableTestCase;
2831
import org.flowable.engine.repository.ProcessDefinition;
@@ -232,4 +235,19 @@ public void testSkipExpressionDisabled() {
232235
.containsOnly(entry("persistentResult", "success"));
233236
}
234237

238+
@Test
239+
@Deployment
240+
public void testScriptOutputWriter() {
241+
JSR223FlowableScriptEngine scriptEngine = (JSR223FlowableScriptEngine) processEngineConfiguration.getScriptEngine();
242+
StringWriter writer = new StringWriter();
243+
scriptEngine.setScriptOutputWriter(writer);
244+
try {
245+
runtimeService.startProcessInstanceByKey("scriptOutputWriter", Collections.singletonMap("name", "Kermit"));
246+
247+
assertThat(writer.toString()).isEqualToNormalizingNewlines("Hello Kermit from Groovy\n");
248+
} finally {
249+
scriptEngine.setScriptOutputWriter(null);
250+
}
251+
}
252+
235253
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<definitions id="definitions"
3+
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
4+
xmlns:flowable="http://flowable.org/bpmn"
5+
targetNamespace="Examples">
6+
7+
<process id="scriptOutputWriter">
8+
9+
<startEvent id="theStart" />
10+
11+
<scriptTask id="juelScriptTask" scriptFormat="juel">
12+
<script>${out:print('Hello ')}${out:print(name)}</script>
13+
</scriptTask>
14+
15+
<scriptTask id="groovyScriptTask" scriptFormat="groovy">
16+
<script>println " from Groovy"</script>
17+
</scriptTask>
18+
19+
<endEvent id="theEnd" />
20+
21+
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="juelScriptTask" />
22+
<sequenceFlow id="flow2" sourceRef="juelScriptTask" targetRef="groovyScriptTask" />
23+
<sequenceFlow id="flow3" sourceRef="groovyScriptTask" targetRef="theEnd" />
24+
25+
</process>
26+
27+
</definitions>

0 commit comments

Comments
 (0)