Re: Configuration setup

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Sunday, February 20, 2011, 6:08:58 PM, Daniel Henrique Alves Lima wrote:

>         Hi, Daniel.
>
>         Is it safe to assume that template loader changing and auto-imports and
> shared variable definitions should be executed once per Configuration
> instance?

Sorry, I couldn't fully decipher this sentence (assume VS should?).
But, last time I have described the rules as stricter than they are.
The real rule is that the Configuration must not be changed while
another operation (like template processing, or even template loading)
is in progress, also if you are using the configuration object from
multiple threads, you have to ensure that the changes made in one
thread will appear from the view point of other threads before it does
anything with the configuration (i.e., you will need to call
"synchronize" here and there). This might sounds complicated, but in
practice all that it translates to is that in a single-threaded
application you can change the Configuration between template
executions, but in a multi-thread application (like a Web application)
you should initialize the Configuration object once and forever, and
only then start using from multiple threads. But after all, using
proper synchronization, you can change the Configuration at a later
point in a multi-thread application as well... (I should add that
setting the templateLoader will erase the template cache. So it works,
but it's not free.)

>         I'm "reconfiguring" the same Configuration instance, with the *same*
> values, more than once. I intend to rework this code.

Overwriting something with the same value can be seen as changing the
object as well... yeah, that's a corner case, and certainly it happens
to work in reality, but the API doesn't guarantee that, so... I
wouldn't do it if I were you. Like, shared variables are backed by a
Map which is not synchronized... from there it depends on the Map
implementation employed internally wether it will glitch.

>        def loaders = [stringLoader, oldLoader]
>        def loader = new MultiTemplateLoader(loaders as TemplateLoader[])
>        configuration.setTemplateLoader(loader)
>
>        autoImport.each {
>         configuration.addAutoImport(it.key, it.value)
>        }
>
>        if (sharedVariables != null) {
>         def replacedSharedVars = new HashSet();
>         sharedVariables.entrySet().each {
>           entry ->
>             def sharedVar = configuration.getSharedVariable(entry.key)
>             if (sharedVar != entry.value) {     
>               replacedSharedVars << entry.key
>               configuration.setSharedVariable(entry.key, entry.value)
>             }
>         }
>        }
>      }
>
>
>
>         Thanks.
>
>         Best regards,
>
>                 Daniel.
>
> Daniel Dekany wrote:
>> I haven't read the source code, but if this is what you are unsure
>> about, the Configuration object must not be changed after it was
>> started to be used for loading/processing templates. If you need
>> different configurations for different templates, either you need
>> multiple configuration objects (which is might not be very efficient
>> as it means you will have two template caches), or you have to do the
>> necessary configuration setting changes in the Environment object
>> before each template processing.
>> 
>> If the above didn't answer what you are unsure about, tell me more.
>> 
>> 
>> Sunday, February 20, 2011, 1:29:24 PM, Daniel Henrique Alves Lima wrote:
>> 
>>>         Hi, everybody.
>>>
>>>         It is a question related to Grails FreeMarker Tags Plugin, Spring
>>> Framework and FreeMarker.
>>>         Just to be sure, the plugin should to execute the following steps once
>>> per Configuration instance:
>>>
>>> http://code.google.com/p/grails-freemarker-tags-plugin/source/browse/trunk/src/groovy/org/codehaus/groovy/grails/plugins/freemarker/AutoConfigHelper.groovy#173
>>>
>>>         Right?
>>>
>>>         The plugin is changing the template loader and it is adding 
>>> auto-imports or shared variables.
>>>         I think there is no need of executing these steps for every created View:
>>>
>>> http://code.google.com/p/grails-freemarker-tags-plugin/issues/detail?id=1#c12
>>>
>>>
>>>         Is there a way of preventing changes to a Configuration object?
>>>
>>>
>>>         Thanks!
>>>
>>>         Best regards,
>>>
>>>                 Daniel.

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.