|
1 | 1 | /* |
2 | | - * Copyright 2009-2012 the original author or authors. |
| 2 | + * Copyright 2009-2014 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package org.apache.ibatis.scripting.xmltags; |
18 | 18 |
|
19 | | -import java.io.StringReader; |
20 | 19 | import java.util.Map; |
21 | 20 | import java.util.concurrent.ConcurrentHashMap; |
22 | 21 |
|
23 | | -import ognl.ExpressionSyntaxException; |
24 | | -import ognl.Node; |
25 | 22 | import ognl.Ognl; |
26 | 23 | import ognl.OgnlException; |
27 | | -import ognl.OgnlParser; |
28 | | -import ognl.ParseException; |
29 | | -import ognl.TokenMgrError; |
30 | 24 |
|
31 | 25 | import org.apache.ibatis.builder.BuilderException; |
32 | 26 |
|
33 | 27 | /** |
34 | | - * |
35 | | - * Caches OGNL parsed expressions. Have a look at |
36 | | - * http://code.google.com/p/mybatis/issues/detail?id=342 |
37 | | - * |
38 | | - */ |
39 | | -/** |
40 | | - * @author Clinton Begin |
| 28 | + * Caches OGNL parsed expressions. |
| 29 | + * |
| 30 | + * @see http://code.google.com/p/mybatis/issues/detail?id=342 |
| 31 | + * |
| 32 | + * @author Eduardo Macarron |
41 | 33 | */ |
42 | | -public class OgnlCache { |
| 34 | +public final class OgnlCache { |
43 | 35 |
|
44 | | - private static final Map<String, ognl.Node> expressionCache = new ConcurrentHashMap<String, ognl.Node>(); |
| 36 | + private static final Map<String, Object> expressionCache = new ConcurrentHashMap<String, Object>(); |
| 37 | + |
| 38 | + private OgnlCache() { |
| 39 | + // Prevent Instantiation of Static Class |
| 40 | + } |
45 | 41 |
|
46 | 42 | public static Object getValue(String expression, Object root) { |
47 | 43 | try { |
48 | | - return Ognl.getValue(parseExpression(expression), root); |
| 44 | + Map<Object, OgnlClassResolver> context = Ognl.createDefaultContext(root, new OgnlClassResolver()); |
| 45 | + return Ognl.getValue(parseExpression(expression), context, root); |
49 | 46 | } catch (OgnlException e) { |
50 | 47 | throw new BuilderException("Error evaluating expression '" + expression + "'. Cause: " + e, e); |
51 | 48 | } |
52 | 49 | } |
53 | 50 |
|
54 | 51 | private static Object parseExpression(String expression) throws OgnlException { |
55 | | - try { |
56 | | - Node node = expressionCache.get(expression); |
57 | | - if (node == null) { |
58 | | - node = new OgnlParser(new StringReader(expression)).topLevelExpression(); |
59 | | - expressionCache.put(expression, node); |
60 | | - } |
61 | | - return node; |
62 | | - } catch (ParseException e) { |
63 | | - throw new ExpressionSyntaxException(expression, e); |
64 | | - } catch (TokenMgrError e) { |
65 | | - throw new ExpressionSyntaxException(expression, e); |
| 52 | + Object node = expressionCache.get(expression); |
| 53 | + if (node == null) { |
| 54 | + node = Ognl.parseExpression(expression); |
| 55 | + expressionCache.put(expression, node); |
66 | 56 | } |
| 57 | + return node; |
67 | 58 | } |
68 | 59 |
|
69 | 60 | } |
0 commit comments