Class StringResourceLoader
- java.lang.Object
-
- org.apache.velocity.runtime.resource.loader.ResourceLoader
-
- org.apache.velocity.runtime.resource.loader.StringResourceLoader
-
public class StringResourceLoader extends ResourceLoader
Resource loader that works with Strings. Users should manually add resources to the repository that is used by the resource loader instance. Below is an example configuration for this loader. Note that 'repository.class' is not necessary; if not provided, the factory will fall back on usingStringResourceRepositoryImplas the default.resource.loader = string string.resource.loader.description = Velocity StringResource loader string.resource.loader.class = org.apache.velocity.runtime.resource.loader.StringResourceLoader string.resource.loader.repository.class = org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl
Resources can be added to the repository like this:
After this, the templates can be retrieved as usual.StringResourceRepository repo = StringResourceLoader.getRepository(); String myTemplateName = "/some/imaginary/path/hello.vm"; String myTemplate = "Hi, ${username}... this is some template!"; repo.putStringResource(myTemplateName, myTemplate);
If there will be multiple StringResourceLoaders used in an application, you should consider specifying a 'string.resource.loader.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:
and add string resources to the repo just as in the previous example.StringResourceRepository repo = StringResourceLoader.getRepository("foo");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 'string.resource.loader.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:
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.StringResourceRepository repo = velocityEngine.getApplicationAttribute("foo");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:
Or for a non-static repository:StringResourceRepository repo = new MyStringResourceRepository(); repo.magicallyAddSomeStringResources(); StringResourceLoader.setRepository("foo", repo);
Then, assuming the 'string.resource.loader.repository.name' property is set to 'some.name', the StringResourceLoader will use that already created repository, rather than creating a new one.StringResourceRepository repo = new MyStringResourceRepository(); repo.magicallyAddSomeStringResources(); velocityEngine.setApplicationAttribute("foo", repo);- Since:
- 1.5
- Version:
- $Id: StringResourceLoader.java 825302 2009-10-14 21:51:39Z nbubna $
- Author:
- Eelco Hillenius, Henning P. Schmiedehausen, Nathan Bubna
-
-
Field Summary
Fields Modifier and Type Field Description protected StringResourceRepositoryrepositorystatic java.lang.StringREPOSITORY_CLASSKey to look up the repository implementation class.static java.lang.StringREPOSITORY_CLASS_DEFAULTThe default implementation class.static java.lang.StringREPOSITORY_ENCODINGKey to look up the repository char encoding.static java.lang.StringREPOSITORY_ENCODING_DEFAULTThe default repository encoding.static java.lang.StringREPOSITORY_NAMEKey to look up the name for the repository to be used.static java.lang.StringREPOSITORY_NAME_DEFAULTThe default name for string resource repositories ('org.apache.velocity.runtime.resource.util.StringResourceRepository').static java.lang.StringREPOSITORY_STATICKey to determine whether the repository should be set as the static one or not.static booleanREPOSITORY_STATIC_DEFAULTBy default, repositories are stored statically (shared across the VM).protected static java.util.MapSTATIC_REPOSITORIES-
Fields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
className, isCachingOn, log, modificationCheckInterval, rsvc
-
-
Constructor Summary
Constructors Constructor Description StringResourceLoader()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidclearRepositories()Removes all statically storedStringResourceRepositorys.StringResourceRepositorycreateRepository(java.lang.String className, java.lang.String encoding)longgetLastModified(Resource resource)Get the last modified time of the InputStream source that was used to create the template.static StringResourceRepositorygetRepository()Returns a reference to the default static repository.static StringResourceRepositorygetRepository(java.lang.String name)Returns a reference to the repository stored statically under the specified name.java.io.InputStreamgetResourceStream(java.lang.String name)Get an InputStream so that the Runtime can build a template with it.voidinit(org.apache.commons.collections.ExtendedProperties 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(java.lang.String name)Removes theStringResourceRepositorystored under the specified name.booleanresourceExists(java.lang.String name)Overrides superclass for better performance.static voidsetRepository(java.lang.String name, StringResourceRepository repo)Sets the specifiedStringResourceRepositoryin static storage under the specified name.-
Methods inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
commonInit, getClassName, getModificationCheckInterval, isCachingOn, setCachingOn, setModificationCheckInterval
-
-
-
-
Field Detail
-
REPOSITORY_STATIC
public static final java.lang.String REPOSITORY_STATIC
Key to determine whether the repository should be set as the static one or not.- Since:
- 1.6
- See Also:
- Constant Field Values
-
REPOSITORY_STATIC_DEFAULT
public static final boolean REPOSITORY_STATIC_DEFAULT
By default, repositories are stored statically (shared across the VM).- Since:
- 1.6
- See Also:
- Constant Field Values
-
REPOSITORY_CLASS
public static final java.lang.String REPOSITORY_CLASS
Key to look up the repository implementation class.- See Also:
- Constant Field Values
-
REPOSITORY_CLASS_DEFAULT
public static final java.lang.String REPOSITORY_CLASS_DEFAULT
The default implementation class.
-
REPOSITORY_NAME
public static final java.lang.String REPOSITORY_NAME
Key to look up the name for the repository to be used.- Since:
- 1.6
- See Also:
- Constant Field Values
-
REPOSITORY_NAME_DEFAULT
public static final java.lang.String REPOSITORY_NAME_DEFAULT
The default name for string resource repositories ('org.apache.velocity.runtime.resource.util.StringResourceRepository').- Since:
- 1.6
-
REPOSITORY_ENCODING
public static final java.lang.String REPOSITORY_ENCODING
Key to look up the repository char encoding.- See Also:
- Constant Field Values
-
REPOSITORY_ENCODING_DEFAULT
public static final java.lang.String REPOSITORY_ENCODING_DEFAULT
The default repository encoding.- See Also:
- Constant Field Values
-
STATIC_REPOSITORIES
protected static final java.util.Map STATIC_REPOSITORIES
-
repository
protected StringResourceRepository repository
-
-
Method Detail
-
getRepository
public static StringResourceRepository getRepository()
Returns a reference to the default static repository.
-
getRepository
public static StringResourceRepository getRepository(java.lang.String name)
Returns a reference to the repository stored statically under the specified name.- Since:
- 1.6
-
setRepository
public static void setRepository(java.lang.String name, StringResourceRepository repo)Sets the specifiedStringResourceRepositoryin static storage under the specified name.- Since:
- 1.6
-
removeRepository
public static StringResourceRepository removeRepository(java.lang.String name)
Removes theStringResourceRepositorystored under the specified name.- Since:
- 1.6
-
clearRepositories
public static void clearRepositories()
Removes all statically storedStringResourceRepositorys.- Since:
- 1.6
-
init
public void init(org.apache.commons.collections.ExtendedProperties configuration)
Description copied from class:ResourceLoaderInitialize the template loader with a a resources class.- Specified by:
initin classResourceLoader- See Also:
ResourceLoader.init(org.apache.commons.collections.ExtendedProperties)
-
createRepository
public StringResourceRepository createRepository(java.lang.String className, java.lang.String encoding)
- Since:
- 1.6
-
resourceExists
public boolean resourceExists(java.lang.String name)
Overrides superclass for better performance.- Overrides:
resourceExistsin classResourceLoader- Parameters:
name- The name of a resource.- Returns:
- true if a resource exists and can be accessed.
- Since:
- 1.6
-
getResourceStream
public java.io.InputStream getResourceStream(java.lang.String name) throws ResourceNotFoundExceptionGet an InputStream so that the Runtime can build a template with it.- Specified by:
getResourceStreamin classResourceLoader- Parameters:
name- name of template to get.- Returns:
- InputStream containing the template.
- Throws:
ResourceNotFoundException- Ff template not found in the RepositoryFactory.
-
isSourceModified
public boolean isSourceModified(Resource resource)
Description copied from class:ResourceLoaderGiven a template, check to see if the source of InputStream has been modified.- Specified by:
isSourceModifiedin classResourceLoader- Returns:
- True if the resource has been modified.
- See Also:
ResourceLoader.isSourceModified(org.apache.velocity.runtime.resource.Resource)
-
getLastModified
public long getLastModified(Resource resource)
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- Returns:
- Time in millis when the resource has been modified.
- See Also:
ResourceLoader.getLastModified(org.apache.velocity.runtime.resource.Resource)
-
-