001    /**
002     * Copyright (c) 2000-2012 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.util;
016    
017    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
018    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
019    
020    /**
021     * @author Raymond Augé
022     */
023    public class ClassLoaderUtil {
024    
025            public static ClassLoader getClassLoader(Class<?> clazz) {
026                    boolean checkGetClassLoader =
027                            PortalSecurityManagerThreadLocal.isCheckGetClassLoader();
028    
029                    try {
030                            PortalSecurityManagerThreadLocal.setCheckGetClassLoader(false);
031    
032                            return clazz.getClassLoader();
033                    }
034                    finally {
035                            PortalSecurityManagerThreadLocal.setCheckGetClassLoader(
036                                    checkGetClassLoader);
037                    }
038            }
039    
040            public static ClassLoader getContextClassLoader() {
041                    boolean checkGetClassLoader =
042                            PortalSecurityManagerThreadLocal.isCheckGetClassLoader();
043    
044                    try {
045                            PortalSecurityManagerThreadLocal.setCheckGetClassLoader(false);
046    
047                            Thread thread = Thread.currentThread();
048    
049                            return thread.getContextClassLoader();
050                    }
051                    finally {
052                            PortalSecurityManagerThreadLocal.setCheckGetClassLoader(
053                                    checkGetClassLoader);
054                    }
055            }
056    
057            public static ClassLoader getPortalClassLoader() {
058                    boolean checkGetClassLoader =
059                            PortalSecurityManagerThreadLocal.isCheckGetClassLoader();
060    
061                    try {
062                            PortalSecurityManagerThreadLocal.setCheckGetClassLoader(false);
063    
064                            return PortalClassLoaderUtil.getClassLoader();
065                    }
066                    finally {
067                            PortalSecurityManagerThreadLocal.setCheckGetClassLoader(
068                                    checkGetClassLoader);
069                    }
070            }
071    
072            public static void setContextClassLoader(ClassLoader classLoader) {
073                    boolean checkGetClassLoader =
074                            PortalSecurityManagerThreadLocal.isCheckGetClassLoader();
075    
076                    try {
077                            PortalSecurityManagerThreadLocal.setCheckGetClassLoader(false);
078    
079                            Thread thread = Thread.currentThread();
080    
081                            thread.setContextClassLoader(classLoader);
082                    }
083                    finally {
084                            PortalSecurityManagerThreadLocal.setCheckGetClassLoader(
085                                    checkGetClassLoader);
086                    }
087            }
088    
089    }