fnug.resource
Interface Resource

All Superinterfaces:
HasLastModifiedBytes
All Known Subinterfaces:
AggregatedResource, ResourceCollection
All Known Implementing Classes:
AbstractAggregatedResource, AbstractResource, DefaultBundleResource, DefaultCompressedResource, DefaultResource, DefaultResourceCollection

public interface Resource
extends HasLastModifiedBytes

Abstraction of a file with a name, bytes and last modified date. Resources can either be a direct representation of a file or be produced by name other resources. Resource instances exist also for non-existant files, in which case getBytes() returns an empty array, and getLastModified() returns -1.

Resources are cached in memory.

Author:
Martin Algesten

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).
 List<String> findRequiresTags()
          Scans the resource for the sequence "* @requires" which makes this resource depend on others.
 String getBasePath()
          Base path is the fist half of a 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 getBasePath() + 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.
 

Method Detail

getBasePath

String getBasePath()
Base path is the fist half of a 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
getBasePath() /my/bundle/
getPath() path/to/resource.js
getFullPath() /my/bundle/path/to/resource.js

Returns:
Base path part of the resource full path.

getPath

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

A path never starts with a /.

See getBasePath() for full example.

Returns:
Path part of a resource full path.

getFullPath

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

Returns:
Full path of the resource.

getContentType

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).

Returns:
the content type.

isJs

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

Returns:
true if this resource is javascript.

isCss

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

Returns:
true if this resource is css.

getBytes

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 checkModified().

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

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

getLastModified

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 checkModified().

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

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

checkModified

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 getBytes() or getLastModified().

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.

findRequiresTags

List<String> findRequiresTags()
Scans the resource for the sequence "* @requires" which makes this resource depend on others. This will only happen if the resource is found to be text, that is getContentType() must be text/xxx, such as text/javascript.

Example:

 /**
  * @requires some/other/javascript.js
  * @requires some/other/styles.css
  */

Returns:
The list of parsed resources found in the file.


Copyright © 2012. All Rights Reserved.