001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.springframework.webflow.context.portlet;
017:
018: import java.util.Iterator;
019:
020: import javax.portlet.PortletRequest;
021: import javax.portlet.PortletSession;
022:
023: import org.springframework.binding.collection.SharedMap;
024: import org.springframework.binding.collection.StringKeyedMapAdapter;
025: import org.springframework.web.util.WebUtils;
026: import org.springframework.webflow.context.servlet.HttpSessionMapBindingListener;
027: import org.springframework.webflow.core.collection.AttributeMapBindingListener;
028: import org.springframework.webflow.core.collection.CollectionUtils;
029:
030: /**
031: * Shared map backed by the Portlet session for accessing session scoped
032: * attributes in a Portlet environment.
033: *
034: * @author Keith Donald
035: */
036: public class PortletSessionMap extends StringKeyedMapAdapter implements
037: SharedMap {
038:
039: /**
040: * The wrapped portlet request, providing access to the session.
041: */
042: private PortletRequest request;
043:
044: /**
045: * The scope to access in the session, either APPLICATION (global) or
046: * PORTLET.
047: */
048: private int scope;
049:
050: /**
051: * Create a new map wrapping the session associated with given request.
052: * @param request the current portlet request
053: * @param scope the scope to access in the session, either
054: * {@link PortletSession#APPLICATION_SCOPE} (global) or
055: * {@link PortletSession#PORTLET_SCOPE}
056: */
057: public PortletSessionMap(PortletRequest request, int scope) {
058: this .request = request;
059: this .scope = scope;
060: }
061:
062: /**
063: * Return the portlet session associated with the wrapped request, or null
064: * if no such session exits.
065: */
066: private PortletSession getSession() {
067: return request.getPortletSession(false);
068: }
069:
070: protected Object getAttribute(String key) {
071: PortletSession session = getSession();
072: if (session == null) {
073: return null;
074: }
075: Object value = session.getAttribute(key, scope);
076: if (value instanceof HttpSessionMapBindingListener) {
077: // unwrap
078: return ((HttpSessionMapBindingListener) value)
079: .getListener();
080: } else {
081: return value;
082: }
083: }
084:
085: protected void setAttribute(String key, Object value) {
086: PortletSession session = request.getPortletSession(true);
087: if (value instanceof AttributeMapBindingListener) {
088: // wrap
089: session.setAttribute(key,
090: new HttpSessionMapBindingListener(
091: (AttributeMapBindingListener) value, this ),
092: scope);
093: } else {
094: session.setAttribute(key, value, scope);
095: }
096: }
097:
098: protected void removeAttribute(String key) {
099: PortletSession session = getSession();
100: if (session != null) {
101: session.removeAttribute(key, scope);
102: }
103: }
104:
105: protected Iterator getAttributeNames() {
106: PortletSession session = getSession();
107: return session == null ? CollectionUtils.EMPTY_ITERATOR
108: : CollectionUtils.toIterator(session
109: .getAttributeNames(scope));
110: }
111:
112: public Object getMutex() {
113: PortletSession session = request.getPortletSession(true);
114: Object mutex = session.getAttribute(
115: WebUtils.SESSION_MUTEX_ATTRIBUTE, scope);
116: return mutex != null ? mutex : session;
117: }
118: }
|