Skip to content

Commit 634689e

Browse files
committed
Merge fix for #193 into brach 3.2.x
1 parent c7659ef commit 634689e

File tree

2 files changed

+76
-28
lines changed

2 files changed

+76
-28
lines changed
Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2012 the original author or authors.
2+
* Copyright 2009-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,54 +16,45 @@
1616

1717
package org.apache.ibatis.scripting.xmltags;
1818

19-
import java.io.StringReader;
2019
import java.util.Map;
2120
import java.util.concurrent.ConcurrentHashMap;
2221

23-
import ognl.ExpressionSyntaxException;
24-
import ognl.Node;
2522
import ognl.Ognl;
2623
import ognl.OgnlException;
27-
import ognl.OgnlParser;
28-
import ognl.ParseException;
29-
import ognl.TokenMgrError;
3024

3125
import org.apache.ibatis.builder.BuilderException;
3226

3327
/**
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
4133
*/
42-
public class OgnlCache {
34+
public final class OgnlCache {
4335

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+
}
4541

4642
public static Object getValue(String expression, Object root) {
4743
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);
4946
} catch (OgnlException e) {
5047
throw new BuilderException("Error evaluating expression '" + expression + "'. Cause: " + e, e);
5148
}
5249
}
5350

5451
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);
6656
}
57+
return node;
6758
}
6859

6960
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.ibatis.scripting.xmltags;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import ognl.ClassResolver;
23+
24+
import org.apache.ibatis.io.Resources;
25+
26+
/**
27+
* Custom ognl {@code ClassResolver} which behaves same like ognl's
28+
* {@code DefaultClassResolver}. But uses the {@code Resources}
29+
* utility class to find the target class instead of {@code Class#forName(String)}.
30+
*
31+
* @see https://github.com/mybatis/mybatis-3/issues/161
32+
*
33+
* @author Daniel Guggi
34+
*
35+
*/
36+
public class OgnlClassResolver implements ClassResolver {
37+
38+
private Map<String, Class<?>> classes = new HashMap<String, Class<?>>(101);
39+
40+
@Override
41+
public Class classForName(String className, Map context) throws ClassNotFoundException {
42+
Class<?> result = null;
43+
if ((result = classes.get(className)) == null) {
44+
try {
45+
result = Resources.classForName(className);
46+
} catch (ClassNotFoundException e1) {
47+
if (className.indexOf('.') == -1) {
48+
result = Resources.classForName("java.lang." + className);
49+
classes.put("java.lang." + className, result);
50+
}
51+
}
52+
classes.put(className, result);
53+
}
54+
return result;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)