edu.internet2.middleware.shibboleth.common.util
Class StringResourceLoader

java.lang.Object
  extended by org.apache.velocity.runtime.resource.loader.ResourceLoader
      extended by edu.internet2.middleware.shibboleth.common.util.StringResourceLoader

public class StringResourceLoader
extends org.apache.velocity.runtime.resource.loader.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 using StringResourceRepositoryImpl as 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.loader.StringResourceRepositoryImpl
 
Resources 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 '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:


 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 '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:


 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 '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.

Version:
535935

Field Summary
protected  org.apache.velocity.runtime.resource.util.StringResourceRepository repository
           
static String REPOSITORY_CLASS
          Key to look up the repository implementation class.
static String REPOSITORY_CLASS_DEFAULT
          The default implementation class.
static String REPOSITORY_ENCODING
          Key to look up the repository char encoding.
static String REPOSITORY_ENCODING_DEFAULT
          The default repository encoding.
static String REPOSITORY_NAME
          Key to look up the name for the repository to be used.
static String REPOSITORY_NAME_DEFAULT
          The default name for string resource repositories ('org.apache.velocity.runtime.resource.util.StringResourceRepository').
static String REPOSITORY_STATIC
          Key to determine whether the repository should be set as the static one or not.
static boolean REPOSITORY_STATIC_DEFAULT
          By default, repositories are stored statically (shared across the VM).
protected static Map STATIC_REPOSITORIES
           
 
Fields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
className, isCachingOn, log, modificationCheckInterval, rsvc
 
Constructor Summary
StringResourceLoader()
           
 
Method Summary
static void clearRepositories()
          Removes all statically stored StringResourceRepositorys.
 org.apache.velocity.runtime.resource.util.StringResourceRepository createRepository(String className, String encoding)
           
 long getLastModified(org.apache.velocity.runtime.resource.Resource resource)
           
static org.apache.velocity.runtime.resource.util.StringResourceRepository getRepository()
          Returns a reference to the default static repository.
static org.apache.velocity.runtime.resource.util.StringResourceRepository getRepository(String name)
          Returns a reference to the repository stored statically under the specified name.
 InputStream getResourceStream(String name)
          Get an InputStream so that the Runtime can build a template with it.
 void init(org.apache.commons.collections.ExtendedProperties configuration)
           
 boolean isSourceModified(org.apache.velocity.runtime.resource.Resource resource)
           
static org.apache.velocity.runtime.resource.util.StringResourceRepository removeRepository(String name)
          Removes the StringResourceRepository stored under the specified name.
static void setRepository(String name, org.apache.velocity.runtime.resource.util.StringResourceRepository repo)
          Sets the specified StringResourceRepository in static storage under the specified name.
 
Methods inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
commonInit, getClassName, getModificationCheckInterval, isCachingOn, setCachingOn, setModificationCheckInterval
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REPOSITORY_STATIC

public static final String REPOSITORY_STATIC
Key to determine whether the repository should be set as the static one or not.

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).

See Also:
Constant Field Values

REPOSITORY_CLASS

public static final String REPOSITORY_CLASS
Key to look up the repository implementation class.

See Also:
Constant Field Values

REPOSITORY_CLASS_DEFAULT

public static final String REPOSITORY_CLASS_DEFAULT
The default implementation class.


REPOSITORY_NAME

public static final String REPOSITORY_NAME
Key to look up the name for the repository to be used.

See Also:
Constant Field Values

REPOSITORY_NAME_DEFAULT

public static final String REPOSITORY_NAME_DEFAULT
The default name for string resource repositories ('org.apache.velocity.runtime.resource.util.StringResourceRepository').


REPOSITORY_ENCODING

public static final String REPOSITORY_ENCODING
Key to look up the repository char encoding.

See Also:
Constant Field Values

REPOSITORY_ENCODING_DEFAULT

public static final String REPOSITORY_ENCODING_DEFAULT
The default repository encoding.

See Also:
Constant Field Values

STATIC_REPOSITORIES

protected static final Map STATIC_REPOSITORIES

repository

protected org.apache.velocity.runtime.resource.util.StringResourceRepository repository
Constructor Detail

StringResourceLoader

public StringResourceLoader()
Method Detail

getRepository

public static org.apache.velocity.runtime.resource.util.StringResourceRepository getRepository()
Returns a reference to the default static repository.


getRepository

public static org.apache.velocity.runtime.resource.util.StringResourceRepository getRepository(String name)
Returns a reference to the repository stored statically under the specified name.


setRepository

public static void setRepository(String name,
                                 org.apache.velocity.runtime.resource.util.StringResourceRepository repo)
Sets the specified StringResourceRepository in static storage under the specified name.


removeRepository

public static org.apache.velocity.runtime.resource.util.StringResourceRepository removeRepository(String name)
Removes the StringResourceRepository stored under the specified name.


clearRepositories

public static void clearRepositories()
Removes all statically stored StringResourceRepositorys.


init

public void init(org.apache.commons.collections.ExtendedProperties configuration)
Specified by:
init in class org.apache.velocity.runtime.resource.loader.ResourceLoader
See Also:
ResourceLoader.init(org.apache.commons.collections.ExtendedProperties)

createRepository

public org.apache.velocity.runtime.resource.util.StringResourceRepository createRepository(String className,
                                                                                           String encoding)

getResourceStream

public InputStream getResourceStream(String name)
                              throws org.apache.velocity.exception.ResourceNotFoundException
Get an InputStream so that the Runtime can build a template with it.

Specified by:
getResourceStream in class org.apache.velocity.runtime.resource.loader.ResourceLoader
Parameters:
name - name of template to get.
Returns:
InputStream containing the template.
Throws:
org.apache.velocity.exception.ResourceNotFoundException - Ff template not found in the RepositoryFactory.

isSourceModified

public boolean isSourceModified(org.apache.velocity.runtime.resource.Resource resource)
Specified by:
isSourceModified in class org.apache.velocity.runtime.resource.loader.ResourceLoader
See Also:
ResourceLoader.isSourceModified(org.apache.velocity.runtime.resource.Resource)

getLastModified

public long getLastModified(org.apache.velocity.runtime.resource.Resource resource)
Specified by:
getLastModified in class org.apache.velocity.runtime.resource.loader.ResourceLoader
See Also:
ResourceLoader.getLastModified(org.apache.velocity.runtime.resource.Resource)


Copyright © 2006-2008 Internet2. All Rights Reserved.