Class 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:
<resource-ref>
<res-ref-name>jdbc/Velocity</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
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
)
- Since:
- 1.5
- Version:
- $Id: DataSourceResourceLoader.java 991660 2010-09-01 19:13:46Z nbubna $
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate InitialContext
private DataSource
private String
private String
private String
private String
private String
Fields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
className, isCachingOn, log, modificationCheckInterval, rsvc
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
closeDbConnection
(Connection conn) Closes connection to the datasourceprivate void
Closes the result set.private void
Closes the PreparedStatement.long
getLastModified
(Resource resource) Get the last modified time of the InputStream source that was used to create the template.getResourceStream
(String name) Get an InputStream so that the Runtime can build a template with it.private PreparedStatement
getStatement
(Connection conn, String columnNames, 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
(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 Connection
Gets connection to the datasource specified through the configuration parameters.private long
readLastModified
(Resource resource, String operation) Fetches the last modification time of the resourcevoid
setDataSource
(DataSource dataSource) Set the DataSource used by this resource loader.Methods inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
commonInit, getClassName, getModificationCheckInterval, isCachingOn, resourceExists, setCachingOn, setModificationCheckInterval
-
Field Details
-
dataSourceName
-
tableName
-
keyColumn
-
templateColumn
-
timestampColumn
-
ctx
-
dataSource
-
-
Constructor Details
-
DataSourceResourceLoader
public DataSourceResourceLoader()
-
-
Method Details
-
init
public void init(org.apache.commons.collections.ExtendedProperties configuration) Description copied from class:ResourceLoader
Initialize the template loader with a a resources class.- Specified by:
init
in classResourceLoader
- See Also:
-
setDataSource
Set the DataSource used by this resource loader. Call this as an alternative to specifying the data source name via properties.- Parameters:
dataSource
- The data source for this ResourceLoader.
-
isSourceModified
Description copied from class:ResourceLoader
Given a template, check to see if the source of InputStream has been modified.- Specified by:
isSourceModified
in classResourceLoader
- Returns:
- True if the resource has been modified.
- See Also:
-
getLastModified
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.- Specified by:
getLastModified
in classResourceLoader
- Returns:
- Time in millis when the resource has been modified.
- See Also:
-
getResourceStream
Get an InputStream so that the Runtime can build a template with it.- Specified by:
getResourceStream
in classResourceLoader
- Parameters:
name
- name of template- Returns:
- InputStream containing template
- Throws:
ResourceNotFoundException
-
readLastModified
Fetches the last modification time of the resource- Parameters:
resource
- Resource object we are finding timestamp ofoperation
- string for logging, indicating caller's intention- Returns:
- timestamp as long
-
openDbConnection
Gets connection to the datasource specified through the configuration parameters.- Returns:
- connection
- Throws:
NamingException
SQLException
-
closeDbConnection
Closes connection to the datasource -
closeResultSet
Closes the result set. -
closeStatement
Closes the PreparedStatement. -
getStatement
private PreparedStatement getStatement(Connection conn, String columnNames, String templateName) throws SQLException Creates the following PreparedStatement 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:
- PreparedStatement
- Throws:
SQLException
-