1 package org.apache.turbine.services.session; 2 3 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one 6 * or more contributor license agreements. See the NOTICE file 7 * distributed with this work for additional information 8 * regarding copyright ownership. The ASF licenses this file 9 * to you under the Apache License, Version 2.0 (the 10 * "License"); you may not use this file except in compliance 11 * with the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, 16 * software distributed under the License is distributed on an 17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 18 * KIND, either express or implied. See the License for the 19 * specific language governing permissions and limitations 20 * under the License. 21 */ 22 23 24 import java.util.Collection; 25 import javax.servlet.http.HttpSession; 26 27 import org.apache.turbine.om.security.User; 28 import org.apache.turbine.services.TurbineServices; 29 30 /** 31 * This is a conveience class provided to allow access to the SessionService 32 * through static methods. The SessionService should ALWAYS be accessed 33 * through this class. 34 * 35 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> 36 * @version $Id: TurbineSession.java 1066925 2011-02-03 19:44:37Z ludwig $ 37 * @see org.apache.turbine.services.session.SessionService 38 */ 39 public abstract class TurbineSession 40 { 41 /** 42 * Gets a list of the active sessions 43 * 44 * @return List of HttpSession objects 45 */ 46 public static Collection<HttpSession> getActiveSessions() 47 { 48 return getService().getActiveSessions(); 49 } 50 51 /** 52 * Adds a session to the current list. This method should only be 53 * called by the listener. 54 * 55 * @param session Session to add 56 */ 57 public static void addSession(HttpSession session) 58 { 59 getService().addSession(session); 60 } 61 62 /** 63 * Removes a session from the current list. This method should only be 64 * called by the listener. 65 * 66 * @param session Session to remove 67 */ 68 public static void removeSession(HttpSession session) 69 { 70 getService().removeSession(session); 71 } 72 73 /** 74 * Determines if a given user is currently logged in. The actual 75 * implementation of the User object must implement the equals() 76 * method. By default, Torque based objects (liek TurbineUser) 77 * have an implementation of equals() that will compare the 78 * result of getPrimaryKey(). 79 * 80 * @param user User to check for 81 * @return true if the user is logged in on one of the 82 * active sessions. 83 */ 84 public static boolean isUserLoggedIn(User user) 85 { 86 return getService().isUserLoggedIn(user); 87 } 88 89 /** 90 * Gets a collection of all user objects representing the users currently 91 * logged in. This will exclude any instances of anonymous user that 92 * Turbine will use before the user actually logs on. 93 * 94 * @return collection of org.apache.turbine.om.security.User objects 95 */ 96 public static Collection<User> getActiveUsers() 97 { 98 return getService().getActiveUsers(); 99 } 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 }