001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    /**
021     * @author Shuyang Zhou
022     */
023    public class ClassResolverUtil {
024    
025            public static Class<?> resolve(String className, ClassLoader classLoader)
026                    throws ClassNotFoundException {
027    
028                    try {
029                            return Class.forName(className, false, classLoader);
030                    }
031                    catch (ClassNotFoundException cnfe) {
032                            Class<?> clazz = _primitiveClasses.get(className);
033    
034                            if (clazz != null) {
035                                    return clazz;
036                            }
037                            else {
038                                    throw cnfe;
039                            }
040                    }
041            }
042    
043            public static Class<?> resolveByContextClassLoader(String className) {
044                    Thread thread = Thread.currentThread();
045    
046                    ClassLoader contextClassLoader = thread.getContextClassLoader();
047    
048                    try {
049                            return Class.forName(className, false, contextClassLoader);
050                    }
051                    catch (ClassNotFoundException cnfe) {
052                            throw new RuntimeException(cnfe);
053                    }
054            }
055    
056            public static Class<?> resolveByPortalClassLoader(String className) {
057                    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
058    
059                    try {
060                            return Class.forName(className, false, portalClassLoader);
061                    }
062                    catch (ClassNotFoundException cnfe) {
063                            throw new RuntimeException(cnfe);
064                    }
065            }
066    
067            public static Class<?> resolveByPortletClassLoader(
068                    String className, String servletContextName) {
069    
070                    ClassLoader classLoader = ClassLoaderPool.getClassLoader(
071                            servletContextName);
072    
073                    try {
074                            return Class.forName(className, false, classLoader);
075                    }
076                    catch (ClassNotFoundException cnfe) {
077                            throw new RuntimeException(cnfe);
078                    }
079            }
080    
081            private static final Map<String, Class<?>> _primitiveClasses =
082                    new HashMap<String, Class<?>>(9, 1.0F);
083    
084            static {
085                    _primitiveClasses.put("boolean", boolean.class);
086                    _primitiveClasses.put("byte", byte.class);
087                    _primitiveClasses.put("char", char.class);
088                    _primitiveClasses.put("short", short.class);
089                    _primitiveClasses.put("int", int.class);
090                    _primitiveClasses.put("long", long.class);
091                    _primitiveClasses.put("float", float.class);
092                    _primitiveClasses.put("double", double.class);
093                    _primitiveClasses.put("void", void.class);
094            }
095    
096    }