001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
018    
019    import java.security.AccessController;
020    import java.security.PrivilegedAction;
021    
022    /**
023     * @author Raymond Aug??
024     */
025    public class ClassLoaderUtil {
026    
027            public static ClassLoader getClassLoader(final Class<?> clazz) {
028                    return AccessController.doPrivileged(
029                            new PrivilegedAction<ClassLoader>() {
030    
031                                    public ClassLoader run() {
032                                            return clazz.getClassLoader();
033                                    }
034    
035                            }
036                    );
037            }
038    
039            public static ClassLoader getContextClassLoader() {
040                    return AccessController.doPrivileged(
041                            new PrivilegedAction<ClassLoader>() {
042    
043                                    public ClassLoader run() {
044                                            Thread thread = Thread.currentThread();
045    
046                                            return thread.getContextClassLoader();
047                                    }
048    
049                            }
050                    );
051            }
052    
053            public static ClassLoader getPortalClassLoader() {
054                    return AccessController.doPrivileged(
055                            new PrivilegedAction<ClassLoader>() {
056    
057                                    public ClassLoader run() {
058                                            return PortalClassLoaderUtil.getClassLoader();
059                                    }
060    
061                            }
062                    );
063            }
064    
065            public static void setContextClassLoader(final ClassLoader classLoader) {
066                    AccessController.doPrivileged(
067                            new PrivilegedAction<Void>() {
068    
069                                    public Void run() {
070                                            Thread thread = Thread.currentThread();
071    
072                                            thread.setContextClassLoader(classLoader);
073    
074                                            return null;
075                                    }
076    
077                            }
078                    );
079            }
080    
081    }