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.kernel.portlet;
016    
017    import com.liferay.portal.kernel.servlet.PortletServlet;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import javax.servlet.ServletContext;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class PortletClassLoaderUtil {
028    
029            public static ClassLoader getClassLoader() {
030                    Thread currentThread = Thread.currentThread();
031    
032                    return _classLoaders.get(currentThread.getId());
033            }
034    
035            public static ClassLoader getClassLoader(String portletId) {
036                    PortletBag portletBag = PortletBagPool.get(portletId);
037    
038                    if (portletBag == null) {
039                            return null;
040                    }
041    
042                    ServletContext servletContext = portletBag.getServletContext();
043    
044                    ClassLoader portletClassLoader =
045                            (ClassLoader)servletContext.getAttribute(
046                                    PortletServlet.PORTLET_CLASS_LOADER);
047    
048                    return portletClassLoader;
049            }
050    
051            public static String getServletContextName() {
052                    return _servletContextName;
053            }
054    
055            public static void setClassLoader(ClassLoader classLoader) {
056                    Thread currentThread = Thread.currentThread();
057    
058                    _classLoaders.put(currentThread.getId(), classLoader);
059            }
060    
061            public static void setServletContextName(String servletContextName) {
062                    _servletContextName = servletContextName;
063            }
064    
065            private static Map<Long, ClassLoader> _classLoaders =
066                    new HashMap<Long, ClassLoader>();
067    
068            private static String _servletContextName;
069    
070    }