Class DelegatingLoader
- java.lang.Object
-
- io.pebbletemplates.pebble.loader.DelegatingLoader
-
- All Implemented Interfaces:
Loader<DelegatingLoaderCacheKey>
public class DelegatingLoader extends java.lang.Object implements Loader<DelegatingLoaderCacheKey>
This loader will delegate control to a list of children loaders. This is the default implementation used by Pebble; it delegates to a classpath loader and a file loader to increase the chances of finding templates with varying setups.
-
-
Constructor Summary
Constructors Constructor Description DelegatingLoader(java.util.List<Loader<?>> loaders)
Constructor provided with a list of children loaders.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DelegatingLoaderCacheKey
createCacheKey(java.lang.String templateName)
This method resolves the given template name to a unique object which can be used as the key within thePebbleEngine.getTemplateCache()
.java.lang.String
getCharset()
java.lang.String
getPrefix()
java.io.Reader
getReader(DelegatingLoaderCacheKey cacheKey)
The reader which will be used by Pebble to read the contents of the template.private <T> java.io.Reader
getReaderInner(Loader<T> delegatingLoader, java.lang.Object cacheKey)
java.lang.String
getSuffix()
java.lang.String
resolveRelativePath(java.lang.String relativePath, java.lang.String anchorPath)
Resolves the givenrelativePath
based on the givenanchorPath
.boolean
resourceExists(java.lang.String templateName)
void
setCharset(java.lang.String charset)
A method for end users to change the charset used by the loader.void
setPrefix(java.lang.String prefix)
Optional prefix to help find templates, ex "/WEB-INF/templates/" or "database_schema."void
setSuffix(java.lang.String suffix)
Optional suffix to help find templates, ex ".html", ".peb"
-
-
-
Field Detail
-
prefix
private java.lang.String prefix
-
suffix
private java.lang.String suffix
-
charset
private java.lang.String charset
-
loaders
private final java.util.List<Loader<?>> loaders
Children loaders to delegate to. The loaders are used in order and as soon as one of them finds a template, the others will not be given a chance to do so.
-
-
Constructor Detail
-
DelegatingLoader
public DelegatingLoader(java.util.List<Loader<?>> loaders)
Constructor provided with a list of children loaders.- Parameters:
loaders
- A list of loaders to delegate to
-
-
Method Detail
-
getReader
public java.io.Reader getReader(DelegatingLoaderCacheKey cacheKey)
Description copied from interface:Loader
The reader which will be used by Pebble to read the contents of the template.- Specified by:
getReader
in interfaceLoader<DelegatingLoaderCacheKey>
- Parameters:
cacheKey
- the cache key to use to load create the reader.- Returns:
- A reader object
-
getReaderInner
private <T> java.io.Reader getReaderInner(Loader<T> delegatingLoader, java.lang.Object cacheKey)
-
getSuffix
public java.lang.String getSuffix()
-
setSuffix
public void setSuffix(java.lang.String suffix)
Description copied from interface:Loader
Optional suffix to help find templates, ex ".html", ".peb"- Specified by:
setSuffix
in interfaceLoader<DelegatingLoaderCacheKey>
- Parameters:
suffix
- Suffix to attach to template names
-
getPrefix
public java.lang.String getPrefix()
-
setPrefix
public void setPrefix(java.lang.String prefix)
Description copied from interface:Loader
Optional prefix to help find templates, ex "/WEB-INF/templates/" or "database_schema."- Specified by:
setPrefix
in interfaceLoader<DelegatingLoaderCacheKey>
- Parameters:
prefix
- Prefix to help find templates
-
getCharset
public java.lang.String getCharset()
-
setCharset
public void setCharset(java.lang.String charset)
Description copied from interface:Loader
A method for end users to change the charset used by the loader.- Specified by:
setCharset
in interfaceLoader<DelegatingLoaderCacheKey>
- Parameters:
charset
- Character set used by the loader when building a reader object
-
resolveRelativePath
public java.lang.String resolveRelativePath(java.lang.String relativePath, java.lang.String anchorPath)
Description copied from interface:Loader
Resolves the givenrelativePath
based on the givenanchorPath
.A path is considered as relative when it starts either with '..' or '.' and followed either by a '/' or '\\' otherwise the assumption is that the provided path is an absolute path.
- Specified by:
resolveRelativePath
in interfaceLoader<DelegatingLoaderCacheKey>
- Parameters:
relativePath
- the relative path which should be resolved.anchorPath
- the anchor path based on which the relative path should be resolved on.- Returns:
- the resolved path or
null
when the path could not be resolved.
-
createCacheKey
public DelegatingLoaderCacheKey createCacheKey(java.lang.String templateName)
Description copied from interface:Loader
This method resolves the given template name to a unique object which can be used as the key within thePebbleEngine.getTemplateCache()
. The returned object will be passed withLoader.getReader(Object)
.The resolve method can eventually add information to the cache key from the context (e.g. user session information, servlet request etc.).
As a concrete example if the loader loads a template created by a user form the database the template name itself is not uniquely identify the template. The identification of the template requires also the user which created the template. Hence for the key the user id and the template name should be used. So the cache key is enhanced by some contextual information.
The implementor of the method can add as many additional contextual information to the returned object. However the following things needs to be considered:
- This method will be called on each
PebbleEngine.getTemplate(String)
. Hence the implementation needs to be fast and eventually use some caching for the lookup process. - The returned object is used within a cache and hence needs to
implement
Object.equals(Object)
andObject.hashCode()
. - The object is kept in memory and hence it should not be to memory heavy.
Depending on this implementation the
PebbleEngine.getTemplateCache()
should be tuned in a way it can operate optimal. E.g. when the number of potential templates is infinite the cache should evict some templates at some point in time otherwise the stability of the memory is not given anymore.- Specified by:
createCacheKey
in interfaceLoader<DelegatingLoaderCacheKey>
- Parameters:
templateName
- The name of the template- Returns:
- Returns the cache key
- This method will be called on each
-
resourceExists
public boolean resourceExists(java.lang.String templateName)
- Specified by:
resourceExists
in interfaceLoader<DelegatingLoaderCacheKey>
-
-