org.apache.cocoon.components.source.impl
Class CachingSource

java.lang.Object
  extended byorg.apache.avalon.framework.logger.AbstractLogEnabled
      extended byorg.apache.cocoon.components.source.impl.CachingSource
All Implemented Interfaces:
Initializable, LogEnabled, Serviceable, Source, XMLizable
Direct Known Subclasses:
TraversableCachingSource

public class CachingSource
extends AbstractLogEnabled
implements Source, Serviceable, Initializable, XMLizable

This class implements a proxy like source that uses another source to get the content. This implementation can cache the content for a given period of time

Syntax for Protocol

cached:http://www.apache.org/[?cocoon:cache-expires=60&cocoon:cache-name=main]

The above examples show how the real source http://www.apache.org is wrapped and the cached contents is used for 60 seconds. The second querystring parameter instructs that the cache key be extended with the string main. This allows the use of multiple cache entries for the same source.

The value of the expires parameter holds some additional semantics. Specifying -1 will yield the cached response to be considered valid always. 0 can be used to achieve the exact opposite. That is to say, the cached contents will be thrown out and updated immediately and unconditionally.

Version:
CVS $Id: CachingSource.java 76260 2004-11-18 17:46:32Z unico $

Nested Class Summary
protected static class CachingSource.SourceMeta
          Data holder for caching Source meta info.
 
Field Summary
protected  boolean async
          asynchronic refresh strategy ?
protected  Cache cache
          The current cache
static String CACHE_EXPIRES_PARAM
           
static String CACHE_NAME_PARAM
           
protected  IdentifierCacheKey cacheKey
          The key used in the store
protected  String cacheName
          cache key extension
protected  int expires
          number of seconds before cached object becomes invalid
protected  boolean freshMeta
          Did we just update meta info?
protected  ServiceManager manager
          The ServiceManager
protected  String protocol
          The used protocol
protected  CachedSourceResponse response
          The cached response (if any)
protected  Source source
          The source object for the real content
protected  String uri
          The full location string
 
Constructor Summary
CachingSource(String protocol, String uri, Source source, int expires, String cacheName, boolean async)
          Construct a new object.
 
Method Summary
protected  CachingSource.SourceMeta createMeta()
           
 void dispose()
          Cleanup.
 boolean exists()
           
protected  IdentifierCacheKey getCacheKey()
          Return the used key.
 long getContentLength()
          Get the content length of the source or -1 if it is not possible to determine the length.
protected  long getExpiration()
          Expires (in milli-seconds)
 InputStream getInputStream()
          Return an InputStream object to read from the source.
 long getLastModified()
          Get the last modification date.
 String getMimeType()
          The mime-type of the content described by this object.
 String getScheme()
          Return the protocol identifier.
protected  String getSourceURI()
          Return the uri of the cached source.
 String getURI()
          Return the unique identifer for this source
 SourceValidity getValidity()
          Get the Validity object.
protected  void initBinaryResponse()
          Initialize the cached response with binary contents.
 void initialize()
          Initialize the Source.
protected  void initMeta(CachingSource.SourceMeta meta, Source source)
           
protected  void initMetaResponse()
          Initialize the cached response with meta info.
protected  void initXMLResponse(boolean refresh)
          Initialize the cached response with XML contents.
protected  byte[] readBinaryResponse(Source source)
          Read binary content from source.
protected  CachingSource.SourceMeta readMeta(Source source)
          Read meta data from source.
protected  byte[] readXMLResponse(Source source, byte[] binary, ServiceManager manager)
          Read XML content from source.
 void refresh()
          Refresh this object and update the last modified date and content length.
protected  void remove()
           
 void service(ServiceManager manager)
          Set the ServiceManager.
 void toSAX(ContentHandler contentHandler)
          Generates SAX events representing the object's state.
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CACHE_EXPIRES_PARAM

public static final String CACHE_EXPIRES_PARAM
See Also:
Constant Field Values

CACHE_NAME_PARAM

public static final String CACHE_NAME_PARAM
See Also:
Constant Field Values

manager

protected ServiceManager manager
The ServiceManager


cache

protected Cache cache
The current cache


source

protected Source source
The source object for the real content


response

protected CachedSourceResponse response
The cached response (if any)


freshMeta

protected boolean freshMeta
Did we just update meta info?


uri

