Class StringResourceLoader
- Direct Known Subclasses:
VelocityScriptEngine.SingleResourceReader
StringResourceRepositoryImpl as the default.
resource.loaders = string resource.loader.string.description = Velocity StringResource loader resource.loader.string.class = org.apache.velocity.runtime.resource.loader.StringResourceLoader resource.loader.string.repository.name = MyRepositoryName (optional, to avoid using the default repository) resource.loader.string.repository.class = org.apache.velocity.runtime.resource.loader.StringResourceRepositoryImplResources can be added to the repository like this:
StringResourceRepository repo = StringResourceLoader.getRepository();
String myTemplateName = "/some/imaginary/path/hello.vm";
String myTemplate = "Hi, ${username}... this is some template!";
repo.putStringResource(myTemplateName, myTemplate);
After this, the templates can be retrieved as usual.
If there will be multiple StringResourceLoaders used in an application, you should consider specifying a 'resource.loader.string.repository.name = foo' property in order to keep you string resources in a non-default repository. This can help to avoid conflicts between different frameworks or components that are using StringResourceLoader. You can then retrieve your named repository like this:
StringResourceRepository repo = StringResourceLoader.getRepository("foo");
and add string resources to the repo just as in the previous example.
If you have concerns about memory leaks or for whatever reason do not wish to have your string repository stored statically as a class member, then you should set 'resource.loader.string.repository.static = false' in your properties. This will tell the resource loader that the string repository should be stored in the Velocity application attributes. To retrieve the repository, do:
StringResourceRepository repo = velocityEngine.getApplicationAttribute("foo");
If you did not specify a name for the repository, then it will be stored under the class name of the repository implementation class (for which the default is 'org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl'). Incidentally, this is also true for the default statically stored repository.
Whether your repository is stored statically or in Velocity's application attributes, you can also manually create and set it prior to Velocity initialization. For a static repository, you can do something like this:
StringResourceRepository repo = new MyStringResourceRepository();
repo.magicallyAddSomeStringResources();
StringResourceLoader.setRepository("foo", repo);
Or for a non-static repository:
StringResourceRepository repo = new MyStringResourceRepository();
repo.magicallyAddSomeStringResources();
velocityEngine.setApplicationAttribute("foo", repo);
Then, assuming the 'resource.loader.string.repository.name' property is set to 'some.name', the StringResourceLoader will use that already created repository, rather than creating a new one.
- Since:
- 1.5
- Version:
- $Id$
- Author:
- Eelco Hillenius, Henning P. Schmiedehausen, Nathan Bubna
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringResourceRepositorythe repository used internally by this resource loaderstatic final StringKey to look up the repository implementation class.static final StringThe default implementation class.static final StringKey to look up the repository char encoding.static final StringKey to look up the name for the repository to be used.static final StringThe default name for string resource repositories ('org.apache.velocity.runtime.resource.util.StringResourceRepository').static final StringKey to determine whether the repository should be set as the static one or not.static final booleanBy default, repositories are stored statically (shared across the VM).protected static final Map<String, StringResourceRepository> Fields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
className, isCachingOn, log, modificationCheckInterval, rsvc -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidRemoves all statically storedStringResourceRepositorys.createRepository(String className, String encoding) longgetLastModified(Resource resource) Get the last modified time of the InputStream source that was used to create the template.static StringResourceRepositoryReturns a reference to the default static repository.static StringResourceRepositorygetRepository(String name) Returns a reference to the repository stored statically under the specified name.getResourceReader(String name, String encoding) Get a reader so that the Runtime can build a template with it.voidinit(ExtProperties configuration) Initialize the template loader with a a resources class.booleanisSourceModified(Resource resource) Given a template, check to see if the source of InputStream has been modified.static StringResourceRepositoryremoveRepository(String name) Removes theStringResourceRepositorystored under the specified name.booleanresourceExists(String name) Overrides superclass for better performance.static voidsetRepository(String name, StringResourceRepository repo) Sets the specifiedStringResourceRepositoryin static storage under the specified name.Methods inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
buildReader, commonInit, getClassName, getModificationCheckInterval, isCachingOn, setCachingOn, setModificationCheckInterval
-
Field Details
-
REPOSITORY_STATIC
Key to determine whether the repository should be set as the static one or not.- Since:
- 1.6
- See Also:
-
REPOSITORY_STATIC_DEFAULT
public static final boolean REPOSITORY_STATIC_DEFAULTBy default, repositories are stored statically (shared across the VM).- Since:
- 1.6
- See Also:
-
REPOSITORY_CLASS
Key to look up the repository implementation class.- See Also:
-
REPOSITORY_CLASS_DEFAULT
The default implementation class. -
REPOSITORY_NAME
Key to look up the name for the repository to be used.- Since:
- 1.6
- See Also:
-
REPOSITORY_NAME_DEFAULT
The default name for string resource repositories ('org.apache.velocity.runtime.resource.util.StringResourceRepository').- Since:
- 1.6
-
REPOSITORY_ENCODING
Key to look up the repository char encoding.- See Also:
-
STATIC_REPOSITORIES
-
repository
the repository used internally by this resource loader
-
-
Constructor Details
-
StringResourceLoader
public StringResourceLoader()
-
-
Method Details
-
getRepository
Returns a reference to the default static repository.- Returns:
- default static repository
-
getRepository
Returns a reference to the repository stored statically under the specified name.- Parameters:
name-- Returns:
- named repository
- Since:
- 1.6
-
setRepository
Sets the specifiedStringResourceRepositoryin static storage under the specified name.- Parameters:
name-repo-- Since:
- 1.6
-
removeRepository
Removes theStringResourceRepositorystored under the specified name.- Parameters:
name-- Returns:
- removed repository
- Since:
- 1.6
-
clearRepositories
public static void clearRepositories()Removes all statically storedStringResourceRepositorys.- Since:
- 1.6
-
init
Description copied from class:ResourceLoaderInitialize the template loader with a a resources class.- Specified by:
initin classResourceLoader- Parameters:
configuration-- See Also:
-
createRepository
- Parameters:
className-encoding-- Returns:
- created repository
- Since:
- 1.6
-
resourceExists
Overrides superclass for better performance.- Overrides:
resourceExistsin classResourceLoader- Parameters:
name- resource name- Returns:
- whether resource exists
- Since:
- 1.6
-
getResourceReader
Get a reader so that the Runtime can build a template with it.- Specified by:
getResourceReaderin classResourceLoader- Parameters:
name- name of template to get.encoding- asked encoding- Returns:
- Reader containing the template.
- Throws:
ResourceNotFoundException- Ff template not found in the RepositoryFactory.- Since:
- 2.0
-
isSourceModified
Description copied from class:ResourceLoaderGiven a template, check to see if the source of InputStream has been modified.- Specified by:
isSourceModifiedin classResourceLoader- Parameters:
resource-- Returns:
- whether resource was modified
- See Also:
-
getLastModified
Description copied from class:ResourceLoaderGet the last modified time of the InputStream source that was used to create the template. We need the template here because we have to extract the name of the template in order to locate the InputStream source.- Specified by:
getLastModifiedin classResourceLoader- Parameters:
resource-- Returns:
- last modified timestamp
- See Also:
-