001    /**
002     * Copyright (c) 2000-present 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.LayoutSet;
021    import com.liferay.portal.service.LayoutLocalServiceUtil;
022    import com.liferay.portal.util.PropsValues;
023    import com.liferay.portal.util.WebKeys;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    import java.util.concurrent.ConcurrentHashMap;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpSession;
031    
032    /**
033     * @author Michael Young
034     */
035    public class PublicRenderParametersPool {
036    
037            public static Map<String, String[]> get(
038                    HttpServletRequest request, long plid) {
039    
040                    if (PropsValues.PORTLET_PUBLIC_RENDER_PARAMETER_DISTRIBUTION_LAYOUT) {
041                            return RenderParametersPool.get(
042                                    request, plid, _PUBLIC_RENDER_PARAMETERS);
043                    }
044    
045                    HttpSession session = request.getSession();
046    
047                    Map<Long, Map<String, String[]>> publicRenderParametersPool =
048                            (Map<Long, Map<String, String[]>>)session.getAttribute(
049                                    WebKeys.PUBLIC_RENDER_PARAMETERS_POOL);
050    
051                    if (publicRenderParametersPool == null) {
052                            publicRenderParametersPool = new ConcurrentHashMap<>();
053    
054                            session.setAttribute(
055                                    WebKeys.PUBLIC_RENDER_PARAMETERS_POOL,
056                                    publicRenderParametersPool);
057                    }
058    
059                    try {
060                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
061    
062                            LayoutSet layoutSet = layout.getLayoutSet();
063    
064                            Map<String, String[]> publicRenderParameters =
065                                    publicRenderParametersPool.get(layoutSet.getLayoutSetId());
066    
067                            if (publicRenderParameters == null) {
068                                    publicRenderParameters = new HashMap<>();
069    
070                                    publicRenderParametersPool.put(
071                                            layoutSet.getLayoutSetId(), publicRenderParameters);
072                            }
073    
074                            return publicRenderParameters;
075                    }
076                    catch (Exception e) {
077                            if (_log.isWarnEnabled()) {
078                                    _log.warn(e, e);
079                            }
080    
081                            return new HashMap<>();
082                    }
083            }
084    
085            private static final String _PUBLIC_RENDER_PARAMETERS =
086                    "PUBLIC_RENDER_PARAMETERS";
087    
088            private static final Log _log = LogFactoryUtil.getLog(
089                    PublicRenderParametersPool.class);
090    
091    }