001package org.apache.turbine.services.session;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import java.util.Collection;
025
026import javax.servlet.http.HttpSession;
027
028import org.apache.turbine.om.security.User;
029import org.apache.turbine.services.TurbineServices;
030
031/**
032 * This is a conveience class provided to allow access to the SessionService
033 * through static methods.  The SessionService should ALWAYS be accessed
034 * through this class.
035 *
036 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
037 * @version $Id: TurbineSession.java 1706239 2015-10-01 13:18:35Z tv $
038 * @see org.apache.turbine.services.session.SessionService
039 */
040public abstract class TurbineSession
041{
042    /**
043     * Gets a list of the active sessions
044     *
045     * @return List of HttpSession objects
046     */
047    public static Collection<HttpSession> getActiveSessions()
048    {
049        return getService().getActiveSessions();
050    }
051
052    /**
053     * Adds a session to the current list.  This method should only be
054     * called by the listener.
055     *
056     * @param session Session to add
057     */
058    public static void addSession(HttpSession session)
059    {
060        getService().addSession(session);
061    }
062
063    /**
064     * Removes a session from the current list.  This method should only be
065     * called by the listener.
066     *
067     * @param session Session to remove
068     */
069    public static void removeSession(HttpSession session)
070    {
071        getService().removeSession(session);
072    }
073
074    /**
075     * Determines if a given user is currently logged in.  The actual
076     * implementation of the User object must implement the equals()
077     * method.  By default, Torque based objects (liek TurbineUser)
078     * have an implementation of equals() that will compare the
079     * result of getPrimaryKey().
080     *
081     * @param user User to check for
082     * @return true if the user is logged in on one of the
083     * active sessions.
084     */
085    public static boolean isUserLoggedIn(User user)
086    {
087        return getService().isUserLoggedIn(user);
088    }
089
090    /**
091     * Gets a collection of all user objects representing the users currently
092     * logged in.  This will exclude any instances of anonymous user that
093     * Turbine will use before the user actually logs on.
094     *
095     * @return collection of org.apache.turbine.om.security.User objects
096     */
097    public static Collection<User> getActiveUsers()
098    {
099        return getService().getActiveUsers();
100    }
101
102    /**
103     * Utility method for accessing the service
104     * implementation
105     *
106     * @return a IntakeService implementation instance
107     */
108    private static SessionService getService()
109    {
110        return (SessionService) TurbineServices
111                .getInstance().getService(SessionService.SERVICE_NAME);
112    }
113
114    /**
115     * Gets the User object of the the specified HttpSession.
116     *
117     * @param session
118     * @return the user from the session
119     */
120    public static User getUserFromSession(HttpSession session)
121    {
122        return getService().getUserFromSession(session);
123    }
124
125    /**
126     * Gets the HttpSession by the session identifier
127     *
128     * @param sessionId the id of the session
129     * @return the session for the given id
130     */
131    public static HttpSession getSession(String sessionId)
132    {
133        return getService().getSession(sessionId);
134    }
135
136    /**
137     * Get a collection of all session on which the given user
138     * is logged in.
139     *
140     * @param user the user
141     * @return Collection of HtttSession objects
142     */
143    public static Collection<HttpSession> getSessionsForUser(User user)
144    {
145        return getService().getSessionsForUser(user);
146    }
147}