Class CommandSession


  • public class CommandSession
    extends java.lang.Object
    Encapsulates the execution context for a command, including the terminal, I/O streams, session variables, working directory, and job state.
    Since:
    4.0
    See Also:
    Command.execute(CommandSession, String[])
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandSession()
      Creates a new command session with the system's standard I/O streams.
      CommandSession​(org.jline.terminal.Terminal terminal)
      Creates a new command session with the specified terminal.
      CommandSession​(org.jline.terminal.Terminal terminal, java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err)
      Creates a new command session with the specified terminal and I/O streams.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.io.PrintStream err()
      Returns the error stream.
      Job foregroundJob()
      Returns the current foreground job.
      CommandSession fork​(java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err)
      Creates a child session that shares the same terminal but uses independent I/O streams.
      java.lang.Object get​(java.lang.String name)
      Returns the value of a session variable.
      java.io.InputStream in()
      Returns the input stream.
      int lastExitCode()
      Returns the exit code of the last executed command.
      java.io.PrintStream out()
      Returns the output stream.
      void put​(java.lang.String name, java.lang.Object value)
      Sets a session variable.
      java.lang.Object remove​(java.lang.String name)
      Removes a session variable.
      void setErr​(java.io.PrintStream err)
      Sets the error stream.
      void setForegroundJob​(Job foregroundJob)
      Sets the current foreground job.
      void setIn​(java.io.InputStream in)
      Sets the input stream.
      void setLastExitCode​(int lastExitCode)
      Sets the exit code of the last executed command.
      void setOut​(java.io.PrintStream out)
      Sets the output stream.
      void setWorkingDirectory​(java.nio.file.Path workingDirectory)
      Sets the working directory.
      org.jline.terminal.Terminal terminal()
      Returns the terminal for this session.
      java.util.Map<java.lang.String,​java.lang.Object> variables()
      Returns all session variables.
      java.nio.file.Path workingDirectory()
      Returns the working directory.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CommandSession

        public CommandSession()
        Creates a new command session with the system's standard I/O streams. The terminal will be null.
      • CommandSession

        public CommandSession​(org.jline.terminal.Terminal terminal)
        Creates a new command session with the specified terminal. I/O streams are derived from the terminal.
        Parameters:
        terminal - the terminal
      • CommandSession

        public CommandSession​(org.jline.terminal.Terminal terminal,
                              java.io.InputStream in,
                              java.io.PrintStream out,
                              java.io.PrintStream err)
        Creates a new command session with the specified terminal and I/O streams.
        Parameters:
        terminal - the terminal
        in - the input stream
        out - the output stream
        err - the error stream
    • Method Detail

      • terminal

        public org.jline.terminal.Terminal terminal()
        Returns the terminal for this session.
        Returns:
        the terminal, or null if not associated
      • in

        public java.io.InputStream in()
        Returns the input stream.
        Returns:
        the input stream
      • out

        public java.io.PrintStream out()
        Returns the output stream.
        Returns:
        the output stream
      • setOut

        public void setOut​(java.io.PrintStream out)
        Sets the output stream.
        Parameters:
        out - the new output stream
      • setIn

        public void setIn​(java.io.InputStream in)
        Sets the input stream.
        Parameters:
        in - the new input stream
      • err

        public java.io.PrintStream err()
        Returns the error stream.
        Returns:
        the error stream
      • setErr

        public void setErr​(java.io.PrintStream err)
        Sets the error stream.
        Parameters:
        err - the new error stream
      • get

        public java.lang.Object get​(java.lang.String name)
        Returns the value of a session variable.
        Parameters:
        name - the variable name
        Returns:
        the value, or null if not set
      • put

        public void put​(java.lang.String name,
                        java.lang.Object value)
        Sets a session variable.
        Parameters:
        name - the variable name
        value - the value
      • remove

        public java.lang.Object remove​(java.lang.String name)
        Removes a session variable.
        Parameters:
        name - the variable name
        Returns:
        the previous value, or null if not set
      • variables

        public java.util.Map<java.lang.String,​java.lang.Object> variables()
        Returns all session variables.
        Returns:
        unmodifiable view of the variables
      • workingDirectory

        public java.nio.file.Path workingDirectory()
        Returns the working directory.
        Returns:
        the working directory, or null
      • setWorkingDirectory

        public void setWorkingDirectory​(java.nio.file.Path workingDirectory)
        Sets the working directory.
        Parameters:
        workingDirectory - the working directory
      • lastExitCode

        public int lastExitCode()
        Returns the exit code of the last executed command.
        Returns:
        the last exit code
      • setLastExitCode

        public void setLastExitCode​(int lastExitCode)
        Sets the exit code of the last executed command.
        Parameters:
        lastExitCode - the exit code
      • foregroundJob

        public Job foregroundJob()
        Returns the current foreground job.
        Returns:
        the foreground job, or null
      • setForegroundJob

        public void setForegroundJob​(Job foregroundJob)
        Sets the current foreground job.
        Parameters:
        foregroundJob - the foreground job
      • fork

        public CommandSession fork​(java.io.InputStream in,
                                   java.io.PrintStream out,
                                   java.io.PrintStream err)
        Creates a child session that shares the same terminal but uses independent I/O streams.

        The child session gets a copy of the current variables and working directory. This is used for concurrent pipeline execution where each stage needs its own I/O streams.

        Parameters:
        in - the input stream for the child session
        out - the output stream for the child session
        err - the error stream for the child session
        Returns:
        a new child session