1 24 package org.riotfamily.riot.security.session; 25 26 import java.io.Serializable ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 30 import javax.servlet.ServletContext ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpSession ; 33 import javax.servlet.http.HttpSessionBindingEvent ; 34 import javax.servlet.http.HttpSessionBindingListener ; 35 36 import org.riotfamily.riot.security.auth.RiotUser; 37 38 49 public class UserHolder implements Serializable , HttpSessionBindingListener { 50 51 private static final String SESSION_KEY = UserHolder.class.getName(); 52 53 private static ArrayList users = new ArrayList (); 54 55 private RiotUser user; 56 57 private SessionMetaData metaData; 58 59 60 public UserHolder(RiotUser user, SessionMetaData metaData) { 61 this.user = user; 62 this.metaData = metaData; 63 } 64 65 public RiotUser getUser() { 66 return this.user; 67 } 68 69 public void setUser(RiotUser user) { 70 this.user = user; 71 } 72 73 public SessionMetaData getSessionMetaData() { 74 return this.metaData; 75 } 76 77 public void valueBound(HttpSessionBindingEvent event) { 78 users.add(this); 79 } 80 81 public void valueUnbound(HttpSessionBindingEvent event) { 82 users.remove(user.getUserId()); 83 metaData.sessionEnded(); 84 ServletContext sc = event.getSession().getServletContext(); 85 LoginManager.getInstance(sc).storeSessionMetaData(metaData); 86 } 87 88 static void updateUser(String userId, RiotUser user) { 89 Iterator it = users.iterator(); 90 while (it.hasNext()) { 91 UserHolder holder = (UserHolder) it.next(); 92 if (holder.getUser() != null 93 && userId.equals(holder.getUser().getUserId())) { 94 95 holder.setUser(user); 96 } 97 } 98 } 99 100 void storeInSession(HttpSession session) { 101 session.setAttribute(SESSION_KEY, this); 102 } 103 104 static void removeFromSession(HttpServletRequest request) { 105 HttpSession session = request.getSession(false); 106 if (session != null) { 107 session.removeAttribute(SESSION_KEY); 108 } 109 } 110 111 static UserHolder getInstance(HttpServletRequest request) { 112 HttpSession session = request.getSession(false); 113 if (session == null) { 114 return null; 115 } 116 return (UserHolder) session.getAttribute(SESSION_KEY); 117 } 118 119 } 120 | Popular Tags |