Help

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

This is the solution I came up with to log user requests:


@Startup
@Scope(ScopeType.APPLICATION)
@Name("mypackage.loggingFilter")
@BypassInterceptors
@Filter
public class LoggingFilter extends AbstractFilter
{

    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
	    throws IOException, ServletException
    {
	if (HttpServletRequest.class.isAssignableFrom(arg0.getClass()))
	{
	    HttpServletRequest r = (HttpServletRequest) arg0;
	    try
	    {
		Lifecycle.beginCall();
		HttpSession ses = r.getSession(false);
		if (ses != null)
		{

		    UserLog log = (UserLog) ses.getAttribute("userLog");
		    if (log != null)
		    {
			UserTransaction transaction = (UserTransaction) Component
				.getInstance("org.jboss.seam.transaction.transaction");
			try
			{

			    transaction.begin();
			    EntityManager e = (EntityManager) Component
				    .getInstance("entityManager");
			    e.joinTransaction();
			    log.setLastRequest(new Date());
			    e.merge(log);
			    e.flush();
			} finally
			{
			    transaction.commit();
			}
		    }
		}
	    } catch (Exception e)
	    {
		System.out.println("COULD NOT LOG USER ACTIVITY " + e.getMessage());
	    } finally
	    {
		Lifecycle.endCall();
	    }
	}
	arg2.doFilter(arg0, arg1);
    }

}

In this simple case all it is updating the last request time for a session scoped UserLog entity. A few things to note:

  • Logging takes place in a separate transaction
  • The session and conversation context will not be available, session scoped components can be accessed using the servlet context as seen here