1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.scripting.xmltags;
17
18 import java.util.Map;
19 import java.util.concurrent.ConcurrentHashMap;
20
21 import ognl.Ognl;
22 import ognl.OgnlContext;
23 import ognl.OgnlException;
24
25 import org.apache.ibatis.builder.BuilderException;
26
27
28
29
30
31
32
33
34 public final class OgnlCache {
35
36 private static final OgnlMemberAccess MEMBER_ACCESS = new OgnlMemberAccess();
37 private static final OgnlClassResolver CLASS_RESOLVER = new OgnlClassResolver();
38 private static final Map<String, Object> expressionCache = new ConcurrentHashMap<>();
39
40 private OgnlCache() {
41
42 }
43
44 public static Object getValue(String expression, Object root) {
45 try {
46 OgnlContext context = Ognl.createDefaultContext(root, MEMBER_ACCESS, CLASS_RESOLVER, null);
47 return Ognl.getValue(parseExpression(expression), context, root);
48 } catch (OgnlException e) {
49 throw new BuilderException("Error evaluating expression '" + expression + "'. Cause: " + e, e);
50 }
51 }
52
53 private static Object parseExpression(String expression) throws OgnlException {
54 Object node = expressionCache.get(expression);
55 if (node == null) {
56 node = Ognl.parseExpression(expression);
57 expressionCache.put(expression, node);
58 }
59 return node;
60 }
61
62 }