fnug.resource
Class DefaultResource

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

public class DefaultResource
extends AbstractResource

Default file resource implementation. Relies on Class.getResource(String) to do its magic. The class also handles the case where the class loader resolved resource is inside a jar-file. That jar file is then unpacked into a temporary directory and the file last modified is read from there.

Author:
Martin Algesten

Nested Class Summary
 
Nested classes/interfaces inherited from class fnug.resource.AbstractResource
AbstractResource.Entry
 
Field Summary
static int DEFAULT_CHECK_MODIFIED_INTERVAL
          Default interval for checking the modified time.
 
Fields inherited from class fnug.resource.AbstractResource
CONTENT_TYPE_TEXT_CSS, CONTENT_TYPE_TEXT_JAVASCRIPT
 
Constructor Summary
DefaultResource(String basePath, String path)
          Creates a new resource setting the base path and path.
DefaultResource(String basePath, String path, int checkModifiedInterval)
          Constructs setting base path path and interval for checking.
 
Method Summary
protected  String decode(String url)
          The URLClassLoader.getResource() encodes the returned URL as UTF-8, but it will not encode the '+' sign (encoding done via internal sun.net.www.ParseUtil).
protected  URL doGetResourceURL(String fullPath)
          Returns the URL of the resource.
protected  File extractJarFile(URL url)
           
 List<String> findRequiresTags()
          Scans the resource for the sequence "* @requires" which makes this resource depend on others.
static URL getResourceURL(String fullPath)
          Returns the URL of the resource.
protected  AbstractResource.Entry readEntry()
          Reads the entry from the class loader using Class.getResource(String).
protected  long readLastModified()
          Returns the last modified date of the file.
protected  boolean readLastModifiedAllowed()
          Tells if we are allowed to check the last modified date.
 
Methods inherited from class fnug.resource.AbstractResource
checkModified, ensureReadEntry, getBasePath, getBytes, getContentType, getFullPath, getLastModified, getPath, isCss, isJs, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_CHECK_MODIFIED_INTERVAL

public static final int DEFAULT_CHECK_MODIFIED_INTERVAL
Default interval for checking the modified time. Set to 3000 .

See Also:
Constant Field Values
Constructor Detail

DefaultResource

public DefaultResource(String basePath,
                       String path,
                       int checkModifiedInterval)
Constructs setting base path path and interval for checking.

Parameters:
basePath - The base path of the resource. See AbstractResource.getBasePath().
path - The path of the resource. See AbstractResource.getPath().
checkModifiedInterval - the interval in millisecond that we are to check the resource last modified date. Any sooner checks will just returned the previously read value. A value of 0 will disable modified checks.

DefaultResource

public DefaultResource(String basePath,
                       String path)
Creates a new resource setting the base path and path. Assumed DEFAULT_CHECK_MODIFIED_INTERVAL for checking modified times.

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

readEntry

protected AbstractResource.Entry readEntry()
Reads the entry from the class loader using Class.getResource(String). Also handles the case where that resource is in a jar file.

Specified by:
readEntry in class AbstractResource
Returns:
the bytes and last modified date.

doGetResourceURL

protected URL doGetResourceURL(String fullPath)
Returns the URL of the resource.

Returns:
the url of the resource or null if not found.

getResourceURL

public static URL getResourceURL(String fullPath)
Returns the URL of the resource.

Returns:
the url of the resource or null if not found.

readLastModified

protected long readLastModified()
Returns the last modified date of the file. If the file was inside a jar-file, the jar file time stamp is also checked, and potentially extracted again.

Specified by:
readLastModified in class AbstractResource
Returns:
the last modified date of the underlying data source, such as a file.

readLastModifiedAllowed

protected boolean readLastModifiedAllowed()
Tells if we are allowed to check the last modified date. This looks at the check interval to assert whether checking is allowed.

Returns:
whether a last modified date is allowed to be checked.

decode

protected String decode(String url)
The URLClassLoader.getResource() encodes the returned URL as UTF-8, but it will not encode the '+' sign (encoding done via internal sun.net.www.ParseUtil). URLDecoder however will replace any '+' with space ' '.

Parameters:
url - url to decode
Returns:
the decoded url

extractJarFile

protected File extractJarFile(URL url)
                       throws IOException
Throws:
IOException

findRequiresTags

public 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 Resource.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.