1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3;
18
19 import java.util.concurrent.Callable;
20
21
22
23
24 public class SynchronizedContext extends MapContext implements JexlContext.AnnotationProcessor {
25 private final JexlContext context;
26
27 public SynchronizedContext(final JexlContext ctxt) {
28 this.context = ctxt;
29 }
30
31
32
33
34
35
36
37 public Object call(final Object var, final JexlScript script) {
38 final String[] parms = script.getParameters();
39 final boolean varisarg = parms != null && parms.length == 1;
40 if (var == null) {
41 return varisarg ? script.execute(context, var) : script.execute(context);
42 }
43 synchronized (var) {
44 return varisarg ? script.execute(context, var) : script.execute(context);
45 }
46 }
47
48 @Override
49 public Object get(final String name) {
50 synchronized (this) {
51 return super.get(name);
52 }
53 }
54
55 @Override
56 public Object processAnnotation(final String name, final Object[] args, final Callable<Object> statement) throws Exception {
57 if ("synchronized".equals(name)) {
58 final Object arg = args[0];
59 synchronized(arg) {
60 return statement.call();
61 }
62 }
63 throw new IllegalArgumentException("unknown annotation " + name);
64 }
65
66 @Override
67 public void set(final String name, final Object value) {
68 synchronized (this) {
69 super.set(name, value);
70 }
71 }
72
73 }