You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Michael Bayne <md...@samskivert.com> on 2001/10/30 08:50:25 UTC

ResourceLoader jockeying

I'm interested in loading resources (templates) based on information
gleaned from the URL. For example, if a page is requested as

http://www.foo.com/thing/beep

I might magically load some templates from a different place than I would
were it to be requested as

http://www.bar.com/thing/beep

The problem being that resource loading takes place with no contextual
information whatsoever. The resource loaders are initialized and then
requested to load resources by name alone.

I could create and initialize two entirely separate instances of my web
application, one for each site and configure each of them with the
appropriate resource loaders so that they each looked in their own
directories for resources that couldn't be found in the webapp .war file,
but that kind of blows. Especially since both sites are just virtual hosts
and are served by the same web server. It would also lose if the number of
different sites became large.

I could provide some sort of invocation context that was provided to
RuntimeInstance.getTemplate() (and getContent() and anything else that
might find invocation specific information to be useful).

An implementation of the context for use in the servlet world would then
provide access to the HttpServletRequest object which would allow my
specialized resource loader to look in the right place based on the server
name specified in the request. But then the resource manager would cache
the resource based on the name alone which wouldn't work.

So the first working solution that comes to mind is to have my servlet
encode the site information in the template path before it calls
getTemplate() (by say, prefixing the site identifier to the path) and then
have my specialized resource loader decode the site information, load the
template accordingly, which the resource manager will cache properly
because the site information is in the resource path.

But it seems like a big fat hack and a resource that is the same for all
sites will be cached multiple times because it appears to the resource
manager to be a completely different resource in each case.

The only way around that seems to be to introduce the concept of
invocation context sensitive resource loading into the resource manager so
that the invocation context can participate in the cache lookup. Something
like the following:

  public Resource getResource (InvocationContext ictx, String resourceName,
                               int resourceType, String encoding)
      throws ResourceNotFoundException, ParseErrorException, Exception
  {
      Object rkey = ictx.getResourceKey(resourceName);
      Resource resource = (Resource) globalCache.get(rkey);
      // ...
  }

such that my special site-sensitive invocation context could return a key
that matched the global resource as well as the site-specific resource.
But this adds undesirable complexity to the resource loading process.

Anyone have any better suggestions?

