1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.velocity;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.Map;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  import javax.servlet.ServletContext;
32  
33  /**
34   * <a href="VelocityContextPool.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class VelocityContextPool {
39  
40      public static boolean containsKey(String name) {
41          return _instance._containsKey(name);
42      }
43  
44      public static ServletContext get(String name) {
45          return _instance._get(name);
46      }
47  
48      public static void put(String name, ServletContext servletContext) {
49          _instance._put(name, servletContext);
50      }
51  
52      public static ServletContext remove(String name) {
53          return _instance._remove(name);
54      }
55  
56      private VelocityContextPool() {
57          _pool = new ConcurrentHashMap<String, ServletContext>();
58      }
59  
60      private boolean _containsKey(String name) {
61          boolean value = _pool.containsKey(name);
62  
63          if (_log.isDebugEnabled()) {
64              _log.debug("Contains key " + name + " " + value);
65          }
66  
67          return value;
68      }
69  
70      private ServletContext _get(String name) {
71          ServletContext servletContext = _pool.get(name);
72  
73          if (_log.isDebugEnabled()) {
74              _log.debug("Get " + name + " " + servletContext);
75          }
76  
77          return servletContext;
78      }
79  
80      private void _put(String name, ServletContext servletContext) {
81          if (_log.isDebugEnabled()) {
82              _log.debug("Put " + name + " " + servletContext);
83          }
84  
85          _pool.put(name, servletContext);
86      }
87  
88      private ServletContext _remove(String name) {
89          ServletContext servletContext = _pool.remove(name);
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug("Remove " + name + " " + servletContext);
93          }
94  
95          return servletContext;
96      }
97  
98      private static Log _log = LogFactoryUtil.getLog(VelocityContextPool.class);
99  
100     private static VelocityContextPool _instance = new VelocityContextPool();
101 
102     private Map<String, ServletContext> _pool;
103 
104 }