Class JShellExec

java.lang.Object
com.github.fracpete.jshell.JShellExec
All Implemented Interfaces:
com.github.fracpete.processoutput4j.core.StreamingProcessOwner, Serializable

public class JShellExec extends Object implements Serializable, com.github.fracpete.processoutput4j.core.StreamingProcessOwner
For executing code via JShell.
Author:
FracPete (fracpete at waikato dot ac dot nz)
See Also:
  • Field Details

    • m_Debug

      protected boolean m_Debug
      whether debugging is on.
    • m_Available

      protected Boolean m_Available
      whether scripting is available.
    • m_Execution

      protected transient com.github.fracpete.processoutput4j.output.StreamingProcessOutput m_Execution
      executes the script.
    • m_JShellExecListeners

      protected Set<JShellExecListener> m_JShellExecListeners
      the listeners that listen for changes.
    • m_JShellErrorListeners

      protected Set<JShellErrorListener> m_JShellErrorListeners
      the listeners for errors.
    • m_StreamingProcessOwner

      protected com.github.fracpete.processoutput4j.core.StreamingProcessOwner m_StreamingProcessOwner
      the streaming process owner to forward the process output to.
  • Constructor Details

    • JShellExec

      public JShellExec()
      Initializes the execution.
  • Method Details

    • setDebug

      public void setDebug(boolean value)
      Sets the debugging flag.
      Parameters:
      value - true if to turn debugging output on
    • getDebug

      public boolean getDebug()
      Returns the debugging flag.
      Returns:
      true if debugging output on
    • debugMsg

      protected void debugMsg(String msg)
      Outputs the debugging message if debugging is enabled.
      Parameters:
      msg - the message output
    • setStreamingProcessOwner

      public void setStreamingProcessOwner(com.github.fracpete.processoutput4j.core.StreamingProcessOwner value)
      Sets the object to stream the output of the process to. If none set, output is output on stdout/stderr.
      Parameters:
      value - the receiver of the output
    • getStreamingProcessOwner

      public com.github.fracpete.processoutput4j.core.StreamingProcessOwner getStreamingProcessOwner()
      Returns the current recipient of the process output. If none set, output is output on stdout/stderr.
      Returns:
      the receiver, null if none set
    • isRunning

      public boolean isRunning()
      Returns whether a script is currently running.
      Returns:
      true if a script is running
    • runScript

      public void runScript(String code)
      Executes the script with no flags.
      Parameters:
      code - the script code to execute
    • runScript

      public void runScript(String code, List<String> runtimeFlags, List<String> remoteRuntimeFlags, List<String> compilerFlags)
      Executes the script.
      Parameters:
      code - the script code to execute
      runtimeFlags - optional runtime flags to pass through (-J gets prefixed automatically) - for JShell (eg -verbose)
      remoteRuntimeFlags - optional runtime flags to pass through (-R gets prefixed automatically) - for JVM that runs code (eg -javaagent:...)
      compilerFlags - optional runtime flags to pass through (-C gets prefixed automatically)
    • stopScript

      public void stopScript()
      Stops a running script.
    • getExecutable

      public String getExecutable()
      Returns the jshell executable.
      Returns:
      the executable path
    • isAvailable

      public boolean isAvailable()
      Checks whether jshell executable is available.
      Returns:
      true if available
    • getOutputType

      public com.github.fracpete.processoutput4j.core.StreamingProcessOutputType getOutputType()
      Returns what output from the process to forward.
      Specified by:
      getOutputType in interface com.github.fracpete.processoutput4j.core.StreamingProcessOwner
      Returns:
      the output type
    • processOutput

      public void processOutput(String line, boolean stdout)
      Processes the incoming line.
      Specified by:
      processOutput in interface com.github.fracpete.processoutput4j.core.StreamingProcessOwner
      Parameters:
      line - the line to process
      stdout - whether stdout or stderr
    • showErrorMessage

      public void showErrorMessage(String msg)
      Outputs the error message.
      Parameters:
      msg - the message to output
    • showErrorMessage

      public void showErrorMessage(String msg, Throwable t)
      Outputs the error message.
      Parameters:
      msg - the message to output
      t - the (optional) associated exception
    • addJShellExecListener

      public void addJShellExecListener(JShellExecListener l)
      Adds the exec listener to the internal list.
      Parameters:
      l - the listener to add
    • removeJShellExecListener

      public void removeJShellExecListener(JShellExecListener l)
      Removes the exec listener to the internal list.
      Parameters:
      l - the listener to remove
    • notifyJShellExecListeners

      public void notifyJShellExecListeners(JShellExecEvent e)
      Notifies all the exec listeners with the specified event.
      Parameters:
      e - the event to send
    • addJShellErrorListener

      public void addJShellErrorListener(JShellErrorListener l)
      Adds the error listener to the internal list.
      Parameters:
      l - the listener to add
    • removeJShellErrorListener

      public void removeJShellErrorListener(JShellErrorListener l)
      Removes the listener to the internal list.
      Parameters:
      l - the listener to remove
    • notifyJShellErrorListeners

      public void notifyJShellErrorListeners(JShellErrorEvent e)
      Notifies all the listeners with the specified event.
      Parameters:
      e - the event to send