Skip to content

Commit

Permalink
Added support for custom classpaths and loading module directories
Browse files Browse the repository at this point in the history
  • Loading branch information
nelson.silva committed May 11, 2012
1 parent e619703 commit 361018a
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main/java/org/vertx/java/deploy/impl/rhino/RhinoVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.commonjs.module.ModuleScript;
import org.mozilla.javascript.commonjs.module.Require;
import org.mozilla.javascript.commonjs.module.RequireBuilder;
import org.mozilla.javascript.commonjs.module.provider.ModuleSource;
import org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider;
import org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider;
import org.vertx.java.core.logging.Logger;
Expand Down Expand Up @@ -100,8 +103,16 @@ public static Require installRequire(Context cx, ScriptableObject scope, List<St
}
}
rb.setModuleScriptProvider(
new SoftCachingModuleScriptProvider(
new UrlModuleSourceProvider(uris, null)));
new SoftCachingModuleScriptProvider(new UrlModuleSourceProvider(uris, null)){
@Override
public ModuleScript getModuleScript(Context cx, String moduleId, URI uri, Scriptable paths) throws Exception {
// Allow loading modules from <dir>/index.js
if(uri != null && new File(uri).isDirectory()){
uri = URI.create(moduleId + File.separator + "index.js");
}
return super.getModuleScript(cx, moduleId, uri, paths);
}
});
Require require = rb.createRequire(cx, scope);
require.install(scope);
return require;
Expand All @@ -110,6 +121,12 @@ public static Require installRequire(Context cx, ScriptableObject scope, List<St
private static Require installRequire(ClassLoader cl, Context cx, ScriptableObject scope){
List<String> modulePaths= new ArrayList<>();

// Add the classpath URLs
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url : urls){
modulePaths.add(url.getPath());
}

// Hack to add the javascript core library to the module path
String corePath = new File(cl.getResource("vertx.js").getPath()).getParent();
modulePaths.add(corePath);
Expand Down

0 comments on commit 361018a

Please sign in to comment.