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.servlet;
16  
17  import java.io.Serializable;
18  
19  import java.util.Map;
20  import java.util.concurrent.ConcurrentHashMap;
21  
22  import javax.servlet.http.HttpSession;
23  
24  /**
25   * <a href="SharedSessionAttributeCache.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Michael C. Han
28   */
29  public class SharedSessionAttributeCache implements Serializable {
30  
31      public static SharedSessionAttributeCache getInstance(HttpSession session) {
32          synchronized (session) {
33              SharedSessionAttributeCache cache =
34                  (SharedSessionAttributeCache)session.getAttribute(_SESSION_KEY);
35  
36              if (cache == null) {
37                  cache = new SharedSessionAttributeCache();
38  
39                  session.setAttribute(_SESSION_KEY, cache);
40              }
41  
42              return cache;
43          }
44      }
45  
46      public boolean contains(String name) {
47          return _attributes.containsKey(name);
48      }
49  
50      public Map<String, Object> getValues() {
51          return _attributes;
52      }
53  
54      public void removeAttribute(String key) {
55          _attributes.remove(key);
56      }
57  
58      public void setAttribute(String key, Object value) {
59          _attributes.put(key, value);
60      }
61  
62      private SharedSessionAttributeCache() {
63          _attributes = new ConcurrentHashMap<String, Object>();
64      }
65  
66      private static final String _SESSION_KEY =
67          SharedSessionAttributeCache.class.getName();
68  
69      private Map<String, Object> _attributes;
70  
71  }