|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--freemarker.template.FileTemplateCache
A TemplateCache that loads templates from a filesystem. Given a directory path, the cache assumes by default that all files in the directory are templates. It can optionally be given a filename suffix for templates.
Before using a FileTemplateCache, you must call its startAutoUpdate() method to begin periodic updates. The default loading policy is LOAD_ON_DEMAND: templates are loaded into the cache only when requested, each template's modification date is checked each time it is requested, and the periodic updates are used only to remove deleted templates from the cache. If the loading policy is set to PRELOAD, all templates are loaded when startAutoUpdate() is called, and all files are checked during each periodic update. If template files will not be changed frequently, use PRELOAD with a long delay value for maximum performance.
For maximum flexibility LOAD_AD_HOC mode exists so that all templates are loaded when startAutoUpdate is called but files are not refreshed periodically. Instead, one can write a client that will ask the FileTemplateCache to update a single template via the Updatable#update(String name) method. Applications with a large number of templates many of which are not frequently updated will work well with LOAD_AD_HOC mode. Since templates are updated 'ad hoc' in this mode rather than periodically calling startAutoUpdate() is not required.
The string argument to the getTemplate() method is interpreted as the template's path relative to the cache's root directory, using a forward slash (/) as a separator (this is to facilitate using URL path info to request templates). For example, if a TemplateCache object was made for the directory templates, which contains a subdirectory foo, in which there is a template file called index.html, you would call getTemplate("foo/index.html") to retrieve that template.
The owner of the cache should implement the CacheListener interface and register itself using addCacheListener().
If the template cannot read its cache directory, the periodic updates will be cancelled until the next time startAutoUpdate() is called.
TemplateCache,
CacheListener,
Updatable.update(String)| Field Summary | |
protected java.io.File |
cacheDir
|
static int |
LOAD_AD_HOC
Used with setLoadingPolicy() to indicate that templates are preloaded but there is no automatic updating of them. |
static int |
LOAD_ON_DEMAND
Used with setLoadingPolicy() to indicate that templates should be loaded as they are requested. |
static int |
PRELOAD
Used with setLoadingPolicy() to indicate that templates should be preloaded. |
| Constructor Summary | |
FileTemplateCache()
Constructs an empty FileTemplateCache. |
|
FileTemplateCache(java.io.File dir)
Constructs a TemplateCache with a directory in which it will look for template files. |
|
FileTemplateCache(java.io.File dir,
long delay)
Constructs a TemplateCache with a directory in which it will look for template files, and a delay representing the number of seconds between cache updates. |
|
FileTemplateCache(java.lang.String path)
Constructs a TemplateCache with a directory in which it will look for template files. |
|
FileTemplateCache(java.lang.String path,
long delay)
Constructs a TemplateCache with a directory in which it will look for template files, and a delay representing the number of seconds between cache updates. |
|
| Method Summary | |
void |
addCacheListener(CacheListener listener)
Registers a CacheListener for this TemplateCache. |
long |
getDelay()
Returns the interval between two cache updates. |
java.io.File |
getDirectory()
Returns the template cache root directory |
java.lang.String |
getEncoding()
Returns the character encoding to be used when reading template files. |
java.lang.String |
getFilenameSuffix()
Returns the template suffix. |
int |
getLoadingPolicy()
Returns the loading policy currently in effect |
java.lang.String |
getPath()
Returns the template cache root directory |
Template |
getTemplate(java.lang.String name)
Gets a template from the cache |
java.util.Iterator |
listCachedFiles()
Returns a list of cached files |
void |
removeCacheListener(CacheListener listener)
Unregisters a CacheListener for this TemplateCache. |
void |
setDelay(long delay)
Sets the interval between two cache updates. |
void |
setDirectory(java.io.File dir)
Sets the template cache root directory |
void |
setEncoding(java.lang.String encoding)
Sets the character encoding to be used when reading template files. |
void |
setFilenameSuffix(java.lang.String filenameSuffix)
Sets the template suffix. |
void |
setLoadingPolicy(int loadingPolicy)
Sets the loading policy for this FileTemplateCache. |
void |
setPath(java.lang.String path)
Sets the template cache root directory |
void |
startAutoUpdate()
Note: startAutoUpdate() is deprecated. |
void |
stopAutoUpdate()
Note: startAutoUpdate() is deprecated. |
void |
update()
Updates the cache. |
void |
update(java.lang.String name)
Update a named template if in the LOAD_AD_HOC mode Do nothing if in other modes |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public static final int LOAD_ON_DEMAND
public static final int PRELOAD
public static final int LOAD_AD_HOC
protected java.io.File cacheDir
| Constructor Detail |
public FileTemplateCache()
public FileTemplateCache(java.lang.String path)
path - the absolute path of the directory containing
templates for this cache.public FileTemplateCache(java.io.File dir)
dir - the directory containing templates for this cache.
public FileTemplateCache(java.lang.String path,
long delay)
path - the absolute path of the directory containing
templates for this cache.delay - the number of seconds between cache updates.
public FileTemplateCache(java.io.File dir,
long delay)
dir - the directory containing templates for this cache.delay - the number of seconds between cache updates.| Method Detail |
public int getLoadingPolicy()
public void setLoadingPolicy(int loadingPolicy)
loadingPolicy - cache modepublic void setPath(java.lang.String path)
path - the absolute path of the directory containing
templates for this cache.public java.lang.String getPath()
public void setDirectory(java.io.File dir)
dir - the root directory containing templates for this cachepublic java.io.File getDirectory()
public void setDelay(long delay)
delay - the number of seconds between cache updatespublic long getDelay()
public void setEncoding(java.lang.String encoding)
encoding - the name of the encoding to be used; this will be
passed to the constructor of InputStreamReader.public java.lang.String getEncoding()
public void setFilenameSuffix(java.lang.String filenameSuffix)
filenameSuffix - the optional filename suffix of template
files to be read for this cache.public java.lang.String getFilenameSuffix()
the - optional filename suffix of template
files to be read for this cache.public void addCacheListener(CacheListener listener)
TemplateCacheaddCacheListener in interface TemplateCachefreemarker.template.TemplateCacheCacheListener - the CacheListener to be registered.CacheListenerpublic void removeCacheListener(CacheListener listener)
TemplateCacheremoveCacheListener in interface TemplateCachefreemarker.template.TemplateCacheCacheListener - the CacheListener to be unregistered.CacheListenerpublic Template getTemplate(java.lang.String name)
getTemplate in interface TemplateCachename - the template's filename, including its
path relative to the cache's root directory.public void startAutoUpdate()
TemplateCacheBegins automatic updates of the cache.
startAutoUpdate in interface TemplateCachepublic void stopAutoUpdate()
TemplateCacheStops automatically updating the cache.
stopAutoUpdate in interface TemplateCachepublic java.util.Iterator listCachedFiles()
listCachedFiles in interface TemplateCachepublic void update(java.lang.String name)
update in interface Updatablename - of template to updatepublic void update()
update in interface Updatable
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||