Debian Codegraph @

rhino/src/org/mozilla/javascript/commonjs/module/provider/CachingModuleScriptProviderBase.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.Reader;
8 import java.io.Serializable;
9 import java.net.URI;
10
11 import org.mozilla.javascript.Context;
12 import org.mozilla.javascript.Scriptable;
13 import org.mozilla.javascript.commonjs.module.ModuleScript;
14 import org.mozilla.javascript.commonjs.module.ModuleScriptProvider;
15
16 /**
17 * Abstract base class that implements caching of loaded module scripts. It
18 * uses a {@link ModuleSourceProvider} to obtain the source text of the
19 * scripts. It supports a cache revalidation mechanism based on validator
20 * objects returned from the {@link ModuleSourceProvider}. Instances of this
21 * class and its subclasses are thread safe (and written to perform decently
22 * under concurrent access).
23 * @author Attila Szegedi
24 * @version $Id: CachingModuleScriptProviderBase.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $
25 */
26 public abstract class CachingModuleScriptProviderBase
27 implements ModuleScriptProvider, Serializable
28 {
29 private static final long serialVersionUID = 1L;
30
31 private static final int loadConcurrencyLevel =
32 Runtime.getRuntime().availableProcessors() * 8;
33 private static final int loadLockShift;
34 private static final int loadLockMask;
35 private static final int loadLockCount;
36 static {
37 int sshift = 0;
38 int ssize = 1;
39 while (ssize < loadConcurrencyLevel) {
40 ++sshift;
41 ssize <<= 1;
42 }
43 loadLockShift = 32 - sshift;
44 loadLockMask = ssize - 1;
45 loadLockCount = ssize;
46 }
47 private final Object[] loadLocks = new Object[loadLockCount]; {
48 for(int i = 0; i < loadLocks.length; ++i) {
49 loadLocks[i] = new Object();
50 }
51 }
52
53 private final ModuleSourceProvider moduleSourceProvider;
54
55 /**
56 * Creates a new module script provider with the specified source.
57 * @param moduleSourceProvider provider for modules' source code
58 */
59 protected CachingModuleScriptProviderBase(
60 ModuleSourceProvider moduleSourceProvider) {
61 this.moduleSourceProvider = moduleSourceProvider;
62 }
63
64 public ModuleScript getModuleScript(Context cx, String moduleId,
65 URI moduleUri, URI baseUri, Scriptable paths) throws Exception
66 {
67 final CachedModuleScript cachedModule1 = getLoadedModule(moduleId);
68 final Object validator1 = getValidator(cachedModule1);
69 final ModuleSource moduleSource = (moduleUri == null)
70 ? moduleSourceProvider.loadSource(moduleId, paths, validator1)
71 : moduleSourceProvider.loadSource(moduleUri, baseUri, validator1);
72 if(moduleSource == ModuleSourceProvider.NOT_MODIFIED) {
73 return cachedModule1.getModule();
74 }
75 if(moduleSource == null) {
76 return null;
77 }
78 final Reader reader = moduleSource.getReader();
79 try {
80 final int idHash = moduleId.hashCode();
81 synchronized(loadLocks[(idHash >>> loadLockShift) & loadLockMask]) {
82 final CachedModuleScript cachedModule2 = getLoadedModule(moduleId);
83 if(cachedModule2 != null) {
84 if(!equal(validator1, getValidator(cachedModule2))) {
85 return cachedModule2.getModule();
86 }
87 }
88 final URI sourceUri = moduleSource.getUri();
89 final ModuleScript moduleScript = new ModuleScript(
90 cx.compileReader(reader, sourceUri.toString(), 1,
91 moduleSource.getSecurityDomain()),
92 sourceUri, moduleSource.getBase());
93 putLoadedModule(moduleId, moduleScript,
94 moduleSource.getValidator());
95 return moduleScript;
96 }
97 }
98 finally {
99 reader.close();
100 }
101 }
102
103 /**
104 * Store a loaded module script for later retrieval using
105 * {@link #getLoadedModule(String)}.
106 * @param moduleId the ID of the module
107 * @param moduleScript the module script
108 * @param validator the validator for the module's source text entity
109 */
110 protected abstract void putLoadedModule(String moduleId,
111 ModuleScript moduleScript, Object validator);
112
113 /**
114 * Retrieves an already loaded moduleScript stored using
115 * {@link #putLoadedModule(String, ModuleScript, Object)}.
116 * @param moduleId the ID of the module
117 * @return a cached module script, or null if the module is not loaded.
118 */
119 protected abstract CachedModuleScript getLoadedModule(String moduleId);
120
121 /**
122 * Instances of this class represent a loaded and cached module script.
123 * @author Attila Szegedi
124 * @version $Id: CachingModuleScriptProviderBase.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $
125 */
126 public static class CachedModuleScript {
127 private final ModuleScript moduleScript;
128 private final Object validator;
129
130 /**
131 * Creates a new cached module script.
132 * @param moduleScript the module script itself
133 * @param validator a validator for the moduleScript's source text
134 * entity.
135 */
136 public CachedModuleScript(ModuleScript moduleScript, Object validator) {
137 this.moduleScript = moduleScript;
138 this.validator = validator;
139 }
140
141 /**
142 * Returns the module script.
143 * @return the module script.
144 */
145 ModuleScript getModule() {
146 return moduleScript;
147 }
148
149 /**
150 * Returns the validator for the module script's source text entity.
151 * @return the validator for the module script's source text entity.
152 */
153 Object getValidator() {
154 return validator;
155 }
156 }
157
158 private static Object getValidator(CachedModuleScript cachedModule) {
159 return cachedModule == null ? null : cachedModule.getValidator();
160 }
161
162 private static boolean equal(Object o1, Object o2) {
163 return o1 == null ? o2 == null : o1.equals(o2);
164 }
165
166 /**
167 * Returns the internal concurrency level utilized by caches in this JVM.
168 * @return the internal concurrency level utilized by caches in this JVM.
169 */
170 protected static int getConcurrencyLevel() {
171 return loadLockCount;
172 }
173 }