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.kernel.model.Layout;
020    import com.liferay.portal.kernel.model.LayoutSet;
021    import com.liferay.portal.kernel.service.LayoutLocalServiceUtil;
022    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.concurrent.ConcurrentHashMap;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpSession;
032    
033    /**
034     * @author Michael Young
035     */
036    public class PublicRenderParametersPool {
037    
038            public static Map<String, String[]> get(
039                    HttpServletRequest request, long plid, boolean warFile) {
040    
041                    Map<String, String[]> map1 = get(request, plid);
042    
043                    if (warFile) {
044                            Map<String, String[]> map2 =_publicRenderParametersMap.get();
045    
046                            map1.putAll(map2);
047    
048                            return new PublicRenderParameters(map1, map2);
049                    }
050    
051                    return map1;
052            }
053    
054            protected static Map<String, String[]> get(
055                    HttpServletRequest request, long plid) {
056    
057                    if (PropsValues.PORTLET_PUBLIC_RENDER_PARAMETER_DISTRIBUTION_LAYOUT) {
058                            return RenderParametersPool.getOrCreate(
059                                    request, plid, _PUBLIC_RENDER_PARAMETERS);
060                    }
061    
062                    HttpSession session = request.getSession();
063    
064                    Map<Long, Map<String, String[]>> publicRenderParametersPool =
065                            (Map<Long, Map<String, String[]>>)session.getAttribute(
066                                    WebKeys.PUBLIC_RENDER_PARAMETERS_POOL);
067    
068                    if (publicRenderParametersPool == null) {
069                            publicRenderParametersPool = new ConcurrentHashMap<>();
070    
071                            session.setAttribute(
072                                    WebKeys.PUBLIC_RENDER_PARAMETERS_POOL,
073                                    publicRenderParametersPool);
074                    }
075    
076                    try {
077                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
078    
079                            LayoutSet layoutSet = layout.getLayoutSet();
080    
081                            Map<String, String[]> publicRenderParameters =
082                                    publicRenderParametersPool.get(layoutSet.getLayoutSetId());
083    
084                            if (publicRenderParameters == null) {
085                                    publicRenderParameters = new HashMap<>();
086    
087                                    publicRenderParametersPool.put(
088                                            layoutSet.getLayoutSetId(), publicRenderParameters);
089                            }
090    
091                            return publicRenderParameters;
092                    }
093                    catch (Exception e) {
094                            if (_log.isWarnEnabled()) {
095                                    _log.warn(e, e);
096                            }
097    
098                            return new HashMap<>();
099                    }
100            }
101    
102            private static final String _PUBLIC_RENDER_PARAMETERS =
103                    "PUBLIC_RENDER_PARAMETERS";
104    
105            private static final Log _log = LogFactoryUtil.getLog(
106                    PublicRenderParametersPool.class);
107    
108            private static final ThreadLocal<Map<String, String[]>>
109                    _publicRenderParametersMap =
110                            new AutoResetThreadLocal<Map<String, String[]>>(
111                                    PublicRenderParametersPool.class +
112                                            "._publicRenderParametersMap",
113                                    new HashMap<String, String[]>());
114    
115    }