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.pull.ApplicationTool;
030
031/**
032 * A pull tool for accessing the SessionService from a velocity template.
033 *
034 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
035 * @version $Id: SessionTool.java 1706239 2015-10-01 13:18:35Z tv $
036 */
037public class SessionTool
038        implements ApplicationTool
039{
040    @Override
041    public void init(Object o)
042    {
043        // empty
044    }
045
046    @Override
047    public void refresh()
048    {
049        // empty
050    }
051
052    /**
053     * Gets a list of the active sessions
054     *
055     * @return List of HttpSession objects
056     */
057    public Collection<HttpSession> getActiveSessions()
058    {
059        return TurbineSession.getActiveSessions();
060    }
061
062    /**
063     * Adds a session to the current list.  This method should only be
064     * called by the listener.
065     *
066     * @param session Session to add
067     */
068    public void addSession(HttpSession session)
069    {
070        TurbineSession.addSession(session);
071    }
072
073    /**
074     * Removes a session from the current list.  This method should only be
075     * called by the listener.
076     *
077     * @param session Session to remove
078     */
079    public void removeSession(HttpSession session)
080    {
081        TurbineSession.removeSession(session);
082    }
083
084    /**
085     * Determines if a given user is currently logged in.  The actual
086     * implementation of the User object must implement the equals()
087     * method.  By default, Torque based objects (liek TurbineUser)
088     * have an implementation of equals() that will compare the
089     * result of getPrimaryKey().
090     *
091     * @param user User to check for
092     * @return true if the user is logged in on one of the
093     * active sessions.
094     */
095    public boolean isUserLoggedIn(User user)
096    {
097        return TurbineSession.isUserLoggedIn(user);
098    }
099
100    /**
101     * Gets a collection of all user objects representing the users currently
102     * logged in.  This will exclude any instances of anonymous user that
103     * Turbine will use before the user actually logs on.
104     *
105     * @return collection of org.apache.turbine.om.security.User objects
106     */
107    public Collection<User> getActiveUsers()
108    {
109        return TurbineSession.getActiveUsers();
110    }
111
112    /**
113     * Gets the User object of the the specified HttpSession.
114     *
115     * @param session
116     * @return the user from the session
117     */
118    public User getUserFromSession(HttpSession session)
119    {
120        return TurbineSession.getUserFromSession(session);
121    }
122
123    /**
124     * Get a collection of all session on which the given user
125     * is logged in.
126     *
127     * @param user the user
128     * @return Collection of HtttSession objects
129     */
130    public Collection<HttpSession> getSessionsForUser(User user)
131    {
132        return TurbineSession.getSessionsForUser(user);
133    }
134}