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                            return DoPrivilegedUtil.wrap(portletContext);
068                    }
069    
070                    PortletApp portletApp = portlet.getPortletApp();
071    
072                    if (portletApp.isWARFile()) {
073                            PortletBag portletBag = PortletBagPool.get(
074                                    portlet.getRootPortletId());
075    
076                            if (portletBag == null) {
077                                    _log.error(
078                                            "Portlet " + portlet.getRootPortletId() +
079                                                    " has a null portlet bag");
080                            }
081    
082                            //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
083    
084                            servletContext = portletBag.getServletContext();
085    
086                            // Context path for the portal must be passed to individual portlets
087    
088                            //ctx.setAttribute(WebKeys.MAIN_PATH, mainPath);
089                    }
090    
091                    portletContext = new PortletContextImpl(portlet, servletContext);
092    
093                    portletContexts.put(portlet.getPortletId(), portletContext);
094    
095                    return DoPrivilegedUtil.wrap(portletContext);
096            }
097    
098            private void _destroy(Portlet portlet) {
099                    _pool.remove(portlet.getRootPortletId());
100            }
101    
102            private static Log _log = LogFactoryUtil.getLog(
103                    PortletContextFactory.class);
104    
105            private static PortletContextFactory _instance =
106                    new PortletContextFactory();
107    
108            private Map<String, Map<String, PortletContext>> _pool;
109    
110    }