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