Static Access to Visit Object?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Static Access to Visit Object?

Patrick Casey
 

            Does anyone have any experience with some sort of static
approach to getting ahold of the visit object? I have a need to get at
information in the visit object (the current user record in particular),
deep, deep, deep in the bowels of some code that doesn't have access to the
cycle object (and really can't; it's running inside a Hibernate
Interceptor).

 

            If worst comes to worst, I'm thinking I may have to implement a
threadlocal method to get ahold of the session and work downstream from
there, but I'm not all that sure how to navigate down from a servlet or
session object to get ahold of the Tapestry specific visit object.

 

            Any help would be appreciated,

 

            --- Pat

Reply | Threaded
Open this post in threaded view
|

Re: Static Access to Visit Object?

pferraro
Is your interceptor set in your Hibernate Configuration object? or when
you created your Hibernate Session?
Ideally, you would set the interceptor when you create your session, so
that your interceptor can be constructed using the current user.
Something like:

Session session = sessionFactory.openSession(new
MyInterceptor(visit.getUser()));

Paul

Patrick Casey wrote:

>
>
>            Does anyone have any experience with some sort of static
>approach to getting ahold of the visit object? I have a need to get at
>information in the visit object (the current user record in particular),
>deep, deep, deep in the bowels of some code that doesn't have access to the
>cycle object (and really can't; it's running inside a Hibernate
>Interceptor).
>
>
>
>            If worst comes to worst, I'm thinking I may have to implement a
>threadlocal method to get ahold of the session and work downstream from
>there, but I'm not all that sure how to navigate down from a servlet or
>session object to get ahold of the Tapestry specific visit object.
>
>
>
>            Any help would be appreciated,
>
>
>
>            --- Pat
>
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Static Access to Visit Object?

Patrick Casey

        I'm using the thread-local pattern for my (Hibernate) sessions, so
my sessions aren't (necessarily) going to be instantiated by Tapestry code.
The very first time a given thread calls for a session (which may or may not
be inside of a tapestry method), I'll spin up a thread-local session for it
that I then use for all subsequent (until thread death) operations within
that thread.

        Any given session then is linked to a thread, not a user, so it may
be serving Bob's GET right now, and 3 seconds later be servicing Eve's POST.
So I can't associate a user object with a Hibernate session unfortunately
even if I could guarantee they were only instantiated by code that had
access to the visit object.

        The user is linked to the current HttpSession though, and I can (if
I have to), write some bodgy code to let me statically get the current
session if I override ApplicationServlet (I'll stuff the active session into
thread-local storage on doGet() and then I can call it back later
statically).

        So if I could get from the HttpSession to the Tapestry visit object,
I'd be golden, but I'm not sure how to do that unfortunately as I don't know
how tapestry stashes itself in the session.

        Perhaps you could help?

        --- Pat

> -----Original Message-----
> From: Paul Ferraro [mailto:[hidden email]]
> Sent: Saturday, May 14, 2005 7:50 PM
> To: Tapestry users
> Subject: Re: Static Access to Visit Object?
>
> Is your interceptor set in your Hibernate Configuration object? or when
> you created your Hibernate Session?
> Ideally, you would set the interceptor when you create your session, so
> that your interceptor can be constructed using the current user.
> Something like:
>
> Session session = sessionFactory.openSession(new
> MyInterceptor(visit.getUser()));
>
> Paul
>
> Patrick Casey wrote:
>
> >
> >
> >            Does anyone have any experience with some sort of static
> >approach to getting ahold of the visit object? I have a need to get at
> >information in the visit object (the current user record in particular),
> >deep, deep, deep in the bowels of some code that doesn't have access to
> the
> >cycle object (and really can't; it's running inside a Hibernate
> >Interceptor).
> >
> >
> >
> >            If worst comes to worst, I'm thinking I may have to implement
> a
> >threadlocal method to get ahold of the session and work downstream from
> >there, but I'm not all that sure how to navigate down from a servlet or
> >session object to get ahold of the Tapestry specific visit object.
> >
> >
> >
> >            Any help would be appreciated,
> >
> >
> >
> >            --- Pat
> >
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Static Access to Visit Object?

Patrick Casey
In reply to this post by pferraro
For what it's worth, if anyone else has this issue, the following approach
works although it'll break if Howard decides to change how he stores stuff
in the session.

        --- Pat


package servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tapestry.ApplicationServlet;
import org.apache.tapestry.engine.AbstractEngine;

import presentation.Visit;

import data.HibHelper;

