You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Stefan Pietschmann <de...@gmail.com> on 2005/09/11 11:40:36 UTC

Session lifecycle listener in cocoon?

Hi there,

 

I want to write a class which does some sort of cleanup when a users's
session ends (via logout or timeout).

 

As I understand it I can write a class which implements HTTPSessionListener
and add it in Tomcat's web.xml under <listeners> and this should work.
(Correct me if I'm wrong).

This relies on the Servlet API and I have to change my Tomcat installation.
However I liked it more if I could add that SessionListener to Cocoon's
web.xml since our project CVS only contains Cocoon, not Tomcat. Is it that
easy to just implement the HTTPSessionListener Interface and add this
Listener to cocoon/WEB-INF/web.xml?

 

Thanx,

Stefan 


Re: AW: Session lifecycle listener in cocoon?

Posted by Ralph Goers <Ra...@dslextreme.com>.
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


AW: Session lifecycle listener in cocoon?

Posted by Stefan Pietschmann <de...@gmail.com>.
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
 
| -----Ursprüngliche Nachricht-----
| Von: Ralph Goers [mailto:Ralph.Goers@dslextreme.com]
| Gesendet: Sonntag, 11. September 2005 18:39
| An: users@cocoon.apache.org
| Betreff: Re: Session lifecycle listener in cocoon?
| 
| It can be even easier. In my app I have a class that implements
| HttpSessionBindingListener. It gets stored in the session and I anchor
| all my session variable in it. When the session is terminated it gets
| called and it cleans up all the objects it knows about. You don't have
| to modify web.xml to do this.
| 
| Ralph
| 
| Stefan Pietschmann wrote:
| 
| > Hi there,
| >
| > I want to write a class which does some sort of cleanup when a users’s
| > session ends (via logout or timeout).
| >
| > As I understand it I can write a class which implements
| > HTTPSessionListener and add it in Tomcat’s web.xml under <listeners>
| > and this should work. (Correct me if I’m wrong).
| >
| > This relies on the Servlet API and I have to change my Tomcat
| > installation. However I liked it more if I could add that
| > SessionListener to Cocoon’s web.xml since our project CVS only
| > contains Cocoon, not Tomcat. Is it that easy to just implement the
| > HTTPSessionListener Interface and add this Listener to
| > cocoon/WEB-INF/web.xml?
| >
| > Thanx,
| >
| > Stefan
| >
| 
| 
| ---------------------------------------------------------------------
| To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
| For additional commands, e-mail: users-help@cocoon.apache.org


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


Re: Session lifecycle listener in cocoon?

Posted by Ralph Goers <Ra...@dslextreme.com>.
It can be even easier. In my app I have a class that implements 
HttpSessionBindingListener. It gets stored in the session and I anchor 
all my session variable in it. When the session is terminated it gets 
called and it cleans up all the objects it knows about. You don't have 
to modify web.xml to do this.

Ralph

Stefan Pietschmann wrote:

> Hi there,
>
> I want to write a class which does some sort of cleanup when a users’s 
> session ends (via logout or timeout).
>
> As I understand it I can write a class which implements 
> HTTPSessionListener and add it in Tomcat’s web.xml under <listeners> 
> and this should work. (Correct me if I’m wrong).
>
> This relies on the Servlet API and I have to change my Tomcat 
> installation. However I liked it more if I could add that 
> SessionListener to Cocoon’s web.xml since our project CVS only 
> contains Cocoon, not Tomcat. Is it that easy to just implement the 
> HTTPSessionListener Interface and add this Listener to 
> cocoon/WEB-INF/web.xml?
>
> Thanx,
>
> Stefan
>


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


Re: Session lifecycle listener in cocoon?

Posted by Jorg Heymans <jh...@domek.be>.

Stefan Pietschmann wrote:

> Cocoon, not Tomcat. Is it that easy to just implement the
> HTTPSessionListener Interface and add this Listener to
> cocoon/WEB-INF/web.xml?
> 

yes.


Jorg


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