KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > context > PortletRequestAttributes


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.portlet.context;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.portlet.PortletRequest;
24 import javax.portlet.PortletSession;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.springframework.util.Assert;
30 import org.springframework.web.context.request.AbstractRequestAttributes;
31 import org.springframework.web.context.request.RequestAttributes;
32 import org.springframework.web.portlet.util.PortletUtils;
33
34 /**
35  * Portlet-based implementation of the RequestAttributes interface.
36  *
37  * <p>Accesses objects from portlet request and portlet session scope,
38  * with a distinction between "session" (the PortletSession's "portlet scope")
39  * and "global session" (the PortletSession's "application scope").
40  *
41  * @author Juergen Hoeller
42  * @since 2.0
43  * @see javax.portlet.PortletRequest#getAttribute
44  * @see javax.portlet.PortletSession#getAttribute
45  * @see javax.portlet.PortletSession#PORTLET_SCOPE
46  * @see javax.portlet.PortletSession#APPLICATION_SCOPE
47  * @see RequestAttributes#SCOPE_SESSION
48  * @see RequestAttributes#SCOPE_GLOBAL_SESSION
49  */

50 public class PortletRequestAttributes extends AbstractRequestAttributes {
51
52     /**
53      * We'll create a lot of these objects, so we don't want a new logger every time.
54      */

55     private static final Log logger = LogFactory.getLog(PortletRequestAttributes.class);
56
57
58     private final PortletRequest request;
59
60     private PortletSession session;
61
62     private final Map JavaDoc sessionAttributesToUpdate = new HashMap JavaDoc();
63
64     private final Map JavaDoc globalSessionAttributesToUpdate = new HashMap JavaDoc();
65
66
67     /**
68      * Create a new PortletRequestAttributes instance for the given request.
69      * @param request current portlet request
70      */

71     public PortletRequestAttributes(PortletRequest request) {
72         Assert.notNull(request, "Request must not be null");
73         this.request = request;
74         // Fetch existing session reference early, to have it available even
75
// after request completion (for example, in a custom child thread).
76
this.session = request.getPortletSession(false);
77     }
78
79
80     /**
81      * Expose the PortletRequest that we're wrapping to subclasses.
82      */

83     protected final PortletRequest getRequest() {
84         return this.request;
85     }
86
87     /**
88      * Exposes the {@link PortletSession} that we're wrapping.
89      * @param allowCreate whether to allow creation of a new session if none exists yet
90      */

91     protected final PortletSession getSession(boolean allowCreate) {
92         try {
93             this.session = this.request.getPortletSession(allowCreate);
94             return this.session;
95         }
96         catch (IllegalStateException JavaDoc ex) {
97             // Couldn't access session... let's check why.
98
if (this.session == null) {
99                 // No matter what happened - we cannot offer a session.
100
throw ex;
101             }
102             // We have a fallback session reference...
103
// Let's see whether it is appropriate to return it.
104
if (allowCreate) {
105                 boolean canAskForExistingSession = false;
106                 try {
107                     this.session = this.request.getPortletSession(false);
108                     canAskForExistingSession = true;
109                 }
110                 catch (IllegalStateException JavaDoc ex2) {
111                 }
112                 if (canAskForExistingSession) {
113                     // Could ask for existing session, hence the IllegalStateException
114
// came from trying to create a new session too late -> rethrow.
115
throw ex;
116                 }
117             }
118             // Else: Could not even ask for existing session, hence we assume that
119
// the request has been completed and the session is accessed later on
120
// (for example, in a custom child thread).
121
return this.session;
122         }
123     }
124
125
126     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
127         if (scope == SCOPE_REQUEST) {
128             return this.request.getAttribute(name);
129         }
130         else {
131             PortletSession session = getSession(false);
132             if (session != null) {
133                 if (scope == SCOPE_GLOBAL_SESSION) {
134                     Object JavaDoc value = session.getAttribute(name, PortletSession.APPLICATION_SCOPE);
135                     if (value != null) {
136                         this.globalSessionAttributesToUpdate.put(name, value);
137                     }
138                     return value;
139                 }
140                 else {
141                     Object JavaDoc value = session.getAttribute(name);
142                     if (value != null) {
143                         this.sessionAttributesToUpdate.put(name, value);
144                     }
145                     return value;
146                 }
147             }
148             else {
149                 return null;
150             }
151         }
152     }
153
154     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
155         if (scope == SCOPE_REQUEST) {
156             this.request.setAttribute(name, value);
157         }
158         else {
159             PortletSession session = getSession(true);
160             if (scope == SCOPE_GLOBAL_SESSION) {
161                 session.setAttribute(name, value, PortletSession.APPLICATION_SCOPE);
162                 this.globalSessionAttributesToUpdate.remove(name);
163             }
164             else {
165                 session.setAttribute(name, value);
166                 this.sessionAttributesToUpdate.remove(name);
167             }
168         }
169     }
170
171     public void removeAttribute(String JavaDoc name, int scope) {
172         if (scope == SCOPE_REQUEST) {
173             this.request.removeAttribute(name);
174             removeRequestDestructionCallback(name);
175         }
176         else {
177             PortletSession session = getSession(false);
178             if (session != null) {
179                 if (scope == SCOPE_GLOBAL_SESSION) {
180                     session.removeAttribute(name, PortletSession.APPLICATION_SCOPE);
181                     this.globalSessionAttributesToUpdate.remove(name);
182                 }
183                 else {
184                     session.removeAttribute(name);
185                     this.sessionAttributesToUpdate.remove(name);
186                 }
187             }
188         }
189     }
190
191     public void registerDestructionCallback(String JavaDoc name, Runnable JavaDoc callback, int scope) {
192         if (scope == SCOPE_REQUEST) {
193             registerRequestDestructionCallback(name, callback);
194         }
195         else {
196             registerSessionDestructionCallback(name, callback);
197         }
198     }
199
200     public String JavaDoc getSessionId() {
201         return getSession(true).getId();
202     }
203
204     public Object JavaDoc getSessionMutex() {
205         return PortletUtils.getSessionMutex(getSession(true));
206     }
207
208
209     /**
210      * Update all accessed session attributes through <code>session.setAttribute</code>
211      * calls, explicitly indicating to the container that they might have been modified.
212      */

