You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Greg Ward <gw...@python.net> on 2005/09/30 20:51:18 UTC

Tapestry 4 migration: how to initialize a Global object?

I'm trying to migrate a Tapestry 3 app to Tapestry 4.  My first
stumbling block is initializing (not creating) my Global object.  Under
Tapestry 3, our app initializes itself like this:

  * our ServletContextListener stuffs various interesting objects into
    the ServletContext (e.g. a Configuration object, the log filename,
    application version string -- stuff that should be setup in the
    ServletContextListener)

  * we provide a custom Engine class that overrides createGlobal();
    it's happy to let Tapestry create the Global object
    with "new Global()", but that does not result in a usable object --
    we have to copy stuff from the ServletContext into the Global object:

    protected Object createGlobal( RequestContext requestContext )
    {
      Global global = (Global) super.createGlobal( context );
      ServletContext context = requestContext.getServlet().getServletContext();
      global.init(
        (Configuration) context.getAttribute( Constants.CONFIGURATION_ATTR ),
        ContextListener.getNotifier( context ),
        (String) context.getAttribute( Constants.PROJECT_NAME_ATTR ),
        (String) context.getAttribute( Constants.PROJECT_VERSION_ATTR ),
        (String) context.getAttribute( Constants.LOG_BASENAME_ATTR ) );
      return global;
    }

I'm absolutely clueless how to do this in Tapestry 4.  I've gotten as
far as creating hivemodule.xml and asking HiveMind to kindly instantiate
one of our Global objects rather than a HashMap ... but then how the
heck do I initialize it?  I can't do it from the ServletContextListener,
because that runs before Tapestry/HiveMind ever enter the picture.  But
everywhere I try to hook in to Tapestry, I can't see how to get my hands
on a ServletContext -- everything's hidden behind WebRequest, and
there's no apparent way to get from a WebRequest to a ServletContext.
Argh!  What am I missing?

Thanks --

        Greg

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


Re: Tapestry 4 migration: how to initialize a Global object?

Posted by Adam Greene <ag...@iq-2000.com>.
You aren't missing anything.  Upgrade to the latest Beta-9 build, the 
ServletContext wasn't available originally (but was "requested" that it be 
added).  Then just inject it as a service, like you would a WebRequest (it 
might be available via infrastructure:, but I haven't looked).

----- Original Message ----- 
From: "Greg Ward" <gw...@python.net>
To: <ta...@jakarta.apache.org>
Sent: Friday, September 30, 2005 3:51 PM
Subject: Tapestry 4 migration: how to initialize a Global object?


> I'm trying to migrate a Tapestry 3 app to Tapestry 4.  My first
> stumbling block is initializing (not creating) my Global object.  Under
> Tapestry 3, our app initializes itself like this:
>
>  * our ServletContextListener stuffs various interesting objects into
>    the ServletContext (e.g. a Configuration object, the log filename,
>    application version string -- stuff that should be setup in the
>    ServletContextListener)
>
>  * we provide a custom Engine class that overrides createGlobal();
>    it's happy to let Tapestry create the Global object
>    with "new Global()", but that does not result in a usable object --
>    we have to copy stuff from the ServletContext into the Global object:
>
>    protected Object createGlobal( RequestContext requestContext )
>    {
>      Global global = (Global) super.createGlobal( context );
>      ServletContext context = 
> requestContext.getServlet().getServletContext();
>      global.init(
>        (Configuration) context.getAttribute( 
> Constants.CONFIGURATION_ATTR ),
>        ContextListener.getNotifier( context ),
>        (String) context.getAttribute( Constants.PROJECT_NAME_ATTR ),
>        (String) context.getAttribute( Constants.PROJECT_VERSION_ATTR ),
>        (String) context.getAttribute( Constants.LOG_BASENAME_ATTR ) );
>      return global;
>    }
>
> I'm absolutely clueless how to do this in Tapestry 4.  I've gotten as
> far as creating hivemodule.xml and asking HiveMind to kindly instantiate
> one of our Global objects rather than a HashMap ... but then how the
> heck do I initialize it?  I can't do it from the ServletContextListener,
> because that runs before Tapestry/HiveMind ever enter the picture.  But
> everywhere I try to hook in to Tapestry, I can't see how to get my hands
> on a ServletContext -- everything's hidden behind WebRequest, and
> there's no apparent way to get from a WebRequest to a ServletContext.
> Argh!  What am I missing?
>
> Thanks --
>
>        Greg
>
> ---------------------------------------------------------------------
> 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: Tapestry 4 migration: how to initialize a Global object?

Posted by Kent Tong <ke...@cpttm.org.mo>.
Greg Ward <gward-tapestry <at> python.net> writes:

> I'm absolutely clueless how to do this in Tapestry 4.  I've gotten as
> far as creating hivemodule.xml and asking HiveMind to kindly instantiate
> one of our Global objects rather than a HashMap ... but then how the
> heck do I initialize it?  I can't do it from the ServletContextListener,
> because that runs before Tapestry/HiveMind ever enter the picture.  But
> everywhere I try to hook in to Tapestry, I can't see how to get my hands
> on a ServletContext -- everything's hidden behind WebRequest, and
> there's no apparent way to get from a WebRequest to a ServletContext.
> Argh!  What am I missing?

Get tapestry 4 beta 8 or later. You can inject the ServletContext like
into your Global object using the id tapestry.globals.ServletContext
(if your Global object is a hivemind service).

--
Author of a book for learning Tapestry (http://www.agileskills2.org/EWDT)


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


Re: Tapestry 4 migration: how to initialize a Global object?

Posted by Barry Books <tr...@gmail.com>.
I don't know if it's the best way but I wrote a filter that puts stuff
into threadlocal and then I wrote interfaces in my global object to
get them out. The nice thing about that is servlets other than
Tapestry also have access to it. Another benefit to this approach is
you have access to the objects in a DataSqueezer. All in all it was
well worth the effort to figure out how filters work.

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