1   /**
2    * Copyright (c) 2000-2008 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.model.Portlet;
26  import com.liferay.portal.model.PortletApp;
27  import com.liferay.portal.velocity.VelocityContextPool;
28  
29  import java.util.Iterator;
30  import java.util.Map;
31  import java.util.concurrent.ConcurrentHashMap;
32  
33  import javax.portlet.PortletContext;
34  
35  import javax.servlet.ServletContext;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * <a href="PortletContextFactory.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
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 = new ConcurrentHashMap<String, Map<String, PortletContext>>();
58      }
59  
60      private PortletContext _create(Portlet portlet, ServletContext ctx) {
61          Map<String, PortletContext> portletContexts = _pool.get(
62              portlet.getRootPortletId());
63  
64          if (portletContexts == null) {
65              portletContexts = new ConcurrentHashMap<String, PortletContext>();
66  
67              _pool.put(portlet.getRootPortletId(), portletContexts);
68          }
69  
70          PortletContext portletContext =
71              portletContexts.get(portlet.getPortletId());
72  
73          if (portletContext == null) {
74              PortletApp portletApp = portlet.getPortletApp();
75  
76              if (portletApp.isWARFile()) {
77                  PortletBag portletBag = PortletBagPool.get(
78                      portlet.getRootPortletId());
79  
80                  if (portletBag == null) {
81                      _log.error(
82                          "Portlet " + portlet.getRootPortletId() +
83                              " has a null portlet bag");
84                  }
85  
86                  //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
87  
88                  ctx = portletBag.getServletContext();
89  
90                  // Context path for the portal must be passed to individual
91                  // portlets
92  
93                  //ctx.setAttribute(WebKeys.MAIN_PATH, mainPath);
94              }
95  
96              portletContext = new PortletContextImpl(portlet, ctx);
97  
98              VelocityContextPool.put(
99                  portletContext.getPortletContextName(), ctx);
100 
101             portletContexts.put(portlet.getPortletId(), portletContext);
102         }
103 
104         return portletContext;
105     }
106 
107     private void _destroy(Portlet portlet) {
108         Map<String, PortletContext> portletContexts = _pool.remove(
109             portlet.getRootPortletId());
110 
111         if (portletContexts == null) {
112             return;
113         }
114 
115         Iterator<Map.Entry<String, PortletContext>> itr =
116             portletContexts.entrySet().iterator();
117 
118         if (itr.hasNext()) {
119             Map.Entry<String, PortletContext> entry = itr.next();
120 
121             PortletContext portletContext = entry.getValue();
122 
123             VelocityContextPool.remove(portletContext.getPortletContextName());
124         }
125     }
126 
127     private static Log _log = LogFactory.getLog(PortletContextFactory.class);
128 
129     private static PortletContextFactory _instance =
130         new PortletContextFactory();
131 
132     private Map<String, Map<String, PortletContext>> _pool;
133 
134 }