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