001/* 002 * Copyright 2002-2015 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.user; 018 019import java.util.Collections; 020import java.util.Set; 021import java.util.concurrent.ConcurrentHashMap; 022import java.util.concurrent.ConcurrentMap; 023import java.util.concurrent.CopyOnWriteArraySet; 024 025import org.springframework.util.Assert; 026 027/** 028 * A default thread-safe implementation of {@link UserSessionRegistry}. 029 * 030 * @author Rossen Stoyanchev 031 * @since 4.0 032 * @deprecated as of 4.2 this class is no longer used, see deprecation notes 033 * on {@link UserSessionRegistry} for more details. 034 */ 035@Deprecated 036@SuppressWarnings({"deprecation", "unused"}) 037public class DefaultUserSessionRegistry implements UserSessionRegistry { 038 039 // userId -> sessionId 040 private final ConcurrentMap<String, Set<String>> userSessionIds = new ConcurrentHashMap<String, Set<String>>(); 041 042 private final Object lock = new Object(); 043 044 045 @Override 046 public Set<String> getSessionIds(String user) { 047 Set<String> set = this.userSessionIds.get(user); 048 return (set != null ? set : Collections.<String>emptySet()); 049 } 050 051 @Override 052 public void registerSessionId(String user, String sessionId) { 053 Assert.notNull(user, "User must not be null"); 054 Assert.notNull(sessionId, "Session ID must not be null"); 055 synchronized (this.lock) { 056 Set<String> set = this.userSessionIds.get(user); 057 if (set == null) { 058 set = new CopyOnWriteArraySet<String>(); 059 this.userSessionIds.put(user, set); 060 } 061 set.add(sessionId); 062 } 063 } 064 065 @Override 066 public void unregisterSessionId(String userName, String sessionId) { 067 Assert.notNull(userName, "User Name must not be null"); 068 Assert.notNull(sessionId, "Session ID must not be null"); 069 synchronized (lock) { 070 Set<String> set = this.userSessionIds.get(userName); 071 if (set != null) { 072 if (set.remove(sessionId) && set.isEmpty()) { 073 this.userSessionIds.remove(userName); 074 } 075 } 076 } 077 } 078 079}