001    /**
002     * Copyright (c) 2000-2013 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 PortletContextFactory {
036    
037            public static PortletContext create(
038                    Portlet portlet, ServletContext servletContext) {
039    
040                    return _instance._create(portlet, servletContext);
041            }
042    
043            public static void destroy(Portlet portlet) {
044                    _instance._destroy(portlet);
045            }
046    
047            private PortletContextFactory() {
048                    _pool = new ConcurrentHashMap<String, Map<String, PortletContext>>();
049            }
050    
051            private PortletContext _create(
052                    Portlet portlet, ServletContext servletContext) {
053    
054                    Map<String, PortletContext> portletContexts = _pool.get(
055                            portlet.getRootPortletId());
056    
057                    if (portletContexts == null) {
058                            portletContexts = new ConcurrentHashMap<String, PortletContext>();
059    
060                            _pool.put(portlet.getRootPortletId(), portletContexts);
061                    }
062    
063                    PortletContext portletContext = portletContexts.get(
064                            portlet.getPortletId());
065    
066                    if (portletContext == null) {
067                            PortletApp portletApp = portlet.getPortletApp();
068    
069                            if (portletApp.isWARFile()) {
070                                    PortletBag portletBag = PortletBagPool.get(
071                                            portlet.getRootPortletId());
072    
073                                    if (portletBag == null) {
074                                            _log.error(
075                                                    "Portlet " + portlet.getRootPortletId() +
076                                                            " has a null portlet bag");
077                                    }
078    
079                                    //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
080    
081                                    servletContext = portletBag.getServletContext();
082    
083                                    // Context path for the portal must be passed to individual
084                                    // portlets
085    
086                                    //ctx.setAttribute(WebKeys.MAIN_PATH, mainPath);
087                            }
088    
089                            portletContext = new PortletContextImpl(portlet, servletContext);
090    
091                            portletContexts.put(portlet.getPortletId(), portletContext);
092                    }
093    
094                    return DoPrivilegedUtil.wrap(portletContext);
095            }
096    
097            private void _destroy(Portlet portlet) {
098                    _pool.remove(portlet.getRootPortletId());
099            }
100    
101            private static Log _log = LogFactoryUtil.getLog(
102                    PortletContextFactory.class);
103    
104            private static PortletContextFactory _instance =
105                    new PortletContextFactory();
106    
107            private Map<String, Map<String, PortletContext>> _pool;
108    
109    }