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.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.Map;
021    import java.util.Set;
022    import java.util.concurrent.ConcurrentHashMap;
023    
024    import javax.servlet.ServletContext;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class ServletContextPool {
030    
031            public static boolean containsKey(String servletContextName) {
032                    return _instance._containsKey(servletContextName);
033            }
034    
035            public static ServletContext get(String servletContextName) {
036                    return _instance._get(servletContextName);
037            }
038    
039            public static Set<String> keySet() {
040                    return _instance._keySet();
041            }
042    
043            public static void put(
044                    String servletContextName, ServletContext servletContext) {
045    
046                    _instance._put(servletContextName, servletContext);
047            }
048    
049            public static ServletContext remove(String servletContextName) {
050                    return _instance._remove(servletContextName);
051            }
052    
053            private ServletContextPool() {
054                    _servletContexts = new ConcurrentHashMap<String, ServletContext>();
055            }
056    
057            private boolean _containsKey(String servletContextName) {
058                    boolean value = _servletContexts.containsKey(servletContextName);
059    
060                    if (_log.isDebugEnabled()) {
061                            _log.debug("Contains key " + servletContextName + " " + value);
062                    }
063    
064                    return value;
065            }
066    
067            private ServletContext _get(String servletContextName) {
068                    ServletContext servletContext = _servletContexts.get(
069                            servletContextName);
070    
071                    if (_log.isDebugEnabled()) {
072                            _log.debug("Get " + servletContextName + " " + servletContext);
073                    }
074    
075                    return servletContext;
076            }
077    
078            private Set<String> _keySet() {
079                    return _servletContexts.keySet();
080            }
081    
082            private void _put(
083                    String servletContextName, ServletContext servletContext) {
084    
085                    if (_log.isDebugEnabled()) {
086                            _log.debug("Put " + servletContextName + " " + servletContext);
087                    }
088    
089                    _servletContexts.put(servletContextName, servletContext);
090            }
091    
092            private ServletContext _remove(String servletContextName) {
093                    ServletContext servletContext = _servletContexts.remove(
094                            servletContextName);
095    
096                    if (_log.isDebugEnabled()) {
097                            _log.debug("Remove " + servletContextName + " " + servletContext);
098                    }
099    
100                    return servletContext;
101            }
102    
103            private static Log _log = LogFactoryUtil.getLog(ServletContextPool.class);
104    
105            private static ServletContextPool _instance = new ServletContextPool();
106    
107            private Map<String, ServletContext> _servletContexts;
108    
109    }