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.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 =
054                            _pool.get(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    }