org.norther.tammi.core.cache
Class BufferedResource<T>

java.lang.Object
  extended by org.norther.tammi.core.cache.BufferedResource<T>
Type Parameters:
T - the resource type.
All Implemented Interfaces:
Serializable, EventListener, CacheListener, Resource<T>
Direct Known Subclasses:
TemporaryResource

public class BufferedResource<T>
extends Object
implements Resource<T>, CacheListener, Serializable

A buffered implementation of Resource maintaining content.

Version:
$Id: BufferedResource.java,v 1.14 2010/01/13 21:39:07 cvsimp Exp $
Author:
Ilkka Priha
See Also:
Serialized Form

Constructor Summary
BufferedResource(Resource<? extends T> res)
          Constructs a new resource.
BufferedResource(Resource<? extends T> res, boolean keep)
          Constructs a new resource with a keep lost flag.
BufferedResource(Resource<T> res, byte[] content)
          Constructs a new resource with ready-made content.
 
Method Summary
 void activated(CachedEntryEvent event)
          Notifies the object that it has been activated to the cache.
 void expired(CachedEntryEvent event)
          Notifies the object that it has been expired from the cache.
protected  byte[] getBuffer()
          Gets the byte buffer.
 long getCheckInterval()
          Gets the interval of checking validity of the resource.
protected  byte[] getContents(InputStream in)
          Gets the contents from the specified stream as bytes.
 InputStream getInputStream()
          Gets the resource as a stream.
 long getLastModified()
          Gets the resource modification time.
 long getLength()
          Gets the length of the resource.
 String getName()
          Gets the name of the resource.
 String getPath()
          Gets the path of the resource.
 Resource<? extends T> getResource()
          Gets the buffered resource.
 int getSizeLimit()
          Gets the size limit for a cached resource.
 T getSource()
          Gets the source of the resource.
 String getVersionTag()
          Gets the version tag of the resource.
 boolean isDeleteOnRelease()
          Checks whether the resource source should be deleted on release.
 boolean isExpireAfterAccess()
          Checks whether the resource should be expired after it has been accessed.
 boolean isValid()
          Checks whether the original resource is still valid.
 void passivated(CachedEntryEvent event)
          Notifies the object that it has been passivated from the cache.
 void release()
          Releases the cached resource.
 void removed(CachedEntryEvent event)
          Notifies the object that it has been removed from the cache.
 void setCheckInterval(long msecs)
          Sets the interval of checking validity of the resource.
 void setDeleteOnRelease(boolean flag)
          Sets whether the resource source should be deleted on release.
 void setExpireAfterAccess(boolean flag)
          Sets whether the resource should be expired after it has been accessed.
 void setPath(String path)
          Sets the path of the resource.
 void setSizeLimit(int limit)
          Sets a size limit for a resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BufferedResource

public BufferedResource(Resource<? extends T> res)
Constructs a new resource.

Parameters:
res - the resource.

BufferedResource

public BufferedResource(Resource<? extends T> res,
                        boolean keep)
Constructs a new resource with a keep lost flag.

Parameters:
res - the resource.
keep - the keep lost flag.

BufferedResource

public BufferedResource(Resource<T> res,
                        byte[] content)
Constructs a new resource with ready-made content.

Parameters:
res - the resource.
content - the content.
Method Detail

getVersionTag

public String getVersionTag()
Description copied from interface: Resource
Gets the version tag of the resource.

Specified by:
getVersionTag in interface Resource<T>
Returns:
the resource tag.

getName

public String getName()
Description copied from interface: Resource
Gets the name of the resource.

Specified by:
getName in interface Resource<T>
Returns:
the resource name.

getPath

public String getPath()
Description copied from interface: Resource
Gets the path of the resource.

Specified by:
getPath in interface Resource<T>
Returns:
the path of the resource.

setPath

public void setPath(String path)
Description copied from interface: Resource
Sets the path of the resource.

Specified by:
setPath in interface Resource<T>
Parameters:
path - the path of the resource.

getSource

public T getSource()
Description copied from interface: Resource
Gets the source of the resource.

Specified by:
getSource in interface Resource<T>
Returns:
the resurce source.

getLength

public long getLength()
Description copied from interface: Resource
Gets the length of the resource.

Specified by:
getLength in interface Resource<T>
Returns:
the resource length or -1 if not known.

