|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectfreemarker.template.FileTemplateCache
public class 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.
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 the loading policy is set,
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.
A combination of the two above is LOAD_ON_DEMAND_WITH_REFRESH_CACHE.
This loads a template on demand the first time it is requested. Subsequently,
it will do periodic refreshes on any templates that have been requested from
its cache. This saves memory if some templates are unlikely to be needed,
since they won't occupy memory until they are requested, while improving
performance over LOAD_ON_DEMAND since it doesn't have to check the
file system every time a template is requested.
For maximum flexibility LOAD_AD_HOC mode exists so that all
templates are loaded when the loading policy is set but files are not
refreshed periodically. Instead, one can write a client that will ask the
FileTemplateCache to update a single template via the
Updateable.update(String) method.
Applications with a large number of templates many of which are not
frequently updated will work well with LOAD_AD_HOC mode.
The first argument to the getItem() 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
getItem("foo/index.html") to retrieve that template.
If a second argument is specified in a call to getItem(), this
will determine the type of object to be retrieved. The types that can be
returned depend on the TemplateRegistry. Three types of object
are registered with the TemplateRegistry by default:
template -- returns a Template object. This is the
default type to be retrieved by the cache.unparsed -- returns an UnparsedTemplate object. This
behaves like other Template objects, but its contents wont be
parsed by FM-Classic.binary -- returns a BinaryData object. This is
useful for caching other file types, such as images.
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 a loading policy is set.
TemplateCache,
CacheEvent,
CacheListener,
Updateable.update(String)| Field Summary | |
|---|---|
static int |
LOAD_AD_HOC
Used with setLoadingPolicy(int) to indicate that templates are
preloaded but there is no automatic updating of them. |
static int |
LOAD_ON_DEMAND
Used with setLoadingPolicy(int) to indicate that templates should be
loaded as they are requested. |
static int |
LOAD_ON_DEMAND_WITH_REFRESH_CACHE
Used with setLoadingPolicy(int) to indicate that templates should be
loaded as they are requested. |
static int |
NULL_CACHE
Used with setLoadingPolicy(int) to indicate that no files are cached. |
static int |
PRELOAD
Used with setLoadingPolicy(int) to indicate that templates should be
preloaded. |
| Constructor Summary | |
|---|---|
FileTemplateCache()
Constructs an empty FileTemplateCache. |
|
FileTemplateCache(java.io.File dir)
Constructs a FileTemplateCache with a directory in which it
will look for template files. |
|
FileTemplateCache(java.io.File dir,
long delay)
Constructs a FileTemplateCache 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 FileTemplateCache with a directory in which it
will look for template files. |
|
FileTemplateCache(java.lang.String path,
long delay)
Constructs a FileTemplateCache 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 Cache. |
CacheListener[] |
getCacheListeners()
Retrieves all the CacheListeners associated with this cache. |
java.lang.String |
getDefaultTemplate()
Retrieves the default template type to be created when retrieving items from the cache. |
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. |
Cacheable |
getItem(java.lang.String name)
Gets the specified template from the cache, using the default template type. |
Cacheable |
getItem(java.lang.String name,
java.lang.String type)
Gets the specified template type from the cache. |
int |
getLoadingPolicy()
Returns the loading policy currently in effect |
int |
getMaximumAge()
Retrieves the maximum age a cache item can be before it is evicted from the cache. |
java.lang.String |
getPath()
Returns the template cache root directory. |
CacheRetriever |
getRetriever()
Gets the CacheRetriever currently in
use. |
TemplateRegistry |
getTemplateRegistry()
Retrieves the current TemplateRegistry
in use. |
java.util.Iterator |
listCachedFiles()
Returns a list of cached files. |
void |
removeCacheListener(CacheListener listener)
Unregisters a CacheListener for a Cache. |
void |
setDefaultTemplate(java.lang.String aTemplateType)
Sets the default template type to be created when retrieving items from the cache. |
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 |
setMaximumAge(int age)
Sets the maximum age a cache item can be before it is evicted from the cache. |
void |
setPath(java.lang.String path)
Sets the template cache root directory. |
void |
setRetriever(CacheRetriever cRetriever)
Sets the CacheRetriever to be used for
the cache. |
void |
setTemplateRegistry(TemplateRegistry cRegistry)
Sets a TemplateRegistry implementation
to use when creating new templates. |
void |
stopAutoUpdate()
Stops the updating of the cache. |
java.lang.String |
toString()
Returns a string representation of the object. |
void |
update()
Updates the cache. |
void |
update(java.lang.String name)
Update a named template if in the LOAD_AD_HOC mode . |
void |
update(java.lang.String name,
java.lang.String type)
Update a named template if in the LOAD_AD_HOC mode. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int LOAD_ON_DEMAND
setLoadingPolicy(int) to indicate that templates should be
loaded as they are requested.
public static final int PRELOAD
setLoadingPolicy(int) to indicate that templates should be
preloaded.
public static final int LOAD_AD_HOC
setLoadingPolicy(int) to indicate that templates are
preloaded but there is no automatic updating of them. Instead, only named
templates are updated when the cache is requested to do so.
public static final int NULL_CACHE
setLoadingPolicy(int) to indicate that no files are cached.
public static final int LOAD_ON_DEMAND_WITH_REFRESH_CACHE
setLoadingPolicy(int) to indicate that templates should be
loaded as they are requested. Once loaded, they are periodically
refreshed as per the PRELOAD policy, rather than checked at each
request.
| Constructor Detail |
|---|
public FileTemplateCache()
FileTemplateCache.
public FileTemplateCache(java.lang.String path)
FileTemplateCache with a directory in which it
will look for template files.
path - the absolute path of the directory containing templates for
this cache.
java.lang.IllegalArgumentException - the root directory is nullpublic FileTemplateCache(java.io.File dir)
FileTemplateCache with a directory in which it
will look for template files.
dir - the directory containing templates for this cache.
java.lang.IllegalArgumentException - the root directory is null
public FileTemplateCache(java.lang.String path,
long delay)
FileTemplateCache with a directory in which it
will look for template files, and a delay representing the number of
seconds between cache updates.
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)
FileTemplateCache with a directory in which it
will look for template files, and a delay representing the number of
seconds between cache updates.
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)
Sets the loading policy for this FileTemplateCache. If
LOAD_ON_DEMAND, templates will be loaded as they are requested,
and each template's file modification date will be checked each time it
is requested. If PRELOAD, all templates in the cache directory
and its subdirectories will be loaded when the cache is started, and new
templates will be added to the cache each time it is updated. If
LOAD_AD_HOC, all templates in the cache directory and its
subdirectories will be loaded when the cache is created and a particular
template file's modification date will be checked each time the client
requests the update of that and only that template.
Defaults to LOAD_ON_DEMAND.
loadingPolicy - cache mode
java.lang.IllegalArgumentException - the caching policy is invalidpublic void setPath(java.lang.String path)
path - the absolute path of the directory containing templates for
this cache.
java.lang.IllegalArgumentException - the root directory is nullpublic java.lang.String getPath()
public void setDirectory(java.io.File dir)
dir - the root directory containing templates for this cache
java.lang.IllegalArgumentException - the root directory is nullpublic java.io.File getDirectory()
public void setDelay(long delay)
Sets the interval between two cache updates. This is meaningful only if
the cache policy is set to LOAD_ON_DEMAND,
LOAD_ON_DEMAND_WITH_REFRESH_CACHE or PRELOAD.
Defaults to five seconds.
delay - the number of seconds between cache updatespublic long getDelay()
LOAD_ON_DEMAND,
LOAD_ON_DEMAND_WITH_REFRESH_CACHE or PRELOAD.
public void setMaximumAge(int age)
Sets the maximum age a cache item can be before it is evicted from the
cache. The age is determined as the number of cache updates since the
item was last accessed. This is meaningful only if the cache policy is
set to LOAD_ON_DEMAND or
LOAD_ON_DEMAND_WITH_REFRESH_CACHE.
Defaults to never expiring.
age - the maximum age before an item is evicted from the cache, or 0
to indicate that items should never be evictedpublic int getMaximumAge()
LOAD_ON_DEMAND or
LOAD_ON_DEMAND_WITH_REFRESH_CACHE.
public void setEncoding(java.lang.String encoding)
null is specified, the default encoding will be used.
setEncoding in interface TextEncodingencoding - the name of the encoding to be used; this will be passed to
the constructor of InputStreamReader.public java.lang.String getEncoding()
null is returned, the default encoding is used.
getEncoding in interface TextEncodingInputStreamReader.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()
public void addCacheListener(CacheListener listener)
CacheListener for this Cache.
addCacheListener in interface Cachelistener - the CacheListener to be registered.CacheListenerpublic void removeCacheListener(CacheListener listener)
CacheListener for a Cache.
removeCacheListener in interface Cachelistener - the CacheListener to be unregistered.CacheListenerpublic CacheListener[] getCacheListeners()
CacheListeners associated with this cache.
getCacheListeners in interface CacheCacheListenerspublic void stopAutoUpdate()
stopAutoUpdate in interface Cachepublic java.util.Iterator listCachedFiles()
listCachedFiles in interface Cachepublic void update(java.lang.String name)
LOAD_AD_HOC mode . Do nothing
if in other modes.
update in interface Updateablename - of template to update
public void update(java.lang.String name,
java.lang.String type)
LOAD_AD_HOC mode. Do nothing
if in other modes.
update in interface Updateablename - the name of template to updatetype - the type of template to updatepublic void update()
LOAD_AD_HOC mode, this does nothing.
update in interface Updateablepublic Cacheable getItem(java.lang.String name)
getItem in interface Cachename - a string uniquely identifying the template.
null if
not found.setDefaultTemplate(java.lang.String)
public Cacheable getItem(java.lang.String name,
java.lang.String type)
getItem in interface Cachename - a string uniquely identifying the template.type - the type of template to be retrieved
null if
not found.setDefaultTemplate(java.lang.String)public CacheRetriever getRetriever()
CacheRetriever currently in
use.
CacheRetriever used for retrieving templatespublic void setRetriever(CacheRetriever cRetriever)
CacheRetriever to be used for
the cache.
cRetriever - the CacheRetriever to be used for retrieving
templatespublic TemplateRegistry getTemplateRegistry()
TemplateRegistry
in use.
getTemplateRegistry in interface RegistryAccepterTemplateRegistry to used when retrieving items
to be cachedpublic void setTemplateRegistry(TemplateRegistry cRegistry)
TemplateRegistry implementation
to use when creating new templates.
setTemplateRegistry in interface RegistryAcceptercRegistry - the TemplateRegistry to be used when retrieving
items to be cachedpublic java.lang.String getDefaultTemplate()
public void setDefaultTemplate(java.lang.String aTemplateType)
TemplateRegistry. Three types of object
are registered with the TemplateRegistry by default:
template -- returns a Template object.unparsed -- returns an UnparsedTemplate object.
This behaves like other Template objects, but its contents
wont be parsed by FM-Classic.binary -- returns a BinaryData object. This is
useful for caching other file types, such as images.
aTemplateType - the type of template to be cached by defaultpublic java.lang.String toString()
toString in class java.lang.Object
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||