Class UrlModuleSourceProvider

java.lang.Object
org.mozilla.javascript.commonjs.module.provider.ModuleSourceProviderBase
org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider
All Implemented Interfaces:
Serializable, ModuleSourceProvider

public class UrlModuleSourceProvider extends ModuleSourceProviderBase
A URL-based script provider that can load modules against a set of base privileged and fallback URIs. It is deliberately not named "URI provider" but a "URL provider" since it actually only works against those URIs that are URLs (and the JRE has a protocol handler for them). It creates cache validators that are suitable for use with both file: and http: URL protocols. Specifically, it is able to use both last-modified timestamps and ETags for cache revalidation, and follows the HTTP cache expiry calculation model, and allows for fallback heuristic expiry calculation when no server specified expiry is provided.
Version:
$Id: UrlModuleSourceProvider.java,v 1.4 2011/04/07 20:26:12 hannes%helma.at Exp $
Author:
Attila Szegedi
See Also:
  • Constructor Details

    • UrlModuleSourceProvider

      public UrlModuleSourceProvider(Iterable<URI> privilegedUris, Iterable<URI> fallbackUris)
      Creates a new module script provider that loads modules against a set of privileged and fallback URIs. It will use a fixed default cache expiry of 60 seconds, and provide no security domain objects for the resource.
      Parameters:
      privilegedUris - an iterable providing the privileged URIs. Can be null if no privileged URIs are used.
      fallbackUris - an iterable providing the fallback URIs. Can be null if no fallback URIs are used.
    • UrlModuleSourceProvider

      public UrlModuleSourceProvider(Iterable<URI> privilegedUris, Iterable<URI> fallbackUris, UrlConnectionExpiryCalculator urlConnectionExpiryCalculator, UrlConnectionSecurityDomainProvider urlConnectionSecurityDomainProvider)
      Creates a new module script provider that loads modules against a set of privileged and fallback URIs. It will use the specified heuristic cache expiry calculator and security domain provider.
      Parameters:
      privilegedUris - an iterable providing the privileged URIs. Can be null if no privileged URIs are used.
      fallbackUris - an iterable providing the fallback URIs. Can be null if no fallback URIs are used.
      urlConnectionExpiryCalculator - the calculator object for heuristic calculation of the resource expiry, used when no expiry is provided by the server of the resource. Can be null, in which case the maximum age of cached entries without validation will be zero.
      urlConnectionSecurityDomainProvider - object that provides security domain objects for the loaded sources. Can be null, in which case the loaded sources will have no security domain associated with them.
  • Method Details

    • loadFromPrivilegedLocations

      protected ModuleSource loadFromPrivilegedLocations(String moduleId, Object validator) throws IOException, URISyntaxException
      Description copied from class: ModuleSourceProviderBase
      Override to obtain a module source from privileged locations. This will be called before source is attempted to be obtained from URIs specified in require.paths.
      Overrides:
      loadFromPrivilegedLocations in class ModuleSourceProviderBase
      Parameters:
      moduleId - the ID of the module
      validator - a validator that can be used to validate an existing cached script. Can be null if there is no cached script available.
      Returns:
      the loaded module script, or null if it can't be found in the privileged locations, or ModuleSourceProvider.NOT_MODIFIED if the existing cached module script is still valid.
      Throws:
      IOException - if the module script was found, but an I/O exception prevented it from being loaded.
      URISyntaxException - if the final URI could not be constructed.
    • loadFromFallbackLocations

      protected ModuleSource loadFromFallbackLocations(String moduleId, Object validator) throws IOException, URISyntaxException
      Description copied from class: ModuleSourceProviderBase
      Override to obtain a module source from fallback locations. This will be called after source is attempted to be obtained from URIs specified in require.paths.
      Overrides:
      loadFromFallbackLocations in class ModuleSourceProviderBase
      Parameters:
      moduleId - the ID of the module
      validator - a validator that can be used to validate an existing cached script. Can be null if there is no cached script available.
      Returns:
      the loaded module script, or null if it can't be found in the privileged locations, or ModuleSourceProvider.NOT_MODIFIED if the existing cached module script is still valid.
      Throws:
      IOException - if the module script was found, but an I/O exception prevented it from being loaded.
      URISyntaxException - if the final URI could not be constructed.
    • loadFromUri

      protected ModuleSource loadFromUri(URI uri, URI base, Object validator) throws IOException, URISyntaxException
      Description copied from class: ModuleSourceProviderBase
      Override in a subclass to load a module script from a logical URI. The URI is absolute but does not have a file name extension such as ".js". It is up to the ModuleSourceProvider implementation to add such an extension.
      Specified by:
      loadFromUri in class ModuleSourceProviderBase
      Parameters:
      uri - the URI of the script, without file name extension.
      base - the base URI the uri was resolved from.
      validator - a validator that can be used to revalidate an existing cached source at the URI. Can be null if there is no cached source available.
      Returns:
      the loaded module script, or null if it can't be found, or ModuleSourceProvider.NOT_MODIFIED if it revalidated the existing cached source against the URI.
      Throws:
      IOException - if the module script was found, but an I/O exception prevented it from being loaded.
      URISyntaxException - if the final URI could not be constructed
    • loadFromActualUri

      protected ModuleSource loadFromActualUri(URI uri, URI base, Object validator) throws IOException
      Throws:
      IOException
    • getCharacterEncoding

      protected String getCharacterEncoding(URLConnection urlConnection)
    • onFailedClosingUrlConnection

      protected void onFailedClosingUrlConnection(URLConnection urlConnection, IOException cause)
      Override if you want to get notified if the URL connection fails to close. Does nothing by default.
      Parameters:
      urlConnection - the connection
      cause - the cause it failed to close.
    • openUrlConnection

      protected URLConnection openUrlConnection(URL url) throws IOException
      Can be overridden in subclasses to customize the URL connection opening process. By default, just calls URL.openConnection().
      Parameters:
      url - the URL
      Returns:
      a connection to the URL.
      Throws:
      IOException - if an I/O error occurs.
    • entityNeedsRevalidation

      protected boolean entityNeedsRevalidation(Object validator)
      Description copied from class: ModuleSourceProviderBase
      Override to determine whether according to the validator, the cached module script needs revalidation. A validator can carry expiry information. If the cached representation is not expired, it doesn' t need revalidation, otherwise it does. When no cache revalidation is required, the external resource will not be contacted at all, so some level of expiry (staleness tolerance) can greatly enhance performance. The default implementation always returns true so it will always require revalidation.
      Overrides:
      entityNeedsRevalidation in class ModuleSourceProviderBase
      Parameters:
      validator - the validator
      Returns:
      returns true if the cached module needs revalidation.