1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.kernel.util.StringMaker;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.model.Portlet;
28 import com.liferay.portal.servlet.PortletContextPool;
29 import com.liferay.portal.servlet.PortletContextWrapper;
30 import com.liferay.portal.velocity.VelocityContextPool;
31 import com.liferay.util.CollectionFactory;
32
33 import java.util.Iterator;
34 import java.util.Map;
35
36 import javax.portlet.PortletContext;
37
38 import javax.servlet.ServletContext;
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 = CollectionFactory.getSyncHashMap();
58 }
59
60 private PortletContext _create(Portlet portlet, ServletContext ctx) {
61 String poolId = PortletContextFactory.class.getName();
62
63 if (!portlet.isWARFile()) {
64 StringMaker sm = new StringMaker();
65
66 sm.append(poolId);
67 sm.append(StringPool.PERIOD);
68 sm.append(portlet.getCompanyId());
69
70 poolId = sm.toString();
71 }
72
73 Map map = (Map)_pool.get(poolId);
74
75 if (map == null) {
76 map = CollectionFactory.getSyncHashMap();
77
78 _pool.put(poolId, map);
79 }
80
81 Map portletContexts = (Map)map.get(portlet.getRootPortletId());
82
83 if (portletContexts == null) {
84 portletContexts = CollectionFactory.getSyncHashMap();
85
86 map.put(portlet.getRootPortletId(), portletContexts);
87 }
88
89 PortletContext portletContext =
90 (PortletContext)portletContexts.get(portlet.getPortletId());
91
92 if (portletContext == null) {
93 if (portlet.isWARFile()) {
94 PortletContextWrapper pcw =
95 PortletContextPool.get(portlet.getRootPortletId());
96
97
99 ctx = pcw.getServletContext();
100
101
104 }
106
107 portletContext = new PortletContextImpl(portlet, ctx);
108
109 VelocityContextPool.put(
110 portletContext.getPortletContextName(), ctx);
111
112 portletContexts.put(portlet.getPortletId(), portletContext);
113 }
114
115 return portletContext;
116 }
117
118 private void _destroy(Portlet portlet) {
119 String poolId = PortletContextFactory.class.getName();
120
121 if (!portlet.isWARFile()) {
122 StringMaker sm = new StringMaker();
123
124 sm.append(poolId);
125 sm.append(StringPool.PERIOD);
126 sm.append(portlet.getCompanyId());
127
128 poolId = sm.toString();
129 }
130
131 Map map = (Map)_pool.get(poolId);
132
133 if (map == null) {
134 return;
135 }
136
137 Map portletContexts = (Map)map.remove(portlet.getRootPortletId());
138
139 if (portletContexts == null) {
140 return;
141 }
142
143 Iterator itr = portletContexts.entrySet().iterator();
144
145 if (itr.hasNext()) {
146 Map.Entry entry = (Map.Entry)itr.next();
147
148 PortletContext portletContext = (PortletContext)entry.getValue();
149
150 VelocityContextPool.remove(portletContext.getPortletContextName());
151 }
152 }
153
154 private static PortletContextFactory _instance =
155 new PortletContextFactory();
156
157 private Map _pool;
158
159 }