cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Messing, Elad" <>
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 !


-----Urspr√ľngliche Nachricht-----
Von: Johannes Textor [] 
Gesendet: Montag, 1. August 2005 19:13
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) {
    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())
        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

  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 ?


Messing, Elad schrieb:

>Hello Johannes 
>	it is quite simple.
>	I am following the solution offered by Gavin here :
>	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 =, 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.

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message