Skip to content

Commit 0ed1a21

Browse files
committed
FreeMarkerConfigurationFactory properly supports TemplateLoaders when recreating Configurations
Issue: SPR-9389
1 parent bad2b90 commit 0ed1a21

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

org.springframework.context.support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
23+
import java.util.LinkedList;
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Properties;
@@ -47,12 +48,12 @@
4748
* <p>The optional "configLocation" property sets the location of a FreeMarker
4849
* properties file, within the current application. FreeMarker properties can be
4950
* overridden via "freemarkerSettings". All of these properties will be set by
50-
* calling FreeMarker's <code>Configuration.setSettings()</code> method and are
51+
* calling FreeMarker's {@code Configuration.setSettings()} method and are
5152
* subject to constraints set by FreeMarker.
5253
*
5354
* <p>The "freemarkerVariables" property can be used to specify a Map of
5455
* shared variables that will be applied to the Configuration via the
55-
* <code>setAllSharedVariables()</code> method. Like <code>setSettings()</code>,
56+
* {@code setAllSharedVariables()} method. Like {@code setSettings()},
5657
* these entries are subject to FreeMarker constraints.
5758
*
5859
* <p>The simplest way to use this class is to specify a "templateLoaderPath";
@@ -109,7 +110,7 @@ public void setConfigLocation(Resource resource) {
109110

110111
/**
111112
* Set properties that contain well-known FreeMarker keys which will be
112-
* passed to FreeMarker's <code>Configuration.setSettings</code> method.
113+
* passed to FreeMarker's {@code Configuration.setSettings} method.
113114
* @see freemarker.template.Configuration#setSettings
114115
*/
115116
public void setFreemarkerSettings(Properties settings) {
@@ -118,7 +119,7 @@ public void setFreemarkerSettings(Properties settings) {
118119

119120
/**
120121
* Set a Map that contains well-known FreeMarker objects which will be passed
121-
* to FreeMarker's <code>Configuration.setAllSharedVariables()</code> method.
122+
* to FreeMarker's {@code Configuration.setAllSharedVariables()} method.
122123
* @see freemarker.template.Configuration#setAllSharedVariables
123124
*/
124125
public void setFreemarkerVariables(Map<String, Object> variables) {
@@ -138,7 +139,7 @@ public void setDefaultEncoding(String defaultEncoding) {
138139
}
139140

140141
/**
141-
* Set a List of <code>TemplateLoader<code>s that will be used to search
142+
* Set a List of {@code TemplateLoader}s that will be used to search
142143
* for templates. For example, one or more custom loaders such as database
143144
* loaders could be configured and injected here.
144145
* @deprecated as of Spring 2.0.1, in favor of the "preTemplateLoaders"
@@ -147,14 +148,14 @@ public void setDefaultEncoding(String defaultEncoding) {
147148
* @see #setPostTemplateLoaders
148149
*/
149150
@Deprecated
150-
public void setTemplateLoaders(TemplateLoader[] templateLoaders) {
151+
public void setTemplateLoaders(TemplateLoader... templateLoaders) {
151152
if (templateLoaders != null) {
152153
this.templateLoaders.addAll(Arrays.asList(templateLoaders));
153154
}
154155
}
155156

156157
/**
157-
* Set a List of <code>TemplateLoader<code>s that will be used to search
158+
* Set a List of {@code TemplateLoader}s that will be used to search
158159
* for templates. For example, one or more custom loaders such as database
159160
* loaders could be configured and injected here.
160161
* <p>The {@link TemplateLoader TemplateLoaders} specified here will be
@@ -164,12 +165,12 @@ public void setTemplateLoaders(TemplateLoader[] templateLoaders) {
164165
* @see #setTemplateLoaderPaths
165166
* @see #postProcessTemplateLoaders
166167
*/
167-
public void setPreTemplateLoaders(TemplateLoader[] preTemplateLoaders) {
168+
public void setPreTemplateLoaders(TemplateLoader... preTemplateLoaders) {
168169
this.preTemplateLoaders = Arrays.asList(preTemplateLoaders);
169170
}
170171

171172
/**
172-
* Set a List of <code>TemplateLoader<code>s that will be used to search
173+
* Set a List of {@code TemplateLoader}s that will be used to search
173174
* for templates. For example, one or more custom loaders such as database
174175
* loaders can be configured.
175176
* <p>The {@link TemplateLoader TemplateLoaders} specified here will be
@@ -179,7 +180,7 @@ public void setPreTemplateLoaders(TemplateLoader[] preTemplateLoaders) {
179180
* @see #setTemplateLoaderPaths
180181
* @see #postProcessTemplateLoaders
181182
*/
182-
public void setPostTemplateLoaders(TemplateLoader[] postTemplateLoaders) {
183+
public void setPostTemplateLoaders(TemplateLoader... postTemplateLoaders) {
183184
this.postTemplateLoaders = Arrays.asList(postTemplateLoaders);
184185
}
185186

@@ -198,20 +199,20 @@ public void setTemplateLoaderPath(String templateLoaderPath) {
198199
* pseudo URLs are supported, as understood by ResourceEditor. Allows for
199200
* relative paths when running in an ApplicationContext.
200201
* <p>Will define a path for the default FreeMarker template loader.
201-
* If a specified resource cannot be resolved to a <code>java.io.File</code>,
202+
* If a specified resource cannot be resolved to a {@code java.io.File},
202203
* a generic SpringTemplateLoader will be used, without modification detection.
203204
* <p>To enforce the use of SpringTemplateLoader, i.e. to not resolve a path
204205
* as file system resource in any case, turn off the "preferFileSystemAccess"
205206
* flag. See the latter's javadoc for details.
206207
* <p>If you wish to specify your own list of TemplateLoaders, do not set this
207-
* property and instead use <code>setTemplateLoaders(List templateLoaders)</code>
208+
* property and instead use {@code setTemplateLoaders(List templateLoaders)}
208209
* @see org.springframework.core.io.ResourceEditor
209210
* @see org.springframework.context.ApplicationContext#getResource
210211
* @see freemarker.template.Configuration#setDirectoryForTemplateLoading
211212
* @see SpringTemplateLoader
212213
* @see #setTemplateLoaders
213214
*/
214-
public void setTemplateLoaderPaths(String[] templateLoaderPaths) {
215+
public void setTemplateLoaderPaths(String... templateLoaderPaths) {
215216
this.templateLoaderPaths = templateLoaderPaths;
216217
}
217218

@@ -229,7 +230,7 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
229230
* Return the Spring ResourceLoader to use for loading FreeMarker template files.
230231
*/
231232
protected ResourceLoader getResourceLoader() {
232-
return resourceLoader;
233+
return this.resourceLoader;
233234
}
234235

235236
/**
@@ -252,7 +253,7 @@ public void setPreferFileSystemAccess(boolean preferFileSystemAccess) {
252253
* Return whether to prefer file system access for template loading.
253254
*/
254255
protected boolean isPreferFileSystemAccess() {
255-
return preferFileSystemAccess;
256+
return this.preferFileSystemAccess;
256257
}
257258

258259

@@ -293,25 +294,27 @@ public Configuration createConfiguration() throws IOException, TemplateException
293294
config.setDefaultEncoding(this.defaultEncoding);
294295
}
295296

297+
List<TemplateLoader> templateLoaders = new LinkedList<TemplateLoader>(this.templateLoaders);
298+
296299
// Register template loaders that are supposed to kick in early.
297300
if (this.preTemplateLoaders != null) {
298-
this.templateLoaders.addAll(this.preTemplateLoaders);
301+
templateLoaders.addAll(this.preTemplateLoaders);
299302
}
300303

301304
// Register default template loaders.
302305
if (this.templateLoaderPaths != null) {
303306
for (String path : this.templateLoaderPaths) {
304-
this.templateLoaders.add(getTemplateLoaderForPath(path));
307+
templateLoaders.add(getTemplateLoaderForPath(path));
305308
}
306309
}
307-
postProcessTemplateLoaders(this.templateLoaders);
310+
postProcessTemplateLoaders(templateLoaders);
308311

309312
// Register template loaders that are supposed to kick in late.
310313
if (this.postTemplateLoaders != null) {
311-
this.templateLoaders.addAll(this.postTemplateLoaders);
314+
templateLoaders.addAll(this.postTemplateLoaders);
312315
}
313316

314-
TemplateLoader loader = getAggregateTemplateLoader(this.templateLoaders);
317+
TemplateLoader loader = getAggregateTemplateLoader(templateLoaders);
315318
if (loader != null) {
316319
config.setTemplateLoader(loader);
317320
}
@@ -323,7 +326,7 @@ public Configuration createConfiguration() throws IOException, TemplateException
323326
/**
324327
* Return a new Configuration object. Subclasses can override this for
325328
* custom initialization, or for using a mock object for testing.
326-
* <p>Called by <code>createConfiguration()</code>.
329+
* <p>Called by {@code createConfiguration()}.
327330
* @return the Configuration object
328331
* @throws IOException if a config file wasn't found
329332
* @throws TemplateException on FreeMarker initialization failure
@@ -374,7 +377,7 @@ protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) {
374377
* To be overridden by subclasses that want to to register custom
375378
* TemplateLoader instances after this factory created its default
376379
* template loaders.
377-
* <p>Called by <code>createConfiguration()</code>. Note that specified
380+
* <p>Called by {@code createConfiguration()}. Note that specified
378381
* "postTemplateLoaders" will be registered <i>after</i> any loaders
379382
* registered by this callback; as a consequence, they are are <i>not</i>
380383
* included in the given List.
@@ -411,7 +414,7 @@ protected TemplateLoader getAggregateTemplateLoader(List<TemplateLoader> templat
411414
* To be overridden by subclasses that want to to perform custom
412415
* post-processing of the Configuration object after this factory
413416
* performed its default initialization.
414-
* <p>Called by <code>createConfiguration()</code>.
417+
* <p>Called by {@code createConfiguration()}.
415418
* @param config the current Configuration object
416419
* @throws IOException if a config file wasn't found
417420
* @throws TemplateException on FreeMarker initialization failure

0 commit comments

Comments
 (0)