org.apache.velocity.runtime.resource.loader
クラス 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.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

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
)

バージョン:
$Id: DataSourceResourceLoader.java,v 1.8 2002/02/10 18:46:58 geirm Exp $
作成者:
David Kinnvall, Paulo Gaspar, Sylwester Lachiewicz

フィールドの概要
private  javax.naming.InitialContext ctx
           
private  javax.sql.DataSource dataSource
           
private  java.lang.String dataSourceName
           
private  java.lang.String keyColumn
           
private  java.lang.String tableName
           
private  java.lang.String templateColumn
           
private  java.lang.String timestampColumn
           
 
クラス org.apache.velocity.runtime.resource.loader.ResourceLoader から継承したフィールド
className, isCachingOn, modificationCheckInterval, rsvc
 
コンストラクタの概要
DataSourceResourceLoader()
           
 
メソッドの概要
private  void closeDbConnection(java.sql.Connection conn)
          Closes connection to the datasource
 long getLastModified(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(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(Resource resource, java.lang.String i_operation)
          Fetches the last modification time of the resource
 
クラス org.apache.velocity.runtime.resource.loader.ResourceLoader から継承したメソッド
commonInit, getClassName, getModificationCheckInterval, isCachingOn, setCachingOn, setModificationCheckInterval
 
クラス java.lang.Object から継承したメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

フィールドの詳細

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
コンストラクタの詳細

DataSourceResourceLoader

public DataSourceResourceLoader()
メソッドの詳細

init

public void init(org.apache.commons.collections.ExtendedProperties configuration)
クラス ResourceLoader の記述:
Initialize the template loader with a a resources class.

定義:
クラス ResourceLoader 内の init

isSourceModified

public boolean isSourceModified(Resource resource)
クラス ResourceLoader の記述:
Given a template, check to see if the source of InputStream has been modified.

定義:
クラス ResourceLoader 内の isSourceModified

getLastModified

public long getLastModified(Resource resource)
クラス 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.

定義:
クラス ResourceLoader 内の getLastModified

getResourceStream

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

定義:
クラス ResourceLoader 内の getResourceStream
パラメータ:
name - name of template
戻り値:
InputStream containing template
例外:
ResourceNotFoundException

readLastModified

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

パラメータ:
resource - Resource object we are finding timestamp of
i_operation - string for logging, indicating caller's intention
戻り値:
timestamp as long

openDbConnection

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

戻り値:
connection
例外:
java.lang.Exception

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()

パラメータ:
conn - connection to datasource
columnNames - columns to fetch from datasource
templateName - name of template to fetch
戻り値:
result set from query
例外:
java.sql.SQLException


Copyright © 2003 Apache Software Foundation. All Rights Reserved.