1   /**
2    * Copyright (c) 2000-2009 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.util.WebKeys;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpSession;
33  
34  /**
35   * <a href="RenderParametersPool.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class RenderParametersPool {
41  
42      public static void clear(HttpServletRequest request, long plid) {
43          Map<String, Map<String, String[]>> plidPool = get(request, plid);
44  
45          plidPool.clear();
46      }
47  
48      public static void clear(
49          HttpServletRequest request, long plid, String portletId) {
50  
51          Map<String, String[]> params = get(request, plid, portletId);
52  
53          params.clear();
54      }
55  
56      public static Map<String, Map<String, String[]>> get(
57          HttpServletRequest request, long plid) {
58  
59          HttpSession session = request.getSession();
60  
61          if (plid <= 0) {
62              return new ConcurrentHashMap<String, Map<String, String[]>>();
63          }
64  
65          Map<Long, Map<String, Map<String, String[]>>> pool =
66              _getRenderParametersPool(session);
67  
68          Map<String, Map<String, String[]>> plidPool = pool.get(plid);
69  
70          if (plidPool == null) {
71              plidPool = new ConcurrentHashMap<String, Map<String, String[]>>();
72  
73              pool.put(plid, plidPool);
74          }
75  
76          return plidPool;
77      }
78  
79      public static Map<String, String[]> get(
80          HttpServletRequest request, long plid, String portletId) {
81  
82          Map<String, Map<String, String[]>> plidPool = get(request, plid);
83  
84          Map<String, String[]> params = plidPool.get(portletId);
85  
86          if (params == null) {
87              params = new HashMap<String, String[]>();
88  
89              plidPool.put(portletId, params);
90          }
91  
92          return params;
93      }
94  
95      public static void put(
96          HttpServletRequest request, long plid, String portletId,
97          Map<String, String[]> params) {
98  
99          Map<String, Map<String, String[]>> plidPool = get(request, plid);
100 
101         plidPool.put(portletId, params);
102     }
103 
104     private static Map<Long, Map<String, Map<String, String[]>>>
105         _getRenderParametersPool(HttpSession session) {
106 
107         Map<Long, Map<String, Map<String, String[]>>> renderParametersPool =
108             (Map<Long, Map<String, Map<String, String[]>>>)session.getAttribute(
109                 WebKeys.PORTLET_RENDER_PARAMETERS);
110 
111         if (renderParametersPool == null) {
112             renderParametersPool = new ConcurrentHashMap
113                 <Long, Map<String, Map<String, String[]>>>();
114 
115             session.setAttribute(
116                 WebKeys.PORTLET_RENDER_PARAMETERS, renderParametersPool);
117         }
118 
119         return renderParametersPool;
120     }
121 
122 }