1 16 package org.springframework.webflow.context.portlet; 17 18 import java.util.Iterator ; 19 20 import javax.portlet.PortletRequest; 21 import javax.portlet.PortletSession; 22 23 import org.springframework.binding.collection.SharedMap; 24 import org.springframework.binding.collection.StringKeyedMapAdapter; 25 import org.springframework.web.util.WebUtils; 26 import org.springframework.webflow.context.servlet.HttpSessionMapBindingListener; 27 import org.springframework.webflow.core.collection.AttributeMapBindingListener; 28 import org.springframework.webflow.core.collection.CollectionUtils; 29 30 36 public class PortletSessionMap extends StringKeyedMapAdapter implements SharedMap { 37 38 41 private PortletRequest request; 42 43 47 private int scope; 48 49 56 public PortletSessionMap(PortletRequest request, int scope) { 57 this.request = request; 58 this.scope = scope; 59 } 60 61 65 private PortletSession getSession() { 66 return request.getPortletSession(false); 67 } 68 69 protected Object getAttribute(String key) { 70 PortletSession session = getSession(); 71 if (session == null) { 72 return null; 73 } 74 Object value = session.getAttribute(key, scope); 75 if (value instanceof HttpSessionMapBindingListener) { 76 return ((HttpSessionMapBindingListener)value).getListener(); 78 } else { 79 return value; 80 } 81 } 82 83 protected void setAttribute(String key, Object value) { 84 PortletSession session = request.getPortletSession(true); 85 if (value instanceof AttributeMapBindingListener) { 86 session.setAttribute(key, new HttpSessionMapBindingListener((AttributeMapBindingListener)value, this), scope); 88 } 89 else { 90 session.setAttribute(key, value, scope); 91 } 92 } 93 94 protected void removeAttribute(String key) { 95 PortletSession session = getSession(); 96 if (session != null) { 97 session.removeAttribute(key, scope); 98 } 99 } 100 101 protected Iterator getAttributeNames() { 102 PortletSession session = getSession(); 103 return session == null ? CollectionUtils.EMPTY_ITERATOR : CollectionUtils.toIterator(session 104 .getAttributeNames(scope)); 105 } 106 107 public Object getMutex() { 108 PortletSession session = request.getPortletSession(true); 109 Object mutex = session.getAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE, scope); 110 return mutex != null ? mutex : session; 111 } 112 } | Popular Tags |