public class TemplateCache
extends java.lang.Object
TemplateLoader
that you can specify in the constructor.
Some aspects of caching is delegated to a CacheStorage
that you can also specify in the constructor.
Typically you don't instantiate or otherwise use this class directly. The Configuration
embeds an
instance of this class, that you access indirectly through Configuration.getTemplate(String)
and other
Configuration
API-s. Then TemplateLoader
and CacheStorage
can be set with
Configuration.setTemplateLoader(TemplateLoader)
and
Configuration.setCacheStorage(CacheStorage)
.
Modifier and Type | Class and Description |
---|---|
static class |
TemplateCache.MaybeMissingTemplate
Used for the return value of
getTemplate(String, Locale, Object, String, boolean) . |
Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_TEMPLATE_UPDATE_DELAY_MILLIS
The default template update delay; see
Configuration.setTemplateUpdateDelayMilliseconds(long) . |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all entries from the cache, forcing reloading of templates
on subsequent
getTemplate(String, Locale, String, boolean)
calls. |
protected static TemplateLoader |
createLegacyDefaultTemplateLoader()
Deprecated.
The
TemplateLoader should be always specified by the constructor caller. |
CacheStorage |
getCacheStorage() |
long |
getDelay()
Gets the delay in milliseconds between checking for newer versions of a
template source.
|
static java.lang.String |
getFullTemplatePath(Environment env,
java.lang.String baseName,
java.lang.String targetName)
Deprecated.
Use
Environment.toFullTemplateName(String, String) instead, as that can throw
MalformedTemplateNameException , and is on a more logical place anyway. |
boolean |
getLocalizedLookup()
Returns if localized template lookup is enabled or not.
|
TemplateCache.MaybeMissingTemplate |
getTemplate(java.lang.String name,
java.util.Locale locale,
java.lang.Object customLookupCondition,
java.lang.String encoding,
boolean parseAsFTL)
Retrieves the template with the given name (and according the specified further parameters) from the template
cache, loading it into the cache first if it's missing/staled.
|
Template |
getTemplate(java.lang.String name,
java.util.Locale locale,
java.lang.String encoding,
boolean parseAsFTL)
Deprecated.
Use
getTemplate(String, Locale, Object, String, boolean) , which can return more detailed
result when the template is missing. |
TemplateConfigurationFactory |
getTemplateConfigurations() |
TemplateLoader |
getTemplateLoader() |
TemplateLookupStrategy |
getTemplateLookupStrategy() |
TemplateNameFormat |
getTemplateNameFormat() |
void |
removeTemplate(java.lang.String name,
java.util.Locale locale,
java.lang.Object customLookupCondition,
java.lang.String encoding,
boolean parse)
Removes an entry from the cache, hence forcing the re-loading of it when it's next time requested.
|
void |
removeTemplate(java.lang.String name,
java.util.Locale locale,
java.lang.String encoding,
boolean parse)
|
void |
setConfiguration(Configuration config)
Deprecated.
Use the
TemplateCache(TemplateLoader, CacheStorage, Configuration) constructor. |
void |
setDelay(long delay)
Sets the delay in milliseconds between checking for newer versions of a
template sources.
|
void |
setLocalizedLookup(boolean localizedLookup)
Setis if localized template lookup is enabled or not.
|
public static final long DEFAULT_TEMPLATE_UPDATE_DELAY_MILLIS
Configuration.setTemplateUpdateDelayMilliseconds(long)
.@Deprecated public TemplateCache()
TemplateCache(TemplateLoader)
instead. The default loader is useless in most
applications, also it can mean a security risk.user.dir
), then from the classpath.@Deprecated public TemplateCache(TemplateLoader templateLoader)
TemplateCache(TemplateLoader, CacheStorage, Configuration)
instead.@Deprecated public TemplateCache(TemplateLoader templateLoader, CacheStorage cacheStorage)
TemplateCache(TemplateLoader, CacheStorage, Configuration)
instead.public TemplateCache(TemplateLoader templateLoader, Configuration config)
TemplateCache(TemplateLoader, CacheStorage, Configuration)
with a new SoftCacheStorage
as the 2nd parameter.public TemplateCache(TemplateLoader templateLoader, CacheStorage cacheStorage, Configuration config)
TemplateCache(TemplateLoader, CacheStorage, TemplateLookupStrategy, TemplateNameFormat, Configuration)
with TemplateLookupStrategy.DEFAULT_2_3_0
and TemplateNameFormat.DEFAULT_2_3_0
.public TemplateCache(TemplateLoader templateLoader, CacheStorage cacheStorage, TemplateLookupStrategy templateLookupStrategy, TemplateNameFormat templateNameFormat, Configuration config)
TemplateCache(TemplateLoader, CacheStorage, TemplateLookupStrategy, TemplateNameFormat,
TemplateConfigurationFactory, Configuration)
with null
for templateConfigurations
-s.public TemplateCache(TemplateLoader templateLoader, CacheStorage cacheStorage, TemplateLookupStrategy templateLookupStrategy, TemplateNameFormat templateNameFormat, TemplateConfigurationFactory templateConfigurations, Configuration config)
templateLoader
- The TemplateLoader
to use. Can be null
, though then every request will result in
TemplateNotFoundException
.cacheStorage
- The CacheStorage
to use. Can't be null
.templateLookupStrategy
- The TemplateLookupStrategy
to use. Can't be null
.templateNameFormat
- The TemplateNameFormat
to use. Can't be null
.templateConfigurations
- The TemplateConfigurationFactory
to use. Can be null
(then all templates will use the
settings coming from the Configuration
as is).config
- The Configuration
this cache will be used for. Can be null
for backward compatibility,
as it can be set with setConfiguration(Configuration)
later.@Deprecated public void setConfiguration(Configuration config)
TemplateCache(TemplateLoader, CacheStorage, Configuration)
constructor.public TemplateLoader getTemplateLoader()
public CacheStorage getCacheStorage()
public TemplateLookupStrategy getTemplateLookupStrategy()
public TemplateNameFormat getTemplateNameFormat()
public TemplateConfigurationFactory getTemplateConfigurations()
public TemplateCache.MaybeMissingTemplate getTemplate(java.lang.String name, java.util.Locale locale, java.lang.Object customLookupCondition, java.lang.String encoding, boolean parseAsFTL) throws java.io.IOException
All parameters must be non-null
, except customLookupCondition
. For the meaning of the parameters
see Configuration.getTemplate(String, Locale, String, boolean)
.
TemplateCache.MaybeMissingTemplate
object that contains the Template
, or a
TemplateCache.MaybeMissingTemplate
object that contains null
as the Template
and information
about the missing template. The return value itself is never null
. Note that exceptions occurring
during template loading will not be classified as a missing template, so they will cause an exception to
be thrown by this method instead of returning a TemplateCache.MaybeMissingTemplate
. The idea is that having a
missing template is normal (not exceptional), providing that the backing storage mechanism could indeed
check that it's missing.MalformedTemplateNameException
- If the name
was malformed according the current TemplateNameFormat
. However, if the
TemplateNameFormat
is TemplateNameFormat.DEFAULT_2_3_0
and
Configuration.getIncompatibleImprovements()
is less than 2.4.0, then instead of throwing this
exception, a TemplateCache.MaybeMissingTemplate
will be returned, similarly as if the template were missing
(the TemplateCache.MaybeMissingTemplate.getMissingTemplateReason()
will describe the real error).java.io.IOException
- If reading the template has failed from a reason other than the template is missing. This method
should never be a TemplateNotFoundException
, as that condition is indicated in the return
value.@Deprecated public Template getTemplate(java.lang.String name, java.util.Locale locale, java.lang.String encoding, boolean parseAsFTL) throws java.io.IOException
getTemplate(String, Locale, Object, String, boolean)
, which can return more detailed
result when the template is missing.TemplateCache.MaybeMissingTemplate.getTemplate()
of the
getTemplate(String, Locale, Object, String, boolean)
return value.java.io.IOException
@Deprecated protected static TemplateLoader createLegacyDefaultTemplateLoader()
TemplateLoader
should be always specified by the constructor caller.public long getDelay()
public void setDelay(long delay)
delay
- the new value of the delaypublic boolean getLocalizedLookup()
public void setLocalizedLookup(boolean localizedLookup)
public void clear()
getTemplate(String, Locale, String, boolean)
calls. If the configured template loader is
stateful
, then its
StatefulTemplateLoader.resetState()
method is invoked as well.public void removeTemplate(java.lang.String name, java.util.Locale locale, java.lang.String encoding, boolean parse) throws java.io.IOException
java.io.IOException
public void removeTemplate(java.lang.String name, java.util.Locale locale, java.lang.Object customLookupCondition, java.lang.String encoding, boolean parse) throws java.io.IOException
setDelay(long)
alone does.
For the meaning of the parameters, see
Configuration.getTemplate(String, Locale, Object, String, boolean, boolean)
java.io.IOException
@Deprecated public static java.lang.String getFullTemplatePath(Environment env, java.lang.String baseName, java.lang.String targetName)
Environment.toFullTemplateName(String, String)
instead, as that can throw
MalformedTemplateNameException
, and is on a more logical place anyway.java.lang.IllegalArgumentException
- If the baseName
or targetName
is malformed according the TemplateNameFormat
in use.