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