org.apache.velocity.runtime.resource.loader
Class DataSourceResourceLoader
java.lang.Object
|
+--org.apache.velocity.runtime.resource.loader.ResourceLoader
|
+--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
timestamp column (for last modification info).
Example configuration snippet for velocity.properties:
resource.loader.1.public.name = DataSource
resource.loader.1.description = Velocity DataSource Resource Loader
resource.loader.1.class = org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader
resource.loader.1.resource.datasource = jdbc/SomeDS
resource.loader.1.resource.table = template_table
resource.loader.1.resource.keycolumn = template_id
resource.loader.1.resource.templatecolumn = template_definition
resource.loader.1.resource.timestampcolumn = template_timestamp
resource.loader.1.cache = false
resource.loader.1.modificationCheckInterval = 60
- Version:
- $Id: DataSourceResourceLoader.java,v 1.7 2001/05/11 03:59:41 geirm Exp $
- Author:
- David Kinnvall, Paulo Gaspar
Method Summary |
private void |
closeDbConnection(java.sql.Connection conn)
Closes connection to the datasource |
long |
getLastModified(Resource resource)
Get the last modified time of the InputStream source
that was used to create the template. |
java.io.InputStream |
getResourceStream(java.lang.String name)
Get an InputStream so that the Runtime can build a
template with it. |
void |
init(org.apache.commons.collections.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. |
private java.sql.Connection |
openDbConnection()
gets connection to the datasource specified through the configuration
parameters. |
private java.sql.ResultSet |
readData(java.sql.Connection conn,
java.lang.String columnNames,
java.lang.String templateName)
Reads the data from the datasource. |
private long |
readLastModified(Resource resource,
java.lang.String i_operation)
Fetches the last modification time of the resource |
Methods inherited from class java.lang.Object |
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait |
dataSourceName
private java.lang.String dataSourceName
tableName
private java.lang.String tableName
keyColumn
private java.lang.String keyColumn
templateColumn
private java.lang.String templateColumn
timestampColumn
private java.lang.String timestampColumn
ctx
private javax.naming.InitialContext ctx
dataSource
private javax.sql.DataSource dataSource
DataSourceResourceLoader
public DataSourceResourceLoader()
init
public void init(org.apache.commons.collections.ExtendedProperties configuration)
- Description copied from class:
ResourceLoader
- Initialize the template loader with a
a resources class.
- Overrides:
init
in class 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.
- Overrides:
isSourceModified
in class ResourceLoader
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.
- Overrides:
getLastModified
in class ResourceLoader
getResourceStream
public java.io.InputStream getResourceStream(java.lang.String name)
throws ResourceNotFoundException
- Get an InputStream so that the Runtime can build a
template with it.
- Overrides:
getResourceStream
in class ResourceLoader
- Parameters:
name
- name of template- Returns:
- InputStream containing template
readLastModified
private long readLastModified(Resource resource,
java.lang.String i_operation)
- Fetches the last modification time of the resource
- Parameters:
resource
- Resource object we are finding timestamp ofi_operation
- string for logging, indicating caller's intention- Returns:
- timestamp as long
openDbConnection
private java.sql.Connection openDbConnection()
throws java.lang.Exception
- gets connection to the datasource specified through the configuration
parameters.
- Returns:
- connection
closeDbConnection
private void closeDbConnection(java.sql.Connection conn)
- Closes connection to the datasource
readData
private java.sql.ResultSet readData(java.sql.Connection conn,
java.lang.String columnNames,
java.lang.String templateName)
throws java.sql.SQLException
- 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 datasourcecolumnNames
- columns to fetch from datasourcetemplateName
- name of template to fetch- Returns:
- result set from query
Copyright © 2001 Apache Software Foundation. All Rights Reserved.