    001    package 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
    024    import java.util.Collection;
    025    import javax.servlet.http.HttpSession;
    026
    027    import org.apache.turbine.om.security.User;
    028    import org.apache.turbine.services.TurbineServices;
    029
    030    /**
    031     * This is a conveience class provided to allow access to the SessionService
    032     * through static methods.  The SessionService should ALWAYS be accessed
    033     * through this class.
    034     *
    035     * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    036     * @version $Id: TurbineSession.java 1066925 2011-02-03 19:44:37Z ludwig $
    037     * @see org.apache.turbine.services.session.SessionService
    038     */
    039    public abstract class TurbineSession
    040    {
    041        /**
    042         * Gets a list of the active sessions
    043         *
    044         * @return List of HttpSession objects
    045         */
    046        public static Collection<HttpSession> getActiveSessions()
    047        {
    048            return getService().getActiveSessions();
    049        }
    050
    051        /**
    052         * Adds a session to the current list.  This method should only be
    053         * called by the listener.
    054         *
    055         * @param session Session to add
    056         */
    057        public static void addSession(HttpSession session)
    058        {
    059            getService().addSession(session);
    060        }
    061
    062        /**
    063         * Removes a session from the current list.  This method should only be
    064         * called by the listener.
    065         *
    066         * @param session Session to remove
    067         */
    068        public static void removeSession(HttpSession session)
    069        {
    070            getService().removeSession(session);
    071        }
    072
    073        /**
    074         * Determines if a given user is currently logged in.  The actual
    075         * implementation of the User object must implement the equals()
    076         * method.  By default, Torque based objects (liek TurbineUser)
    077         * have an implementation of equals() that will compare the
    078         * result of getPrimaryKey().
    079         *
    080         * @param user User to check for
    081         * @return true if the user is logged in on one of the
    082         * active sessions.
    083         */
    084        public static boolean isUserLoggedIn(User user)
    085        {
    086            return getService().isUserLoggedIn(user);
    087        }
    088
    089        /**
    090         * Gets a collection of all user objects representing the users currently
    091         * logged in.  This will exclude any instances of anonymous user that
    092         * Turbine will use before the user actually logs on.
    093         *
    094         * @return collection of org.apache.turbine.om.security.User objects
    095         */
    096        public static Collection<User> getActiveUsers()
    097        {
    098            return getService().getActiveUsers();
    099        }
    100
    101        /**
    102         * Utility method for accessing the service
    103         * implementation
    104         *
    105         * @return a IntakeService implementation instance
    106         */
    107        private static SessionService getService()
    108        {
    109            return (SessionService) TurbineServices
    110                    .getInstance().getService(SessionService.SERVICE_NAME);
    111        }
    112
    113        /**
    114         * Gets the User object of the the specified HttpSession.
    115         *
    116         * @param session
    117         * @return
    118         */
    119        public static User getUserFromSession(HttpSession session)
    120        {
    121            return getService().getUserFromSession(session);
    122        }
    123
    124        /**
    125         * Gets the HttpSession by the session identifier
    126         *
    127         * @param sessionId
    128         * @return
    129         */
    130        public static HttpSession getSession(String sessionId)
    131        {
    132            return getService().getSession(sessionId);
    133        }
    134
    135        /**
    136         * Get a collection of all session on which the given user
    137         * is logged in.
    138         *
    139         * @param user the user
    140         * @return Collection of HtttSession objects
    141         */
    142        public static Collection<HttpSession> getSessionsForUser(User user)
    143        {
    144            return getService().getSessionsForUser(user);
    145        }
    146    }