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