You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Patrick Casey <pa...@adelphia.net> on 2005/05/14 22:51:43 UTC

Static Access to Visit Object?

 

            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


Re: Static Access to Visit Object?

Posted by Pablo Ruggia <pr...@gmail.com>.
> 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: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Static Access to Visit Object?

Posted by Andreas Andreou <an...@di.uoa.gr>.
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: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Static Access to Visit Object?

Posted by Pablo Ruggia <pr...@gmail.com>.
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 <pa...@adelphia.net> 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:pmf8@columbia.edu]
> > 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: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: Static Access to Visit Object?

Posted by Patrick Casey <pa...@adelphia.net>.
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:pmf8@columbia.edu]
> 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: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: Static Access to Visit Object?

Posted by Patrick Casey <pa...@adelphia.net>.
	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:pmf8@columbia.edu]
> 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: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Static Access to Visit Object?

Posted by Paul Ferraro <pm...@columbia.edu>.
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: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org