org.apache.velocity.runtime.resource.loader

Class 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

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,v 1.8.8.1 2004/03/03 23:23:02 geirm Exp $

Author: David Kinnvall Paulo Gaspar Sylwester Lachiewicz

Field Summary
InitialContextctx
DataSourcedataSource
StringdataSourceName
StringkeyColumn
StringtableName
StringtemplateColumn
StringtimestampColumn
Method Summary
voidcloseDbConnection(Connection conn)
Closes connection to the datasource
longgetLastModified(Resource resource)
InputStreamgetResourceStream(String name)
Get an InputStream so that the Runtime can build a template with it.
voidinit(ExtendedProperties configuration)
booleanisSourceModified(Resource resource)
ConnectionopenDbConnection()
gets connection to the datasource specified through the configuration parameters.
ResultSetreadData(Connection conn, String columnNames, String templateName)
Reads the data from the datasource.
longreadLastModified(Resource resource, String i_operation)
Fetches the last modification time of the resource

Field Detail

ctx

private InitialContext ctx

dataSource

private DataSource dataSource

dataSourceName

private String dataSourceName

keyColumn

private String keyColumn

tableName

private String tableName

templateColumn

private String templateColumn

timestampColumn

private String timestampColumn

Method Detail

closeDbConnection

private void closeDbConnection(Connection conn)
Closes connection to the datasource

getLastModified

public long getLastModified(Resource resource)

getResourceStream

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

Parameters: name name of template

Returns: InputStream containing template

init

public void init(ExtendedProperties configuration)

isSourceModified

public boolean isSourceModified(Resource resource)

openDbConnection

private Connection openDbConnection()
gets connection to the datasource specified through the configuration parameters.

Returns: connection

readData

private ResultSet readData(Connection conn, String columnNames, String templateName)
Reads the data from the datasource. It simply does the following query :
SELECT columnNames FROM tableName WHERE keyColumn = 'templateName'
where keyColumn is a class member set in init()

Parameters: conn connection to datasource columnNames columns to fetch from datasource templateName name of template to fetch

Returns: result set from query

readLastModified

private long readLastModified(Resource resource, String i_operation)
Fetches the last modification time of the resource

Parameters: resource Resource object we are finding timestamp of i_operation string for logging, indicating caller's intention

Returns: timestamp as long

Copyright B) 2002 Apache Software Foundation. All Rights Reserved.