001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.portlet.PortletBag;
020 import com.liferay.portal.kernel.portlet.PortletBagPool;
021 import com.liferay.portal.model.Portlet;
022 import com.liferay.portal.model.PortletApp;
023 import com.liferay.portal.security.lang.DoPrivilegedUtil;
024
025 import java.util.Map;
026 import java.util.concurrent.ConcurrentHashMap;
027
028 import javax.portlet.PortletContext;
029
030 import javax.servlet.ServletContext;
031
032
035 public class PortletContextFactory {
036
037 public static PortletContext create(
038 Portlet portlet, ServletContext servletContext) {
039
040 return _instance._create(portlet, servletContext);
041 }
042
043 public static void destroy(Portlet portlet) {
044 _instance._destroy(portlet);
045 }
046
047 private PortletContextFactory() {
048 _pool = new ConcurrentHashMap<String, Map<String, PortletContext>>();
049 }
050
051 private PortletContext _create(
052 Portlet portlet, ServletContext servletContext) {
053
054 Map<String, PortletContext> portletContexts = _pool.get(
055 portlet.getRootPortletId());
056
057 if (portletContexts == null) {
058 portletContexts = new ConcurrentHashMap<String, PortletContext>();
059
060 _pool.put(portlet.getRootPortletId(), portletContexts);
061 }
062
063 PortletContext portletContext = portletContexts.get(
064 portlet.getPortletId());
065
066 if (portletContext != null) {
067 return DoPrivilegedUtil.wrap(portletContext);
068 }
069
070 PortletApp portletApp = portlet.getPortletApp();
071
072 if (portletApp.isWARFile()) {
073 PortletBag portletBag = PortletBagPool.get(
074 portlet.getRootPortletId());
075
076 if (portletBag == null) {
077 _log.error(
078 "Portlet " + portlet.getRootPortletId() +
079 " has a null portlet bag");
080 }
081
082
083
084 servletContext = portletBag.getServletContext();
085
086
087
088
089 }
090
091 portletContext = new PortletContextImpl(portlet, servletContext);
092
093 portletContexts.put(portlet.getPortletId(), portletContext);
094
095 return DoPrivilegedUtil.wrap(portletContext);
096 }
097
098 private void _destroy(Portlet portlet) {
099 _pool.remove(portlet.getRootPortletId());
100 }
101
102 private static Log _log = LogFactoryUtil.getLog(
103 PortletContextFactory.class);
104
105 private static PortletContextFactory _instance =
106 new PortletContextFactory();
107
108 private Map<String, Map<String, PortletContext>> _pool;
109
110 }