protected final String uri
The full location string


protocol

protected final String protocol
The used protocol


cacheKey

protected final IdentifierCacheKey cacheKey
The key used in the store


expires

protected final int expires
number of seconds before cached object becomes invalid


cacheName

protected final String cacheName
cache key extension


async

protected final boolean async
asynchronic refresh strategy ?

Constructor Detail

CachingSource

public CachingSource(String protocol,
                     String uri,
                     Source source,
                     int expires,
                     String cacheName,
                     boolean async)
Construct a new object.

Method Detail

service

public void service(ServiceManager manager)
             throws ServiceException
Set the ServiceManager.

Specified by:
service in interface Serviceable
Throws:
ServiceException

initialize

public void initialize()
                throws Exception
Initialize the Source.

Specified by:
initialize in interface Initializable
Throws:
Exception

dispose

public void dispose()
Cleanup.


initMetaResponse

protected void initMetaResponse()
                         throws IOException
Initialize the cached response with meta info.

Throws:
IOException - if an the binary response could not be initialized

initBinaryResponse

protected void initBinaryResponse()
                           throws IOException
Initialize the cached response with binary contents.

Throws:
IOException - if an the binary response could not be initialized

initXMLResponse

protected void initXMLResponse(boolean refresh)
                        throws SAXException,
                               IOException,
                               CascadingIOException
Initialize the cached response with XML contents.

Parameters:
refresh - whether to force refresh.
Throws:
SAXException - if something happened during xml processing
IOException - if an IO level error occured
CascadingIOException - wraps all other exception types

getScheme

public String getScheme()
Return the protocol identifier.

Specified by:
getScheme in interface Source

getContentLength

public long getContentLength()
Get the content length of the source or -1 if it is not possible to determine the length.

Specified by:
getContentLength in interface Source

getLastModified

public long getLastModified()
Get the last modification date.

Specified by:
getLastModified in interface Source
Returns:
The last modification in milliseconds since January 1, 1970 GMT or 0 if it is unknown

getMimeType

public String getMimeType()
The mime-type of the content described by this object. If the source is not able to determine the mime-type by itself this can be null.

Specified by:
getMimeType in interface Source

getInputStream

public InputStream getInputStream()
                           throws IOException,
                                  SourceException
Return an InputStream object to read from the source.

Specified by:
getInputStream in interface Source
Throws:
IOException
SourceException

getURI

public String getURI()
Return the unique identifer for this source

Specified by:
getURI in interface Source

exists

public boolean exists()
Specified by:
exists in interface Source
See Also:
Source.exists()

getValidity

public SourceValidity getValidity()
Get the Validity object. This can either wrap the last modification date or the expires information or... If it is currently not possible to calculate such an information null is returned.

Specified by:
getValidity in interface Source

refresh

public void refresh()
Refresh this object and update the last modified date and content length. This method will try to refresh the cached contents.

Specified by:
refresh in interface Source

remove

protected void remove()

toSAX

public void toSAX(ContentHandler contentHandler)
           throws SAXException
Generates SAX events representing the object's state.

Specified by:
toSAX in interface XMLizable
Throws:
SAXException

getSourceURI

protected String getSourceURI()
Return the uri of the cached source.


getCacheKey

protected IdentifierCacheKey getCacheKey()
Return the used key.


getExpiration

protected long getExpiration()
Expires (in milli-seconds)


readXMLResponse

protected byte[] readXMLResponse(Source source,
                                 byte[] binary,
                                 ServiceManager manager)
                          throws SAXException,
                                 IOException,
                                 CascadingIOException
Read XML content from source.

Returns:
content from source
Throws:
SAXException
IOException
CascadingIOException

readBinaryResponse

protected byte[] readBinaryResponse(Source source)
                             throws IOException,
                                    SourceNotFoundException
Read binary content from source.

Returns:
content from source
Throws:
IOException
SourceNotFoundException

readMeta

protected final CachingSource.SourceMeta readMeta(Source source)
                                           throws IOException
Read meta data from source.

Returns:
source meta data
Throws:
IOException

createMeta

protected CachingSource.SourceMeta createMeta()

initMeta

protected void initMeta(CachingSource.SourceMeta meta,
                        Source source)
                 throws IOException
Throws:
IOException


Copyright ? 1999-2005 The Apache Software Foundation. All Rights Reserved.