1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.servlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.util.Map;
21  import java.util.Set;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  import javax.servlet.ServletContext;
25  
26  /**
27   * <a href="ServletContextPool.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class ServletContextPool {
32  
33      public static boolean containsKey(String servletContextName) {
34          return _instance._containsKey(servletContextName);
35      }
36  
37      public static ServletContext get(String servletContextName) {
38          return _instance._get(servletContextName);
39      }
40  
41      public static Set<String> keySet() {
42          return _instance._keySet();
43      }
44  
45      public static void put(
46          String servletContextName, ServletContext servletContext) {
47  
48          _instance._put(servletContextName, servletContext);
49      }
50  
51      public static ServletContext remove(String servletContextName) {
52          return _instance._remove(servletContextName);
53      }
54  
55      private ServletContextPool() {
56          _servletContexts = new ConcurrentHashMap<String, ServletContext>();
57      }
58  
59      private boolean _containsKey(String servletContextName) {
60          boolean value = _servletContexts.containsKey(servletContextName);
61  
62          if (_log.isDebugEnabled()) {
63              _log.debug("Contains key " + servletContextName + " " + value);
64          }
65  
66          return value;
67      }
68  
69      private ServletContext _get(String servletContextName) {
70          ServletContext servletContext = _servletContexts.get(
71              servletContextName);
72  
73          if (_log.isDebugEnabled()) {
74              _log.debug("Get " + servletContextName + " " + servletContext);
75          }
76  
77          return servletContext;
78      }
79  
80      private Set<String> _keySet() {
81          return _servletContexts.keySet();
82      }
83  
84      private void _put(
85          String servletContextName, ServletContext servletContext) {
86  
87          if (_log.isDebugEnabled()) {
88              _log.debug("Put " + servletContextName + " " + servletContext);
89          }
90  
91          _servletContexts.put(servletContextName, servletContext);
92      }
93  
94      private ServletContext _remove(String servletContextName) {
95          ServletContext servletContext = _servletContexts.remove(
96              servletContextName);
97  
98          if (_log.isDebugEnabled()) {
99              _log.debug("Remove " + servletContextName + " " + servletContext);
100         }
101 
102         return servletContext;
103     }
104 
105     private static Log _log = LogFactoryUtil.getLog(ServletContextPool.class);
106 
107     private static ServletContextPool _instance = new ServletContextPool();
108 
109     private Map<String, ServletContext> _servletContexts;
110 
111 }