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