1   /**
2    * Copyright (c) 2000-2007 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.util.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.model.Portlet;
28  import com.liferay.portal.servlet.PortletContextPool;
29  import com.liferay.portal.servlet.PortletContextWrapper;
30  import com.liferay.portal.velocity.VelocityContextPool;
31  import com.liferay.util.CollectionFactory;
32  
33  import java.util.Iterator;
34  import java.util.Map;
35  
36  import javax.portlet.PortletContext;
37  
38  import javax.servlet.ServletContext;
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 = CollectionFactory.getSyncHashMap();
58      }
59  
60      private PortletContext _create(Portlet portlet, ServletContext ctx) {
61          String poolId = PortletContextFactory.class.getName();
62  
63          if (!portlet.isWARFile()) {
64              StringMaker sm = new StringMaker();
65  
66              sm.append(poolId);
67              sm.append(StringPool.PERIOD);
68              sm.append(portlet.getCompanyId());
69  
70              poolId = sm.toString();
71          }
72  
73          Map map = (Map)_pool.get(poolId);
74  
75          if (map == null) {
76              map = CollectionFactory.getSyncHashMap();
77  
78              _pool.put(poolId, map);
79          }
80  
81          Map portletContexts = (Map)map.get(portlet.getRootPortletId());
82  
83          if (portletContexts == null) {
84              portletContexts = CollectionFactory.getSyncHashMap();
85  
86              map.put(portlet.getRootPortletId(), portletContexts);
87          }
88  
89          PortletContext portletContext =
90              (PortletContext)portletContexts.get(portlet.getPortletId());
91  
92          if (portletContext == null) {
93              if (portlet.isWARFile()) {
94                  PortletContextWrapper pcw =
95                      PortletContextPool.get(portlet.getRootPortletId());
96  
97                  //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
98  
99                  ctx = pcw.getServletContext();
100 
101                 // Context path for the portal must be passed to individual
102                 // portlets
103 
104                 //ctx.setAttribute(WebKeys.MAIN_PATH, mainPath);
105             }
106 
107             portletContext = new PortletContextImpl(portlet, ctx);
108 
109             VelocityContextPool.put(
110                 portletContext.getPortletContextName(), ctx);
111 
112             portletContexts.put(portlet.getPortletId(), portletContext);
113         }
114 
115         return portletContext;
116     }
117 
118     private void _destroy(Portlet portlet) {
119         String poolId = PortletContextFactory.class.getName();
120 
121         if (!portlet.isWARFile()) {
122             StringMaker sm = new StringMaker();
123 
124             sm.append(poolId);
125             sm.append(StringPool.PERIOD);
126             sm.append(portlet.getCompanyId());
127 
128             poolId = sm.toString();
129         }
130 
131         Map map = (Map)_pool.get(poolId);
132 
133         if (map == null) {
134             return;
135         }
136 
137         Map portletContexts = (Map)map.remove(portlet.getRootPortletId());
138 
139         if (portletContexts == null) {
140             return;
141         }
142 
143         Iterator itr = portletContexts.entrySet().iterator();
144 
145         if (itr.hasNext()) {
146             Map.Entry entry = (Map.Entry)itr.next();
147 
148             PortletContext portletContext = (PortletContext)entry.getValue();
149 
150             VelocityContextPool.remove(portletContext.getPortletContextName());
151         }
152     }
153 
154     private static PortletContextFactory _instance =
155         new PortletContextFactory();
156 
157     private Map _pool;
158 
159 }