001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://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,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.context.request;
018
019import org.springframework.beans.factory.ObjectFactory;
020import org.springframework.lang.Nullable;
021
022/**
023 * Session-backed {@link org.springframework.beans.factory.config.Scope}
024 * implementation.
025 *
026 * <p>Relies on a thread-bound {@link RequestAttributes} instance, which
027 * can be exported through {@link RequestContextListener},
028 * {@link org.springframework.web.filter.RequestContextFilter} or
029 * {@link org.springframework.web.servlet.DispatcherServlet}.
030 *
031 * @author Rod Johnson
032 * @author Juergen Hoeller
033 * @author Rob Harrop
034 * @since 2.0
035 * @see RequestContextHolder#currentRequestAttributes()
036 * @see RequestAttributes#SCOPE_SESSION
037 * @see RequestContextListener
038 * @see org.springframework.web.filter.RequestContextFilter
039 * @see org.springframework.web.servlet.DispatcherServlet
040 */
041public class SessionScope extends AbstractRequestAttributesScope {
042
043        @Override
044        protected int getScope() {
045                return RequestAttributes.SCOPE_SESSION;
046        }
047
048        @Override
049        public String getConversationId() {
050                return RequestContextHolder.currentRequestAttributes().getSessionId();
051        }
052
053        @Override
054        public Object get(String name, ObjectFactory<?> objectFactory) {
055                Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
056                synchronized (mutex) {
057                        return super.get(name, objectFactory);
058                }
059        }
060
061        @Override
062        @Nullable
063        public Object remove(String name) {
064                Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
065                synchronized (mutex) {
066                        return super.remove(name);
067                }
068        }
069
070}