Debian Codegraph @

rhino/src/org/mozilla/javascript/commonjs/module/provider/ModuleSourceProviderBase.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.javascript.commonjs.module.provider;
6
7 import java.io.File;
8 import java.io.IOException;
9 import java.io.Serializable;
10 import java.net.MalformedURLException;
11 import java.net.URI;
12 import java.net.URISyntaxException;
13
14 import org.mozilla.javascript.ScriptRuntime;
15 import org.mozilla.javascript.Scriptable;
16 import org.mozilla.javascript.ScriptableObject;
17
18 /**
19 * A base implementation for all module script providers that actually load
20 * module scripts. Performs validation of identifiers, allows loading from
21 * preferred locations (attempted before require.paths), from require.paths
22 * itself, and from fallback locations (attempted after require.paths). Note
23 * that while this base class strives to be as generic as possible, it does
24 * have loading from an URI built into its design, for the simple reason that
25 * the require.paths is defined in terms of URIs.
26 * @author Attila Szegedi
27 * @version $Id: ModuleSourceProviderBase.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $
28 */
29 public abstract class ModuleSourceProviderBase implements
30 ModuleSourceProvider, Serializable
31 {
32 private static final long serialVersionUID = 1L;
33
34 public ModuleSource loadSource(String moduleId, Scriptable paths,
35 Object validator) throws IOException, URISyntaxException
36 {
37 if(!entityNeedsRevalidation(validator)) {
38 return NOT_MODIFIED;
39 }
40
41 ModuleSource moduleSource = loadFromPrivilegedLocations(
42 moduleId, validator);
43 if(moduleSource != null) {
44 return moduleSource;
45 }
46 if(paths != null) {
47 moduleSource = loadFromPathArray(moduleId, paths,
48 validator);
49 if(moduleSource != null) {
50 return moduleSource;
51 }
52 }
53 return loadFromFallbackLocations(moduleId, validator);
54 }
55
56 public ModuleSource loadSource(URI uri, URI base, Object validator)
57 throws IOException, URISyntaxException {
58 return loadFromUri(uri, base, validator);
59 }
60
61 private ModuleSource loadFromPathArray(String moduleId,
62 Scriptable paths, Object validator) throws IOException
63 {
64 final long llength = ScriptRuntime.toUint32(
65 ScriptableObject.getProperty(paths, "length"));
66 // Yeah, I'll ignore entries beyond Integer.MAX_VALUE; so sue me.
67 int ilength = llength > Integer.MAX_VALUE ? Integer.MAX_VALUE :
68 (int)llength;
69
70 for(int i = 0; i < ilength; ++i) {
71 final String path = ensureTrailingSlash(
72 ScriptableObject.getTypedProperty(paths, i, String.class));
73 try {
74 URI uri = new URI(path);
75 if (!uri.isAbsolute()) {
76 uri = new File(path).toURI().resolve("");
77 }
78 final ModuleSource moduleSource = loadFromUri(
79 uri.resolve(moduleId), uri, validator);
80 if(moduleSource != null) {
81 return moduleSource;
82 }
83 }
84 catch(URISyntaxException e) {
85 throw new MalformedURLException(e.getMessage());
86 }
87 }
88 return null;
89 }
90
91 private static String ensureTrailingSlash(String path) {
92 return path.endsWith("/") ? path : path.concat("/");
93 }
94
95 /**
96 * Override to determine whether according to the validator, the cached
97 * module script needs revalidation. A validator can carry expiry
98 * information. If the cached representation is not expired, it doesn'
99 * t need revalidation, otherwise it does. When no cache revalidation is
100 * required, the external resource will not be contacted at all, so some
101 * level of expiry (staleness tolerance) can greatly enhance performance.
102 * The default implementation always returns true so it will always require
103 * revalidation.
104 * @param validator the validator
105 * @return returns true if the cached module needs revalidation.
106 */
107 protected boolean entityNeedsRevalidation(Object validator) {
108 return true;
109 }
110
111 /**
112 * Override in a subclass to load a module script from a logical URI. The
113 * URI is absolute but does not have a file name extension such as ".js".
114 * It is up to the ModuleSourceProvider implementation to add such an
115 * extension.
116 * @param uri the URI of the script, without file name extension.
117 * @param base the base URI the uri was resolved from.
118 * @param validator a validator that can be used to revalidate an existing
119 * cached source at the URI. Can be null if there is no cached source
120 * available.
121 * @return the loaded module script, or null if it can't be found, or
122 * {@link ModuleSourceProvider#NOT_MODIFIED} if it revalidated the existing
123 * cached source against the URI.
124 * @throws IOException if the module script was found, but an I/O exception
125 * prevented it from being loaded.
126 * @throws URISyntaxException if the final URI could not be constructed
127 */
128 protected abstract ModuleSource loadFromUri(URI uri, URI base,
129 Object validator) throws IOException, URISyntaxException;
130
131 /**
132 * Override to obtain a module source from privileged locations. This will
133 * be called before source is attempted to be obtained from URIs specified
134 * in require.paths.
135 * @param moduleId the ID of the module
136 * @param validator a validator that can be used to validate an existing
137 * cached script. Can be null if there is no cached script available.
138 * @return the loaded module script, or null if it can't be found in the
139 * privileged locations, or {@link ModuleSourceProvider#NOT_MODIFIED} if
140 * the existing cached module script is still valid.
141 * @throws IOException if the module script was found, but an I/O exception
142 * prevented it from being loaded.
143 * @throws URISyntaxException if the final URI could not be constructed.
144 */
145 protected ModuleSource loadFromPrivilegedLocations(
146 String moduleId, Object validator)
147 throws IOException, URISyntaxException
148 {
149 return null;
150 }
151
152 /**
153 * Override to obtain a module source from fallback locations. This will
154 * be called after source is attempted to be obtained from URIs specified
155 * in require.paths.
156 * @param moduleId the ID of the module
157 * @param validator a validator that can be used to validate an existing
158 * cached script. Can be null if there is no cached script available.
159 * @return the loaded module script, or null if it can't be found in the
160 * privileged locations, or {@link ModuleSourceProvider#NOT_MODIFIED} if
161 * the existing cached module script is still valid.
162 * @throws IOException if the module script was found, but an I/O exception
163 * prevented it from being loaded.
164 * @throws URISyntaxException if the final URI could not be constructed.
165 */
166 protected ModuleSource loadFromFallbackLocations(
167 String moduleId, Object validator)
168 throws IOException, URISyntaxException
169 {
170 return null;
171 }
172 }