1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.velocity;
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.concurrent.ConcurrentHashMap;
22  
23  import javax.servlet.ServletContext;
24  
25  /**
26   * <a href="VelocityContextPool.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class VelocityContextPool {
31  
32      public static boolean containsKey(String name) {
33          return _instance._containsKey(name);
34      }
35  
36      public static ServletContext get(String name) {
37          return _instance._get(name);
38      }
39  
40      public static void put(String name, ServletContext servletContext) {
41          _instance._put(name, servletContext);
42      }
43  
44      public static ServletContext remove(String name) {
45          return _instance._remove(name);
46      }
47  
48      private Map<String, ServletContext> _pool;
49  
50      private VelocityContextPool() {
51          _pool = new ConcurrentHashMap<String, ServletContext>();
52      }
53  
54      private boolean _containsKey(String name) {
55          boolean value = _pool.containsKey(name);
56  
57          if (_log.isDebugEnabled()) {
58              _log.debug("Contains key " + name + " " + value);
59          }
60  
61          return value;
62      }
63  
64      private ServletContext _get(String name) {
65          ServletContext servletContext = _pool.get(name);
66  
67          if (_log.isDebugEnabled()) {
68              _log.debug("Get " + name + " " + servletContext);
69          }
70  
71          return servletContext;
72      }
73  
74      private void _put(String name, ServletContext servletContext) {
75          if (_log.isDebugEnabled()) {
76              _log.debug("Put " + name + " " + servletContext);
77          }
78  
79          _pool.put(name, servletContext);
80      }
81  
82      private ServletContext _remove(String name) {
83          ServletContext servletContext = _pool.remove(name);
84  
85          if (_log.isDebugEnabled()) {
86              _log.debug("Remove " + name + " " + servletContext);
87          }
88  
89          return servletContext;
90      }
91  
92      private static Log _log = LogFactoryUtil.getLog(VelocityContextPool.class);
93  
94      private static VelocityContextPool _instance = new VelocityContextPool();
95  
96  }