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.kernel.servlet.ServletContextPool;
022 import com.liferay.portal.model.Portlet;
023 import com.liferay.portal.model.PortletApp;
024
025 import java.util.Iterator;
026 import java.util.Map;
027 import java.util.concurrent.ConcurrentHashMap;
028
029 import javax.portlet.PortletContext;
030
031 import javax.servlet.ServletContext;
032
033
036 public class PortletContextFactory {
037
038 public static PortletContext create(
039 Portlet portlet, ServletContext servletContext) {
040
041 return _instance._create(portlet, servletContext);
042 }
043
044 public static void destroy(Portlet portlet) {
045 _instance._destroy(portlet);
046 }
047
048 private PortletContextFactory() {
049 _pool = new ConcurrentHashMap<String, Map<String, PortletContext>>();
050 }
051
052 private PortletContext _create(
053 Portlet portlet, ServletContext servletContext) {
054
055 Map<String, PortletContext> portletContexts = _pool.get(
056 portlet.getRootPortletId());
057
058 if (portletContexts == null) {
059 portletContexts = new ConcurrentHashMap<String, PortletContext>();
060
061 _pool.put(portlet.getRootPortletId(), portletContexts);
062 }
063
064 PortletContext portletContext =
065 portletContexts.get(portlet.getPortletId());
066
067 if (portletContext == null) {
068 PortletApp portletApp = portlet.getPortletApp();
069
070 if (portletApp.isWARFile()) {
071 PortletBag portletBag = PortletBagPool.get(
072 portlet.getRootPortletId());
073
074 if (portletBag == null) {
075 _log.error(
076 "Portlet " + portlet.getRootPortletId() +
077 " has a null portlet bag");
078 }
079
080
081
082 servletContext = portletBag.getServletContext();
083
084
085
086
087
088 }
089
090 portletContext = new PortletContextImpl(portlet, servletContext);
091
092 portletContexts.put(portlet.getPortletId(), portletContext);
093 }
094
095 return portletContext;
096 }
097
098 private void _destroy(Portlet portlet) {
099 Map<String, PortletContext> portletContexts = _pool.remove(
100 portlet.getRootPortletId());
101
102 if (portletContexts == null) {
103 return;
104 }
105
106 Iterator<Map.Entry<String, PortletContext>> itr =
107 portletContexts.entrySet().iterator();
108
109 if (itr.hasNext()) {
110 Map.Entry<String, PortletContext> entry = itr.next();
111
112 PortletContext portletContext = entry.getValue();
113
114 ServletContextPool.remove(portletContext.getPortletContextName());
115 }
116 }
117
118 private static Log _log = LogFactoryUtil.getLog(
119 PortletContextFactory.class);
120
121 private static PortletContextFactory _instance =
122 new PortletContextFactory();
123
124 private Map<String, Map<String, PortletContext>> _pool;
125
126 }