Class 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.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
    )

    Since:
    1.5
    Version:
    $Id: DataSourceResourceLoader.java 991660 2010-09-01 19:13:46Z nbubna $
    • Field Detail

      • dataSourceName

        private java.lang.String dataSourceName
      • tableName

        private java.lang.String tableName
      • keyColumn

        private java.lang.String keyColumn
      • templateColumn

        private java.lang.String templateColumn
      • timestampColumn

        private java.lang.String timestampColumn
      • ctx

        private javax.naming.InitialContext ctx
      • dataSource

        private javax.sql.DataSource dataSource
    • Constructor Detail

      • DataSourceResourceLoader

        public DataSourceResourceLoader()
    • Method Detail

      • setDataSource

        public void setDataSource​(javax.sql.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.
      • readLastModified

        private long readLastModified​(Resource resource,
                                      java.lang.String operation)
        Fetches the last modification time of the resource
        Parameters:
        resource - Resource object we are finding timestamp of
        operation - string for logging, indicating caller's intention
        Returns:
        timestamp as long
      • openDbConnection

        private java.sql.Connection openDbConnection()
                                              throws javax.naming.NamingException,
                                                     java.sql.SQLException
        Gets connection to the datasource specified through the configuration parameters.
        Returns:
        connection
        Throws:
        javax.naming.NamingException
        java.sql.SQLException
      • closeDbConnection

        private void closeDbConnection​(java.sql.Connection conn)
        Closes connection to the datasource
      • closeResultSet

        private void closeResultSet​(java.sql.ResultSet rs)
        Closes the result set.
      • closeStatement

        private void closeStatement​(java.sql.PreparedStatement ps)
        Closes the PreparedStatement.
      • getStatement

        private java.sql.PreparedStatement getStatement​(java.sql.Connection conn,
                                                        java.lang.String columnNames,
                                                        java.lang.String templateName)
                                                 throws java.sql.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 datasource
        columnNames - columns to fetch from datasource
        templateName - name of template to fetch
        Returns:
        PreparedStatement
        Throws:
        java.sql.SQLException