1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.CollectionFactory;
26  
27  import java.util.Map;
28  
29  import javax.servlet.ServletContext;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * <a href="VelocityContextPool.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class VelocityContextPool {
41  
42      public static boolean containsKey(String name) {
43          return _instance._containsKey(name);
44      }
45  
46      public static ServletContext get(String name) {
47          return _instance._get(name);
48      }
49  
50      public static void put(String name, ServletContext ctx) {
51          _instance._put(name, ctx);
52      }
53  
54      public static ServletContext remove(String name) {
55          return _instance._remove(name);
56      }
57  
58      private VelocityContextPool() {
59          _pool = CollectionFactory.getSyncHashMap();
60      }
61  
62      private boolean _containsKey(String name) {
63          boolean value = _pool.containsKey(name);
64  
65          if (_log.isDebugEnabled()) {
66              _log.debug("Contains key " + name + " " + value);
67          }
68  
69          return value;
70      }
71  
72      private ServletContext _get(String name) {
73          ServletContext ctx = (ServletContext)_pool.get(name);
74  
75          if (_log.isDebugEnabled()) {
76              _log.debug("Get " + name + " " + ctx);
77          }
78  
79          return ctx;
80      }
81  
82      private void _put(String name, ServletContext ctx) {
83          if (_log.isDebugEnabled()) {
84              _log.debug("Put " + name + " " + ctx);
85          }
86  
87          _pool.put(name, ctx);
88      }
89  
90      private ServletContext _remove(String name) {
91          ServletContext ctx = (ServletContext)_pool.remove(name);
92  
93          if (_log.isDebugEnabled()) {
94              _log.debug("Remove " + name + " " + ctx);
95          }
96  
97          return ctx;
98      }
99  
100     private static Log _log = LogFactory.getLog(VelocityContextPool.class);
101 
102     private static VelocityContextPool _instance = new VelocityContextPool();
103 
104     private Map _pool;
105 
106 }