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:
and Tomcat 4 server.xml file:
[...]
[...]
[...]
[...]
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
)
className, isCachingOn, log, modificationCheckInterval, rsvc
Constructor and Description |
---|
DataSourceResourceLoader() |
Modifier and Type | Method and Description |
---|---|
long |
getLastModified(Resource resource)
Get the last modified time of the InputStream source
that was used to create the template.
|
Reader |
getResourceReader(String name,
String encoding)
Get an InputStream so that the Runtime can build a
template with it.
|
protected PreparedStatement |
getStatement(Connection conn,
String columnNames,
String tableName,
String keyColumn,
String templateName)
Creates the following PreparedStatement query :
SELECT columnNames FROM tableName WHERE keyColumn = 'templateName' where keyColumn is a class member set in init() |
void |
init(ExtProperties 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.
|
buildReader, commonInit, getClassName, getModificationCheckInterval, isCachingOn, resourceExists, setCachingOn, setModificationCheckInterval
public void init(ExtProperties configuration)
ResourceLoader
init
in class ResourceLoader
ResourceLoader.init(org.apache.velocity.util.ExtProperties)
public void setDataSource(DataSource dataSource)
dataSource
- The data source for this ResourceLoader.public boolean isSourceModified(Resource resource)
ResourceLoader
isSourceModified
in class ResourceLoader
ResourceLoader.isSourceModified(org.apache.velocity.runtime.resource.Resource)
public long getLastModified(Resource resource)
ResourceLoader
getLastModified
in class ResourceLoader
ResourceLoader.getLastModified(org.apache.velocity.runtime.resource.Resource)
public Reader getResourceReader(String name, String encoding) throws ResourceNotFoundException
getResourceReader
in class ResourceLoader
name
- name of templateencoding
- asked encodingResourceNotFoundException
protected PreparedStatement getStatement(Connection conn, String columnNames, String tableName, String keyColumn, String templateName) throws SQLException
conn
- connection to datasourcecolumnNames
- columns to fetch from datasourcetableName
- table to fetch fromkeyColumn
- column whose value should match templateNametemplateName
- name of template to fetchSQLException
Copyright © 2000–2017 The Apache Software Foundation. All rights reserved.