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            @Override
040            public PortletConfig create(
041                    Portlet portlet, ServletContext servletContext) {
042    
043                    Map<String, PortletConfig> portletConfigs = _pool.get(
044                            portlet.getRootPortletId());
045    
046                    if (portletConfigs == null) {
047                            portletConfigs = new ConcurrentHashMap<String, PortletConfig>();
048    
049                            _pool.put(portlet.getRootPortletId(), portletConfigs);
050                    }
051    
052                    PortletConfig portletConfig = portletConfigs.get(
053                            portlet.getPortletId());
054    
055                    if (portletConfig == null) {
056                            PortletContext portletContext = PortletContextFactory.create(
057                                    portlet, servletContext);
058    
059                            portletConfig = new PortletConfigImpl(portlet, portletContext);
060    
061                            portletConfigs.put(portlet.getPortletId(), portletConfig);
062                    }
063    
064                    return DoPrivilegedUtil.wrap(portletConfig);
065            }
066    
067            @Override
068            public void destroy(Portlet portlet) {
069                    _pool.remove(portlet.getRootPortletId());
070            }
071    
072            @Override
073            public PortletConfig update(Portlet portlet) {
074                    Map<String, PortletConfig> portletConfigs = _pool.get(
075                            portlet.getRootPortletId());
076    
077                    if (portletConfigs == null) {
078                            return null;
079                    }
080    
081                    PortletConfig portletConfig = portletConfigs.get(
082                            portlet.getPortletId());
083    
084                    PortletContext portletContext = portletConfig.getPortletContext();
085    
086                    portletConfig = new PortletConfigImpl(portlet, portletContext);
087    
088                    portletConfigs.put(portlet.getPortletId(), portletConfig);
089    
090                    return DoPrivilegedUtil.wrap(portletConfig);
091            }
092    
093            private Map<String, Map<String, PortletConfig>> _pool;
094    
095    }