1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.util.WebKeys;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpSession;
25  
26  /**
27   * <a href="RenderParametersPool.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class RenderParametersPool {
32  
33      public static void clear(HttpServletRequest request, long plid) {
34          Map<String, Map<String, String[]>> plidPool = get(request, plid);
35  
36          plidPool.clear();
37      }
38  
39      public static void clear(
40          HttpServletRequest request, long plid, String portletId) {
41  
42          Map<String, String[]> params = get(request, plid, portletId);
43  
44          params.clear();
45      }
46  
47      public static Map<String, Map<String, String[]>> get(
48          HttpServletRequest request, long plid) {
49  
50          HttpSession session = request.getSession();
51  
52          if (plid <= 0) {
53              return new ConcurrentHashMap<String, Map<String, String[]>>();
54          }
55  
56          Map<Long, Map<String, Map<String, String[]>>> pool =
57              _getRenderParametersPool(session);
58  
59          Map<String, Map<String, String[]>> plidPool = pool.get(plid);
60  
61          if (plidPool == null) {
62              plidPool = new ConcurrentHashMap<String, Map<String, String[]>>();
63  
64              pool.put(plid, plidPool);
65          }
66  
67          return plidPool;
68      }
69  
70      public static Map<String, String[]> get(
71          HttpServletRequest request, long plid, String portletId) {
72  
73          Map<String, Map<String, String[]>> plidPool = get(request, plid);
74  
75          Map<String, String[]> params = plidPool.get(portletId);
76  
77          if (params == null) {
78              params = new HashMap<String, String[]>();
79  
80              plidPool.put(portletId, params);
81          }
82  
83          return params;
84      }
85  
86      public static void put(
87          HttpServletRequest request, long plid, String portletId,
88          Map<String, String[]> params) {
89  
90          Map<String, Map<String, String[]>> plidPool = get(request, plid);
91  
92          plidPool.put(portletId, params);
93      }
94  
95      private static Map<Long, Map<String, Map<String, String[]>>>
96          _getRenderParametersPool(HttpSession session) {
97  
98          Map<Long, Map<String, Map<String, String[]>>> renderParametersPool =
99              (Map<Long, Map<String, Map<String, String[]>>>)session.getAttribute(
100                 WebKeys.PORTLET_RENDER_PARAMETERS);
101 
102         if (renderParametersPool == null) {
103             renderParametersPool = new ConcurrentHashMap
104                 <Long, Map<String, Map<String, String[]>>>();
105 
106             session.setAttribute(
107                 WebKeys.PORTLET_RENDER_PARAMETERS, renderParametersPool);
108         }
109 
110         return renderParametersPool;
111     }
112 
113 }