SessionCount.java revision 71ef1e179613c2e17b9ad5f1c9db6bf533941f9d
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * The contents of this file are subject to the terms
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * of the Common Development and Distribution License
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * (the License). You may not use this file except in
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * compliance with the License.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * You can obtain a copy of the License at
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * https://opensso.dev.java.net/public/CDDLv1.0.html or
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * See the License for the specific language governing
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * permission and limitations under the License.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * When distributing Covered Code, include this CDDL
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Header Notice in each file and include the License file
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * If applicable, add the following below the CDDL Header,
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * with the fields enclosed by brackets [] replaced by
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * your own identifying information:
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * "Portions Copyrighted [year] [name of copyright owner]"
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * $Id: SessionCount.java,v 1.5 2008/06/25 05:41:31 qcheng Exp $
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Portions Copyrighted 2011-2013 ForgeRock Inc
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncimport com.iplanet.dpro.session.share.SessionRequest;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncimport com.iplanet.dpro.session.share.SessionResponse;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncimport com.sun.identity.common.configuration.SiteConfiguration;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncimport com.sun.identity.session.util.RestrictedTokenContext;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * <code>SessionCount</code> represents the session count for a given user
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * in 3 different mutually exclusive deployment modes from which the user
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * sessions can be obtained.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * <li> SINGLE_SERVER_MODE :
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync *I Simply return the local sessions for the given user.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * <li> MULTI_SERVER_MODE :
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Query other AM servers for the sessions for the given user Add the numbers
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * up (including the local one) and return the value. If any of the AM servers
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * is down, simply ignores it since all the sessions maintained by that instance
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * are not available.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * <li> SFO_MODE : Fetch the sessions for the given user directly from the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * session repository.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncpublic class SessionCount {
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync // SessionInfoMap: uuid -> Set (list of sids)
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync /* Single server mode*/
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync /* Multiserver mode */
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync /* Directly from Session repository*/
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync private static Debug debug = SessionService.sessionDebug;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync private static boolean useLocalSessionsInMultiServerMode = false;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync private static boolean caseSensitiveUUID =
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync SystemProperties.getAsBoolean(Constants.CASE_SENSITIVE_UUID);
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync debug.error("SessionConstraint: Failied to get the "
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync + "SSOTokenManager instance.");
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync || !SiteConfiguration.getSites(getAdminToken()).isEmpty()))) {
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync //If an error occurs fallback to multi server mode
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync // Without this property defined the default will be false which is
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync // backwards compatable.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync SystemProperties.getAsBoolean(Constants.USE_LOCAL_SESSIONS_IN_MULTI_SERVER_MODE);
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync debug.message("SessionCount: useLocalSessionsInMultiServerMode set to " + useLocalSessionsInMultiServerMode);
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync SessionService ss = SessionService.getSessionService();
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync + " Failed to get the session service instance");
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * Returns the expiration information of all sessions belonging to a user
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync * (uuid). The returned value will be a Map (sid->expiration_time).
if (!caseSensitiveUUID) {
switch (deploymentMode) {
case SINGLE_SERVER_MODE:
case MULTI_SERVER_MODE:
if (useLocalSessionsInMultiServerMode()) {
case SFO_MODE:
return sessions;
static boolean useLocalSessionsInMultiServerMode() {
synchronized (sessions) {
return retSessions;
} catch (Exception e) {
return sessions;
return sessions;
.getRepository();
} catch (Exception e) {
return sessions;
Set sessions = (Set) uuidSessionMap.get((caseSensitiveUUID) ? is.getUUID() : is.getUUID().toLowerCase());
return sres;
throw se;
} catch (Exception e) {
throw new SessionException(e);
} catch (Exception e) {
return adminToken;