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