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.model.PortletApp;
018    
019    import java.util.Map;
020    import java.util.concurrent.ConcurrentHashMap;
021    
022    import javax.portlet.PortletContext;
023    import javax.portlet.filter.FilterConfig;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class FilterConfigFactory {
029    
030            public static FilterConfig create(
031                    com.liferay.portal.model.PortletFilter portletFilter,
032                    PortletContext ctx) {
033    
034                    return _instance._create(portletFilter, ctx);
035            }
036    
037            public static void destroy(
038                    com.liferay.portal.model.PortletFilter portletFilter) {
039    
040                    _instance._destroy(portletFilter);
041            }
042    
043            private FilterConfigFactory() {
044                    _pool = new ConcurrentHashMap<String, Map<String, FilterConfig>>();
045            }
046    
047            private FilterConfig _create(
048                    com.liferay.portal.model.PortletFilter portletFilter,
049                    PortletContext ctx) {
050    
051                    PortletApp portletApp = portletFilter.getPortletApp();
052    
053                    Map<String, FilterConfig> filterConfigs = _pool.get(
054                            portletApp.getServletContextName());
055    
056                    if (filterConfigs == null) {
057                            filterConfigs = new ConcurrentHashMap<String, FilterConfig>();
058    
059                            _pool.put(portletApp.getServletContextName(), filterConfigs);
060                    }
061    
062                    FilterConfig filterConfig = filterConfigs.get(
063                            portletFilter.getFilterName());
064    
065                    if (filterConfig == null) {
066                            filterConfig = new FilterConfigImpl(
067                                    portletFilter.getFilterName(), ctx,
068                                    portletFilter.getInitParams());
069    
070                            filterConfigs.put(portletFilter.getFilterName(), filterConfig);
071                    }
072    
073                    return filterConfig;
074            }
075    
076            private void _destroy(
077                    com.liferay.portal.model.PortletFilter portletFilter) {
078    
079                    PortletApp portletApp = portletFilter.getPortletApp();
080    
081                    _pool.remove(portletApp.getServletContextName());
082            }
083    
084            private static FilterConfigFactory _instance = new FilterConfigFactory();
085    
086            private Map<String, Map<String, FilterConfig>> _pool;
087    
088    }