|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.velocity.runtime.resource.loader.ResourceLoader
org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader
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
)
Field Summary | |
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.lang.String |
timestampColumn
|
Fields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader |
className, isCachingOn, modificationCheckInterval, rsvc |
Constructor Summary | |
DataSourceResourceLoader()
|
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 |
void |
setDataSource(javax.sql.DataSource source)
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 |
Field Detail |
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
Constructor Detail |
public DataSourceResourceLoader()
Method Detail |
public void init(org.apache.commons.collections.ExtendedProperties configuration)
ResourceLoader
init
in class ResourceLoader
public void setDataSource(javax.sql.DataSource source)
source
- public boolean isSourceModified(Resource resource)
ResourceLoader
isSourceModified
in class ResourceLoader
public long getLastModified(Resource resource)
ResourceLoader
getLastModified
in class ResourceLoader
public java.io.InputStream getResourceStream(java.lang.String name) throws ResourceNotFoundException
getResourceStream
in class ResourceLoader
name
- name of template
ResourceNotFoundException
private long readLastModified(Resource resource, java.lang.String i_operation)
resource
- Resource object we are finding timestamp ofi_operation
- string for logging, indicating caller's intention
private java.sql.Connection openDbConnection() throws javax.naming.NamingException, java.sql.SQLException
javax.naming.NamingException
java.sql.SQLException
private void closeDbConnection(java.sql.Connection conn)
private java.sql.ResultSet readData(java.sql.Connection conn, java.lang.String columnNames, java.lang.String templateName) throws java.sql.SQLException
conn
- connection to datasourcecolumnNames
- columns to fetch from datasourcetemplateName
- name of template to fetch
java.sql.SQLException
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |