Class UrlModuleSourceProvider
java.lang.Object
org.mozilla.javascript.commonjs.module.provider.ModuleSourceProviderBase
org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider
- All Implemented Interfaces:
Serializable
,ModuleSourceProvider
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:
-
Field Summary
Fields inherited from interface org.mozilla.javascript.commonjs.module.provider.ModuleSourceProvider
NOT_MODIFIED
-
Constructor Summary
ConstructorDescriptionUrlModuleSourceProvider
(Iterable<URI> privilegedUris, Iterable<URI> fallbackUris) Creates a new module script provider that loads modules against a set of privileged and fallback URIs.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. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
entityNeedsRevalidation
(Object validator) Override to determine whether according to the validator, the cached module script needs revalidation.protected String
getCharacterEncoding
(URLConnection urlConnection) protected ModuleSource
loadFromActualUri
(URI uri, URI base, Object validator) protected ModuleSource
loadFromFallbackLocations
(String moduleId, Object validator) Override to obtain a module source from fallback locations.protected ModuleSource
loadFromPrivilegedLocations
(String moduleId, Object validator) Override to obtain a module source from privileged locations.protected ModuleSource
loadFromUri
(URI uri, URI base, Object validator) Override in a subclass to load a module script from a logical URI.protected void
onFailedClosingUrlConnection
(URLConnection urlConnection, IOException cause) Override if you want to get notified if the URL connection fails to close.protected URLConnection
openUrlConnection
(URL url) Can be overridden in subclasses to customize the URL connection opening process.Methods inherited from class org.mozilla.javascript.commonjs.module.provider.ModuleSourceProviderBase
loadSource, loadSource
-
Constructor Details
-
UrlModuleSourceProvider
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 classModuleSourceProviderBase
- Parameters:
moduleId
- the ID of the modulevalidator
- 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 classModuleSourceProviderBase
- Parameters:
moduleId
- the ID of the modulevalidator
- 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 classModuleSourceProviderBase
- 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
- Throws:
IOException
-
getCharacterEncoding
-
onFailedClosingUrlConnection
Override if you want to get notified if the URL connection fails to close. Does nothing by default.- Parameters:
urlConnection
- the connectioncause
- the cause it failed to close.
-
openUrlConnection
Can be overridden in subclasses to customize the URL connection opening process. By default, just callsURL.openConnection()
.- Parameters:
url
- the URL- Returns:
- a connection to the URL.
- Throws:
IOException
- if an I/O error occurs.
-
entityNeedsRevalidation
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 classModuleSourceProviderBase
- Parameters:
validator
- the validator- Returns:
- returns true if the cached module needs revalidation.
-