| 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.Reader; |
| 8 | import java.io.Serializable; |
| 9 | import java.net.URI; |
| 10 | |
| 11 | /** |
| 12 | * Represents the source text of the module as a tuple of a reader, a URI, a |
| 13 | * security domain, and a cache validator. |
| 14 | * <h1>Cache validators</h1> |
| 15 | * Validators are used by caches subclassed from |
| 16 | * {@link CachingModuleScriptProviderBase} to avoid repeated loading of |
| 17 | * unmodified resources as well as automatic reloading of modified resources. |
| 18 | * Such a validator can be any value that can be used to detect modification or |
| 19 | * non-modification of the resource that provided the source of the module. It |
| 20 | * can be as simple as a tuple of a URI or a file path, and a last-modified |
| 21 | * date, or an ETag (in case of HTTP). It is left to the implementation. It is |
| 22 | * also allowed to carry expiration information (i.e. in case of HTTP |
| 23 | * expiration header, or if a default expiration is used by the source provider |
| 24 | * to avoid too frequent lookup of the resource), and to short-circuit the |
| 25 | * validation in case the validator indicates the cached representation has not |
| 26 | * yet expired. All these are plainly recommendations; the validators are |
| 27 | * considered opaque and should only make sure to implement |
| 28 | * {@link Object#equals(Object)} as caches themselves can rely on it to compare |
| 29 | * them semantically. Also, it is advisable to have them be serializable. |
| 30 | * @author Attila Szegedi |
| 31 | * @version $Id: ModuleSource.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $ |
| 32 | */ |
| 33 | public class ModuleSource implements Serializable |
| 34 | { |
| 35 | private static final long serialVersionUID = 1L; |
| 36 | |
| 37 | private final Reader reader; |
| 38 | private final Object securityDomain; |
| 39 | private final URI uri; |
| 40 | private final URI base; |
| 41 | private final Object validator; |
| 42 | |
| 43 | /** |
| 44 | * Creates a new module source. |
| 45 | * @param reader the reader returning the source text of the module. |
| 46 | * @param securityDomain the object representing the security domain for |
| 47 | * the module's source (passed to Rhino script compiler). |
| 48 | * @param uri the URI of the module's source text |
| 49 | * @param validator a validator that can be used for subsequent cache |
| 50 | * validation of the source text. |
| 51 | */ |
| 52 | public ModuleSource(Reader reader, Object securityDomain, URI uri, |
| 53 | URI base, Object validator) { |
| 54 | this.reader = reader; |
| 55 | this.securityDomain = securityDomain; |
| 56 | this.uri = uri; |
| 57 | this.base = base; |
| 58 | this.validator = validator; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the reader returning the source text of the module. Note that |
| 63 | * subsequent calls to this method return the same object, thus it is not |
| 64 | * possible to read the source twice. |
| 65 | * @return the reader returning the source text of the module. |
| 66 | */ |
| 67 | public Reader getReader() { |
| 68 | return reader; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the object representing the security domain for the module's |
| 73 | * source. |
| 74 | * @return the object representing the security domain for the module's |
| 75 | * source. |
| 76 | */ |
| 77 | public Object getSecurityDomain() { |
| 78 | return securityDomain; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the URI of the module source text. |
| 83 | * @return the URI of the module source text. |
| 84 | */ |
| 85 | public URI getUri() { |
| 86 | return uri; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the base URI from which this module source was loaded, or null |
| 91 | * if it was loaded from an absolute URI. |
| 92 | * @return the base URI, or null. |
| 93 | */ |
| 94 | public URI getBase() { |
| 95 | return base; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns the validator that can be used for subsequent cache validation |
| 100 | * of the source text. |
| 101 | * @return the validator that can be used for subsequent cache validation |
| 102 | * of the source text. |
| 103 | */ |
| 104 | public Object getValidator() { |
| 105 | return validator; |
| 106 | } |
| 107 | } |