KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > listeners > UserWatcher


1 /*
2  * $$Id: UserWatcher.java,v 1.3 2005/06/07 12:32:34 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on 24.05.2003
27  *
28  */

29 package org.jresearch.gossip.listeners;
30
31 import java.sql.SQLException JavaDoc;
32
33 import javax.servlet.ServletContext JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35 import javax.servlet.http.HttpSessionAttributeListener JavaDoc;
36 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
37
38 import org.apache.struts.Globals;
39 import org.apache.struts.util.MessageResources;
40 import org.jresearch.gossip.IConst;
41 import org.jresearch.gossip.am.PermissionGuardFactory;
42 import org.jresearch.gossip.beans.user.Entry;
43 import org.jresearch.gossip.beans.user.User;
44 import org.jresearch.gossip.configuration.Configurator;
45 import org.jresearch.gossip.constants.UserStatus;
46 import org.jresearch.gossip.dao.ForumDAO;
47 import org.jresearch.gossip.dao.UserDAO;
48 import org.jresearch.gossip.exception.ConfiguratorException;
49 import org.jresearch.gossip.exception.SystemException;
50 import org.jresearch.gossip.log.LogLevel;
51 import org.jresearch.gossip.log.avalon.JGossipLog;
52
53 /**
54  * DOCUMENT ME!
55  *
56  * @author Bel
57  */

58 public class UserWatcher implements HttpSessionAttributeListener JavaDoc {
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
64      */

65     private Entry getCurrentEntry(HttpSessionBindingEvent JavaDoc evt) {
66         User user = (User) evt.getSession().getAttribute(
67                 IConst.SESSION.USER_KEY);
68         Entry entry = new Entry(user.getName(), evt.getSession().getId(), user
69                 .getIp());
70
71         return entry;
72     }
73
74     /**
75      * DOCUMENT ME!
76      *
77      * @param evt
78      * DOCUMENT ME!
79      */

80     public void attributeAdded(HttpSessionBindingEvent JavaDoc evt) {
81         if (evt.getName().equals(IConst.SESSION.USER_KEY)) {
82             HttpSession JavaDoc session = evt.getSession();
83             cleanUpPermissions(session);
84             ServletContext JavaDoc app = session.getServletContext();
85             ForumDAO forumdao = ForumDAO.getInstance();
86             User user = (User) evt.getSession().getAttribute(
87                     IConst.SESSION.USER_KEY);
88
89             try {
90                 savePermissionGuard(session);
91                 if (user.getStatus() > 0) {
92                     forumdao.removeEntry(user.getName());
93                 }
94
95                 forumdao.addEntry(getCurrentEntry(evt));
96
97                 // write message in forum log
98
JGossipLog.audit(LogLevel.INFO, user, ((MessageResources) app
99                         .getAttribute(Globals.MESSAGES_KEY
100                                 + Configurator.getInstance().get(
101                                         IConst.CONFIG.MODULE_PREFIX)))
102                         .getMessage("logs.LOG1"), session);
103             } catch (SQLException JavaDoc sqle) {
104                 sqle.printStackTrace();
105             } catch (SystemException e) {
106                 e.printStackTrace();
107             }
108
109             app.log("UserWatcher: User bean is added with login="
110                     + user.getName());
111         }
112     }
113
114     /*
115      * (non-Javadoc)
116      *
117      * @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
118      */

119     public void attributeRemoved(HttpSessionBindingEvent JavaDoc evt) {
120         if (evt.getName().equals(IConst.SESSION.USER_KEY)) {
121             HttpSession JavaDoc session = evt.getSession();
122             try {
123                 cleanUpPermissions(session);
124             } catch (IllegalStateException JavaDoc e) {
125                 /* ignore this exception */
126             }
127
128             ServletContext JavaDoc app = session.getServletContext();
129
130             User user = (User) evt.getValue();
131
132             UserDAO dao = UserDAO.getInstance();
133             ForumDAO forumdao = ForumDAO.getInstance();
134
135             try {
136                 forumdao.removeEntry(session.getId());
137
138                 if (user.getStatus() != UserStatus.GUEST) {
139                     dao.updateIntime(user.getName());
140                 }
141                 // write message in forum log
142
JGossipLog.audit(LogLevel.INFO, user, ((MessageResources) app
143                         .getAttribute(Globals.MESSAGES_KEY
144                                 + Configurator.getInstance().get(
145                                         IConst.CONFIG.MODULE_PREFIX)))
146                         .getMessage("logs.LOG23"), session);
147             } catch (SQLException JavaDoc sqle) {
148                 sqle.printStackTrace();
149             } catch (ConfiguratorException e) {
150                 e.printStackTrace();
151             }
152
153             app.log("UserWatcher: User bean with login=" + user.getName()
154                     + " is removed");
155         }
156     }
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent)
162      */

163     public void attributeReplaced(HttpSessionBindingEvent JavaDoc evt) {
164         if (evt.getName().equals(IConst.SESSION.USER_KEY)) {
165             HttpSession JavaDoc session = evt.getSession();
166             cleanUpPermissions(session);
167             ServletContext JavaDoc app = session.getServletContext();
168
169             ForumDAO forumdao = ForumDAO.getInstance();
170             User user = (User) evt.getSession().getAttribute(
171                     IConst.SESSION.USER_KEY);
172
173             try {
174
175                 if (user.getStatus() > 0) {
176                     forumdao.removeEntry(user.getName());
177                 }
178
179                 if (!forumdao.isEntryExist(session.getId())) {
180                     forumdao.addEntry(getCurrentEntry(evt));
181                 } else {
182                     forumdao.updateEntry(getCurrentEntry(evt));
183                 }
184                 savePermissionGuard(session);
185             } catch (SQLException JavaDoc sqle) {
186                 sqle.printStackTrace();
187             } catch (SystemException e) {
188                 e.printStackTrace();
189             }
190
191             app.log("UserWatcher: User bean is replaced, new login="
192                     + user.getName());
193         }
194     }
195
196     private void savePermissionGuard(HttpSession JavaDoc session)
197             throws SystemException {
198         User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);
199         if (user.getStatus() == UserStatus.GUEST
200                 || (user.getInfo().getEmail() != null)) {
201             session.setAttribute(IConst.SESSION.PERMISSION_GUARD_KEY,
202                     PermissionGuardFactory.getInstance().createGuard(
203                             user.getName()));
204         } else if (Configurator.getInstance().getBoolean(
205                 IConst.CONFIG.ENABLE_EXT_SIGN_ON)) {
206             session.setAttribute(IConst.SESSION.PERMISSION_GUARD_KEY,
207                     PermissionGuardFactory.getInstance().createGuard(
208                             user.getStatus()));
209         }
210     }
211
212     private void cleanUpPermissions(HttpSession JavaDoc session) {
213         session.removeAttribute(IConst.SESSION.PERMISSION_GUARD_KEY);
214     }
215 }
Popular Tags