213     protected void updateAccessedSessionAttributes() {
214         PortletSession session = this.request.getPortletSession(false);
215         if (session != null) {
216             for (Iterator JavaDoc it = this.sessionAttributesToUpdate.entrySet().iterator(); it.hasNext();) {
217                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
218                 String JavaDoc name = (String JavaDoc) entry.getKey();
219                 Object JavaDoc newValue = entry.getValue();
220                 Object JavaDoc oldValue = session.getAttribute(name);
221                 if (oldValue == newValue) {
222                     session.setAttribute(name, newValue);
223                 }
224             }
225             for (Iterator JavaDoc it = this.globalSessionAttributesToUpdate.entrySet().iterator(); it.hasNext();) {
226                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
227                 String JavaDoc name = (String JavaDoc) entry.getKey();
228                 Object JavaDoc newValue = entry.getValue();
229                 Object JavaDoc oldValue = session.getAttribute(name, PortletSession.APPLICATION_SCOPE);
230                 if (oldValue == newValue) {
231                     session.setAttribute(name, newValue, PortletSession.APPLICATION_SCOPE);
232                 }
233             }
234         }
235         this.sessionAttributesToUpdate.clear();
236         this.globalSessionAttributesToUpdate.clear();
237     }
238
239     /**
240      * Register the given callback as to be executed after session termination.
241      * @param name the name of the attribute to register the callback for
242      * @param callback the callback to be executed for destruction
243      */

244     private void registerSessionDestructionCallback(String JavaDoc name, Runnable JavaDoc callback) {
245         if (logger.isWarnEnabled()) {
246             logger.warn("Could not register destruction callback [" + callback + "] for attribute '" + name +
247                     "' for session scope because Portlet API 1.0 does not support session attribute callbacks");
248         }
249     }
250
251 }
252
Popular Tags