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.portlet;
022:
023: import com.liferay.portal.kernel.portlet.LiferayPortletSession;
024: import com.liferay.portal.kernel.util.StringPool;
025:
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.Enumeration;
029: import java.util.List;
030: import java.util.StringTokenizer;
031:
032: import javax.portlet.PortletContext;
033: import javax.portlet.PortletSession;
034:
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpSession;
037:
038: /**
039: * <a href="PortletSessionImpl.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class PortletSessionImpl implements LiferayPortletSession {
045:
046: public static final String getPortletScope(String portletName,
047: long plid) {
048: String portletScope = _PORTLET_SCOPE_NAMESPACE + portletName
049: + _LAYOUT_SEPARATOR + plid;
050:
051: return portletScope;
052: }
053:
054: public static final String getPortletScopeName(String portletName,
055: long plid, String name) {
056:
057: String portletScopeName = getPortletScope(portletName, plid)
058: + StringPool.QUESTION + name;
059:
060: return portletScopeName;
061: }
062:
063: public PortletSessionImpl(HttpServletRequest req,
064: String portletName, PortletContext ctx,
065: String portalSessionId, long plid) {
066:
067: _req = req;
068: _portletName = portletName;
069: _ctx = ctx;
070: _creationTime = System.currentTimeMillis();
071: _lastAccessedTime = _creationTime;
072: _interval = getHttpSession().getMaxInactiveInterval();
073: _new = true;
074: _invalid = false;
075: _portalSessionId = portalSessionId;
076: _plid = plid;
077: }
078:
079: public PortletContext getPortletContext() {
080: return _ctx;
081: }
082:
083: public String getId() {
084: return getHttpSession().getId();
085: }
086:
087: public long getCreationTime() {
088: if (_invalid) {
089: throw new IllegalStateException();
090: }
091:
092: return _creationTime;
093: }
094:
095: public long getLastAccessedTime() {
096: return _lastAccessedTime;
097: }
098:
099: public void setLastAccessedTime(long lastAccessedTime) {
100: _lastAccessedTime = lastAccessedTime;
101: _new = false;
102: }
103:
104: public int getMaxInactiveInterval() {
105: return _interval;
106: }
107:
108: public void setMaxInactiveInterval(int interval) {
109: _interval = interval;
110: }
111:
112: public boolean isNew() {
113: if (_invalid) {
114: throw new IllegalStateException();
115: }
116:
117: return _new;
118: }
119:
120: public Object getAttribute(String name) {
121: if (name == null) {
122: throw new IllegalArgumentException();
123: }
124:
125: if (_invalid) {
126: throw new IllegalStateException();
127: }
128:
129: return getAttribute(name, PortletSession.PORTLET_SCOPE);
130: }
131:
132: public Object getAttribute(String name, int scope) {
133: if (name == null) {
134: throw new IllegalArgumentException();
135: }
136:
137: if (_invalid) {
138: throw new IllegalStateException();
139: }
140:
141: if (scope == PortletSession.PORTLET_SCOPE) {
142: return getHttpSession().getAttribute(
143: _getPortletScopeName(name));
144: } else {
145: return getHttpSession().getAttribute(name);
146: }
147: }
148:
149: public Enumeration getAttributeNames() {
150: if (_invalid) {
151: throw new IllegalStateException();
152: }
153:
154: return getAttributeNames(PortletSession.PORTLET_SCOPE);
155: }
156:
157: public Enumeration getAttributeNames(int scope) {
158: if (_invalid) {
159: throw new IllegalStateException();
160: }
161:
162: if (scope == PortletSession.PORTLET_SCOPE) {
163: List attributeNames = new ArrayList();
164:
165: String portletScope = getPortletScope(_portletName, _plid);
166:
167: Enumeration enu = getHttpSession().getAttributeNames();
168:
169: while (enu.hasMoreElements()) {
170: String name = (String) enu.nextElement();
171:
172: StringTokenizer st = new StringTokenizer(name,
173: StringPool.QUESTION);
174:
175: if (st.countTokens() == 2) {
176: if (st.nextToken().equals(portletScope)) {
177: attributeNames.add(st.nextToken());
178: }
179: }
180: }
181:
182: return Collections.enumeration(attributeNames);
183: } else {
184: return getHttpSession().getAttributeNames();
185: }
186: }
187:
188: public void removeAttribute(String name) {
189: if (name == null) {
190: throw new IllegalArgumentException();
191: }
192:
193: if (_invalid) {
194: throw new IllegalStateException();
195: }
196:
197: removeAttribute(name, PortletSession.PORTLET_SCOPE);
198: }
199:
200: public void removeAttribute(String name, int scope) {
201: if (name == null) {
202: throw new IllegalArgumentException();
203: }
204:
205: if (_invalid) {
206: throw new IllegalStateException();
207: }
208:
209: if (scope == PortletSession.PORTLET_SCOPE) {
210: getHttpSession()
211: .removeAttribute(_getPortletScopeName(name));
212: } else {
213: getHttpSession().removeAttribute(name);
214: }
215: }
216:
217: public void setAttribute(String name, Object value) {
218: if (name == null) {
219: throw new IllegalArgumentException();
220: }
221:
222: if (_invalid) {
223: throw new IllegalStateException();
224: }
225:
226: setAttribute(name, value, PortletSession.PORTLET_SCOPE);
227: }
228:
229: public void setAttribute(String name, Object value, int scope) {
230: if (name == null) {
231: throw new IllegalArgumentException();
232: }
233:
234: if (_invalid) {
235: throw new IllegalStateException();
236: }
237:
238: if (scope == PortletSession.PORTLET_SCOPE) {
239: getHttpSession().setAttribute(_getPortletScopeName(name),
240: value);
241: } else {
242: getHttpSession().setAttribute(name, value);
243: }
244: }
245:
246: public void invalidate() {
247: if (_invalid) {
248: throw new IllegalStateException();
249: }
250:
251: getHttpSession().invalidate();
252:
253: _invalid = true;
254: }
255:
256: public boolean isValid() {
257: return !_invalid;
258: }
259:
260: public String getPortalSessionId() {
261: return _portalSessionId;
262: }
263:
264: public HttpSession getHttpSession() {
265: if (_ses == null) {
266: return _req.getSession();
267: } else {
268: return _ses;
269: }
270: }
271:
272: public void setHttpSession(HttpSession ses) {
273: _ses = ses;
274: }
275:
276: private String _getPortletScopeName(String name) {
277: return getPortletScopeName(_portletName, _plid, name);
278: }
279:
280: private static final String _PORTLET_SCOPE_NAMESPACE = "javax.portlet.p.";
281:
282: private static final String _LAYOUT_SEPARATOR = "_LAYOUT_";
283:
284: private HttpServletRequest _req;
285: private HttpSession _ses;
286: private String _portletName;
287: private PortletContext _ctx;
288: private long _creationTime;
289: private long _lastAccessedTime;
290: private int _interval;
291: private boolean _new;
292: private boolean _invalid;
293: private String _portalSessionId;
294: private long _plid;
295:
296: }
|