import schedule.ScheduleReader;
import utils.Clock;
import utils.Log;

public class MyServlet extends ApplicationServlet {
        private static final long serialVersionUID = 4049633486447393072L;
        private static final ThreadLocal fCurrentSession = new
ThreadLocal();
        private static final String
ENGINE_PREFIX="org.apache.tapestry.engine";

       
        public static Visit getVisit() {
                HttpSession s = (HttpSession) fCurrentSession.get();
                Enumeration en = s.getAttributeNames();
                while (en.hasMoreElements()) {
                        String attrib = (String) en.nextElement();
                        Log.info(attrib);
                        if (attrib.startsWith(ENGINE_PREFIX)) {
                                AbstractEngine e = (AbstractEngine)
s.getAttribute(attrib);
                                return (Visit) e.getVisit();
                        }
                }
                return null;
        }


        protected void doService(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
                Clock c = new Clock();
                HttpSession s = request.getSession();
                fCurrentSession.set(s);
                super.doService(request, response);
                String temp = "Total Render time = " + c;
                Log.info(temp);
                HibHelper.closeSession();
        }

}

> -----Original Message-----
> From: Paul Ferraro [mailto:[hidden email]]
> Sent: Saturday, May 14, 2005 7:50 PM
> To: Tapestry users
> Subject: Re: Static Access to Visit Object?
>
> Is your interceptor set in your Hibernate Configuration object? or when
> you created your Hibernate Session?
> Ideally, you would set the interceptor when you create your session, so
> that your interceptor can be constructed using the current user.
> Something like:
>
> Session session = sessionFactory.openSession(new
> MyInterceptor(visit.getUser()));
>
> Paul
>
> Patrick Casey wrote:
>
> >
> >
> >            Does anyone have any experience with some sort of static
> >approach to getting ahold of the visit object? I have a need to get at
> >information in the visit object (the current user record in particular),
> >deep, deep, deep in the bowels of some code that doesn't have access to
> the
> >cycle object (and really can't; it's running inside a Hibernate
> >Interceptor).
> >
> >
> >
> >            If worst comes to worst, I'm thinking I may have to implement
> a
> >threadlocal method to get ahold of the session and work downstream from
> >there, but I'm not all that sure how to navigate down from a servlet or
> >session object to get ahold of the Tapestry specific visit object.
> >
> >
> >
> >            Any help would be appreciated,
> >
> >
> >
> >            --- Pat
> >
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Static Access to Visit Object?

Pablo Ruggia
I'm doing this:
I use a thread local variable in a class. And I subclass Base engine
this way (UserContext is the class where I have the thread local
variable Visit and statics methos getVisit and setVisit)

public class MyEngine extends BaseEngine {

    protected void setupForRequest(RequestContext context) {
           super.setupForRequest(context);

            UserContext.setVisit(getVisit());
    }

}

I'm using it without around a month without any problem, but I've
heard that I wouldn't sublcass BaseEngine in Picasso, so ........

On 5/15/05, Patrick Casey <[hidden email]> wrote:

