Top Description Inners Methods
org.graalvm.polyglot.io

public Interface ProcessHandler

Imports
java.io.IOException, .OutputStream, java.util.ArrayList, .Collections, .HashMap, .List, .Map, .Objects, org.graalvm.polyglot.Context.Builder

Service-provider for guest languages process builder. This interface allows embedder to intercept subprocess creation done by guest languages.
Since
19.1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ProcessHandler.ProcessCommand

Subprocess attributes passed to start method.

public static class
ProcessHandler.Redirect

Represents a source of subprocess input or a destination of subprocess output.

Method Summary

Modifier and TypeMethod and Description
public Process
start(ProcessHandler.ProcessCommand
the subprocess attributes
command
)

A request to start a new subprocess with given attributes.

Method Detail

startback to summary
public Process start(ProcessHandler.ProcessCommand command) throws IOException

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 ProcessCommand#getDirectory(). The 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 ProcessCommand#getEnvironment(), the initial value of ProcessBuilder#environment() is cleaned. The ProcessCommand#getEnvironment() contains the environment variables set by guest language and possibly also the JVM process environment depending on value of 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:ProcessHandler.ProcessCommand

the subprocess attributes

Exceptions
IOException:
if the process fails to execute
SecurityException:
if the process creation was forbidden by this handler
Since
19.1.0
org.graalvm.polyglot.io back to summary

public final Class ProcessHandler.ProcessCommand

extends Object
Class Inheritance

Subprocess attributes passed to start method.
Since
19.1.0

Field Summary

Modifier and TypeField and Description
private List<String>
private String
private Map<String, String>
private ProcessHandler.Redirect
private ProcessHandler.Redirect
private ProcessHandler.Redirect
private boolean

Constructor Summary

AccessConstructor and Description
pack-priv
ProcessCommand(List<String> command, String cwd, Map<String, String> environment, boolean redirectErrorStream, ProcessHandler.Redirect inputRedirect, ProcessHandler.Redirect outputRedirect, ProcessHandler.Redirect errorRedirect)

Method Summary

Modifier and TypeMethod and Description
public static ProcessHandler.ProcessCommand
create(List<String> cmd, String cwd, Map<String, String> environment, boolean redirectErrorStream, ProcessHandler.Redirect inputRedirect, ProcessHandler.Redirect outputRedirect, ProcessHandler.Redirect errorRedirect)

Creates a ProcessCommand.

public List<String>
getCommand()

Returns the subprocess executable and arguments as an immutable list.

public String
getDirectory()

Returns the subprocess working directory.

public Map<String, String>
getEnvironment()

Returns the subprocess environment as an immutable map.

public ProcessHandler.Redirect
getErrorRedirect()

Returns the standard error output destination.

public ProcessHandler.Redirect
getInputRedirect()

Returns the standard input source.

public ProcessHandler.Redirect
getOutputRedirect()

Returns the standard output destination.

public boolean
isRedirectErrorStream()

Return whether the standard error output should be merged into standard output.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cmdback to summary
private List<String> cmd
cwdback to summary
private String cwd
environmentback to summary
private Map<String, String> environment
errorRedirectback to summary
private ProcessHandler.Redirect errorRedirect
inputRedirectback to summary
private ProcessHandler.Redirect inputRedirect
outputRedirectback to summary
private ProcessHandler.Redirect outputRedirect
redirectErrorStreamback to summary
private boolean redirectErrorStream

Constructor Detail

ProcessCommandback to summary
pack-priv ProcessCommand(List<String> command, String cwd, Map<String, String> environment, boolean redirectErrorStream, ProcessHandler.Redirect inputRedirect, ProcessHandler.Redirect outputRedirect, ProcessHandler.Redirect errorRedirect)

Method Detail

createback to summary
public static ProcessHandler.ProcessCommand create(List<String> cmd, String cwd, Map<String, String> environment, boolean redirectErrorStream, ProcessHandler.Redirect inputRedirect, ProcessHandler.Redirect outputRedirect, ProcessHandler.Redirect errorRedirect)

