simple.http.serve
Class CacheContentFactory

java.lang.Object
  extended by simple.http.serve.CacheContentFactory
All Implemented Interfaces:
ContentFactory

public class CacheContentFactory
extends java.lang.Object
implements ContentFactory

The CacheContentFactory is used to create a factory that can be used to create and cache files as instances of the Content interface. This is useful when a hot spot cache is required for frequently referenced files. The defaults for the file cache use a maximum file size of eight kilobytes.

This factory object can limit the items stored within the cache by file size. This ensures that the resources consumed by the file cache can be restricted. Also the concurrency parameters for the cache can be specified as regions and region limits.

Author:
Niall Gallagher
See Also:
CacheContext, TimeCache

Field Summary
protected  TimeCache cache
          This is used to cache the content objects created.
protected  int size
          This is the maximum allowed size for a cached file.
 
Constructor Summary
protected CacheContentFactory()
          Constructor for the CacheContentFactory object.
  CacheContentFactory(int size)
          Constructor for the CacheContentFactory object.
  CacheContentFactory(int size, int regions, int limit)
          Constructor for the CacheContentFactory object.
 
Method Summary
protected  Content getContent(Context context, java.lang.String target)
          This is used to create the Content instances.
protected  Content getContent(Context context, java.lang.String target, int timeout)
          This method allows a size to be specified for the maximum buffer size.
 Content getInstance(Context context, java.lang.String target)
          This implementation of the getInstance method will cache the created Content object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cache

protected TimeCache cache
This is used to cache the content objects created.


size

protected int size
This is the maximum allowed size for a cached file.

Constructor Detail

CacheContentFactory

protected CacheContentFactory()
Constructor for the CacheContentFactory object. This object provides caching for any context implementation that makes heavy use of Content objects. Caching done by this object ensures that the content objects are kept in memory for at least one minute, to change this behaviour this object can be subclassed and use different settings.


CacheContentFactory

public CacheContentFactory(int size)
Constructor for the CacheContentFactory object. This object provides caching for any context implementation that makes heavy use of Content objects. Caching done by this object ensures that the content objects are kept in memory for at least one minute, to change this behaviour this object can be subclassed and use different settings.

Parameters:
size - this is the maximum allow file size to cache

CacheContentFactory

public CacheContentFactory(int size,
                           int regions,
                           int limit)
Constructor for the CacheContentFactory object. This object provides caching for any context implementation that makes heavy use of Content objects. Caching done by this object ensures that the content objects are kept in memory for at least one minute, to change this behaviour this object can be subclassed and use different settings.

Parameters:
size - this is the maximum allow file size to cache
regions - this is the number of locks the cache uses
limit - this is the maximum capacity of each LRU list
Method Detail

getInstance

public Content getInstance(Context context,
                           java.lang.String target)
                    throws ContentException
This implementation of the getInstance method will cache the created Content object. This is done to increase the performance of the Context for objects that are requested frequently. The size of the file that is cached is dependant on the construction parameters.

If the file referenced is greater than the specified maximum then a one kilobyte buffer is cached for that file as is used to transfer the contents of the file to the output stream.

Specified by:
getInstance in interface ContentFactory
Parameters:
target - the request URI for the content required
context - this is the context used to locate the file
Returns:
this returns a Content instance
Throws:
ContentException - this is thrown if there is an error locating the specified file, or creating the object

getContent

protected Content getContent(Context context,
                             java.lang.String target,
                             int timeout)
                      throws java.io.IOException
This method allows a size to be specified for the maximum buffer size. If the file resource is less than the buffer size then the entire contents of the file are buffered within the issued implementation. This will allocate up to 1 kilobyte for files that are larger than the specified buffer size.

This implementation of the getContent method will cache the created Content object. This is done to increase the performance of the Context for objects that are requested frequently. The buffer size that can be specified provides a guide for how much memory should be taken up with the content cached object.

Parameters:
context - this is the object used to locate the file
target - this is the request URI that identifies the file
timeout - the number of milliseconds to cache the file
Throws:
java.io.IOException - this is thrown if the file resource does not exist or cannot be accessed

getContent

protected Content getContent(Context context,
                             java.lang.String target)
                      throws java.io.IOException
This is used to create the Content instances. The getInstance method can be used by subclasses that want to introduce dynamic Content objects. This enables the getContent method to cache the resulting instances without having to know what types they are.

By default the CacheContentFactory produces objects that will write static content as it appears on the underlying file system. This uses the specified size to ensure that the buffers used by the content are not larger than the maximum.

Parameters:
context - this is the object used to locate the file
target - this is the request URI that identifies the file
Throws:
java.io.IOException - this is thrown if the file resource does not exist or cannot be accessed