1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jexl3.scripting;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.io.Reader;
24 import java.io.Writer;
25 import java.lang.ref.Reference;
26 import java.lang.ref.SoftReference;
27
28 import javax.script.AbstractScriptEngine;
29 import javax.script.Bindings;
30 import javax.script.Compilable;
31 import javax.script.CompiledScript;
32 import javax.script.ScriptContext;
33 import javax.script.ScriptEngine;
34 import javax.script.ScriptEngineFactory;
35 import javax.script.ScriptException;
36 import javax.script.SimpleBindings;
37
38 import org.apache.commons.jexl3.JexlBuilder;
39 import org.apache.commons.jexl3.JexlContext;
40 import org.apache.commons.jexl3.JexlEngine;
41 import org.apache.commons.jexl3.JexlException;
42 import org.apache.commons.jexl3.JexlScript;
43 import org.apache.commons.jexl3.introspection.JexlPermissions;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class JexlScriptEngine extends AbstractScriptEngine implements Compilable {
68
69
70
71 private static final class FactorySingletonHolder {
72
73 static final JexlScriptEngineFactory DEFAULT_FACTORY = new JexlScriptEngineFactory();
74
75
76 private FactorySingletonHolder() {}
77 }
78
79
80
81
82 private final class JexlCompiledScript extends CompiledScript {
83
84 private final JexlScript script;
85
86
87
88
89
90
91 JexlCompiledScript(final JexlScript theScript) {
92 script = theScript;
93 }
94
95 @Override
96 public Object eval(final ScriptContext context) throws ScriptException {
97
98 context.setAttribute(CONTEXT_KEY, context, ScriptContext.ENGINE_SCOPE);
99 try {
100 final JexlContext ctxt = new JexlContextWrapper(context);
101 return script.execute(ctxt);
102 } catch (final Exception e) {
103 throw scriptException(e);
104 }
105 }
106
107 @Override
108 public ScriptEngine getEngine() {
109 return JexlScriptEngine.this;
110 }
111
112 @Override
113 public String toString() {
114 return script.getSourceText();
115 }
116 }
117
118
119
120
121
122 private final class JexlContextWrapper implements JexlContext {
123
124 final ScriptContext scriptContext;
125
126
127
128
129
130
131 JexlContextWrapper (final ScriptContext theContext){
132 scriptContext = theContext;
133 }
134
135 @Override
136 public Object get(final String name) {
137 final Object o = scriptContext.getAttribute(name);
138 if (JEXL_OBJECT_KEY.equals(name)) {
139 if (o != null) {
140 LOG.warn("JEXL is a reserved variable name, user-defined value is ignored");
141 }
142 return jexlObject;
143 }
144 return o;
145 }
146
147 @Override
148 public boolean has(final String name) {
149 final Bindings bnd = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
150 return bnd.containsKey(name);
151 }
152
153 @Override
154 public void set(final String name, final Object value) {
155 int scope = scriptContext.getAttributesScope(name);
156 if (scope == -1) {
157 scope = ScriptContext.ENGINE_SCOPE;
158 }
159 scriptContext.getBindings(scope).put(name , value);
160 }
161
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 public class JexlScriptObject {
181
182
183
184
185
186
187
188
189
190 public JexlEngine getEngine() {
191 return jexlEngine;
192 }
193
194
195
196
197
198
199 public PrintWriter getErr() {
200 final Writer error = context.getErrorWriter();
201 if (error instanceof PrintWriter) {
202 return (PrintWriter) error;
203 }
204 if (error != null) {
205 return new PrintWriter(error, true);
206 }
207 return null;
208 }
209
210
211
212
213
214
215 public Reader getIn() {
216 return context.getReader();
217 }
218
219
220
221
222
223
224 public Log getLogger() {
225 return LOG;
226 }
227
228
229
230
231
232
233 public PrintWriter getOut() {
234 final Writer out = context.getWriter();
235 if (out instanceof PrintWriter) {
236 return (PrintWriter) out;
237 }
238 if (out != null) {
239 return new PrintWriter(out, true);
240 }
241 return null;
242 }
243
244
245
246
247
248
249 public Class<System> getSystem() {
250 return System.class;
251 }
252 }
253
254
255
256
257
258 private static Reference<JexlEngine> ENGINE;
259
260
261
262
263 private static JexlPermissions PERMISSIONS;
264
265
266 static final Log LOG = LogFactory.getLog(JexlScriptEngine.class);
267
268
269 static final int CACHE_SIZE = 512;
270
271
272 public static final String CONTEXT_KEY = "context";
273
274
275 public static final String JEXL_OBJECT_KEY = "JEXL";
276
277
278
279
280 private static JexlEngine getEngine() {
281 JexlEngine engine = ENGINE != null ? ENGINE.get() : null;
282 if (engine == null) {
283 synchronized (JexlScriptEngineFactory.class) {
284 engine = ENGINE != null ? ENGINE.get() : null;
285 if (engine == null) {
286 final JexlBuilder builder = new JexlBuilder()
287 .strict(true)
288 .safe(false)
289 .logger(JexlScriptEngine.LOG)
290 .cache(JexlScriptEngine.CACHE_SIZE);
291 if (PERMISSIONS != null ) {
292 builder.permissions(PERMISSIONS);
293 }
294 engine = builder.create();
295 ENGINE = new SoftReference<>(engine);
296 }
297 }
298 }
299 return engine;
300 }
301
302
303
304
305
306
307
308
309
310 private static String readerToString(final Reader scriptReader) throws ScriptException {
311 final StringBuilder buffer = new StringBuilder();
312 BufferedReader reader;
313 if (scriptReader instanceof BufferedReader) {
314 reader = (BufferedReader) scriptReader;
315 } else {
316 reader = new BufferedReader(scriptReader);
317 }
318 try {
319 String line;
320 while ((line = reader.readLine()) != null) {
321 buffer.append(line).append('\n');
322 }
323 return buffer.toString();
324 } catch (final IOException e) {
325 throw new ScriptException(e);
326 }
327 }
328
329 static ScriptException scriptException(final Exception e) {
330 Exception xany = e;
331
332 if (xany instanceof JexlException) {
333 final Throwable cause = xany.getCause();
334 if (cause instanceof Exception) {
335 xany = (Exception) cause;
336 }
337 }
338 return new ScriptException(xany);
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356 public static void setInstance(final JexlEngine engine) {
357 ENGINE = new SoftReference<>(engine);
358 }
359
360
361
362
363
364
365
366
367
368
369
370 public static void setPermissions(final JexlPermissions permissions) {
371 PERMISSIONS = permissions;
372 ENGINE = null;
373 }
374
375
376 final JexlScriptObject jexlObject;
377
378
379 final ScriptEngineFactory parentFactory;
380
381
382 final JexlEngine jexlEngine;
383
384
385
386
387
388
389
390 public JexlScriptEngine() {
391 this(FactorySingletonHolder.DEFAULT_FACTORY);
392 }
393
394
395
396
397
398
399
400 public JexlScriptEngine(final ScriptEngineFactory factory) {
401 if (factory == null) {
402 throw new NullPointerException("ScriptEngineFactory must not be null");
403 }
404 parentFactory = factory;
405 jexlEngine = getEngine();
406 jexlObject = new JexlScriptObject();
407 }
408
409 @Override
410 public CompiledScript compile(final Reader script) throws ScriptException {
411
412 if (script == null) {
413 throw new NullPointerException("script must be non-null");
414 }
415 return compile(readerToString(script));
416 }
417
418 @Override
419 public CompiledScript compile(final String script) throws ScriptException {
420
421 if (script == null) {
422 throw new NullPointerException("script must be non-null");
423 }
424 try {
425 final JexlScript jexlScript = jexlEngine.createScript(script);
426 return new JexlCompiledScript(jexlScript);
427 } catch (final Exception e) {
428 throw scriptException(e);
429 }
430 }
431
432 @Override
433 public Bindings createBindings() {
434 return new SimpleBindings();
435 }
436
437 @Override
438 public Object eval(final Reader reader, final ScriptContext context) throws ScriptException {
439
440 if (reader == null || context == null) {
441 throw new NullPointerException("script and context must be non-null");
442 }
443 return eval(readerToString(reader), context);
444 }
445
446 @Override
447 public Object eval(final String script, final ScriptContext context) throws ScriptException {
448
449 if (script == null || context == null) {
450 throw new NullPointerException("script and context must be non-null");
451 }
452
453 context.setAttribute(CONTEXT_KEY, context, ScriptContext.ENGINE_SCOPE);
454 try {
455 final JexlScript jexlScript = jexlEngine.createScript(script);
456 final JexlContext ctxt = new JexlContextWrapper(context);
457 return jexlScript.execute(ctxt);
458 } catch (final Exception e) {
459 throw scriptException(e);
460 }
461 }
462
463 @Override
464 public ScriptEngineFactory getFactory() {
465 return parentFactory;
466 }
467 }