001    /**
002     * Copyright (c) 2000-2012 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 void clear() {
032                    _instance._servletContexts.clear();
033            }
034    
035            public static boolean containsKey(String servletContextName) {
036                    return _instance._containsKey(servletContextName);
037            }
038    
039            public static ServletContext get(String servletContextName) {
040                    return _instance._get(servletContextName);
041            }
042    
043            public static Set<String> keySet() {
044                    return _instance._keySet();
045            }
046    
047            public static void put(
048                    String servletContextName, ServletContext servletContext) {
049    
050                    _instance._put(servletContextName, servletContext);
051            }
052    
053            public static ServletContext remove(String servletContextName) {
054                    return _instance._remove(servletContextName);
055            }
056    
057            private ServletContextPool() {
058                    _servletContexts = new ConcurrentHashMap<String, ServletContext>();
059            }
060    
061            private boolean _containsKey(String servletContextName) {
062                    if (servletContextName == null) {
063                            return false;
064                    }
065    
066                    boolean value = _servletContexts.containsKey(servletContextName);
067    
068                    if (_log.isDebugEnabled()) {
069                            _log.debug("Contains key " + servletContextName + " " + value);
070                    }
071    
072                    return value;
073            }
074    
075            private ServletContext _get(String servletContextName) {
076                    ServletContext servletContext = _servletContexts.get(
077                            servletContextName);
078    
079                    if (_log.isDebugEnabled()) {
080                            _log.debug("Get " + servletContextName + " " + servletContext);
081                    }
082    
083                    return servletContext;
084            }
085    
086            private Set<String> _keySet() {
087                    return _servletContexts.keySet();
088            }
089    
090            private void _put(
091                    String servletContextName, ServletContext servletContext) {
092    
093                    if (_log.isDebugEnabled()) {
094                            _log.debug("Put " + servletContextName + " " + servletContext);
095                    }
096    
097                    _servletContexts.put(servletContextName, servletContext);
098            }
099    
100            private ServletContext _remove(String servletContextName) {
101                    ServletContext servletContext = _servletContexts.remove(
102                            servletContextName);
103    
104                    if (_log.isDebugEnabled()) {
105                            _log.debug("Remove " + servletContextName + " " + servletContext);
106                    }
107    
108                    return servletContext;
109            }
110    
111            private static Log _log = LogFactoryUtil.getLog(ServletContextPool.class);
112    
113            private static ServletContextPool _instance = new ServletContextPool();
114    
115            private Map<String, ServletContext> _servletContexts;
116    
117    }