1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
47  public class PortletContextFactory {
48  
49      public static PortletContext create(
50          Portlet portlet, ServletContext servletContext) {
51  
52          return _instance._create(portlet, servletContext);
53      }
54  
55      public static void destroy(Portlet portlet) {
56          _instance._destroy(portlet);
57      }
58  
59      private PortletContextFactory() {
60          _pool = new ConcurrentHashMap<String, Map<String, PortletContext>>();
61      }
62  
63      private PortletContext _create(
64          Portlet portlet, ServletContext servletContext) {
65  
66          Map<String, PortletContext> portletContexts = _pool.get(
67              portlet.getRootPortletId());
68  
69          if (portletContexts == null) {
70              portletContexts = new ConcurrentHashMap<String, PortletContext>();
71  
72              _pool.put(portlet.getRootPortletId(), portletContexts);
73          }
74  
75          PortletContext portletContext =
76              portletContexts.get(portlet.getPortletId());
77  
78          if (portletContext == null) {
79              PortletApp portletApp = portlet.getPortletApp();
80  
81              if (portletApp.isWARFile()) {
82                  PortletBag portletBag = PortletBagPool.get(
83                      portlet.getRootPortletId());
84  
85                  if (portletBag == null) {
86                      _log.error(
87                          "Portlet " + portlet.getRootPortletId() +
88                              " has a null portlet bag");
89                  }
90  
91                  //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
92  
93                  servletContext = portletBag.getServletContext();
94  
95                  // Context path for the portal must be passed to individual
96                  // portlets
97  
98                  //ctx.setAttribute(WebKeys.MAIN_PATH, mainPath);
99              }
100 
101             portletContext = new PortletContextImpl(portlet, servletContext);
102 
103             VelocityContextPool.put(
104                 portletContext.getPortletContextName(), servletContext);
105 
106             portletContexts.put(portlet.getPortletId(), portletContext);
107         }
108 
109         return portletContext;
110     }
111 
112     private void _destroy(Portlet portlet) {
113         Map<String, PortletContext> portletContexts = _pool.remove(
114             portlet.getRootPortletId());
115 
116         if (portletContexts == null) {
117             return;
118         }
119 
120         Iterator<Map.Entry<String, PortletContext>> itr =
121             portletContexts.entrySet().iterator();
122 
123         if (itr.hasNext()) {
124             Map.Entry<String, PortletContext> entry = itr.next();
125 
126             PortletContext portletContext = entry.getValue();
127 
128             VelocityContextPool.remove(portletContext.getPortletContextName());
129         }
130     }
131 
132     private static Log _log =
133          LogFactoryUtil.getLog(PortletContextFactory.class);
134 
135     private static PortletContextFactory _instance =
136         new PortletContextFactory();
137 
138     private Map<String, Map<String, PortletContext>> _pool;
139 
140 }