cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Messing, Elad" <elad.mess...@eml-d.villa-bosch.de>
Subject AW: AW: Accessing the cocoon session from my Hibernate User-Type
Date Tue, 02 Aug 2005 09:24:01 GMT
Hello Johannes !
	Thank you for your idea !
	I actually came to the same conclusion myself, when waiting to your reply and doing some
googling :). 
	However I have used the ThreadLocal object. I didn't quite understand the differences between
the Inherited and the ThreadLocal, and when I should use each... I will try to research it.

	Another thought I had is regarding what and when to store in the ThreadLocal.
	What - I am not sure I should store the whole user request. I mean - If I only need the locale,
why store the whole request ?
	When - I thought about using the locale action - which already is in-charge of locale maintaining,
to save the locale in the ThreadLocale. I thought about inherit it, and add the needed code
(User.setLocale( locale ); )

	What do you think ??

	Thank you !

Elad
	

-----Urspr√ľngliche Nachricht-----
Von: Johannes Textor [mailto:jc.textor@gmx.de] 
Gesendet: Montag, 1. August 2005 19:13
An: users@cocoon.apache.org
Betreff: Re: AW: Accessing the cocoon session from my Hibernate User-Type

Hi Elad,

I think I have a suggestion for this:

Since there is no way (I am aware of) to access Cocoon from a Hibernate object, the most straightforward
solution might be using an InheritedThreadLocale to store the HTTPRequest in.
A User object which would retrieve the current locale from the HTTP request might look something
like this:

public class User {

    private static InheritableThreadLocal request = new
InheritableThreadLocal() {
        protected Object initialValue() {
          return null;
        }
      };
    public static Object getRequest() {
        return request.get();
    }
    public static void setRequest(Object r) {
        request.set(r);
    }
    public static String getLanguage(){
        if( request.get() != null )
           /* Don't create a session if there is none */
            if( ((HttpServletRequest) request.get()).getSession( false ) != null ){
                return (String) ((HttpServletRequest) request.get())
                    .getSession()
                    .getAttribute("locale");
            }
        return "en-US";
    }
}

I assume you are already using an OpenSessionInView servlet filter, so you might add the following
code to store the HTTP Request object as an
InheritableThreadLocale:


  public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException, ServletException {
    // store request object as ThreadLocal
    User.setRequest( request );
     
    // Pass the request on to cocoon
    chain.doFilter(request, response);
   
    ....

Like this, you could call User.getLanguage() from whereever it might be, and it would return
the locale associated with the current HTTP Session. Since we are using an InheritableThreadLocal,
it should work regardless of how cocoon forks the request internally. Since the Request object
itself is stored as a ThreadLocal, changes to the HTTP Session by child threads will be reflected
back to all threads opened by the request.

Make Sense ?

Regards,
Johannes

Messing, Elad schrieb:

>Hello Johannes 
>	it is quite simple.
>	I am following the solution offered by Gavin here :
>	
>http://www.theserverside.com/blogs/showblog.tss?id=HibernateInternation
>al
>
>	Can you see the function : "User.current().getLanguage()"?
>	So - for me this function is still not implemented. This is because Cocoon is managing
the Locale in the current HTTP session, and I do not know how to get to it.
>	
>	In general sense, I am using a flow script which uses the hibernate session opened to
query the hibernate object :
>
>	var indexID = cocoon.parameters["id"];
>	openHibernateSession();
>	var index = Packages.de.emld.wlan.business.hibernate.dao.IndexDAO.get(hs, indexID);
>	cocoon.sendPage("/template/dynamic/showIndex.jx", {"index" : index} );	
>
>	Quite straight forward.
>
>	Now, "Index" is a hibernate mapped class. One of the property is this special translated
property, which requires the locale.
>	
>	So my question is how from this hibernate user-type class I can gain Access to the Cocoon
current session?
>
>	I hope this is more clear now.
>
>Elad
>
>  
>

---------------------------------------------------------------------
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


Mime
View raw message