Re: TemplateLoader/Configuration Question
Jonathan Revusky <[email protected]> Wed, 26 Feb 2003 17:14:21 +0100
| Newsgroups | gmane.comp.web.freemarker.devel,gmane.comp.java.jpublish.devel |
|---|---|
| Message-ID | <[email protected]> |
On Wednesday, February 26, 2003, at 04:51 PM, Andy Zeneski wrote:
>
> Anthony,
>
> I am was looking at mine too and I need a Configuration object in
> order to
> do <#include> properly. Without the Configuration it couldn't find the
> included files.
>
> Now, does JPublish create a unique instance of the ViewRenderer for
> each
> webapp? If so, can we not create the FTL Configuration in the init()
> method
> or does init() get called before setSiteContext()?
>
> I assumed init() was called first, so I did this in my render method:
>
> BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
> WrappingTemplateModel.setDefaultObjectWrapper(wrapper);
> if (config == null) {
> synchronized(this) {
> if (config == null) {
> config =
> freemarker.template.Configuration.getDefaultConfiguration();
>
> config.setDirectoryForTemplateLoading(siteContext.getContextRoot());
> config.setObjectWrapper(wrapper);
> Debug.logInfo("Set directory for template
> (includes)
> loading: " + siteContext.getContextRoot(), module);
> }
> }
> }
>
> So, only the first page will have to load the configuration. BTW
> config is
> an instance variable. This might not work if ViewRenderer instances are
> shared between webapps, this I have not tested yet.
BTW, you don't absolutely have to use the singleton pattern with the
Configuration object either. You could instantiate a different one for
different webapps or servlets and so on and they could use different
settings. It's just that the singleton pattern is probably suitable for
the majority of cases, unless you have special needs. So that's what
you see in our example code.
So, you could alternatively new the Configuration in your init() method
above (rather than getting vended the singleton instance) if that
suited your purposes better. (You could *even* instantiate a new one
for each request, as Anthony said, but I don't see why that would ever
be necessary.)
Ciao,
Jonathan
>
> -Andy
>
>
>
>> -----Original Message-----
>> From: [email protected]
>> [mailto:[email protected]]On Behalf Of
>> Anthony Eden
>> Sent: Wednesday, February 26, 2003 9:53 PM
>> To: [email protected]
>> Subject: [Freemarker-devel] TemplateLoader/Configuration Question
>>
>>
>> Right now JPublish constructs a Template directly and thus can
>> not take advantage of any caching in template engines. I am
>> working on the FreeMarker view renderer right now and I have a
>> question about performance:
>>
>> Which is likely to pose a larger performance threat (speed and
>> memory):
>>
>> a.) Construct a new Configuration and TemplateLoader for every
>> request. In this case the template loader will still have the
>> ability to return a valid value for the getLastModified() method.
>> b.) Construct a Template without caching for every request and
>> thus require the template to be parsed for every request.
>>
>> Sincerely,
>> Anthony Eden
>>
>>
>>
>> -------------------------------------------------------
>> This SF.net email is sponsored by: Scholarships for Techies!
>> Can't afford IT training? All 2003 ictp students receive scholarships.
>> Get hands-on training in Microsoft, Cisco, Sun, Linux/UNIX, and more.
>> www.ictp.com/training/sourceforge.asp
>> _______________________________________________
>> FreeMarker-devel mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/freemarker-devel
>>
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Scholarships for Techies!
> Can't afford IT training? All 2003 ictp students receive scholarships.
> Get hands-on training in Microsoft, Cisco, Sun, Linux/UNIX, and more.
> www.ictp.com/training/sourceforge.asp
> _______________________________________________
> FreeMarker-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-devel
>
-------------------------------------------------------
This SF.net email is sponsored by: Scholarships for Techies!
Can't afford IT training? All 2003 ictp students receive scholarships.
Get hands-on training in Microsoft, Cisco, Sun, Linux/UNIX, and more.
www.ictp.com/training/sourceforge.asp