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.Portlet;
020    import com.liferay.portal.kernel.model.PortletApp;
021    import com.liferay.portal.kernel.portlet.PortletBag;
022    import com.liferay.portal.kernel.portlet.PortletBagPool;
023    import com.liferay.portal.kernel.portlet.PortletContextFactory;
024    import com.liferay.portal.security.lang.DoPrivilegedUtil;
025    
026    import java.util.Map;
027    import java.util.concurrent.ConcurrentHashMap;
028    
029    import javax.portlet.PortletContext;
030    
031    import javax.servlet.ServletContext;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class PortletContextFactoryImpl implements PortletContextFactory {
037    
038            @Override
039            public PortletContext create(
040                    Portlet portlet, ServletContext servletContext) {
041    
042                    Map<String, PortletContext> portletContexts = _pool.get(
043                            portlet.getRootPortletId());
044    
045                    if (portletContexts == null) {
046                            portletContexts = new ConcurrentHashMap<>();
047    
048                            _pool.put(portlet.getRootPortletId(), portletContexts);
049                    }
050    
051                    PortletContext portletContext = portletContexts.get(
052                            portlet.getPortletId());
053    
054                    if (portletContext != null) {
055                            return DoPrivilegedUtil.wrap(portletContext);
056                    }
057    
058                    PortletApp portletApp = portlet.getPortletApp();
059    
060                    if (portletApp.isWARFile()) {
061                            PortletBag portletBag = PortletBagPool.get(
062                                    portlet.getRootPortletId());
063    
064                            if (portletBag == null) {
065                                    _log.error(
066                                            "Portlet " + portlet.getRootPortletId() +
067                                                    " has a null portlet bag");
068                            }
069    
070                            //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
071    
072                            servletContext = portletBag.getServletContext();
073    
074                            // Context path for the portal must be passed to individual portlets
075    
076                            //ctx.setAttribute(WebKeys.MAIN_PATH, mainPath);
077                    }
078    
079                    portletContext = new PortletContextImpl(portlet, servletContext);
080    
081                    portletContexts.put(portlet.getPortletId(), portletContext);
082    
083                    return DoPrivilegedUtil.wrap(portletContext);
084            }
085    
086            @Override
087            public PortletContext createUntrackedInstance(
088                    Portlet portlet, ServletContext servletContext) {
089    
090                    PortletContext portletContext = new PortletContextImpl(
091                            portlet, servletContext);
092    
093                    return DoPrivilegedUtil.wrap(portletContext);
094            }
095    
096            @Override
097            public void destroy(Portlet portlet) {
098                    _pool.remove(portlet.getRootPortletId());
099            }
100    
101            private static final Log _log = LogFactoryUtil.getLog(
102                    PortletContextFactoryImpl.class);
103    
104            private final Map<String, Map<String, PortletContext>> _pool =
105                    new ConcurrentHashMap<>();
106    
107    }