> For what it's worth, if anyone else has this issue, the following approach
> works although it'll break if Howard decides to change how he stores stuff
> in the session.
>
>         --- Pat
>
> package servlet;
>
> import java.io.IOException;
> import java.util.Enumeration;
>
> import javax.servlet.ServletConfig;
> import javax.servlet.ServletException;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
> import javax.servlet.http.HttpSession;
>
> import org.apache.tapestry.ApplicationServlet;
> import org.apache.tapestry.engine.AbstractEngine;
>
> import presentation.Visit;
>
> import data.HibHelper;
>
> import schedule.ScheduleReader;
> import utils.Clock;
> import utils.Log;
>
> public class MyServlet extends ApplicationServlet {
>         private static final long serialVersionUID = 4049633486447393072L;
>         private static final ThreadLocal fCurrentSession = new
> ThreadLocal();
>         private static final String
> ENGINE_PREFIX="org.apache.tapestry.engine";
>
>         public static Visit getVisit() {
>                 HttpSession s = (HttpSession) fCurrentSession.get();
>                 Enumeration en = s.getAttributeNames();
>                 while (en.hasMoreElements()) {
>                         String attrib = (String) en.nextElement();
>                         Log.info(attrib);
>                         if (attrib.startsWith(ENGINE_PREFIX)) {
>                                 AbstractEngine e = (AbstractEngine)
> s.getAttribute(attrib);
>                                 return (Visit) e.getVisit();
>                         }
>                 }
>                 return null;
>         }
>
>         protected void doService(HttpServletRequest request,
> HttpServletResponse response) throws IOException, ServletException {
>                 Clock c = new Clock();
>                 HttpSession s = request.getSession();
>                 fCurrentSession.set(s);
>                 super.doService(request, response);
>                 String temp = "Total Render time = " + c;
>                 Log.info(temp);
>                 HibHelper.closeSession();
>         }
>
> }
>
> > -----Original Message-----
> > From: Paul Ferraro [mailto:[hidden email]]
> > Sent: Saturday, May 14, 2005 7:50 PM
> > To: Tapestry users
> > Subject: Re: Static Access to Visit Object?
> >
> > Is your interceptor set in your Hibernate Configuration object? or when
> > you created your Hibernate Session?
> > Ideally, you would set the interceptor when you create your session, so
> > that your interceptor can be constructed using the current user.
> > Something like:
> >
> > Session session = sessionFactory.openSession(new
> > MyInterceptor(visit.getUser()));
> >
> > Paul
> >
> > Patrick Casey wrote:
> >
> > >
> > >
> > >            Does anyone have any experience with some sort of static
> > >approach to getting ahold of the visit object? I have a need to get at
> > >information in the visit object (the current user record in particular),
> > >deep, deep, deep in the bowels of some code that doesn't have access to
> > the
> > >cycle object (and really can't; it's running inside a Hibernate
> > >Interceptor).
> > >
> > >
> > >
> > >            If worst comes to worst, I'm thinking I may have to implement
> > a
> > >threadlocal method to get ahold of the session and work downstream from
> > >there, but I'm not all that sure how to navigate down from a servlet or
> > >session object to get ahold of the Tapestry specific visit object.
> > >
> > >
> > >
> > >            Any help would be appreciated,
> > >
> > >
> > >
> > >            --- Pat
> > >
> > >
> > >
> > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Static Access to Visit Object?

Joel Trunick
In reply to this post by Patrick Casey

We use the ThreadLocal method as well. This should be a Tapestry "Best
Practice" as it avoids Visit bloat.

Possibly Howard would give us an interface that has the getVisit(),
getHasVisit(), createVisit() trio so that we can stuff this interface in
ThreadLocal and can avoid creating the Visit until it is needed.

J

-----Original Message-----
From: Pablo Ruggia [mailto:[hidden email]]
Sent: Tuesday, May 17, 2005 12:32 AM
To: Tapestry users
Subject: Re: Static Access to Visit Object?

I'm doing this:
I use a thread local variable in a class. And I subclass Base engine
this way (UserContext is the class where I have the thread local
variable Visit and statics methos getVisit and setVisit)

public class MyEngine extends BaseEngine {

    protected void setupForRequest(RequestContext context) {
           super.setupForRequest(context);

            UserContext.setVisit(getVisit());
    }

}

I'm using it without around a month without any problem, but I've
heard that I wouldn't sublcass BaseEngine in Picasso, so ........

On 5/15/05, Patrick Casey <[hidden email]> wrote:
> For what it's worth, if anyone else has this issue, the following
approach
> works although it'll break if Howard decides to change how he stores
stuff

> in the session.
>
>         --- Pat
>
> package servlet;
>
> import java.io.IOException;
> import java.util.Enumeration;
>
> import javax.servlet.ServletConfig;
> import javax.servlet.ServletException;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
> import javax.servlet.http.HttpSession;
>
> import org.apache.tapestry.ApplicationServlet;
> import org.apache.tapestry.engine.AbstractEngine;
>
> import presentation.Visit;
>
> import data.HibHelper;
>
> import schedule.ScheduleReader;
> import utils.Clock;
> import utils.Log;
>
> public class MyServlet extends ApplicationServlet {
>         private static final long serialVersionUID =
4049633486447393072L;

>         private static final ThreadLocal fCurrentSession = new
> ThreadLocal();
>         private static final String
> ENGINE_PREFIX="org.apache.tapestry.engine";
>
>         public static Visit getVisit() {
>                 HttpSession s = (HttpSession) fCurrentSession.get();
>                 Enumeration en = s.getAttributeNames();
>                 while (en.hasMoreElements()) {
>                         String attrib = (String) en.nextElement();
>                         Log.info(attrib);
>                         if (attrib.startsWith(ENGINE_PREFIX)) {
>                                 AbstractEngine e = (AbstractEngine)
> s.getAttribute(attrib);
>                                 return (Visit) e.getVisit();
>                         }
>                 }
>                 return null;
>         }
>
>         protected void doService(HttpServletRequest request,
> HttpServletResponse response) throws IOException, ServletException {
>                 Clock c = new Clock();
>                 HttpSession s = request.getSession();
>                 fCurrentSession.set(s);
>                 super.doService(request, response);
>                 String temp = "Total Render time = " + c;
>                 Log.info(temp);
>                 HibHelper.closeSession();
>         }
>
> }
>
> > -----Original Message-----
> > From: Paul Ferraro [mailto:[hidden email]]
> > Sent: Saturday, May 14, 2005 7:50 PM
> > To: Tapestry users
> > Subject: Re: Static Access to Visit Object?
> >
> > Is your interceptor set in your Hibernate Configuration object? or
when
> > you created your Hibernate Session?
> > Ideally, you would set the interceptor when you create your session,
so

