Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setting a ClassLoader Supplier #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions core/src/main/java/io/apigee/trireme/core/Sandbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -55,7 +57,7 @@ public class Sandbox
private boolean hideOsDetails;
private ClassShutter extraClassShutter;
private boolean allowJarLoading = true;
private ClassLoader classLoader = null;
private ClassLoaderSupplier classLoaderSupplier = null;

/**
* Create a new sandbox that will not affect anything in any way.
Expand Down Expand Up @@ -85,7 +87,7 @@ public Sandbox(Sandbox parent)
this.hideOsDetails = parent.hideOsDetails;
this.extraClassShutter = parent.extraClassShutter;
this.allowJarLoading = parent.allowJarLoading;
this.classLoader = parent.classLoader;
this.classLoaderSupplier = parent.classLoaderSupplier;
if (parent.mounts != null) {
this.mounts = new ArrayList<Map.Entry<String, String>>(parent.mounts);
}
Expand Down Expand Up @@ -279,15 +281,36 @@ public boolean isAllowJarLoading() {
}

/**
* Allows specifying the ClassLoader that will be used by the "trireme-support" module's
* "loadJars" method to load JAR files into the currently-running script.
* A Supplier that returns an instance of a ClassLoader to be used
* for loading Java classes into the currently-running script.
* <p/>
* The Supplier should return a new instance for every invocation of this method
* if it is desired that classes loaded by any one invocation of the
* "trireme-support" module's "loadJars" method are isolated from classes loaded
* from another invocation of the "loadJars" method.
* <p/>
* The Supplier may return the same instance for every invocation of this method
* if such isolation is not desired.
*/
public Sandbox setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
public interface ClassLoaderSupplier {
ClassLoader getClassLoader(URL[] urlArray);
}

/**
* Allows specifying the Supplier that will return an instance of a ClassLoader
* that will be used by the "trireme-support" module's "loadJars" method to load JAR files
* into the currently-running script.
*/
public Sandbox setClassLoaderSupplier(ClassLoaderSupplier classLoaderSupplier) {
this.classLoaderSupplier = classLoaderSupplier;
return this;
}

public ClassLoader getClassLoader() {
return classLoader;
/**
* Returns the ClassLoaderSupplier if set, null otherwise.
*/
public ClassLoaderSupplier getClassLoaderSupplier() {
return classLoaderSupplier;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,18 @@ public static Object loadJars(Context cx, Scriptable thisObj, Object[] args, Fun
throw Utils.makeError(cx, thisObj, "Cannot get URL for JAR file :" + e);
}
}
URL[] urlArray = urls.toArray(new URL[urls.size()]);

Sandbox sandbox = self.runtime.getSandbox();
ClassLoader loader = null;
if (sandbox != null) { // use custom class loader if specified
loader = sandbox.getClassLoader();
Sandbox.ClassLoaderSupplier supplier = sandbox.getClassLoaderSupplier();
if (supplier != null) {
loader = supplier.getClassLoader(urlArray);
}
}
if (loader == null) { // fallback
loader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
loader = new URLClassLoader(urlArray);
}

// Load the classes in to the module registry for this script only.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void testLoadingDisabled()
public void testLoadingUsingCustomClassLoader()
throws NodeException, InterruptedException, ExecutionException {

ArrayList<URL> urls = new ArrayList<URL>();
final ArrayList<URL> urls = new ArrayList<URL>();
String[] jarNames = {"target/test-classes/testjar.jar", "target/test-classes/depjar.jar"};
for (String jn : jarNames) {
File jarFile = new File(jn);
Expand All @@ -121,9 +121,11 @@ public void testLoadingUsingCustomClassLoader()
}
}

Sandbox sb = new Sandbox().setClassLoader(
new URLClassLoader(urls.toArray(new URL[urls.size()]))
);
Sandbox sb = new Sandbox().setClassLoaderSupplier(new Sandbox.ClassLoaderSupplier() {
public ClassLoader getClassLoader(URL[] urlArray) {
return new URLClassLoader(urls.toArray(new URL[urls.size()]));
}
});
NodeScript script = env.createScript("jarload.js",
new File("target/test-classes/tests/jarload.js"),
new String[]{"Foo", "25"});
Expand All @@ -139,8 +141,12 @@ public void testLoadingUsingCustomClassLoader()
public void testLoadingFailsUsingInvalidCustomClassLoader()
throws NodeException, InterruptedException, ExecutionException {

URL empty[] = new URL[0];
Sandbox sb = new Sandbox().setClassLoader(new URLClassLoader(empty));
final URL empty[] = new URL[0];
Sandbox sb = new Sandbox().setClassLoaderSupplier(new Sandbox.ClassLoaderSupplier() {
public ClassLoader getClassLoader(URL[] urlArray) {
return new URLClassLoader(empty);
}
});
NodeScript script = env.createScript("jarload.js",
new File("target/test-classes/tests/jarload.js"),
new String[]{"Foo", "25"});
Expand Down