Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.roller.presentation.velocity
Class DatabaseResourceLoader  view DatabaseResourceLoader download DatabaseResourceLoader.java

java.lang.Object
  extended byorg.apache.velocity.runtime.resource.loader.ResourceLoader
      extended byorg.roller.presentation.velocity.DatabaseResourceLoader

public class DatabaseResourceLoader
extends org.apache.velocity.runtime.resource.loader.ResourceLoader

This is a simple template file loader that loads templates from a Database instead of plain files.

Dave Johnson: Modified to use a regular old JDBC connection specified by driver class and connection URL.

It can be configured with a datasource name, a table name, id column (name), content column (the template body) and a timestamp column (for last modification info).

Example configuration snippet for velocity.properties:

resource.loader.1.public.name =Database
resource.loader.1.description = Roller Database Resource Loader
resource.loader.1.class = org.roller.presentation.velocity.DatabaseResourceLoader
resource.loader.1.resource.jdbc_class = driver_class
resource.loader.1.resource.jdbc_url = connection_url
resource.loader.1.resource.table = template_table
resource.loader.1.resource.keycolumn = template_id
resource.loader.1.resource.templatecolumn = template_definition
resource.loader.1.resource.timestampcolumn = template_timestamp
resource.loader.1.cache = false
resource.loader.1.modificationCheckInterval = 60

Version:
$Id: DatabaseResourceLoader.java,v 1.3 2002/08/09 17:21:42 mraible Exp $

Field Summary
private  javax.naming.InitialContext ctx
           
private  java.lang.String jdbcClass
           
private  java.lang.String jdbcPassword
           
private  java.lang.String jdbcUrl
           
private  java.lang.String jdbcUser
           
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
DatabaseResourceLoader()
           
 
Method Summary
private  void closeDbConnection(java.sql.Connection conn)
          Closes connection to the datasource
 long getLastModified(org.apache.velocity.runtime.resource.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(org.apache.velocity.runtime.resource.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(org.apache.velocity.runtime.resource.Resource resource, java.lang.String i_operation)
          Fetches the last modification time of the resource
 
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

jdbcClass

private java.lang.String jdbcClass

jdbcUrl

private java.lang.String jdbcUrl

jdbcUser

private java.lang.String jdbcUser

jdbcPassword

private java.lang.String jdbcPassword

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
Constructor Detail

DatabaseResourceLoader

public DatabaseResourceLoader()
Method Detail

init

public void init(org.apache.commons.collections.ExtendedProperties configuration)
Description copied from class: org.apache.velocity.runtime.resource.loader.ResourceLoader
Initialize the template loader with a a resources class.


isSourceModified

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


getLastModified

public long getLastModified(org.apache.velocity.runtime.resource.Resource resource)
Description copied from class: org.apache.velocity.runtime.resource.loader.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.


getResourceStream

public java.io.InputStream getResourceStream(java.lang.String name)
                                      throws org.apache.velocity.exception.ResourceNotFoundException
Get an InputStream so that the Runtime can build a template with it.


readLastModified

private long readLastModified(org.apache.velocity.runtime.resource.Resource resource,
                              java.lang.String i_operation)
Fetches the last modification time of the resource


openDbConnection

private java.sql.Connection openDbConnection()
                                      throws java.lang.Exception
gets connection to the datasource specified through the configuration parameters.


closeDbConnection

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


readData

private java.sql.ResultSet readData(java.sql.Connection conn,
                                    java.lang.String columnNames,
                                    java.lang.String templateName)
                             throws java.sql.SQLException
Reads the data from the datasource. It simply does the following query :
SELECT columnNames FROM tableName WHERE keyColumn = 'templateName'
where keyColumn is a class member set in init()