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