Skip to content

Commit 51a9c65

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)
1 parent 5beefc4 commit 51a9c65

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

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

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

73+
/**
74+
* Whether the {@code lang:import} function is available in scripts.
75+
* It allows loading any class and calling any of its public static methods, so it is disabled by default.
76+
*/
77+
protected boolean importFunctionEnabled;
78+
6679
public JuelScriptEngine(ScriptEngineFactory scriptEngineFactory) {
80+
this(scriptEngineFactory, false);
81+
}
82+
83+
public JuelScriptEngine(ScriptEngineFactory scriptEngineFactory, boolean importFunctionEnabled) {
6784
this.scriptEngineFactory = scriptEngineFactory;
85+
this.importFunctionEnabled = importFunctionEnabled;
6886
// Resolve the ExpressionFactory
6987
expressionFactory = ExpressionFactoryResolver.resolveExpressionFactory();
7088
}
@@ -112,10 +130,18 @@ public Bindings createBindings() {
112130
}
113131

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

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

170-
SecurityManager securityManager = System.getSecurityManager();
171-
if (securityManager == null) {
196+
if (importFunctionEnabled) {
172197
scriptCtx.setAttribute("lang:import", getImportMethod(), ScriptContext.ENGINE_SCOPE);
173198
}
174199

@@ -208,7 +233,19 @@ private static Method getPrintMethod() {
208233
}
209234

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

214251
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;
@@ -246,4 +249,19 @@ public void testSkipExpressionDisabled() {
246249
.containsOnly(entry("persistentResult", "success"));
247250
}
248251

252+
@Test
253+
@Deployment
254+
public void testScriptOutputWriter() {
255+
JSR223FlowableScriptEngine scriptEngine = (JSR223FlowableScriptEngine) processEngineConfiguration.getScriptEngine();
256+
StringWriter writer = new StringWriter();
257+
scriptEngine.setScriptOutputWriter(writer);
258+
try {
259+
runtimeService.startProcessInstanceByKey("scriptOutputWriter", Collections.singletonMap("name", "Kermit"));
260+
261+
assertThat(writer.toString()).isEqualToNormalizingNewlines("Hello Kermit from Groovy\n");
262+
} finally {
263+
scriptEngine.setScriptOutputWriter(null);
264+
}
265+
}
266+
249267
}
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)