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.security.pacl.DoPrivileged;
018    import com.liferay.portal.model.Portlet;
019    import com.liferay.portal.security.lang.DoPrivilegedUtil;
020    
021    import java.util.Map;
022    import java.util.concurrent.ConcurrentHashMap;
023    
024    import javax.portlet.PortletConfig;
025    import javax.portlet.PortletContext;
026    
027    import javax.servlet.ServletContext;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    @DoPrivileged
033    public class PortletConfigFactoryImpl implements PortletConfigFactory {
034    
035            public PortletConfigFactoryImpl() {
036                    _pool = new ConcurrentHashMap<String, Map<String, PortletConfig>>();
037            }
038    
039            public PortletConfig create(
040                    Portlet portlet, ServletContext servletContext) {
041    
042                    Map<String, PortletConfig> portletConfigs = _pool.get(
043                            portlet.getRootPortletId());
044    
045                    if (portletConfigs == null) {
046                            portletConfigs = new ConcurrentHashMap<String, PortletConfig>();
047    
048                            _pool.put(portlet.getRootPortletId(), portletConfigs);
049                    }
050    
051                    PortletConfig portletConfig = portletConfigs.get(
052                            portlet.getPortletId());
053    
054                    if (portletConfig == null) {
055                            PortletContext portletContext = PortletContextFactory.create(
056                                    portlet, servletContext);
057    
058                            portletConfig = new PortletConfigImpl(portlet, portletContext);
059    
060                            portletConfigs.put(portlet.getPortletId(), portletConfig);
061                    }
062    
063                    return DoPrivilegedUtil.wrap(portletConfig);
064            }
065    
066            public void destroy(Portlet portlet) {
067                    _pool.remove(portlet.getRootPortletId());
068            }
069    
070            public PortletConfig update(Portlet portlet) {
071                    Map<String, PortletConfig> portletConfigs = _pool.get(
072                            portlet.getRootPortletId());
073    
074                    if (portletConfigs == null) {
075                            return null;
076                    }
077    
078                    PortletConfig portletConfig = portletConfigs.get(
079                            portlet.getPortletId());
080    
081                    PortletContext portletContext = portletConfig.getPortletContext();
082    
083                    portletConfig = new PortletConfigImpl(portlet, portletContext);
084    
085                    portletConfigs.put(portlet.getPortletId(), portletConfig);
086    
087                    return DoPrivilegedUtil.wrap(portletConfig);
088            }
089    
090            private Map<String, Map<String, PortletConfig>> _pool;
091    
092    }