1
22
23 package com.liferay.util.servlet;
24
25 import com.liferay.util.ListUtil;
26
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.concurrent.ConcurrentHashMap;
32
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpSession;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39
45 public class SharedSessionWrapper implements HttpSession {
46
47 public SharedSessionWrapper(HttpSession ses) {
48 this(ses, new ConcurrentHashMap<String, Object>());
49 }
50
51 public SharedSessionWrapper(
52 HttpSession ses, Map<String, Object> sharedAttributes) {
53
54 if (ses == null) {
55 _ses = new NullSession();
56
57 if (_log.isWarnEnabled()) {
58 _log.warn("Wrapped session is null");
59 }
60 }
61 else {
62 _ses = ses;
63 }
64
65 _sharedAttributes = sharedAttributes;
66 }
67
68 public Object getAttribute(String name) {
69 Object value = _ses.getAttribute(name);
70
71 if (value == null) {
72 value = _sharedAttributes.get(name);
73 }
74
75 return value;
76 }
77
78 public Enumeration<String> getAttributeNames() {
79 if (_sharedAttributes.size() > 0) {
80 List<String> names = ListUtil.fromEnumeration(_ses.getAttributeNames());
81
82 for (String name : _sharedAttributes.keySet()) {
83 names.add(name);
84 }
85
86 return Collections.enumeration(names);
87 }
88 else {
89 return _ses.getAttributeNames();
90 }
91 }
92
93 public long getCreationTime() {
94 return _ses.getCreationTime();
95 }
96
97 public String getId() {
98 return _ses.getId();
99 }
100
101 public long getLastAccessedTime() {
102 return _ses.getLastAccessedTime();
103 }
104
105 public int getMaxInactiveInterval() {
106 return _ses.getMaxInactiveInterval();
107 }
108
109 public ServletContext getServletContext() {
110 return _ses.getServletContext();
111 }
112
113
116 public javax.servlet.http.HttpSessionContext getSessionContext() {
117 return _ses.getSessionContext();
118 }
119
120 public Object getValue(String name) {
121 return getAttribute(name);
122 }
123
124 public String[] getValueNames() {
125 List<String> names = ListUtil.fromEnumeration(getAttributeNames());
126
127 return names.toArray(new String[names.size()]);
128 }
129
130 public void invalidate() {
131 _ses.invalidate();
132 }
133
134 public boolean isNew() {
135 return _ses.isNew();
136 }
137
138 public void putValue(String name, Object value) {
139 setAttribute(name, value);
140 }
141
142 public void removeAttribute(String name) {
143 _ses.removeAttribute(name);
144 }
145
146 public void removeValue(String name) {
147 removeAttribute(name);
148 }
149
150 public void setAttribute(String name, Object value) {
151 _ses.setAttribute(name, value);
152 }
153
154 public void setMaxInactiveInterval(int maxInactiveInterval) {
155 _ses.setMaxInactiveInterval(maxInactiveInterval);
156 }
157
158 private static Log _log = LogFactory.getLog(SharedSessionWrapper.class);
159
160 private HttpSession _ses;
161 private Map<String, Object> _sharedAttributes;
162
163 }