org.apache.velocity.runtime.resource.loader
public class: DataSourceResourceLoader [javadoc |
source]
java.lang.Object
org.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:
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
)
- author:
< - a href="mailto:wglass@forio.com">Will Glass-Husain
- author:
< - a href="mailto:matt@raibledesigns.com">Matt Raible
- author:
< - a href="mailto:david.kinnvall@alertir.com">David Kinnvall
- author:
< - a href="mailto:paulo.gaspar@krankikom.de">Paulo Gaspar
- author:
< - a href="mailto:lachiewicz@plusnet.pl">Sylwester Lachiewicz
- author:
< - a href="mailto:henning@apache.org">Henning P. Schmiedehausen
- version:
$ - Id: DataSourceResourceLoader.java 471259 2006-11-04 20:26:57Z henning $
| Method from org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader Detail: |
public long getLastModified(Resource resource) {
return readLastModified(resource, "getting timestamp");
}
|
public synchronized InputStream getResourceStream(String name) throws ResourceNotFoundException {
if (org.apache.commons.lang.StringUtils.isEmpty(name))
{
throw new ResourceNotFoundException ("DataSourceResourceLoader: "
+ "Template name was empty or null");
}
Connection conn = null;
ResultSet rs = null;
try
{
conn = openDbConnection();
rs = readData(conn, templateColumn, name);
if (rs.next())
{
InputStream ascStream = rs.getAsciiStream(templateColumn);
if (ascStream == null)
{
throw new ResourceNotFoundException("DataSourceResourceLoader: "
+ "template column for '" + name + "' is null");
}
return new BufferedInputStream(ascStream);
}
else
{
throw new ResourceNotFoundException("DataSourceResourceLoader: "
+ "could not find resource '" + name + "'");
}
}
catch(SQLException sqle)
{
String msg = "DataSourceResourceLoader: database problem while getting resource '"
+ name + "': ";
log.error(msg, sqle);
throw new ResourceNotFoundException(msg);
}
catch(NamingException ne)
{
String msg = "DataSourceResourceLoader: database problem while getting resource '"
+ name + "': ";
log.error(msg, ne);
throw new ResourceNotFoundException(msg);
}
finally
{
closeResultSet(rs);
closeDbConnection(conn);
}
}
Get an InputStream so that the Runtime can build a
template with it. |
public void init(ExtendedProperties configuration) {
dataSourceName = StringUtils.nullTrim(configuration.getString("resource.datasource"));
tableName = StringUtils.nullTrim(configuration.getString("resource.table"));
keyColumn = StringUtils.nullTrim(configuration.getString("resource.keycolumn"));
templateColumn = StringUtils.nullTrim(configuration.getString("resource.templatecolumn"));
timestampColumn = StringUtils.nullTrim(configuration.getString("resource.timestampcolumn"));
if (dataSource != null)
{
if (log.isDebugEnabled())
{
log.debug("DataSourceResourceLoader: using dataSource instance with table \""
+ tableName + "\"");
log.debug("DataSourceResourceLoader: using columns \""
+ keyColumn + "\", \"" + templateColumn + "\" and \""
+ timestampColumn + "\"");
}
log.trace("DataSourceResourceLoader initialized.");
}
else if (dataSourceName != null)
{
if (log.isDebugEnabled())
{
log.debug("DataSourceResourceLoader: using \"" + dataSourceName
+ "\" datasource with table \"" + tableName + "\"");
log.debug("DataSourceResourceLoader: using columns \""
+ keyColumn + "\", \"" + templateColumn + "\" and \""
+ timestampColumn + "\"");
}
log.trace("DataSourceResourceLoader initialized.");
}
else
{
log.warn("DataSourceResourceLoader not properly initialized. No DataSource was identified.");
}
}
|
public boolean isSourceModified(Resource resource) {
return (resource.getLastModified() !=
readLastModified(resource, "checking timestamp"));
}
|
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
Set the DataSource used by this resource loader. Call this as an alternative to
specifying the data source name via properties. |