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