Skip to content

Commit

Permalink
registering JVM shutdown hook now fails gracefully on GAE
Browse files Browse the repository at this point in the history
  • Loading branch information
oberhamsi committed Dec 28, 2012
1 parent 4577809 commit 3a1f5e9
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/org/ringojs/engine/RhinoEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public RhinoEngine(RingoConfig config, Map<String, Object> globals)
contextFactory = new RingoContextFactory(this, config);
repositories = config.getRepositories();
wrapFactory = config.getWrapFactory();

loaders = new ModuleLoader[] {
new JsModuleLoader(), new JsonModuleLoader(), new ClassModuleLoader()
};
Expand Down Expand Up @@ -139,11 +139,15 @@ public RhinoEngine(RingoConfig config, Map<String, Object> globals)
if (debugger != null) {
debugger.setBreak();
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
shutdown();
}
});
try {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
shutdown();
}
});
} catch (java.security.AccessControlException e) {
log.log(Level.WARNING, "Could not register shutdown hook due to security exception", e);
}
} finally {
Context.exit();
}
Expand Down Expand Up @@ -550,7 +554,7 @@ protected Resource loadPackage(String moduleName, Repository localPath)

return findResource(moduleName + "/index", loaders, localPath);
}

private Scriptable parseJsonResource(Resource resource) throws IOException {
JsonParser parser = new JsonParser(Context.getCurrentContext(), globalScope);
try {
Expand All @@ -564,7 +568,7 @@ private Scriptable parseJsonResource(Resource resource) throws IOException {
throw new RuntimeException(px);
}
}

private String getStringProperty(Scriptable obj, String name, String defaultValue) {
Object value = ScriptableObject.getProperty(obj, name);
if (value != null && value != ScriptableObject.NOT_FOUND) {
Expand Down Expand Up @@ -801,7 +805,7 @@ public Resource findResource(String path, ModuleLoader[] loaders,
}
res.setAbsolute(true);
return res;
} else if (localRoot != null &&
} else if (localRoot != null &&
(path.startsWith("./") || path.startsWith("../"))) {
String newpath = localRoot.getRelativePath() + path;
return findResource(newpath, loaders, null);
Expand Down Expand Up @@ -833,7 +837,7 @@ public Repository findRepository(String path, Repository localPath) throws IOExc
}
return config.getRepository(normalizePath(path));
}

public ModuleLoader getModuleLoader(Resource resource) {
String name = resource.getName();
for (ModuleLoader loader : loaders) {
Expand Down Expand Up @@ -864,23 +868,23 @@ public synchronized void addModuleLoader(String extension, Object value) {
newLoaders[length] = new ScriptedModuleLoader(extension, function);
loaders = newLoaders;
}

public synchronized void removeModuleLoader(String extension) {
int length = loaders.length;
for (int i = 0; i < length; i++) {
if (loaders[i] instanceof ScriptedModuleLoader &&
if (loaders[i] instanceof ScriptedModuleLoader &&
extension.equals(loaders[i].getExtension())) {
ModuleLoader[] newLoaders = new ModuleLoader[length - 1];
if (i > 0)
if (i > 0)
System.arraycopy(loaders, 0, newLoaders, 0, i);
if (i < length - 1)
if (i < length - 1)
System.arraycopy(loaders, i + 1, newLoaders, i, length - i - 1);
loaders = newLoaders;
return;
}
}
}

public static String normalizePath(String path) {
if (!path.contains("./")) {
return path;
Expand Down

0 comments on commit 3a1f5e9

Please sign in to comment.