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