Interface ProcessHandler


public interface ProcessHandler
Service-provider for guest languages process builder. This interface allows embedder to intercept subprocess creation done by guest languages.
Since:
19.1.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Subprocess attributes passed to start method.
    static final class 
    Represents a source of subprocess input or a destination of subprocess output.
  • Method Summary

    Modifier and Type
    Method
    Description
    A request to start a new subprocess with given attributes.
  • Method Details

    • start

      A request to start a new subprocess with given attributes.

      The default implementation uses ProcessBuilder to create the new subprocess. The subprocess current working directory is set to ProcessHandler.ProcessCommand.getDirectory(). The ProcessHandler.ProcessCommand.getDirectory() value was either explicitely set by the guest language or the FileSystem's current working directory is used. The subprocess environment is set to ProcessHandler.ProcessCommand.getEnvironment(), the initial value of ProcessBuilder.environment() is cleaned. The ProcessHandler.ProcessCommand.getEnvironment() contains the environment variables set by guest language and possibly also the JVM process environment depending on value of Context.Builder.allowEnvironmentAccess(org.graalvm.polyglot.EnvironmentAccess). Implementation example:

       public Process start(ProcessCommand command) throws IOException {
           ProcessBuilder builder = new ProcessBuilder(command.getCommand()).redirectErrorStream(command.isRedirectErrorStream()).redirectInput(
                           asProcessBuilderRedirect(command.getInputRedirect())).redirectOutput(asProcessBuilderRedirect(command.getOutputRedirect())).redirectError(
                                           asProcessBuilderRedirect(command.getErrorRedirect()));
           Map<String, String> env = builder.environment();
           env.clear();
           env.putAll(command.getEnvironment());
           String cwd = command.getDirectory();
           if (cwd != null) {
               builder.directory(Paths.get(cwd).toFile());
           }
           return builder.start();
       }
      
       private static java.lang.ProcessBuilder.Redirect asProcessBuilderRedirect(ProcessHandler.Redirect redirect) {
           if (redirect == ProcessHandler.Redirect.PIPE) {
               return java.lang.ProcessBuilder.Redirect.PIPE;
           } else if (redirect == ProcessHandler.Redirect.INHERIT) {
               return java.lang.ProcessBuilder.Redirect.INHERIT;
           } else {
               throw new IllegalStateException("Unsupported redirect: " + redirect);
           }
       }
       
      Parameters:
      command - the subprocess attributes
      Throws:
      SecurityException - if the process creation was forbidden by this handler
      IOException - if the process fails to execute
      Since:
      19.1.0