org.apache.cocoon.reading
Class DatabaseReader

java.lang.Object
  extended byorg.apache.cocoon.util.AbstractLogEnabled
      extended byorg.apache.cocoon.reading.AbstractReader
          extended byorg.apache.cocoon.reading.ServiceableReader
              extended byorg.apache.cocoon.reading.DatabaseReader
All Implemented Interfaces:
org.apache.cocoon.caching.CacheableProcessingComponent, Configurable, Disposable, Poolable, org.apache.cocoon.reading.Reader, Recyclable, Serviceable, org.apache.cocoon.sitemap.SitemapModelComponent, org.apache.cocoon.sitemap.SitemapOutputComponent

public class DatabaseReader
extends org.apache.cocoon.reading.ServiceableReader
implements Configurable, Disposable, org.apache.cocoon.caching.CacheableProcessingComponent

This Reader pulls a resource from a database. It is configured with the Connection to use, parameters specify the table and column to pull the image from, and source specifies the source key information.

Version:
$Id: DatabaseReader.html 1304280 2012-03-23 11:18:01Z ilgrosso $

Field Summary
 
Fields inherited from class org.apache.cocoon.reading.ServiceableReader
manager
 
Fields inherited from class org.apache.cocoon.reading.AbstractReader
objectModel, out, parameters, resolver, source
 
Fields inherited from interface org.apache.cocoon.reading.Reader
ROLE
 
Constructor Summary
DatabaseReader()
           
 
Method Summary
 void configure(Configuration conf)
          Configure the Reader so that we can use the same database for all instances.
 void dispose()
           
 void generate()
          Generates the resource we need to retrieve verbatim from the database.
 Serializable getKey()
          Generate the unique key.
 String getMimeType()
           
protected  String getQuery()
          This method builds the query string used for accessing the database.
 SourceValidity getValidity()
          Generate the validity object.
protected  boolean modifiedSince(ResultSet set, org.apache.cocoon.environment.Request request, org.apache.cocoon.environment.Response response)
          Tests whether a resource has been modified or not.
 void recycle()
           
 void serialize(org.apache.cocoon.environment.Response response)
          This method actually performs the serialization.
 void setDataSource(DataSource dataSource)
          Set a data source to be used by this component.
 void setup(org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String src, Parameters par)
          Set the SourceResolver the object model Map, the source and sitemap Parameters used to process the request.
 
Methods inherited from class org.apache.cocoon.reading.ServiceableReader
service
 
Methods inherited from class org.apache.cocoon.reading.AbstractReader
getLastModified, setOutputStream, shouldSetContentLength
 
Methods inherited from class org.apache.cocoon.util.AbstractLogEnabled
getLogger, setLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseReader

public DatabaseReader()
Method Detail

setDataSource

public void setDataSource(DataSource dataSource)
Set a data source to be used by this component.

Parameters:
dataSource - A datasource.

configure

public void configure(Configuration conf)
               throws ConfigurationException
Configure the Reader so that we can use the same database for all instances.

Specified by:
configure in interface Configurable
Throws:
ConfigurationException

setup

public void setup(org.apache.cocoon.environment.SourceResolver resolver,
                  Map objectModel,
                  String src,
                  Parameters par)
           throws org.apache.cocoon.ProcessingException,
                  SAXException,
                  IOException
Set the SourceResolver the object model Map, the source and sitemap Parameters used to process the request.

Specified by:
setup in interface org.apache.cocoon.sitemap.SitemapModelComponent
Throws:
org.apache.cocoon.ProcessingException
SAXException
IOException

generate

public void generate()
              throws org.apache.cocoon.ProcessingException,
                     SAXException,
                     IOException
Generates the resource we need to retrieve verbatim from the database. Granted, this can be used for any resource from a database, so we may want to get rid of the bias toward images. This reader requires a number of parameters:
   <parameter name="table" value="database_table_name"/>
   <parameter name="image" value="database_resource_column_name"/>
   <parameter name="key" value="database_lookup_key_column_name"/>
 
Please note that if any of those parameters are missing, this Reader cannot function. There are a number of other parameters that allow you to provide hints for the reader to optimize resource use:
   <parameter name="last-modified" value="database_timestamp_column_name"/>
   <parameter name="content-type" value="content_mime_type"/>
   <parameter name="type-column" value="database_content_mime_type_column"/>
   <parameter name="expires" value="number_of_millis_before_refresh"/>
   <parameter name="where" value="alternate_key = 'foo'"/>
   <parameter name="order-by" value="alternate_key DESC"/>
 
Lastly, the key value is derived from the value of the source string.

Specified by:
generate in interface org.apache.cocoon.reading.Reader
Throws:
org.apache.cocoon.ProcessingException
SAXException
IOException

getQuery

protected String getQuery()
                   throws org.apache.cocoon.ProcessingException
This method builds the query string used for accessing the database. If the required parameters do not exist, then we cannot build a correct query.

Throws:
org.apache.cocoon.ProcessingException

modifiedSince

protected boolean modifiedSince(ResultSet set,
                                org.apache.cocoon.environment.Request request,
                                org.apache.cocoon.environment.Response response)
                         throws SQLException
Tests whether a resource has been modified or not. As Blobs and database columns usually do not have intrinsic dates on them (at least easily accessible), we have to have a database column that holds a date for the resource. Please note, that the database column must be a Timestamp column. In the absence of such a column this method always returns true. This is because databases are much more prone to change than filesystems, and don't have intrinsic timestamps on column updates.

Throws:
SQLException

serialize

public void serialize(org.apache.cocoon.environment.Response response)
               throws IOException,
                      SQLException
This method actually performs the serialization.

Throws:
IOException
SQLException

getKey

public Serializable getKey()
Generate the unique key. This key must be unique inside the space of this component.

Specified by:
getKey in interface org.apache.cocoon.caching.CacheableProcessingComponent
Returns:
The generated key hashes the src

getValidity

public SourceValidity getValidity()
Generate the validity object.

Specified by:
getValidity in interface org.apache.cocoon.caching.CacheableProcessingComponent
Returns:
The generated validity object or null if the component is currently not cacheable.

recycle

public void recycle()
Specified by:
recycle in interface Recyclable
See Also:
AbstractReader.recycle()

dispose

public void dispose()
Specified by:
dispose in interface Disposable
See Also:
Disposable.dispose()

getMimeType

public String getMimeType()
Specified by:
getMimeType in interface org.apache.cocoon.sitemap.SitemapOutputComponent
See Also:
AbstractReader.getMimeType()


Copyright 1999-2008 The Apache Software Foundation. All Rights Reserved.