KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > security > session > UserHolder


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.security.session;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpSession JavaDoc;
33 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
34 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
35
36 import org.riotfamily.riot.security.auth.RiotUser;
37
38 /**
39  * Class that holds a reference to a RiotUser. An instance of this class is
40  * stored in the HttpSession. Additionally each instance is placed in a static
41  * list which allows us to access/update all currently logged in users.
42  * <p>
43  * The class also implements the HttpSessionBindingListener interface and
44  * persists the SessionMetaData as soon as the session expires.
45  *
46  * @author Felix Gnass [fgnass at neteye dot de]
47  * @since 6.5
48  */

49 public class UserHolder implements Serializable JavaDoc, HttpSessionBindingListener JavaDoc {
50
51     private static final String JavaDoc SESSION_KEY = UserHolder.class.getName();
52     
53     private static ArrayList JavaDoc users = new ArrayList JavaDoc();
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 JavaDoc event) {
78         users.add(this);
79     }
80     
81     public void valueUnbound(HttpSessionBindingEvent JavaDoc event) {
82         users.remove(user.getUserId());
83         metaData.sessionEnded();
84         ServletContext JavaDoc sc = event.getSession().getServletContext();
85         LoginManager.getInstance(sc).storeSessionMetaData(metaData);
86     }
87     
88     static void updateUser(String JavaDoc userId, RiotUser user) {
89         Iterator JavaDoc 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 JavaDoc session) {
101         session.setAttribute(SESSION_KEY, this);
102     }
103     
104     static void removeFromSession(HttpServletRequest JavaDoc request) {
105         HttpSession JavaDoc session = request.getSession(false);
106         if (session != null) {
107             session.removeAttribute(SESSION_KEY);
108         }
109     }
110     
111     static UserHolder getInstance(HttpServletRequest JavaDoc request) {
112         HttpSession JavaDoc 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