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