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.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    }