001    /**
002     * Copyright (c) 2000-2010 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.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                    PortletConfig portletConfig = portletConfigs.get(
072                            portlet.getPortletId());
073    
074                    PortletContext portletContext = portletConfig.getPortletContext();
075    
076                    portletConfig = new PortletConfigImpl(portlet, portletContext);
077    
078                    portletConfigs.put(portlet.getPortletId(), portletConfig);
079    
080                    return portletConfig;
081            }
082    
083            private Map<String, Map<String, PortletConfig>> _pool;
084    
085    }