-- mdb /o)\ They're on their way there.
       \(o/


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by Michael Bayne <md...@samskivert.com>.
On Tue, 30 Oct 2001, Geir Magnusson Jr. wrote:

> What?  You are talking about the *input encoding* here - Velocity
> doesn't care about the *output encoding* - that can be specified on a
> request by request basis.

Ah, my mistake. I didn't see the part where the input encoding was read
from the configuration file. I assumed that it came from the request.

-- mdb  "the free soul is rare, but you know it when you see it --
  /o)\   basically because you feel good, very good, when you are
  \(o/   near or with them."  -- Charles Bukowski


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/30/01 3:09 AM, "Michael Bayne" <md...@samskivert.com> wrote:

> On Mon, 29 Oct 2001, Michael Bayne wrote:
> 
>> Something like the following:
>> 
>>   public Resource getResource (InvocationContext ictx, String resourceName,
>>                                int resourceType, String encoding)
>>       throws ResourceNotFoundException, ParseErrorException, Exception
>>   {
>>       Object rkey = ictx.getResourceKey(resourceName);
>>       Resource resource = (Resource) globalCache.get(rkey);
>>       // ...
>>   }
>> 
>> such that my special site-sensitive invocation context could return a key
>> that matched the global resource as well as the site-specific resource.
> 
> This isn't entirely correct. In order to properly match the most specific
> resource for the request, something more sophisticated than a plain old
> hashtable would have to be used.
> 
> But, I noticed in poking around in the source that this invocation context
> sensitive caching would also afford the opportunity to properly handle
> requests for a resource in different content encodings as well.
> 
> Presently, the first time a resource is requested, its encoding is set to
> the desired encoding at that time. This encoding is used for all
> subsequent requests regardless of their desired encoding until the
> resource expires at which time, it may inherit a new encoding based on the
> request that caused it to be reloaded.

What?  You are talking about the *input encoding* here - Velocity doesn't
care about the *output encoding* - that can be specified on a request by
request basis.

The input encoding, the encoding Velociy uses to take a stream of bytes and
convert to characters is fixed, right?  A given resource is just a static
pile of bits that have an encoding associated with them to convert the
bytestream into characters.  I can't imagine that you would be able to
concoct a template where you can switch how you decode the incoming
bytestream (off disk, say)  and it would make sense - you would be
brilliantly lucky to have some character sequence in one encoding decode
into anything but nonesense from another encoding, right?

> 
> This doesn't seem like desirable behavior. One would expect that the
> encoding would be passed to the resource loader which could potentially
> provide content tailored to the requested encoding. For languages where
> multiple encodings of the same content might exist, the resource loader
> could automatically translate the source encoding into the appropriate
> encoding for the request (EUC-JP to Shift-JIS for example).

That's the output encoding, isn't it?  I mean you have some resource, held
internally as a set of characters, and you then encode going out into the
appropriate bytestream in the encoding suggested by the client.
 
> At the very least, it would avoid the current questionable behavior that
> arises if the first request for a document happens to be an incorrect
> encoding, all subsequent requests (until it expires from the cache) will
> be given that incorrect encoding even though they requested the resource
> with the correct encoding.

You the PROGRAMMER decide what the encoding is of a document is when loading
- if it is wrong, it is because YOU specified the wrong encoding - that
isn't the encoding specified by a web browers on the request...  I think
that this is appropriate.


> -- mdb /o)\ "Be on the lookout for a twenty dollar bill that
>      \(o/  says 'payment for monkey' in big red letters."
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by Michael Bayne <md...@samskivert.com>.
On Mon, 29 Oct 2001, Michael Bayne wrote:

> Something like the following:
>
>   public Resource getResource (InvocationContext ictx, String resourceName,
>                                int resourceType, String encoding)
>       throws ResourceNotFoundException, ParseErrorException, Exception
>   {
>       Object rkey = ictx.getResourceKey(resourceName);
>       Resource resource = (Resource) globalCache.get(rkey);
>       // ...
>   }
>
> such that my special site-sensitive invocation context could return a key
> that matched the global resource as well as the site-specific resource.

This isn't entirely correct. In order to properly match the most specific
resource for the request, something more sophisticated than a plain old
hashtable would have to be used.

But, I noticed in poking around in the source that this invocation context
sensitive caching would also afford the opportunity to properly handle
requests for a resource in different content encodings as well.

Presently, the first time a resource is requested, its encoding is set to
the desired encoding at that time. This encoding is used for all
subsequent requests regardless of their desired encoding until the
resource expires at which time, it may inherit a new encoding based on the
request that caused it to be reloaded.

This doesn't seem like desirable behavior. One would expect that the
encoding would be passed to the resource loader which could potentially
provide content tailored to the requested encoding. For languages where
multiple encodings of the same content might exist, the resource loader
could automatically translate the source encoding into the appropriate
encoding for the request (EUC-JP to Shift-JIS for example).

At the very least, it would avoid the current questionable behavior that
arises if the first request for a document happens to be an incorrect
encoding, all subsequent requests (until it expires from the cache) will
be given that incorrect encoding even though they requested the resource
with the correct encoding.

-- mdb /o)\ "Be on the lookout for a twenty dollar bill that
       \(o/  says 'payment for monkey' in big red letters."


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/30/01 8:53 PM, "Michael Bayne" <md...@samskivert.com> wrote:

> On Tue, 30 Oct 2001, Geir Magnusson Jr. wrote:
> 
>>> Can I do this such that the context that is currently in effect is used to
>>> render the requested page. For example, I want to do something like so:
>>> 
>>> header.vm:
>>> ...
>>> <b>$title</b>
>>> ...
>>> 
>>> page.vm:
>>> #set ($title = "I love this page!")
>>> #parse "header.vm"
>>> ...
>>> 
>> 
>> Of course.
> 
> In my limited understanding, a call to something like:
> 
> $renderer.render("/header.vm")
> 
> would result in a call to a method that looked something like this:
> 
>   public void render (String templatePath)
> 
> How would that object go about getting access to the context in effect
> when this method call was invoked? I guess I could put a reference to the
> context itself in the context and pass that as an argument to the method.
> 

Yes, that would work.  Or you could stuff a reference to the current context
into the Renderer object before putting in the context to keep the designer
from having to worry about such issues.

Or, since you are an interface kind of guy ;-> make your own Context that
looks to see, when objects are placed in the context, if those  objects
implement a 'ContextHungry' interface, and then let your context
automatically stuff it into the object.


Or, why not just call #parse() ?

geir
-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by Michael Bayne <md...@samskivert.com>.
On Tue, 30 Oct 2001, Geir Magnusson Jr. wrote:

> > Can I do this such that the context that is currently in effect is used to
> > render the requested page. For example, I want to do something like so:
> >
> > header.vm:
> > ...
> > <b>$title</b>
> > ...
> >
> > page.vm:
> > #set ($title = "I love this page!")
> > #parse "header.vm"
> > ...
> >
>
> Of course.

In my limited understanding, a call to something like:

$renderer.render("/header.vm")

would result in a call to a method that looked something like this:

    public void render (String templatePath)

How would that object go about getting access to the context in effect
when this method call was invoked? I guess I could put a reference to the
context itself in the context and pass that as an argument to the method.

-- mdb /o)\ "Be on the lookout for a twenty dollar bill that
       \(o/  says 'payment for monkey' in big red letters."


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/30/01 8:28 PM, "Michael Bayne" <md...@samskivert.com> wrote:

> On Tue, 30 Oct 2001, Jon Stevens wrote:
> 
>> That is why with Turbine we have a single 'renderer' class that knows
>> how to properly resolve the location of the templates.
>> 
>> Example:
>> 
>> <http://scarab.tigris.org/source/browse/scarab/src/templates/layouts/Default
>> .vm?rev=1.28&content-type=text/x-cvsweb-markup>
>> 
>> $renderer.render("screens", $data, $template)
>> 
>> The benefit of doing things this way is that your problem is solved
>> and you can also do things like specify a 'default' that can be used
>> if the requested one cannot be found.
> 
> Can I do this such that the context that is currently in effect is used to
> render the requested page. For example, I want to do something like so:
> 
> header.vm:
> ...
> <b>$title</b>
> ...
> 
> page.vm:
> #set ($title = "I love this page!")
> #parse "header.vm"
> ...
> 

Of course.

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by Michael Bayne <md...@samskivert.com>.
On Tue, 30 Oct 2001, Jon Stevens wrote:

> That is why with Turbine we have a single 'renderer' class that knows
> how to properly resolve the location of the templates.
>
> Example:
>
> <http://scarab.tigris.org/source/browse/scarab/src/templates/layouts/Default
> .vm?rev=1.28&content-type=text/x-cvsweb-markup>
>
> $renderer.render("screens", $data, $template)
>
> The benefit of doing things this way is that your problem is solved
> and you can also do things like specify a 'default' that can be used
> if the requested one cannot be found.

Can I do this such that the context that is currently in effect is used to
render the requested page. For example, I want to do something like so:

header.vm:
...
<b>$title</b>
...

page.vm:
#set ($title = "I love this page!")
#parse "header.vm"
...

-- mdb /o)\ "On a cellular level, I'm really quite busy."
       \(o/


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by Jon Stevens <jo...@latchkey.com>.
on 10/30/01 3:20 PM, "Michael Bayne" <md...@samskivert.com> wrote:

> However, if, in my main template I do:
> 
> #parse "/header.vm"
> 
> it won't go through the same site-sensitive resolution process. This is
> especially a pain because it's exaclty the headers and footers that you
> tend to want to be site-specific, while the top-level templates are more
> likely to be site-agnostic.

That is why with Turbine we have a single 'renderer' class that knows how to
properly resolve the location of the templates.

Example:

<http://scarab.tigris.org/source/browse/scarab/src/templates/layouts/Default
.vm?rev=1.28&content-type=text/x-cvsweb-markup>

$renderer.render("screens", $data, $template)

The benefit of doing things this way is that your problem is solved and you
can also do things like specify a 'default' that can be used if the
requested one cannot be found.

We use this technique in Scarab for having the ability to customize the
navigations depending on which page you request.

$renderer.render("navigations", $data, $template, "Default.vm")

-jon


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/30/01 6:20 PM, "Michael Bayne" <md...@samskivert.com> wrote:

> On Tue, 30 Oct 2001, Geir Magnusson Jr. wrote:
> 
>> To solve the problem of 'common' stuff, have three directories :
>> 
>> /common/
>> /www.bar.com
>> /www.foo.com
>> 
>> And then, your servlet first does something like
>> 
>> Template t = getTemplate( "www.bar.com/beep.vm" )
>> 
>> And then if that fails (as it's a 'common' template)
>> 
>> t = getTemplate( "common/beep.vm" )
>> 
>> Then the servlet doesn't have to think very hard, just try and try
>> again, and as new sites are added, just drop the site specific stuff
>> in it's own directory, and the single webapp can handle it.
> 
> However, if, in my main template I do:
> 
> #parse "/header.vm"
> 
> it won't go through the same site-sensitive resolution process. This is
> especially a pain because it's exaclty the headers and footers that you
> tend to want to be site-specific, while the top-level templates are more
> likely to be site-agnostic.

Rightl, so do something like

#parse( $headertemplate )

And let your controller fill in the blanks based on context.

The idea here is to put your application logic in the controller, not bury
it in the resource loader...

geir

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by Michael Bayne <md...@samskivert.com>.
On Tue, 30 Oct 2001, Geir Magnusson Jr. wrote:

> To solve the problem of 'common' stuff, have three directories :
>
> /common/
> /www.bar.com
> /www.foo.com
>
> And then, your servlet first does something like
>
> Template t = getTemplate( "www.bar.com/beep.vm" )
>
> And then if that fails (as it's a 'common' template)
>
> t = getTemplate( "common/beep.vm" )
>
> Then the servlet doesn't have to think very hard, just try and try
> again, and as new sites are added, just drop the site specific stuff
> in it's own directory, and the single webapp can handle it.

However, if, in my main template I do:

#parse "/header.vm"

it won't go through the same site-sensitive resolution process. This is
especially a pain because it's exaclty the headers and footers that you
tend to want to be site-specific, while the top-level templates are more
likely to be site-agnostic.

-- mdb /o)\ "I believe this can never work 'correctly' for any desirable
       \(o/  value of 'correctly'."  -- Jon Watte


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ResourceLoader jockeying

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/30/01 2:50 AM, "Michael Bayne" <md...@samskivert.com> wrote:

> I'm interested in loading resources (templates) based on information
> gleaned from the URL. For example, if a page is requested as
> 
> http://www.foo.com/thing/beep
> 
> I might magically load some templates from a different place than I would
> were it to be requested as
> 
> http://www.bar.com/thing/beep
> 
> The problem being that resource loading takes place with no contextual
> information whatsoever. The resource loaders are initialized and then
> requested to load resources by name alone.
> 
> I could create and initialize two entirely separate instances of my web
> application, one for each site and configure each of them with the
> appropriate resource loaders so that they each looked in their own
> directories for resources that couldn't be found in the webapp .war file,
> but that kind of blows. Especially since both sites are just virtual hosts
> and are served by the same web server. It would also lose if the number of
> different sites became large.
> 
> I could provide some sort of invocation context that was provided to
> RuntimeInstance.getTemplate() (and getContent() and anything else that
> might find invocation specific information to be useful).
> 
> An implementation of the context for use in the servlet world would then
> provide access to the HttpServletRequest object which would allow my
> specialized resource loader to look in the right place based on the server
> name specified in the request. But then the resource manager would cache
> the resource based on the name alone which wouldn't work.
> 
> So the first working solution that comes to mind is to have my servlet
> encode the site information in the template path before it calls
> getTemplate() (by say, prefixing the site identifier to the path) and then
> have my specialized resource loader decode the site information, load the
> template accordingly, which the resource manager will cache properly
> because the site information is in the resource path.

Why wouldn't you put the same logic up in servlet land, and just use the
loaders that wee have?

Either way, some big of code has to decide which template to use from where.

To solve the problem of 'common' stuff, have three directories :

/common/ 
/www.bar.com
/www.foo.com

And then, your servlet first does something like

Template t = getTemplate( "www.bar.com/beep.vm" )

And then if that fails (as it's a 'common' template)

t = getTemplate( "common/beep.vm" )

Then the servlet doesn't have to think very hard, just try and try again,
and as new sites are added, just drop the site specific stuff in it's own
directory, and the single webapp can handle it.

That's just one solution that pops to mind.

> 
> But it seems like a big fat hack and a resource that is the same for all
> sites will be cached multiple times because it appears to the resource
> manager to be a completely different resource in each case.
> 
> The only way around that seems to be to introduce the concept of
> invocation context sensitive resource loading into the resource manager so
> that the invocation context can participate in the cache lookup. Something
> like the following:
> 
> public Resource getResource (InvocationContext ictx, String resourceName,
>                              int resourceType, String encoding)
>     throws ResourceNotFoundException, ParseErrorException, Exception
> {
>     Object rkey = ictx.getResourceKey(resourceName);
>     Resource resource = (Resource) globalCache.get(rkey);
>     // ...
> }
> 
> such that my special site-sensitive invocation context could return a key
> that matched the global resource as well as the site-specific resource.
> But this adds undesirable complexity to the resource loading process.
> 
> Anyone have any better suggestions?

Will think more while spinning :)

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>