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