View Javadoc
1   /**
2    *    Copyright 2009-2015 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  package org.apache.ibatis.scripting.xmltags;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import ognl.ClassResolver;
22  
23  import org.apache.ibatis.io.Resources;
24  
25  /**
26   * Custom ognl {@code ClassResolver} which behaves same like ognl's
27   * {@code DefaultClassResolver}. But uses the {@code Resources}
28   * utility class to find the target class instead of {@code Class#forName(String)}. 
29   * 
30   * @see https://github.com/mybatis/mybatis-3/issues/161
31   * 
32   * @author Daniel Guggi
33   * 
34   */
35  public class OgnlClassResolver implements ClassResolver {
36  
37    private Map<String, Class<?>> classes = new HashMap<String, Class<?>>(101);
38  
39    @Override
40    public Class classForName(String className, Map context) throws ClassNotFoundException {
41      Class<?> result = null;
42      if ((result = classes.get(className)) == null) {
43        try {
44          result = Resources.classForName(className);
45        } catch (ClassNotFoundException e1) {
46          if (className.indexOf('.') == -1) {
47            result = Resources.classForName("java.lang." + className);
48            classes.put("java.lang." + className, result);
49          }
50        }
51        classes.put(className, result);
52      }
53      return result;
54    }
55  
56  }