1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.portlet.PortletBag;
25  import com.liferay.portal.kernel.portlet.PortletBagPool;
26  import com.liferay.portal.model.Portlet;
27  import com.liferay.portal.model.PortletApp;
28  import com.liferay.portal.velocity.VelocityContextPool;
29  
30  import java.util.Iterator;
31  import java.util.Map;
32  import java.util.concurrent.ConcurrentHashMap;
33  
34  import javax.portlet.PortletContext;
35  
36  import javax.servlet.ServletContext;
37  
38  /**
39   * <a href="PortletContextFactory.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class PortletContextFactory {
45  
46      public static PortletContext create(
47          Portlet portlet, ServletContext servletContext) {
48  
49          return _instance._create(portlet, servletContext);
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(
61          Portlet portlet, ServletContext servletContext) {
62  
63          Map<String, PortletContext> portletContexts = _pool.get(
64              portlet.getRootPortletId());
65  
66          if (portletContexts == null) {
67              portletContexts = new ConcurrentHashMap<String, PortletContext>();
68  
69              _pool.put(portlet.getRootPortletId(), portletContexts);
70          }
71  
72          PortletContext portletContext =
73              portletContexts.get(portlet.getPortletId());
74  
75          if (portletContext == null) {
76              PortletApp portletApp = portlet.getPortletApp();
77  
78              if (portletApp.isWARFile()) {
79                  PortletBag portletBag = PortletBagPool.get(
80                      portlet.getRootPortletId());
81  
82                  if (portletBag == null) {
83                      _log.error(
84                          "Portlet " + portlet.getRootPortletId() +
85                              " has a null portlet bag");
86                  }
87  
88                  //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
89  
90                  servletContext = portletBag.getServletContext();
91  
92                  // Context path for the portal must be passed to individual
93                  // portlets
94  
95                  //ctx.setAttribute(WebKeys.MAIN_PATH, mainPath);
96              }
97  
98              portletContext = new PortletContextImpl(portlet, servletContext);
99  
100             VelocityContextPool.put(
101                 portletContext.getPortletContextName(), servletContext);
102 
103             portletContexts.put(portlet.getPortletId(), portletContext);
104         }
105 
106         return portletContext;
107     }
108 
109     private void _destroy(Portlet portlet) {
110         Map<String, PortletContext> portletContexts = _pool.remove(
111             portlet.getRootPortletId());
112 
113         if (portletContexts == null) {
114             return;
115         }
116 
117         Iterator<Map.Entry<String, PortletContext>> itr =
118             portletContexts.entrySet().iterator();
119 
120         if (itr.hasNext()) {
121             Map.Entry<String, PortletContext> entry = itr.next();
122 
123             PortletContext portletContext = entry.getValue();
124 
125             VelocityContextPool.remove(portletContext.getPortletContextName());
126         }
127     }
128 
129     private static Log _log =
130         LogFactoryUtil.getLog(PortletContextFactory.class);
131 
132     private static PortletContextFactory _instance =
133         new PortletContextFactory();
134 
135     private Map<String, Map<String, PortletContext>> _pool;
136 
137 }