Source code: com/RuntimeCollective/webapps/tag/UpdateSessionTag.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/UpdateSessionTag.java,v 1.13 2003/09/30 15:13:19 joe Exp $
2 * $Revision: 1.13 $
3 * $Date: 2003/09/30 15:13:19 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.tag;
31
32 import com.RuntimeCollective.webapps.bean.Session;
33 import com.RuntimeCollective.webapps.bean.User;
34 import com.RuntimeCollective.webapps.bean.TrackedUser;
35 import com.RuntimeCollective.webapps.RuntimeParameters;
36
37 import java.util.Date;
38
39 import javax.servlet.http.HttpSession;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.jsp.JspException;
42 import javax.servlet.jsp.JspWriter;
43 import javax.servlet.jsp.PageContext;
44 import javax.servlet.jsp.tagext.TagSupport;
45 import org.apache.struts.action.Action;
46 import org.apache.struts.action.ActionError;
47 import org.apache.struts.action.ActionErrors;
48
49
50 /**
51 * Update the webapps Session of the logged on User, if there are any.
52 * <p>
53 * The only thing we actually update is the LastUsedDate of the Session.
54 * <br>
55 * If there are no User or Session, don't do anything.
56 * <p>
57 * <b>Important:</b> This tag must be placed on a jsp page before anything is written to the response.
58 * It's safest to always put it at the top.
59 *
60 * @version $Id: UpdateSessionTag.java,v 1.13 2003/09/30 15:13:19 joe Exp $
61 */
62
63 public class UpdateSessionTag extends TagSupport {
64
65 /** The key of the session-scope bean we look for. */
66 protected static String userKey = RuntimeParameters.get("logonUserKey");
67
68 /** The key of the session-scope bean we look for. */
69 protected static String sessionKey = Session.SESSION_KEY;
70
71 /** Defer our checking until the end of this tag is encountered.
72 * @exception JspException if a JSP exception has occurred
73 */
74 public int doStartTag() throws JspException {
75 return (SKIP_BODY);
76 }
77
78 /**
79 * Look for a User, and then for its Session. Update and save the session if there is one.
80 * @exception JspException if a JSP exception has occurred
81 */
82 public int doEndTag() throws JspException {
83
84 // Look for User
85 HttpSession httpSession = pageContext.getSession();
86
87 if (httpSession != null) {
88
89 User user = (User) httpSession.getAttribute(userKey);
90 if (user != null) {
91 user = (User) RuntimeParameters.getStore().get(User.class.getName(), user.getId());
92
93 // Check that the user is not archived, if it is a TrackedUser
94 if ((user instanceof TrackedUser) && (((TrackedUser) user).isArchived())) {
95 // de-authorise the user
96 httpSession.setAttribute(userKey, null);
97 httpSession.setAttribute(sessionKey, null);
98
99 } else {
100 // Look for Session
101 Session session = (Session) httpSession.getAttribute(sessionKey);
102 if ((session != null) && (session.getTheUser().getId() == user.getId())) {
103
104 try {
105 session = (Session) RuntimeParameters.getStore().get(Session.class.getName(), session.getId());
106
107 // Update and save the Session, if it is still active, ie not timed out
108 if (!session.hasTimedOut()) {
109 RuntimeParameters.logDebug(this, UPDATED_MESSAGE);
110 session.setLastUsedDate(new Date());
111 try {
112 if (doingQuickUpdate()) {
113 session.doQuickUpdate();
114 } else {
115 RuntimeParameters.getStore().save(session);
116 }
117 } catch (RuntimeException e) {
118 // the session may not be in the db anymore (eg if you have just reloaded the db)
119 // get rid of it
120 RuntimeParameters.log(this, "Throwing away an old Session which cannot be saved.");
121 httpSession.setAttribute(sessionKey, null);
122 }
123 }
124 } catch (RuntimeException e) {
125 // the Session may not be reloadable from the EBS,
126 // if eg the datamodel was dropped and reloaded
127 RuntimeParameters.logWarn(this, "Could not reload Session object.");
128
129 // remove the Session from the session
130 httpSession.removeAttribute(sessionKey);
131 }
132 }
133 }
134 }
135 }
136
137 return (EVAL_PAGE);
138 }
139
140 /** Whether or not the "doQuickUpdateSession" param has been set to "true". */
141 protected static boolean doingQuickUpdate() {
142 if (!quickUpdateRead) {
143 quickUpdateResult = "true".equals(RuntimeParameters.getParam("doQuickUpdateSession"));
144 quickUpdateRead = true;
145 }
146
147 return quickUpdateResult;
148 }
149
150 protected static boolean quickUpdateRead = false;
151 protected static boolean quickUpdateResult = false;
152 protected static String UPDATED_MESSAGE = "User Session updated and saved.";
153 }
154