001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.servlet;
022:
023: import com.liferay.util.CollectionFactory;
024: import com.liferay.util.ListUtil;
025:
026: import java.util.Collections;
027: import java.util.Enumeration;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031:
032: import javax.servlet.ServletContext;
033: import javax.servlet.http.HttpSession;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: /**
039: * <a href="SharedSessionWrapper.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class SharedSessionWrapper implements HttpSession {
045:
046: public SharedSessionWrapper(HttpSession ses) {
047: this (ses, CollectionFactory.getSyncHashMap());
048: }
049:
050: public SharedSessionWrapper(HttpSession ses, Map sharedAttributes) {
051: if (ses == null) {
052: _ses = new NullSession();
053:
054: if (_log.isWarnEnabled()) {
055: _log.warn("Wrapped session is null");
056: }
057: } else {
058: _ses = ses;
059: }
060:
061: _sharedAttributes = sharedAttributes;
062: }
063:
064: public Object getAttribute(String name) {
065: Object value = _ses.getAttribute(name);
066:
067: if (value == null) {
068: value = _sharedAttributes.get(name);
069: }
070:
071: return value;
072: }
073:
074: public Enumeration getAttributeNames() {
075: if (_sharedAttributes.size() > 0) {
076: List names = ListUtil.fromEnumeration(_ses
077: .getAttributeNames());
078:
079: Iterator itr = _sharedAttributes.keySet().iterator();
080:
081: while (itr.hasNext()) {
082: String name = (String) itr.next();
083:
084: names.add(name);
085: }
086:
087: return Collections.enumeration(names);
088: } else {
089: return _ses.getAttributeNames();
090: }
091: }
092:
093: public long getCreationTime() {
094: return _ses.getCreationTime();
095: }
096:
097: public String getId() {
098: return _ses.getId();
099: }
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: /**
114: * @deprecated
115: */
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 names = ListUtil.fromEnumeration(getAttributeNames());
126:
127: return (String[]) names.toArray(new String[0]);
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
159: .getLog(SharedSessionWrapper.class);
160:
161: private HttpSession _ses;
162: private Map _sharedAttributes;
163:
164: }
|