cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Johannes Textor <>
Subject Re: AW: Accessing the cocoon session from my Hibernate User-Type
Date Mon, 01 Aug 2005 17:12:35 GMT
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

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:

View raw message