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

java.lang.Object
  extended byorg.apache.cocoon.components.source.impl.CachingSourceFactory
All Implemented Interfaces:
BeanNameAware, SourceFactory, URIAbsolutizer

public class CachingSourceFactory
extends Object
implements URIAbsolutizer, SourceFactory, BeanNameAware

This class implements a proxy like source caches the contents of the source it wraps. This implementation can cache the content either for a given period of time or until an external event invalidates the cached response.

When using the timeout approach you have a choice between two separate revalidation strategies:

Protocol syntax

The URL needs to contain the URL of the cached source, an expiration period in seconds, and optionally a cache key: cached:http://www.apache.org/[?cocoon:cache-expires=60][&cocoon:cache-name=main][&cocoon:cache-fail=true].

The above examples shows 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 cache-fail argument lets subsequent syncronous requests, that have to be refreshed, fail, in the case that the wrapped source can't be reached. The default value for cache-fail is true.

This factory creates either instances of CachingSource or TraversableCachingSource depending on the whether the wrapped Source is an instance of TraversableSource.

If other types of sources should be supported, subclass this class and override the method instantiateSource(String, String, Source, int, String, boolean).

In order to use the CachingSourceFactory in Cocoon, you have to set it up as Spring bean. For this purpose you can use the abstract bean definition with the name org.apache.excalibur.source.SourceFactory/caching::abstract:
<bean name="org.apache.excalibur.source.SourceFactory/cached::abstract" class="org.apache.cocoon.components.source.impl.CachingSourceFactory" abstract="true"> <property name="cache" ref="org.apache.cocoon.caching.Cache"/> <property name="sourceResolver" ref="org.apache.excalibur.source.SourceResolver"/> <property name="serviceManager" ref="org.apache.avalon.framework.service.ServiceManager"/> <property name="validityStrategy"> <bean class="org.apache.cocoon.components.source.impl.ExpiresCachingSourceValidityStrategy"/> </property> </bean>

It is also possible to override dependencies partially there.

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

Field Summary
protected  boolean async
          Asynchronous ?
protected  org.apache.cocoon.caching.Cache cache
          The cache
protected  int defaultExpires
           
protected  SourceRefresher refresher
          The refresher
protected  SourceResolver resolver
          The SourceResolver
protected  String scheme
          Protocol prefix / factory name
protected  ServiceManager serviceManager
          The Cocoon service manager
protected  CachingSourceValidityStrategy validityStrategy
          Validity strategy implementation
 
Fields inherited from interface org.apache.excalibur.source.SourceFactory
ROLE
 
Constructor Summary
CachingSourceFactory()
           
 
Method Summary
 String absolutize(String baseURI, String location)
           
protected  CachingSource createCachingSource(String uri, String wrappedUri, Source wrappedSource, int expires, String cacheName, boolean fail)
          Actually creates a new CachingSource.
 Source getSource(String location, Map parameters)
          Get a Source object.
protected  CachingSource instantiateSource(String uri, String wrappedUri, Source wrappedSource, int expires, String cacheName, boolean fail)
           
 void release(Source source)
          Release a Source object.
 void setAsync(boolean async)
          Optional property whether the source is asyncronous.
 void setBeanName(String beanName)
           
 void setCache(org.apache.cocoon.caching.Cache cache)
          Mandatory dependency on an implementation of Cache.
 void setDefaultExpires(int expires)
          Optional property the default expiry time in seconds.
 void setServiceManager(ServiceManager serviceManager)
          Mandatory dependency on an implementation of ServiceManager.
 void setSourceRefresher(SourceRefresher refresher)
          Optional dependency on an implementation of SourceRefresher.
 void setSourceResolver(SourceResolver resolver)
          Mandatory dependency on an implementation of SourceResolver.
 void setValidityStrategy(CachingSourceValidityStrategy validityStrategy)
          Mandatory dependency on an implementation of CachingSourceValidityStrategy.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

scheme

protected String scheme
Protocol prefix / factory name


async

protected boolean async
Asynchronous ?


defaultExpires

protected int defaultExpires

validityStrategy

protected CachingSourceValidityStrategy validityStrategy
Validity strategy implementation


cache

protected org.apache.cocoon.caching.Cache cache
The cache


refresher

protected SourceRefresher refresher
The refresher


serviceManager

protected ServiceManager serviceManager
The Cocoon service manager


resolver

protected SourceResolver resolver
The SourceResolver

Constructor Detail

CachingSourceFactory

public CachingSourceFactory()
Method Detail

getSource

public Source getSource(String location,
                        Map parameters)
                 throws IOException
Get a Source object.

Specified by:
getSource in interface SourceFactory
Parameters:
parameters - This is optional.
Throws:
IOException

createCachingSource

protected CachingSource createCachingSource(String uri,
                                            String wrappedUri,
                                            Source wrappedSource,
                                            int expires,
                                            String cacheName,
                                            boolean fail)
                                     throws SourceException
Actually creates a new CachingSource. Can be overriden in subclasses

Throws:
SourceException

instantiateSource

protected CachingSource instantiateSource(String uri,
                                          String wrappedUri,
                                          Source wrappedSource,
                                          int expires,
                                          String cacheName,
                                          boolean fail)

release

public void release(Source source)
Release a Source object.

Specified by:
release in interface SourceFactory

absolutize

public String absolutize(String baseURI,
                         String location)
Specified by:
absolutize in interface URIAbsolutizer
See Also:
URIAbsolutizer.absolutize(java.lang.String, java.lang.String)

setCache

public void setCache(org.apache.cocoon.caching.Cache cache)
Mandatory dependency on an implementation of Cache.


setValidityStrategy

public void setValidityStrategy(CachingSourceValidityStrategy validityStrategy)
Mandatory dependency on an implementation of CachingSourceValidityStrategy.


setServiceManager

public void setServiceManager(ServiceManager serviceManager)
Mandatory dependency on an implementation of ServiceManager.


setSourceResolver

public void setSourceResolver(SourceResolver resolver)
Mandatory dependency on an implementation of SourceResolver.


setSourceRefresher

public void setSourceRefresher(SourceRefresher refresher)
Optional dependency on an implementation of SourceRefresher.


setAsync

public void setAsync(boolean async)
Optional property whether the source is asyncronous. The default value is false.


setDefaultExpires

public void setDefaultExpires(int expires)
Optional property the default expiry time in seconds. Default is -1 which means that is valid forever.


setBeanName

public void setBeanName(String beanName)
Specified by:
setBeanName in interface BeanNameAware


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