Package 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 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

     resource.loader.ds.statements_pool_max_size = 50

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>

and Tomcat 4 server.xml file:

      [...]
      <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
      );

Prior to Velocity 2.4, this class should not be considered thread-safe.

Since Velocity 2.4, the handling of JDBC connections and prepared statements is delegated to the `DatabaseObjectsFactory` instance. The default class for this database objects factory is `DefaultDatabaseObjectsFactory`, which obtains a new connection from the data source and prepares statements at each query. You can configure this resource loader to use the

invalid reference

    org.apache.velocity.runtime.resource.loader.CachingDatabaseObjectsFactory

which will keep a single connection and tries to reuse prepared statements. statements

Since:
  1.5
Version:
  $Id$
Author:
  Will Glass-Husain, Matt Raible, David Kinnvall, Paulo Gaspar, Sylwester Lachiewicz, Henning P. Schmiedehausen

## Field Summary

### Fields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader

`className, isCachingOn, log, modificationCheckInterval, rsvc`

## Constructor Summary

Constructors
Constructor
Description
`DataSourceResourceLoader()`

## Method Summary

Modifier and Type
Method
Description
`void`
`clear()`
Frees all resources.
`protected ResultSet`
`fetchResult(PreparedStatement ps, String templateName)`
Fetches the result for a given template name.
`long`
`getLastModified(Resource resource)`
Get the last modified time of the InputStream source that was used to create the template.
`protected Reader`
`getReader(ResultSet resultSet, String column)`
Gets a reader from a result set's column.
`protected Reader`
`getReader(ResultSet resultSet, String column, String encoding)`
Deprecated.
the 'encoding' parameter is useless, it should have been set in the database.
`Reader`
`getResourceReader(String name, 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.
`void`
`setDataSource(DataSource dataSource)`
Set the DataSource used by this resource loader.

### Methods inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader

`buildReader, commonInit, getClassName, getModificationCheckInterval, isCachingOn, resourceExists, setCachingOn, setModificationCheckInterval`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### DataSourceResourceLoader

public DataSourceResourceLoader()

## Method Details

### init

public void init(ExtProperties configuration)
Description copied from class: `ResourceLoader`
Initialize the template loader with a a resources class.

Specified by:
  `init` in class `ResourceLoader`
Parameters:
  `configuration` -
See Also:
  - `ResourceLoader.init(org.apache.velocity.util.ExtProperties)`

### setDataSource

public void setDataSource(DataSource dataSource)
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

public boolean isSourceModified(Resource resource)
Description copied from class: `ResourceLoader`
Given a template, check to see if the source of InputStream has been modified.

Specified by:
  `isSourceModified` in class `ResourceLoader`
Parameters:
  `resource` -
Returns:
  True if the resource has been modified.
See Also:
  - `ResourceLoader.isSourceModified(org.apache.velocity.runtime.resource.Resource)`

### 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.

Specified by:
  `getLastModified` in class `ResourceLoader`
Parameters:
  `resource` -
Returns:
  Time in millis when the resource has been modified.
See Also:
  - `ResourceLoader.getLastModified(org.apache.velocity.runtime.resource.Resource)`

### getResourceReader

public Reader getResourceReader(String name, String encoding) throws ResourceNotFoundException
Get an InputStream so that the Runtime can build a template with it.

Specified by:
  `getResourceReader` in class `ResourceLoader`
Parameters:
  `name` - name of template
  `encoding` - asked encoding
Returns:
  InputStream containing template
Throws:
  `ResourceNotFoundException`
Since:
  2.0

### fetchResult

protected ResultSet fetchResult(PreparedStatement ps, String templateName) throws SQLException
Fetches the result for a given template name. Inherit this method if there is any calculation to perform on the template name.

Parameters:
  `ps` - target prepared statement
  `templateName` - input template name
Returns:
  result set
Throws:
  `SQLException`

### getReader

protected Reader getReader(ResultSet resultSet, String column, String encoding) throws SQLException
Deprecated.
the 'encoding' parameter is useless, it should have been set in the database. Use `getReader(ResultSet, String)`
Gets a reader from a result set's column.

Parameters:
  `resultSet` - result set
  `column` - template column
  `encoding` - template encoding (unused)
Returns:
  reader
Throws:
  `SQLException`

### getReader

protected Reader getReader(ResultSet resultSet, String column) throws SQLException
Gets a reader from a result set's column.

Parameters:
  `resultSet` - result set
  `column` - template column
Returns:
  reader
Throws:
  `SQLException`

### clear

public void clear()
Frees all resources.