com.sun.faces.push.WebsocketUserManager Maven / Gradle / Ivy
Show all versions of javax.faces Show documentation
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2016 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.faces.push;
import static com.sun.faces.cdi.CdiUtils.getBeanInstance;
import static java.util.Collections.emptySet;
import static java.util.Collections.synchronizedSet;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.push.Push;
/**
*
* This web socket user manager holds all web socket users registered by <f:websocket>
.
*
* @author Bauke Scholtz
* @see Push
* @since 2.3
*/
@ApplicationScoped
public class WebsocketUserManager {
// Constants ------------------------------------------------------------------------------------------------------
private static final int ESTIMATED_USER_CHANNELS_PER_APPLICATION = 1;
private static final int ESTIMATED_USER_CHANNELS_PER_SESSION = 1;
private static final int ESTIMATED_SESSIONS_PER_USER = 2;
private static final int ESTIMATED_CHANNELS_IDS_PER_USER = ESTIMATED_SESSIONS_PER_USER * ESTIMATED_USER_CHANNELS_PER_APPLICATION * ESTIMATED_USER_CHANNELS_PER_SESSION;
// Properties -----------------------------------------------------------------------------------------------------
private final ConcurrentMap>> userChannels = new ConcurrentHashMap<>();
private final ConcurrentMap> applicationUsers = new ConcurrentHashMap<>();
// Actions --------------------------------------------------------------------------------------------------------
/**
* Register application user based on given user and session based user ID.
* @param user The user.
* @param userId The session based user ID.
*/
protected void register(Serializable user, String userId) {
synchronized (applicationUsers) {
if (!applicationUsers.containsKey(user)) {
applicationUsers.putIfAbsent(user, synchronizedSet(new HashSet(ESTIMATED_SESSIONS_PER_USER)));
}
applicationUsers.get(user).add(userId);
}
}
/**
* Add user channel ID associated with given session based user ID and channel name.
* @param userId The session based user ID.
* @param channel The channel name.
* @param channelId The channel identifier.
*/
protected void addChannelId(String userId, String channel, String channelId) {
if (!userChannels.containsKey(userId)) {
userChannels.putIfAbsent(userId, new ConcurrentHashMap>(ESTIMATED_USER_CHANNELS_PER_APPLICATION));
}
ConcurrentMap> channelIds = userChannels.get(userId);
if (!channelIds.containsKey(channel)) {
channelIds.putIfAbsent(channel, synchronizedSet(new HashSet(ESTIMATED_USER_CHANNELS_PER_SESSION)));
}
channelIds.get(channel).add(channelId);
}
/**
* Resolve the user associated with given channel name and ID.
* @param channel The channel name.
* @param channelId The channel identifier.
* @return The user associated with given channel name and ID.
*/
protected Serializable getUser(String channel, String channelId) {
for (Entry> applicationUser : applicationUsers.entrySet()) {
for (String userId : applicationUser.getValue()) { // "Normally" this contains only 1 entry, so it isn't that inefficient as it looks like.
if (getApplicationUserChannelIds(userId, channel).contains(channelId)) {
return applicationUser.getKey();
}
}
}
return null;
}
/**
* Resolve the user-specific channel IDs associated with given user and channel name.
* @param user The user.
* @param channel The channel name.
* @return The user-specific channel IDs associated with given user and channel name.
*/
protected Set getChannelIds(Serializable user, String channel) {
Set channelIds = new HashSet<>(ESTIMATED_CHANNELS_IDS_PER_USER);
Set userIds = applicationUsers.get(user);
if (userIds != null) {
for (String userId : userIds) {
channelIds.addAll(getApplicationUserChannelIds(userId, channel));
}
}
return channelIds;
}
/**
* Deregister application user associated with given user and session based user ID.
* @param user The user.
* @param userId The session based user ID.
*/
protected void deregister(Serializable user, String userId) {
userChannels.remove(userId);
synchronized (applicationUsers) {
Set userIds = applicationUsers.get(user);
userIds.remove(userId);
if (userIds.isEmpty()) {
applicationUsers.remove(user);
}
}
}
// Internal (static because package private methods in CDI beans are subject to memory leaks) ---------------------
/**
* For internal usage only. This makes it possible to save and restore user specific channels during server
* restart/failover in {@link WebsocketChannelManager}.
*/
static ConcurrentMap>> getUserChannels() {
return getBeanInstance(WebsocketUserManager.class, true).userChannels;
}
// Helpers --------------------------------------------------------------------------------------------------------
private Set getApplicationUserChannelIds(String userId, String channel) {
Map> channels = userChannels.get(userId);
if (channels != null) {
Set channelIds = channels.get(channel);
if (channelIds != null) {
return channelIds;
}
}
return emptySet();
}
}