fnug.resource
Class AbstractResource

java.lang.Object
  extended by fnug.resource.AbstractResource
All Implemented Interfaces:
HasLastModifiedBytes, Resource
Direct Known Subclasses:
AbstractAggregatedResource, DefaultCompressedResource, DefaultResource

public abstract class AbstractResource
extends Object
implements Resource

Abstract supertype for Resource implementations. Reads a classpath resource /fnug/mime.types to get a FileTypeMap for getContentType().

Author:
Martin Algesten

Nested Class Summary
protected  class AbstractResource.Entry
          Wrapper class for returning data from readEntry().
 
Field Summary
static String CONTENT_TYPE_TEXT_CSS
          Content type text/css.
static String CONTENT_TYPE_TEXT_JAVASCRIPT
          Content type text/javascript.
 
Constructor Summary
protected AbstractResource(String basePath, String path)
          Constructor setting necessary fields.
 
Method Summary
 boolean checkModified()
          Compares this resource in memory held last modified date with the one on disk (or for aggregated resource, all the aggregates are checked).
static String contentTypeForPath(String path)
          Returns the content type for the given path by consulting a mime type map.
protected  boolean ensureReadEntry()
          Ensures the bytes and lastmodified is read from the underlying resource (such as a file).
 String getBasePath()
          Base path is the fist half of a Resource.getFullPath().
 byte[] getBytes()
          The bytes that is this resource data.
 String getContentType()
          The mime type of the resource such as text/javascript or text/css.
 String getFullPath()
          The full path, which is simply two strings concatenated as Resource.getBasePath() + Resource.getPath().
 long getLastModified()
          Tells this resource last modified date.
 String getPath()
          The second part of a full path, that is the part after the base path.
 boolean isCss()
          Shortcut to determine if this resource is css.
 boolean isJs()
          Shortcut to determine if this resource is javascript.
protected abstract  AbstractResource.Entry readEntry()
          Reads the actual entry providing the bytes and lastModified date back to the caller.
protected abstract  long readLastModified()
          Reads just the last modified date from the actual data source.
 String toString()
          Returns getPath().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface fnug.resource.Resource
findRequiresTags
 

Field Detail

CONTENT_TYPE_TEXT_CSS

public static final String CONTENT_TYPE_TEXT_CSS
Content type text/css.

See Also:
Constant Field Values

CONTENT_TYPE_TEXT_JAVASCRIPT

public static final String CONTENT_TYPE_TEXT_JAVASCRIPT
Content type text/javascript.

See Also:
Constant Field Values
Constructor Detail

AbstractResource

protected AbstractResource(String basePath,
                           String path)
Constructor setting necessary fields.

Parameters:
basePath - The base path of the resource, see getBasePath().
path - The path of the resource, see getPath().
Method Detail

getBasePath

public String getBasePath()
Base path is the fist half of a Resource.getFullPath(). It is tightly connected to the concept of a Bundle, where the bundle is "rooted" at a base path and any resource is resolved from this directory.

A base path always ends with a /.

For example, a bundle may be BundleConfig.basePath() to be rooted at /my/bundle/. In this case we get:

/my/bundle/path/to/resource.js
Resource.getBasePath() /my/bundle/
Resource.getPath() path/to/resource.js
Resource.getFullPath() /my/bundle/path/to/resource.js

Specified by:
getBasePath in interface Resource
Returns:
Base path part of the resource full path.

getPath

public String getPath()
The second part of a full path, that is the part after the base path.

A path never starts with a /.

See Resource.getBasePath() for full example.

Specified by:
getPath in interface Resource
Returns:
Path part of a resource full path.

getFullPath

public String getFullPath()
The full path, which is simply two strings concatenated as Resource.getBasePath() + Resource.getPath().

Specified by:
getFullPath in interface Resource
Returns:
Full path of the resource.

contentTypeForPath

public static String contentTypeForPath(String path)
Returns the content type for the given path by consulting a mime type map.

Parameters:
path - the path to consult.
Returns:
the content type.

getContentType

public String getContentType()
The mime type of the resource such as text/javascript or text/css. The implementation uses a FileTypeMap to map the file suffix to content type. Notice that this content type never includes the encoding (which is assumed to be UTF-8).

Specified by:
getContentType in interface Resource
Returns:
the content type.

isJs

public boolean isJs()
Shortcut to determine if this resource is javascript. Typically checked by looking at the Resource.getContentType().

Specified by:
isJs in interface Resource
Returns:
true if this resource is javascript.

isCss

public boolean isCss()
Shortcut to determine if this resource is css. Typically checked by looking at the Resource.getContentType().

Specified by:
isCss in interface Resource
Returns:
true if this resource is css.

getBytes

public byte[] getBytes()
The bytes that is this resource data. Notice that resource data is held in memory. This method only triggers an actual file system read if this is the first time the resource is being accessed. Likewise, aggregated resources are only built on first access, and then held. To trigger rebuilding call Resource.checkModified().

Notice that for resources pointing to non-existant files Resource.getBytes() return an empty array, never null (but Resource.getLastModified() returns -1).

Specified by:
getBytes in interface HasLastModifiedBytes
Specified by:
getBytes in interface Resource
Returns:
the timestamped bytes.

ensureReadEntry

protected final boolean ensureReadEntry()
Ensures the bytes and lastmodified is read from the underlying resource (such as a file). This will in turn call readEntry() which needs to be implemented to do the actual reading.

Returns:
true if the entry was read.

getLastModified

public long getLastModified()
Tells this resource last modified date. This only reads the file system on the first access, subsequent calls returns the in memory held data. A reread from disk can only be triggered using Resource.checkModified().

For non-existing resource, Resource.getLastModified() returns -1.

Specified by:
getLastModified in interface HasLastModifiedBytes
Specified by:
getLastModified in interface Resource
Returns:
the last modified of the HasLastModifiedBytes.getBytes().

checkModified

public boolean checkModified()
Compares this resource in memory held last modified date with the one on disk (or for aggregated resource, all the aggregates are checked). If the last modified on disk is found to be newer than the one in memory, the in memory data is dropped and reread on next Resource.getBytes() or Resource.getLastModified().

Specified by:
checkModified in interface Resource
Returns:
true if the file system resource (or aggregates in case of aggregated resources) has a newer last modified date which caused the in memory held data to be dropped.

readEntry

protected abstract AbstractResource.Entry readEntry()
Reads the actual entry providing the bytes and lastModified date back to the caller. This must be implemented in subclasses. It must just read the actual data without checking whether bytes or lastModified has been read already.

Returns:
the bytes and last modified date.

readLastModified

protected abstract long readLastModified()
Reads just the last modified date from the actual data source. This is used by checkModified() to compare the stored last modified with the one on disk.

Returns:
the last modified date of the underlying data source, such as a file.

toString

public String toString()
Returns getPath().

Overrides:
toString in class Object


Copyright © 2012. All Rights Reserved.