001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.model.Portlet;
018    
019    import java.util.Map;
020    import java.util.concurrent.ConcurrentHashMap;
021    
022    import javax.portlet.PortletConfig;
023    import javax.portlet.PortletContext;
024    
025    import javax.servlet.ServletContext;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PortletConfigFactoryImpl implements PortletConfigFactory {
031    
032            public PortletConfigFactoryImpl() {
033                    _pool = new ConcurrentHashMap<String, Map<String, PortletConfig>>();
034            }
035    
036            public PortletConfig create(
037                    Portlet portlet, ServletContext servletContext) {
038    
039                    Map<String, PortletConfig> portletConfigs =
040                            _pool.get(portlet.getRootPortletId());
041    
042                    if (portletConfigs == null) {
043                            portletConfigs = new ConcurrentHashMap<String, PortletConfig>();
044    
045                            _pool.put(portlet.getRootPortletId(), portletConfigs);
046                    }
047    
048                    PortletConfig portletConfig = portletConfigs.get(
049                            portlet.getPortletId());
050    
051                    if (portletConfig == null) {
052                            PortletContext portletContext =
053                                    PortletContextFactory.create(portlet, servletContext);
054    
055                            portletConfig = new PortletConfigImpl(portlet, portletContext);
056    
057                            portletConfigs.put(portlet.getPortletId(), portletConfig);
058                    }
059    
060                    return portletConfig;
061            }
062    
063            public void destroy(Portlet portlet) {
064                    _pool.remove(portlet.getRootPortletId());
065            }
066    
067            public PortletConfig update(Portlet portlet) {
068                    Map<String, PortletConfig> portletConfigs =
069                            _pool.get(portlet.getRootPortletId());
070    
071                    if (portletConfigs == null) {
072                            return null;
073                    }
074    
075                    PortletConfig portletConfig = portletConfigs.get(
076                            portlet.getPortletId());
077    
078                    PortletContext portletContext = portletConfig.getPortletContext();
079    
080                    portletConfig = new PortletConfigImpl(portlet, portletContext);
081    
082                    portletConfigs.put(portlet.getPortletId(), portletConfig);
083    
084                    return portletConfig;
085            }
086    
087            private Map<String, Map<String, PortletConfig>> _pool;
088    
089    }