1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.portlet.PortletBag;
20  import com.liferay.portal.kernel.portlet.PortletBagPool;
21  import com.liferay.portal.model.Portlet;
22  import com.liferay.portal.model.PortletApp;
23  import com.liferay.portal.velocity.VelocityContextPool;
24  
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import javax.portlet.PortletContext;
30  
31  import javax.servlet.ServletContext;
32  
33  /**
34   * <a href="PortletContextFactory.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class PortletContextFactory {
39  
40      public static PortletContext create(
41          Portlet portlet, ServletContext servletContext) {
42  
43          return _instance._create(portlet, servletContext);
44      }
45  
46      public static void destroy(Portlet portlet) {
47          _instance._destroy(portlet);
48      }
49  
50      private PortletContextFactory() {
51          _pool = new ConcurrentHashMap<String, Map<String, PortletContext>>();
52      }
53  
54      private PortletContext _create(
55          Portlet portlet, ServletContext servletContext) {
56  
57          Map<String, PortletContext> portletContexts = _pool.get(
58              portlet.getRootPortletId());
59  
60          if (portletContexts == null) {
61              portletContexts = new ConcurrentHashMap<String, PortletContext>();
62  
63              _pool.put(portlet.getRootPortletId(), portletContexts);
64          }
65  
66          PortletContext portletContext =
67              portletContexts.get(portlet.getPortletId());
68  
69          if (portletContext == null) {
70              PortletApp portletApp = portlet.getPortletApp();
71  
72              if (portletApp.isWARFile()) {
73                  PortletBag portletBag = PortletBagPool.get(
74                      portlet.getRootPortletId());
75  
76                  if (portletBag == null) {
77                      _log.error(
78                          "Portlet " + portlet.getRootPortletId() +
79                              " has a null portlet bag");
80                  }
81  
82                  //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
83  
84                  servletContext = portletBag.getServletContext();
85  
86                  // Context path for the portal must be passed to individual
87                  // portlets
88  
89                  //ctx.setAttribute(WebKeys.MAIN_PATH, mainPath);
90              }
91  
92              portletContext = new PortletContextImpl(portlet, servletContext);
93  
94              VelocityContextPool.put(
95                  portletContext.getPortletContextName(), servletContext);
96  
97              portletContexts.put(portlet.getPortletId(), portletContext);
98          }
99  
100         return portletContext;
101     }
102 
103     private void _destroy(Portlet portlet) {
104         Map<String, PortletContext> portletContexts = _pool.remove(
105             portlet.getRootPortletId());
106 
107         if (portletContexts == null) {
108             return;
109         }
110 
111         Iterator<Map.Entry<String, PortletContext>> itr =
112             portletContexts.entrySet().iterator();
113 
114         if (itr.hasNext()) {
115             Map.Entry<String, PortletContext> entry = itr.next();
116 
117             PortletContext portletContext = entry.getValue();
118 
119             VelocityContextPool.remove(portletContext.getPortletContextName());
120         }
121     }
122 
123     private static Log _log = LogFactoryUtil.getLog(
124         PortletContextFactory.class);
125 
126     private static PortletContextFactory _instance =
127         new PortletContextFactory();
128 
129     private Map<String, Map<String, PortletContext>> _pool;
130 
131 }