org.apache.velocity.runtime.resource.loader
Class DataSourceResourceLoader

java.lang.Object
  extended by org.apache.velocity.runtime.resource.loader.ResourceLoader
      extended by org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader

public class DataSourceResourceLoader
extends ResourceLoader

This is a simple template file loader that loads templates from a DataSource instead of plain files.

It can be configured with a datasource name, a table name, id column (name), content column (the template body) and a datetime column (for last modification info).

Example configuration snippet for velocity.properties:

resource.loader = file, ds

ds.resource.loader.public.name = DataSource
ds.resource.loader.description = Velocity DataSource Resource Loader
ds.resource.loader.class = org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader
ds.resource.loader.resource.datasource = java:comp/env/jdbc/Velocity
ds.resource.loader.resource.table = tb_velocity_template
ds.resource.loader.resource.keycolumn = id_template
ds.resource.loader.resource.templatecolumn = template_definition
ds.resource.loader.resource.timestampcolumn = template_timestamp
ds.resource.loader.cache = false
ds.resource.loader.modificationCheckInterval = 60

Optionally, the developer can instantiate the DataSourceResourceLoader and set the DataSource via code in a manner similar to the following:

DataSourceResourceLoader ds = new DataSourceResourceLoader();
ds.setDataSource(DATASOURCE);
Velocity.setProperty("ds.resource.loader.instance",ds);

The property ds.resource.loader.class should be left out, otherwise all the other properties in velocity.properties would remain the same.

Example WEB-INF/web.xml:


Velocity template DataSource
jdbc/Velocity
javax.sql.DataSource
Container



and Tomcat 4 server.xml file:
[...]

[...]


driverClassName
org.hsql.jdbcDriver


driverName
jdbc:HypersonicSQL:database


user
database_username


password
database_password


[...]

[...]

Example sql script:
CREATE TABLE tb_velocity_template (
id_template varchar (40) NOT NULL ,
template_definition text (16) NOT NULL ,
template_timestamp datetime NOT NULL
)

Version:
$Id: DataSourceResourceLoader.java 471259 2006-11-04 20:26:57Z henning $
Author:
Will Glass-Husain, Matt Raible, David Kinnvall, Paulo Gaspar, Sylwester Lachiewicz, Henning P. Schmiedehausen

Field Summary
 
Fields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
className, isCachingOn, log, modificationCheckInterval, rsvc
 
Constructor Summary
DataSourceResourceLoader()
           
 
Method Summary
 long getLastModified(Resource resource)
          Get the last modified time of the InputStream source that was used to create the template.
 InputStream getResourceStream(String name)
          Get an InputStream so that the Runtime can build a template with it.
 void init(ExtendedProperties configuration)
          Initialize the template loader with a a resources class.
 boolean isSourceModified(Resource resource)
          Given a template, check to see if the source of InputStream has been modified.
 void setDataSource(DataSource dataSource)
          Set the DataSource used by this resource loader.
 
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
 

Constructor Detail

DataSourceResourceLoader

public DataSourceResourceLoader()
Method Detail

init

public void init(ExtendedProperties configuration)
Description copied from class: ResourceLoader
Initialize the template loader with a a resources class.

Specified by:
init in class ResourceLoader
See Also:
ResourceLoader.init(org.apache.commons.collections.ExtendedProperties)

setDataSource

public void setDataSource(DataSource dataSource)
Set the DataSource used by this resource loader. Call this as an alternative to specifying the data source name via properties.

Parameters:
dataSource - The data source for this ResourceLoader.

isSourceModified

public boolean isSourceModified(Resource resource)
Description copied from class: ResourceLoader
Given a template, check to see if the source of InputStream has been modified.

Specified by:
isSourceModified in class ResourceLoader
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: ResourceLoader
Get 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:
getLastModified in class ResourceLoader
Returns:
Time in millis when the resource has been modified.
See Also:
ResourceLoader.getLastModified(org.apache.velocity.runtime.resource.Resource)

getResourceStream

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

Specified by:
getResourceStream in class ResourceLoader
Parameters:
name - name of template
Returns:
InputStream containing template
Throws:
ResourceNotFoundException


Copyright © 2000-2007 The Apache Software Foundation. All Rights Reserved.