Class SandboxExecutor

java.lang.Object
org.codehaus.commons.compiler.sandbox.SandboxExecutor

public final class SandboxExecutor extends Object
Executes sandboxed code under SandboxLimits.
    IScriptEvaluator se = ...;
    se.setSandboxPolicy(policy);
    se.cook(script);

    SandboxExecutor executor = new SandboxExecutor(limits);
    Object result = executor.call(() -> se.evaluate(arguments));

Each call(Callable) executes the task on a new thread. Code that JANINO compiled with a SandboxPolicy checks the limits regularly (see Guard) and stops when one is exceeded. Code from other compilers is only subject to the timeout, and cannot be stopped: when it does not terminate after the timeout, its thread is abandoned (see SandboxLimitExceededException.isThreadTerminated()).

The limits apply only to code that runs inside call(Callable); e.g. the instantiation of a generated class (which runs its static initializer and its constructor), and the methods of an object that the sandboxed code returns, and which the host invokes later, must also be executed through a SandboxExecutor. SandboxPolicy.Builder.requireExecutor() makes sure that this is not forgotten.

  • Constructor Details

    • SandboxExecutor

      public SandboxExecutor(SandboxLimits limits)
      Uses daemon threads.
    • SandboxExecutor

      public SandboxExecutor(SandboxLimits limits, @Nullable ThreadFactory threadFactory)
      Parameters:
      threadFactory - Creates the threads that execute the tasks; null means to create daemon threads
  • Method Details