001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.util.WebKeys;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpSession;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class RenderParametersPool {
030    
031            public static void clear(HttpServletRequest request, long plid) {
032                    Map<String, Map<String, String[]>> plidPool = get(request, plid);
033    
034                    plidPool.clear();
035            }
036    
037            public static void clear(
038                    HttpServletRequest request, long plid, String portletId) {
039    
040                    Map<String, String[]> params = get(request, plid, portletId);
041    
042                    params.clear();
043            }
044    
045            public static Map<String, Map<String, String[]>> get(
046                    HttpServletRequest request, long plid) {
047    
048                    HttpSession session = request.getSession();
049    
050                    if (plid <= 0) {
051                            return new ConcurrentHashMap<String, Map<String, String[]>>();
052                    }
053    
054                    Map<Long, Map<String, Map<String, String[]>>> pool =
055                            _getRenderParametersPool(session);
056    
057                    Map<String, Map<String, String[]>> plidPool = pool.get(plid);
058    
059                    if (plidPool == null) {
060                            plidPool = new ConcurrentHashMap<String, Map<String, String[]>>();
061    
062                            pool.put(plid, plidPool);
063                    }
064    
065                    return plidPool;
066            }
067    
068            public static Map<String, String[]> get(
069                    HttpServletRequest request, long plid, String portletId) {
070    
071                    Map<String, Map<String, String[]>> plidPool = get(request, plid);
072    
073                    Map<String, String[]> params = plidPool.get(portletId);
074    
075                    if (params == null) {
076                            params = new HashMap<String, String[]>();
077    
078                            plidPool.put(portletId, params);
079                    }
080    
081                    return params;
082            }
083    
084            public static void put(
085                    HttpServletRequest request, long plid, String portletId,
086                    Map<String, String[]> params) {
087    
088                    Map<String, Map<String, String[]>> plidPool = get(request, plid);
089    
090                    plidPool.put(portletId, params);
091            }
092    
093            private static Map<Long, Map<String, Map<String, String[]>>>
094                    _getRenderParametersPool(HttpSession session) {
095    
096                    Map<Long, Map<String, Map<String, String[]>>> renderParametersPool =
097                            (Map<Long, Map<String, Map<String, String[]>>>)session.getAttribute(
098                                    WebKeys.PORTLET_RENDER_PARAMETERS);
099    
100                    if (renderParametersPool == null) {
101                            renderParametersPool = new ConcurrentHashMap
102                                    <Long, Map<String, Map<String, String[]>>>();
103    
104                            session.setAttribute(
105                                    WebKeys.PORTLET_RENDER_PARAMETERS, renderParametersPool);
106                    }
107    
108                    return renderParametersPool;
109            }
110    
111    }