> > that your interceptor can be constructed using the current user.
> > Something like:
> >
> > Session session = sessionFactory.openSession(new
> > MyInterceptor(visit.getUser()));
> >
> > Paul
> >
> > Patrick Casey wrote:
> >
> > >
> > >
> > >            Does anyone have any experience with some sort of
static
> > >approach to getting ahold of the visit object? I have a need to get
at
> > >information in the visit object (the current user record in
particular),
> > >deep, deep, deep in the bowels of some code that doesn't have
access to
> > the
> > >cycle object (and really can't; it's running inside a Hibernate
> > >Interceptor).
> > >
> > >
> > >
> > >            If worst comes to worst, I'm thinking I may have to
implement
> > a
> > >threadlocal method to get ahold of the session and work downstream
from
> > >there, but I'm not all that sure how to navigate down from a
servlet or

> > >session object to get ahold of the Tapestry specific visit object.
> > >
> > >
> > >
> > >            Any help would be appreciated,
> > >
> > >
> > >
> > >            --- Pat
> > >
> > >
> > >
> > >
> >
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail:
[hidden email]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Static Access to Visit Object?

Andreas Andreou-4
In reply to this post by Pablo Ruggia
Pablo Ruggia wrote:

>I'm doing this:
>I use a thread local variable in a class. And I subclass Base engine
>this way (UserContext is the class where I have the thread local
>variable Visit and statics methos getVisit and setVisit)
>
>public class MyEngine extends BaseEngine {
>
>    protected void setupForRequest(RequestContext context) {
>           super.setupForRequest(context);
>
>            UserContext.setVisit(getVisit());
>    }
>
>}
>
>I'm using it without around a month without any problem, but I've
>heard that I wouldn't sublcass BaseEngine in Picasso, so ........
>  
>
That's probably true... Why not create a BasePage
and set up the thread local there?


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Static Access to Visit Object?

Joel Trunick
In reply to this post by Patrick Casey

If you don't care that the Visit is created before it is needed, it
doesn't matter. However, it would be ideal if your ThreadLocal could
create the Visit when it is actually needed.

J

-----Original Message-----
From: Andreas Andreou [mailto:[hidden email]]
Sent: Tuesday, May 17, 2005 12:44 PM
To: Tapestry users
Subject: Re: Static Access to Visit Object?

Pablo Ruggia wrote:

>I'm doing this:
>I use a thread local variable in a class. And I subclass Base engine
>this way (UserContext is the class where I have the thread local
>variable Visit and statics methos getVisit and setVisit)
>
>
>I'm using it without around a month without any problem, but I've
>heard that I wouldn't sublcass BaseEngine in Picasso, so ........
>  
>
That's probably true... Why not create a BasePage
and set up the thread local there?


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Static Access to Visit Object?

Pablo Ruggia
In reply to this post by Andreas Andreou-4
> That's probably true... Why not create a BasePage
> and set up the thread local there?
>
>

In what method can I put that logic so it calls before my listeners ?
I'm doing it in Engine because I know that setupForRequest() is one of
the firsts method that get called before a request.
Is there a similar method that I will be able to override in Picasso ?

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Static Access to Visit Object?

Pablo Ruggia
In reply to this post by Joel Trunick
On 5/17/05, Joel Trunick <[hidden email]> wrote:
>
> If you don't care that the Visit is created before it is needed, it
> doesn't matter. However, it would be ideal if your ThreadLocal could
> create the Visit when it is actually needed.
>

BaseEngine's method getVisit() does not create the visit. If the
VisitObject have not been created, returns null. It does not beheave
like page's method getVisit(). Page's getVisit() is equals to
BaseEngine getVisit(IRequestCycle cycle).

So with the solution I told, you don't have the problem you're describing.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]