001/*
002 * Copyright 2002-2017 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.messaging.simp;
018
019import org.springframework.beans.factory.ObjectFactory;
020import org.springframework.beans.factory.config.Scope;
021
022/**
023 * A {@link Scope} implementation exposing the attributes of a SiMP session
024 * (e.g. WebSocket session).
025 *
026 * <p>Relies on a thread-bound {@link SimpAttributes} instance exported by
027 * {@link org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler}.
028 *
029 * @author Rossen Stoyanchev
030 * @since 4.1
031 */
032public class SimpSessionScope implements Scope {
033
034        @Override
035        public Object get(String name, ObjectFactory<?> objectFactory) {
036                SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
037                Object scopedObject = simpAttributes.getAttribute(name);
038                if (scopedObject != null) {
039                        return scopedObject;
040                }
041                synchronized (simpAttributes.getSessionMutex()) {
042                        scopedObject = simpAttributes.getAttribute(name);
043                        if (scopedObject == null) {
044                                scopedObject = objectFactory.getObject();
045                                simpAttributes.setAttribute(name, scopedObject);
046                        }
047                        return scopedObject;
048                }
049        }
050
051        @Override
052        public Object remove(String name) {
053                SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
054                synchronized (simpAttributes.getSessionMutex()) {
055                        Object value = simpAttributes.getAttribute(name);
056                        if (value != null) {
057                                simpAttributes.removeAttribute(name);
058                                return value;
059                        }
060                        else {
061                                return null;
062                        }
063                }
064        }
065
066        @Override
067        public void registerDestructionCallback(String name, Runnable callback) {
068                SimpAttributesContextHolder.currentAttributes().registerDestructionCallback(name, callback);
069        }
070
071        @Override
072        public Object resolveContextualObject(String key) {
073                return null;
074        }
075
076        @Override
077        public String getConversationId() {
078                return SimpAttributesContextHolder.currentAttributes().getSessionId();
079        }
080
081}