KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > context > portlet > PortletSessionMap


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package org.springframework.webflow.context.portlet;
17
18 import java.util.Iterator JavaDoc;
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 /**
31  * Shared map backed by the Portlet session for accessing session scoped
32  * attributes in a Portlet environment.
33  *
34  * @author Keith Donald
35  */

36 public class PortletSessionMap extends StringKeyedMapAdapter implements SharedMap {
37
38     /**
39      * The wrapped portlet request, providing access to the session.
40      */

41     private PortletRequest request;
42
43     /**
44      * The scope to access in the session, either APPLICATION (global) or
45      * PORTLET.
46      */

47     private int scope;
48
49     /**
50      * Create a new map wrapping the session associated with given request.
51      * @param request the current portlet request
52      * @param scope the scope to access in the session, either
53      * {@link PortletSession#APPLICATION_SCOPE} (global) or
54      * {@link PortletSession#PORTLET_SCOPE}
55      */

56     public PortletSessionMap(PortletRequest request, int scope) {
57         this.request = request;
58         this.scope = scope;
59     }
60
61     /**
62      * Return the portlet session associated with the wrapped request, or null
63      * if no such session exits.
64      */

65     private PortletSession getSession() {
66         return request.getPortletSession(false);
67     }
68
69     protected Object JavaDoc getAttribute(String JavaDoc key) {
70         PortletSession session = getSession();
71         if (session == null) {
72             return null;
73         }
74         Object JavaDoc value = session.getAttribute(key, scope);
75         if (value instanceof HttpSessionMapBindingListener) {
76             // unwrap
77
return ((HttpSessionMapBindingListener)value).getListener();
78         } else {
79             return value;
80         }
81     }
82
83     protected void setAttribute(String JavaDoc key, Object JavaDoc value) {
84         PortletSession session = request.getPortletSession(true);
85         if (value instanceof AttributeMapBindingListener) {
86             // wrap
87
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 JavaDoc key) {
95         PortletSession session = getSession();
96         if (session != null) {
97             session.removeAttribute(key, scope);
98         }
99     }
100
101     protected Iterator JavaDoc getAttributeNames() {
102         PortletSession session = getSession();
103         return session == null ? CollectionUtils.EMPTY_ITERATOR : CollectionUtils.toIterator(session
104                 .getAttributeNames(scope));
105     }
106
107     public Object JavaDoc getMutex() {
108         PortletSession session = request.getPortletSession(true);
109         Object JavaDoc mutex = session.getAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE, scope);
110         return mutex != null ? mutex : session;
111     }
112 }
Popular Tags