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