getLastModified

public long getLastModified()
Description copied from interface: Resource
Gets the resource modification time.

Specified by:
getLastModified in interface Resource<T>
Returns:
the resource modification time.

getCheckInterval

public long getCheckInterval()
Description copied from interface: Resource
Gets the interval of checking validity of the resource.

Specified by:
getCheckInterval in interface Resource<T>
Returns:
the check interval in milliseconds.

setCheckInterval

public void setCheckInterval(long msecs)
Description copied from interface: Resource
Sets the interval of checking validity of the resource.

Specified by:
setCheckInterval in interface Resource<T>
Parameters:
msecs - the check interval in milliseconds (-1 = only once).

isExpireAfterAccess

public boolean isExpireAfterAccess()
Description copied from interface: Resource
Checks whether the resource should be expired after it has been accessed.

Specified by:
isExpireAfterAccess in interface Resource<T>
Returns:
true if to be expired, false otherwise.

setExpireAfterAccess

public void setExpireAfterAccess(boolean flag)
Description copied from interface: Resource
Sets whether the resource should be expired after it has been accessed.

Specified by:
setExpireAfterAccess in interface Resource<T>
Parameters:
flag - true if to be expired, false otherwise.

isDeleteOnRelease

public boolean isDeleteOnRelease()
Description copied from interface: Resource
Checks whether the resource source should be deleted on release.

Specified by:
isDeleteOnRelease in interface Resource<T>
Returns:
true if to be deleted, false otherwise.

setDeleteOnRelease

public void setDeleteOnRelease(boolean flag)
Description copied from interface: Resource
Sets whether the resource source should be deleted on release.

Specified by:
setDeleteOnRelease in interface Resource<T>
Parameters:
flag - true if to be deleted, false otherwise.

getSizeLimit

public int getSizeLimit()
Description copied from interface: Resource
Gets the size limit for a cached resource.

Specified by:
getSizeLimit in interface Resource<T>
Returns:
the limit for a resource size in bytes.

setSizeLimit

public void setSizeLimit(int limit)
Description copied from interface: Resource
Sets a size limit for a resource.

Specified by:
setSizeLimit in interface Resource<T>
Parameters:
limit - a limit for a resource size in bytes (-1 = unlimited).

getInputStream

public InputStream getInputStream()
                           throws IOException
Description copied from interface: Resource
Gets the resource as a stream.

Specified by:
getInputStream in interface Resource<T>
Returns:
the resource as a stream.
Throws:
IOException - on I/O errors.

isValid

public boolean isValid()
Description copied from interface: Resource
Checks whether the original resource is still valid.

Specified by:
isValid in interface Resource<T>
Returns:
true for a valid resource, otherwise false.

release

public void release()
Description copied from interface: Resource
Releases the cached resource.

Specified by:
release in interface Resource<T>

expired

public void expired(CachedEntryEvent event)
Description copied from interface: CacheListener
Notifies the object that it has been expired from the cache.

Specified by:
expired in interface CacheListener
Parameters:
event - the cached entry event.

removed

public void removed(CachedEntryEvent event)
Description copied from interface: CacheListener
Notifies the object that it has been removed from the cache.

Specified by:
removed in interface CacheListener
Parameters:
event - the cached entry event.

passivated

public void passivated(CachedEntryEvent event)
Description copied from interface: CacheListener
Notifies the object that it has been passivated from the cache.

Specified by:
passivated in interface CacheListener
Parameters:
event - the cached entry event.

activated

public void activated(CachedEntryEvent event)
Description copied from interface: CacheListener
Notifies the object that it has been activated to the cache.

Specified by:
activated in interface CacheListener
Parameters:
event - the cached entry event.

getResource

public Resource<? extends T> getResource()
Gets the buffered resource.

Returns:
the resource.

getBuffer

protected byte[] getBuffer()
                    throws IOException
Gets the byte buffer.

Returns:
the byte buffer.
Throws:
IOException - on I/O errors.

getContents

protected byte[] getContents(InputStream in)
                      throws IOException
Gets the contents from the specified stream as bytes.

Parameters:
in - the input stream to read from.
Returns:
the resource contents as bytes.
Throws:
IOException - on I/O errors.


Copyright © 2004 The Norther Organization. All rights reserved.