Author: sohil.shah(a)jboss.com
Date: 2008-08-25 21:29:43 -0400 (Mon, 25 Aug 2008)
New Revision: 11735
Modified:
branches/JBoss_Portal_2_6_1_JBPORTAL-2128/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java
Log:
applying the fix
Modified:
branches/JBoss_Portal_2_6_1_JBPORTAL-2128/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java
===================================================================
---
branches/JBoss_Portal_2_6_1_JBPORTAL-2128/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java 2008-08-25
15:48:15 UTC (rev 11734)
+++
branches/JBoss_Portal_2_6_1_JBPORTAL-2128/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java 2008-08-26
01:29:43 UTC (rev 11735)
@@ -22,10 +22,18 @@
******************************************************************************/
package org.jboss.portal.core.aspects.server;
+import java.security.Principal;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.servlet.http.HttpServletRequest;
+
import org.jboss.logging.Logger;
import org.jboss.portal.common.invocation.AttributeResolver;
import org.jboss.portal.common.invocation.InvocationException;
-import org.jboss.portal.core.CoreConstants;
import org.jboss.portal.identity.CachedUserImpl;
import org.jboss.portal.identity.NoSuchUserException;
import org.jboss.portal.identity.User;
@@ -34,20 +42,6 @@
import org.jboss.portal.server.ServerInterceptor;
import org.jboss.portal.server.ServerInvocation;
-import javax.naming.InitialContext;
-import javax.naming.NameNotFoundException;
-import javax.naming.NamingException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
-import javax.servlet.http.HttpSessionBindingEvent;
-import javax.servlet.http.HttpSessionBindingListener;
-import javax.transaction.UserTransaction;
-import javax.transaction.Transaction;
-import java.io.Serializable;
-import java.security.Principal;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
/**
* The interceptor is responsible for managing the user identity lifecycle based on the
principal name returned by the
@@ -122,7 +116,6 @@
{
boolean trace = log.isTraceEnabled();
HttpServletRequest req = invocation.getServerContext().getClientRequest();
- HttpSession httpSession = req.getSession();
// Get scope
AttributeResolver principalScopeResolver =
invocation.getContext().getAttributeResolver(ServerInvocation.PRINCIPAL_SCOPE);
@@ -155,6 +148,9 @@
{
// Fetch user info
user = getUserModule().findUserByUserName(userName);
+
+ //Set Last login date
+ getUserProfileModule().setProperty(user, User.INFO_USER_LAST_LOGIN_DATE,
"" + new Date());
// Get profile
profile = getUserProfileModule().getProperties(user);
@@ -192,54 +188,6 @@
}
}
- //
- if (user == null)
- {
- // Erase any previous user finalizer, the side effect of unbinding the session
attribute will finalize the previous user
- UserFinalizer finalizer =
(UserFinalizer)httpSession.getAttribute(CoreConstants.Servlet.Session.USER_FINALIZER);
- if (finalizer != null)
- {
- if (trace)
- {
- log.trace("Removing finalizer for user=" + finalizer.userId);
- }
- httpSession.setAttribute(CoreConstants.Servlet.Session.USER_FINALIZER,
null);
- }
- }
- else
- {
- // Get the current finalizer if any
- UserFinalizer finalizer =
(UserFinalizer)httpSession.getAttribute(CoreConstants.Servlet.Session.USER_FINALIZER);
-
- // Get the user id from the request user
- if (finalizer == null)
- {
- if (trace)
- {
- log.trace("Adding finalizer for user=" + user.getUserName());
- }
-
- // Set the user finalizer
- finalizer = new UserFinalizer(user.getId());
-
- // Bind the new one, the side effect finalizes the previous user
- httpSession.setAttribute(CoreConstants.Servlet.Session.USER_FINALIZER,
finalizer);
- }
- else if (!finalizer.userId.equals(user.getId()))
- {
- if (trace)
- {
- log.trace("Replacing the finalizer for user=" + finalizer.userId
+ " with user=" + user.getId());
- }
-
- // Set the user finalizer
- finalizer = new UserFinalizer(user.getId());
-
- // Bind the new one, the side effect finalizes the previous user
- httpSession.setAttribute(CoreConstants.Servlet.Session.USER_FINALIZER,
finalizer);
- }
- }
-
try
{
// Continue the invocation
@@ -264,119 +212,5 @@
public void setCacheUser(boolean cacheUser)
{
this.cacheUser = cacheUser;
- }
-
- /** Implement the last login date feature. */
- private static class UserFinalizer implements Serializable,
HttpSessionBindingListener
- {
-
- /** The serialVersionUID */
- private static final long serialVersionUID = -8534276945533635226L;
-
- private static final Logger log = Logger.getLogger(UserFinalizer.class);
-
- private final Object userId;
-
- public UserFinalizer(Object userId)
- {
- if (userId == null)
- {
- throw new IllegalArgumentException("No user id present");
- }
- this.userId = userId;
- }
-
- public void valueBound(HttpSessionBindingEvent event)
- {
- }
-
- public void valueUnbound(HttpSessionBindingEvent event)
- {
- try
- {
- finalizeUser();
- }
- catch (Exception e)
- {
- log.warn("Cannot set last login date for user with id '" +
userId + "' : " + e.getMessage());
- log.debug("Failed to set user last login date (may happen during server
shutdown): ", e);
- }
- }
-
- protected void finalizeUser() throws Exception
- {
- /**
- * Note: Due to a possible bug in JBoss Cache Transaction propagation within a
cluster,
- * We need to spawn a separate thread with its own new transaction to perform
the finalizeUser
- * logic.
- *
- * Without that, it results in data deadlocks in the cluster.
- */
- Thread thread = new Thread(new FinalizeRunner());
- thread.start();
- thread.join();
- }
-
- /**
- *
- * @author soshah
- *
- */
- private class FinalizeRunner implements Runnable
- {
- public void run()
- {
- UserTransaction tx = null;
- try
- {
- //
- if (log.isTraceEnabled())
- {
- log.trace("Finalizing user " + userId);
- }
-
- //
- // Here we use JNDI to locate the module as this finalizer could have
been
- // migrated in the session of another node of the cluster
- UserModule userModule = null;
- UserProfileModule userProfileModule = null;
- InitialContext ctx = new InitialContext();
- tx = (UserTransaction)ctx.lookup("UserTransaction");
- tx.begin();
- try
- {
- userModule = (UserModule)new
InitialContext().lookup("java:portal/UserModule");
- userProfileModule = (UserProfileModule)new
InitialContext().lookup("java:portal/UserProfileModule");
- }
- catch (NameNotFoundException ignore)
- {
- // Name is not bound anymore, it could happen during a shutdown, we
don't do anything
- }
-
- // Get user and set last visit date to now if we can
- if (userModule != null && userProfileModule != null)
- {
- try
- {
- User user = userModule.findUserById(userId);
- //user.setLastVisitDate(new Date());
- userProfileModule.setProperty(user, User.INFO_USER_LAST_LOGIN_DATE,
new Date().toString());
-
- }
- catch (NoSuchUserException e)
- {
- // User is not found
- log.warn("Trying to finalize non existing user " +
userId);
- }
- }
- tx.commit();
- }
- catch(Exception e)
- {
- try{tx.rollback();}catch(Exception rbe){}
- log.debug(this, e);
- }
- }
- }
- }
+ }
}
Show replies by date