cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralph Goers <Ralph.Go...@dslextreme.com>
Subject Re: AW: Session lifecycle listener in cocoon?
Date Sun, 11 Sep 2005 18:24:20 GMT
Here is some example code. It contains a "User" object and a "Resource" 
object. It calles the Resource's release method and the User's logout 
method when the session terminates.  This code uses Cocoon's Session 
object but would work just as well if you use the Container's 
HttpSession object.

import org.apache.cocoon.environment.Session;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.io.Serializable;

public final class SessionData implements HttpSessionBindingListener, 
Serializable
{
    public static final String SESSION_DATA = "SessionData";
    private UserObject user = null;
    private SomResource resource = null;

    public static SessionData getInstance(Session session)
    {
        SessionData sessionData = (SessionData) 
session.getAttribute(SESSION_DATA);

        if (sessionData == null)
        {
            sessionData = new SessionData();
            session.setAttribute(SESSION_DATA, sessionData);
        }
        return sessionData;
    }

    /**
     * Constructor is private requiring instances to be obtained via 
getInstance.
     */
    private SessionData()
    {
    }

    public  SomeResource getResource()
   {
        return this.resource;
   }

   public void setResource(SomeResource resource)
   {
        this.resource = resource;
   }

    public  UserObject getUserObject()
   {
        return this.user;
   }

   public void setUserObject(UserObject user)
   {
        this.user = user;
   }
    /**
     * Notify that this object has been bound to the session
     * @param event the session binding event
     */
    public void valueBound(HttpSessionBindingEvent event)
    {
    }

    /**
     * Notify that this object has been unbound from the session.
     * @param event the session binding event.
     */
    public void valueUnbound(HttpSessionBindingEvent event)
    {
        if (this.resource != null)
        {
             this.resource.release();
        }
        if (this.user != null)
        {
            this.user.logout();
        }
    }
}

Ralph


Stefan Pietschmann wrote:

>I read your post about this elsewhere. Sounds interesting, so how do you
>"store" it in the session. When and where is this Listener
>initialized/added?
>
>Stefan
> 
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message