001    /**
002     * Copyright (c) 2000-present 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.model.Portlet;
018    import com.liferay.portal.kernel.model.PortletConstants;
019    import com.liferay.portal.kernel.portlet.PortletConfigFactory;
020    import com.liferay.portal.kernel.portlet.PortletContextFactory;
021    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
022    import com.liferay.portal.security.lang.DoPrivilegedUtil;
023    
024    import java.util.Map;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    import javax.portlet.PortletConfig;
028    import javax.portlet.PortletContext;
029    
030    import javax.servlet.ServletContext;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    @DoPrivileged
036    public class PortletConfigFactoryImpl implements PortletConfigFactory {
037    
038            public PortletConfigFactoryImpl() {
039                    _pool = new ConcurrentHashMap<>();
040            }
041    
042            @Override
043            public PortletConfig create(
044                    Portlet portlet, ServletContext servletContext) {
045    
046                    Map<String, PortletConfig> portletConfigs = _pool.get(
047                            portlet.getRootPortletId());
048    
049                    if (portletConfigs == null) {
050                            portletConfigs = new ConcurrentHashMap<>();
051    
052                            _pool.put(portlet.getRootPortletId(), portletConfigs);
053                    }
054    
055                    PortletConfig portletConfig = portletConfigs.get(
056                            portlet.getPortletId());
057    
058                    if (portletConfig == null) {
059                            PortletContext portletContext = _portletContextFactory.create(
060                                    portlet, servletContext);
061    
062                            portletConfig = new PortletConfigImpl(portlet, portletContext);
063    
064                            portletConfigs.put(portlet.getPortletId(), portletConfig);
065                    }
066    
067                    return DoPrivilegedUtil.wrap(portletConfig);
068            }
069    
070            @Override
071            public void destroy(Portlet portlet) {
072                    _pool.remove(portlet.getRootPortletId());
073            }
074    
075            @Override
076            public PortletConfig get(Portlet portlet) {
077                    return get(portlet.getPortletId());
078            }
079    
080            @Override
081            public PortletConfig get(String portletId) {
082                    String rootPortletId = PortletConstants.getRootPortletId(portletId);
083    
084                    Map<String, PortletConfig> portletConfigs = _pool.get(rootPortletId);
085    
086                    if (portletConfigs == null) {
087                            return null;
088                    }
089    
090                    return portletConfigs.get(portletId);
091            }
092    
093            public void setPortletContextFactory(
094                    PortletContextFactory portletContextFactory) {
095    
096                    _portletContextFactory = portletContextFactory;
097            }
098    
099            @Override
100            public PortletConfig update(Portlet portlet) {
101                    Map<String, PortletConfig> portletConfigs = _pool.get(
102                            portlet.getRootPortletId());
103    
104                    if (portletConfigs == null) {
105                            return null;
106                    }
107    
108                    PortletConfig portletConfig = portletConfigs.get(
109                            portlet.getPortletId());
110    
111                    PortletContext portletContext = portletConfig.getPortletContext();
112    
113                    portletConfig = new PortletConfigImpl(portlet, portletContext);
114    
115                    portletConfigs.put(portlet.getPortletId(), portletConfig);
116    
117                    return DoPrivilegedUtil.wrap(portletConfig);
118            }
119    
120            private final Map<String, Map<String, PortletConfig>> _pool;
121            private PortletContextFactory _portletContextFactory;
122    
123    }