KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > request > SessionScope


1 /*
2  * Copyright 2002-2007 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.context.request;
18
19 import org.springframework.beans.factory.ObjectFactory;
20
21 /**
22  * Session-backed {@link org.springframework.beans.factory.config.Scope}
23  * implementation.
24  *
25  * <p>Relies on a thread-bound {@link RequestAttributes} instance, which
26  * can be exported through {@link RequestContextListener},
27  * {@link org.springframework.web.filter.RequestContextFilter} or
28  * {@link org.springframework.web.servlet.DispatcherServlet}.
29  *
30  * <p>This <code>Scope</code> will also work for Portlet environments,
31  * through an alternate <code>RequestAttributes</code> implementation
32  * (as exposed out-of-the-box by Spring's
33  * {@link org.springframework.web.portlet.DispatcherPortlet}.
34  *
35  * @author Rod Johnson
36  * @author Juergen Hoeller
37  * @author Rob Harrop
38  * @since 2.0
39  * @see RequestContextHolder#currentRequestAttributes()
40  * @see RequestAttributes#SCOPE_SESSION
41  * @see RequestAttributes#SCOPE_GLOBAL_SESSION
42  * @see RequestContextListener
43  * @see org.springframework.web.filter.RequestContextFilter
44  * @see org.springframework.web.servlet.DispatcherServlet
45  * @see org.springframework.web.portlet.DispatcherPortlet
46  */

47 public class SessionScope extends AbstractRequestAttributesScope {
48
49     private final int scope;
50
51
52     /**
53      * Create a new SessionScope, storing attributes in a locally
54      * isolated session (or default session, if there is no distinction
55      * between a global session and a component-specific session).
56      */

57     public SessionScope() {
58         this.scope = RequestAttributes.SCOPE_SESSION;
59     }
60
61     /**
62      * Create a new SessionScope, specifying whether to store attributes
63      * in the global session, provided that such a distinction is available.
64      * <p>This distinction is important for Portlet environments, where there
65      * are two notions of a session: "portlet scope" and "application scope".
66      * If this flag is on, objects will be put into the "application scope" session;
67      * else they will end up in the "portlet scope" session (the typical default).
68      * <p>In a Servlet environment, this flag is effectively ignored.
69      * @param globalSession <code>true</code> in case of the global session as target;
70      * <code>false</code> in case of a component-specific session as target
71      * @see org.springframework.web.portlet.context.PortletRequestAttributes
72      * @see org.springframework.web.context.request.ServletRequestAttributes
73      */

74     public SessionScope(boolean globalSession) {
75         this.scope = (globalSession ? RequestAttributes.SCOPE_GLOBAL_SESSION : RequestAttributes.SCOPE_SESSION);
76     }
77
78
79     protected int getScope() {
80         return this.scope;
81     }
82
83     public String JavaDoc getConversationId() {
84         return RequestContextHolder.currentRequestAttributes().getSessionId();
85     }
86
87     public Object JavaDoc get(String JavaDoc name, ObjectFactory objectFactory) {
88         Object JavaDoc mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
89         synchronized (mutex) {
90             return super.get(name, objectFactory);
91         }
92     }
93
94     public Object JavaDoc remove(String JavaDoc name) {
95         Object JavaDoc mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
96         synchronized (mutex) {
97             return super.remove(name);
98         }
99     }
100
101 }
102
Popular Tags