1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.model.Portlet;
26 import com.liferay.portal.model.PortletApp;
27 import com.liferay.portal.velocity.VelocityContextPool;
28
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.concurrent.ConcurrentHashMap;
32
33 import javax.portlet.PortletContext;
34
35 import javax.servlet.ServletContext;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40
46 public class PortletContextFactory {
47
48 public static PortletContext create(Portlet portlet, ServletContext ctx) {
49 return _instance._create(portlet, ctx);
50 }
51
52 public static void destroy(Portlet portlet) {
53 _instance._destroy(portlet);
54 }
55
56 private PortletContextFactory() {
57 _pool = new ConcurrentHashMap<String, Map<String, PortletContext>>();
58 }
59
60 private PortletContext _create(Portlet portlet, ServletContext ctx) {
61 Map<String, PortletContext> portletContexts = _pool.get(
62 portlet.getRootPortletId());
63
64 if (portletContexts == null) {
65 portletContexts = new ConcurrentHashMap<String, PortletContext>();
66
67 _pool.put(portlet.getRootPortletId(), portletContexts);
68 }
69
70 PortletContext portletContext =
71 portletContexts.get(portlet.getPortletId());
72
73 if (portletContext == null) {
74 PortletApp portletApp = portlet.getPortletApp();
75
76 if (portletApp.isWARFile()) {
77 PortletBag portletBag = PortletBagPool.get(
78 portlet.getRootPortletId());
79
80 if (portletBag == null) {
81 _log.error(
82 "Portlet " + portlet.getRootPortletId() +
83 " has a null portlet bag");
84 }
85
86
88 ctx = portletBag.getServletContext();
89
90
93 }
95
96 portletContext = new PortletContextImpl(portlet, ctx);
97
98 VelocityContextPool.put(
99 portletContext.getPortletContextName(), ctx);
100
101 portletContexts.put(portlet.getPortletId(), portletContext);
102 }
103
104 return portletContext;
105 }
106
107 private void _destroy(Portlet portlet) {
108 Map<String, PortletContext> portletContexts = _pool.remove(
109 portlet.getRootPortletId());
110
111 if (portletContexts == null) {
112 return;
113 }
114
115 Iterator<Map.Entry<String, PortletContext>> itr =
116 portletContexts.entrySet().iterator();
117
118 if (itr.hasNext()) {
119 Map.Entry<String, PortletContext> entry = itr.next();
120
121 PortletContext portletContext = entry.getValue();
122
123 VelocityContextPool.remove(portletContext.getPortletContextName());
124 }
125 }
126
127 private static Log _log = LogFactory.getLog(PortletContextFactory.class);
128
129 private static PortletContextFactory _instance =
130 new PortletContextFactory();
131
132 private Map<String, Map<String, PortletContext>> _pool;
133
134 }