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.loaders = file, ds
resource.loader.ds.description = Velocity DataSource Resource Loader
resource.loader.ds.class = org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader
resource.loader.ds.resource.datasource_url = java:comp/env/jdbc/Velocity
resource.loader.ds.resource.table = tb_velocity_template
resource.loader.ds.resource.key_column = id_template
resource.loader.ds.resource.template_column = template_definition
resource.loader.ds.resource.timestamp_column = template_timestamp
resource.loader.ds.cache = false
resource.loader.ds.modification_check_interval = 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("resource.loader.ds.instance",ds);
The property resource.loader.ds.class
should be left out, otherwise all the other
properties in velocity.properties would remain the same.
Example WEB-INF/web.xml:
<resource-ref>
<description>Velocity template DataSource</description>
<res-ref-name>jdbc/Velocity</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
[...]
<Context path="/exampleVelocity" docBase="exampleVelocity" debug="0">
[...]
<ResourceParams name="jdbc/Velocity">
<parameter>
<name>driverClassName</name>
<value>org.hsql.jdbcDriver</value>
</parameter>
<parameter>
<name>driverName</name>
<value>jdbc:HypersonicSQL:database</value>
</parameter>
<parameter>
<name>user</name>
<value>database_username</value>
</parameter>
<parameter>
<name>password</name>
<value>database_password</value>
</parameter>
</ResourceParams>
[...]
</Context>
[...]
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
);
Modifier and Type | Class and Description |
---|---|
private static class |
DataSourceResourceLoader.SelfCleaningReader |
Modifier and Type | Field and Description |
---|---|
private java.sql.Connection |
connection |
private javax.naming.InitialContext |
ctx |
private javax.sql.DataSource |
dataSource |
private java.lang.String |
dataSourceName |
private java.lang.String |
keyColumn |
private java.lang.String |
tableName |
private java.lang.String |
templateColumn |
private java.sql.PreparedStatement |
templatePrepStatement |
private java.lang.String |
timestampColumn |
private java.sql.PreparedStatement |
timestampPrepStatement |
className, isCachingOn, log, modificationCheckInterval, rsvc
Constructor and Description |
---|
DataSourceResourceLoader() |
Modifier and Type | Method and Description |
---|---|
private void |
checkDBConnection()
Checks the connection is valid
|
private void |
closeDBConnection()
Closes the prepared statements and the connection to the datasource
|
private void |
closeResultSet(java.sql.ResultSet rs)
Closes the result set.
|
protected java.sql.ResultSet |
fetchResult(java.sql.PreparedStatement ps,
java.lang.String templateName)
Fetches the result for a given template name.
|
protected void |
finalize()
Close DB connection on finalization
|
long |
getLastModified(Resource resource)
Get the last modified time of the InputStream source
that was used to create the template.
|
protected java.io.Reader |
getReader(java.sql.ResultSet resultSet,
java.lang.String column,
java.lang.String encoding)
Gets a reader from a result set's column
|
java.io.Reader |
getResourceReader(java.lang.String name,
java.lang.String encoding)
Get an InputStream so that the Runtime can build a
template with it.
|
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.
|
private void |
openDBConnection()
Gets connection to the datasource specified through the configuration
parameters.
|
protected java.sql.PreparedStatement |
prepareStatement(java.sql.Connection conn,
java.lang.String columnNames,
java.lang.String tableName,
java.lang.String keyColumn)
Creates the following PreparedStatement query :
SELECT columnNames FROM tableName WHERE keyColumn = 'templateName' where keyColumn is a class member set in init() |
private long |
readLastModified(Resource resource,
java.lang.String operation)
Fetches the last modification time of the resource
|
void |
setDataSource(javax.sql.DataSource dataSource)
Set the DataSource used by this resource loader.
|
buildReader, commonInit, getClassName, getModificationCheckInterval, isCachingOn, resourceExists, setCachingOn, setModificationCheckInterval
private java.lang.String dataSourceName
private java.lang.String tableName
private java.lang.String keyColumn
private java.lang.String templateColumn
private java.lang.String timestampColumn
private javax.naming.InitialContext ctx
private javax.sql.DataSource dataSource
private java.sql.Connection connection
private java.sql.PreparedStatement templatePrepStatement
private java.sql.PreparedStatement timestampPrepStatement
public void init(ExtProperties configuration)
ResourceLoader
init
in class ResourceLoader
ResourceLoader.init(org.apache.velocity.util.ExtProperties)
public void setDataSource(javax.sql.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 java.io.Reader getResourceReader(java.lang.String name, java.lang.String encoding) throws ResourceNotFoundException
getResourceReader
in class ResourceLoader
name
- name of templateencoding
- asked encodingResourceNotFoundException
private long readLastModified(Resource resource, java.lang.String operation)
resource
- Resource object we are finding timestamp ofoperation
- string for logging, indicating caller's intentionprivate void openDBConnection() throws javax.naming.NamingException, java.sql.SQLException
javax.naming.NamingException
java.sql.SQLException
private void checkDBConnection() throws javax.naming.NamingException, java.sql.SQLException
javax.naming.NamingException
java.sql.SQLException
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
private void closeDBConnection()
private void closeResultSet(java.sql.ResultSet rs)
protected java.sql.PreparedStatement prepareStatement(java.sql.Connection conn, java.lang.String columnNames, java.lang.String tableName, java.lang.String keyColumn) throws java.sql.SQLException
conn
- connection to datasourcecolumnNames
- columns to fetch from datasourcetableName
- table to fetch fromkeyColumn
- column whose value should match templateNamejava.sql.SQLException
protected java.sql.ResultSet fetchResult(java.sql.PreparedStatement ps, java.lang.String templateName) throws java.sql.SQLException
ps
- target prepared statementtemplateName
- input template namejava.sql.SQLException
protected java.io.Reader getReader(java.sql.ResultSet resultSet, java.lang.String column, java.lang.String encoding) throws java.sql.SQLException
resultSet
- column
- encoding
- java.sql.SQLException