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(
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 = LogFactory.getLog(PortletContextFactory.class);
132 
133     private static PortletContextFactory _instance =
134         new PortletContextFactory();
135 
136     private Map<String, Map<String, PortletContext>> _pool;
137 
138 }