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.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  }