Creates a ProcessCommand. Not for direct use.

Since
23.1.0
getCommandback to summary
public List<String> getCommand()

Returns the subprocess executable and arguments as an immutable list.

Since
19.1.0
getDirectoryback to summary
public String getDirectory()

Returns the subprocess working directory.

Since
19.1.0
getEnvironmentback to summary
public Map<String, String> getEnvironment()

Returns the subprocess environment as an immutable map.

Since
19.1.0
getErrorRedirectback to summary
public ProcessHandler.Redirect getErrorRedirect()

Returns the standard error output destination.

Since
19.1.0
getInputRedirectback to summary
public ProcessHandler.Redirect getInputRedirect()

Returns the standard input source.

Since
19.1.0
getOutputRedirectback to summary
public ProcessHandler.Redirect getOutputRedirect()

Returns the standard output destination.

Since
19.1.0
isRedirectErrorStreamback to summary
public boolean isRedirectErrorStream()

Return whether the standard error output should be merged into standard output.

Since
19.1.0
org.graalvm.polyglot.io back to summary

public final Class ProcessHandler.Redirect

extends Object
Class Inheritance

Represents a source of subprocess input or a destination of subprocess output.
Since
19.1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum

Field Summary

Modifier and TypeField and Description
public static final ProcessHandler.Redirect
INHERIT

Indicates that subprocess I/O source or destination will be the same as those of the current process.

public static final ProcessHandler.Redirect
PIPE

Indicates that subprocess I/O will be connected to the current Java process using a pipe.

private final OutputStream
private final ProcessHandler.Redirect.Type

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public static ProcessHandler.Redirect
createRedirectToStream(OutputStream stream)

Creates a ProcessCommand.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public OutputStream
getOutputStream()

Returns the output stream to redirect to.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

INHERITback to summary
public static final ProcessHandler.Redirect INHERIT

Indicates that subprocess I/O source or destination will be the same as those of the current process.

Since
19.1.0
PIPEback to summary
public static final ProcessHandler.Redirect PIPE

Indicates that subprocess I/O will be connected to the current Java process using a pipe.

Since
19.1.0
streamback to summary
private final OutputStream stream
typeback to summary
private final ProcessHandler.Redirect.Type type

Constructor Detail

Redirectback to summary
pack-priv Redirect(ProcessHandler.Redirect.Type type, OutputStream stream)

Method Detail

createRedirectToStreamback to summary
public static ProcessHandler.Redirect createRedirectToStream(OutputStream stream)

Creates a ProcessCommand. Not for direct use.

Since
23.1.0
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
Since
19.1.0
getOutputStreamback to summary
public OutputStream getOutputStream()

Returns the output stream to redirect to.

Since
23.1.0
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
Since
19.1.0
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
Since
19.1.0
org.graalvm.polyglot.io back to summary

pack-priv final Enum ProcessHandler.Redirect.Type

extends Enum<ProcessHandler.Redirect.Type>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final ProcessHandler.Redirect.Type
public static final ProcessHandler.Redirect.Type
PIPE

The type of Redirect.PIPE.

public static final ProcessHandler.Redirect.Type
STREAM

The type of Redirect.stream.

Constructor Summary

AccessConstructor and Description
private
Type()

Field Detail

INHERITback to summary
public static final ProcessHandler.Redirect.Type INHERIT

The type of Redirect.INHERIT.

PIPEback to summary
public static final ProcessHandler.Redirect.Type PIPE

The type of Redirect.PIPE.

STREAMback to summary
public static final ProcessHandler.Redirect.Type STREAM

The type of Redirect.stream.

Constructor Detail

Typeback to summary
private Type()

Method Detail

valueOfback to summary
public static ProcessHandler.Redirect.Type valueOf(String name)
valuesback to summary
public static ProcessHandler.Redirect.Type[] values()