Class RubyScriptRunner


  • public class RubyScriptRunner
    extends java.lang.Object
    Provides an easy way to execute Ruby scripts.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.function.BiConsumer<java.lang.Object,​org.jruby.embed.internal.BiVariableMap> completedCallback  
      protected java.util.function.Consumer<java.lang.String> errorOutput  
      protected org.jruby.embed.ScriptingContainer scriptingContainer  
      protected java.lang.Thread scriptThread  
      protected java.util.function.Consumer<java.lang.String> standardOutput  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void interrupt()
      Interrupts running script thread if there is one running.
      boolean isAlive()
      Checks if script thread exists and is currently running.
      void join()
      Joins script thread (if it exists) via Thread.join().
      void join​(long timeoutMillis)
      Joins script thread (if it exists) via Thread.join(long).
      void runFileAsync​(java.io.File scriptFile, java.lang.String nuixVersion, java.util.Map<java.lang.String,​java.lang.Object> variables)
      Runs a ruby script asynchronously
      void runScriptAsync​(java.lang.String script, java.lang.String nuixVersion, java.util.Map<java.lang.String,​java.lang.Object> variables)
      Runs a ruby script asynchronously
      void setErrorOutputConsumer​(java.util.function.Consumer<java.lang.String> errorOutput)
      Sets the Consumer which will receive error output while script is running.
      void setStandardOutputConsumer​(java.util.function.Consumer<java.lang.String> standardOutput)
      Sets the Consumer which will receive standard output while script is running.
      void whenScriptCompletes​(java.util.function.BiConsumer<java.lang.Object,​org.jruby.embed.internal.BiVariableMap> completedCallback)
      Allows you to provide a callback to be invoked when script finishes.
      • Methods inherited from class java.lang.Object

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

      • scriptingContainer

        protected org.jruby.embed.ScriptingContainer scriptingContainer
      • scriptThread

        protected java.lang.Thread scriptThread
      • standardOutput

        protected java.util.function.Consumer<java.lang.String> standardOutput
      • errorOutput

        protected java.util.function.Consumer<java.lang.String> errorOutput
      • completedCallback

        protected java.util.function.BiConsumer<java.lang.Object,​org.jruby.embed.internal.BiVariableMap> completedCallback
    • Constructor Detail

      • RubyScriptRunner

        public RubyScriptRunner()
    • Method Detail

      • whenScriptCompletes

        public void whenScriptCompletes​(java.util.function.BiConsumer<java.lang.Object,​org.jruby.embed.internal.BiVariableMap> completedCallback)
        Allows you to provide a callback to be invoked when script finishes.
        Parameters:
        completedCallback - A Consumer that accepts an Object (the final returned value) and a Map containing all the variables in the scripting container upon completion.
      • interrupt

        public void interrupt()
        Interrupts running script thread if there is one running. See also isAlive().
      • isAlive

        public boolean isAlive()
        Checks if script thread exists and is currently running.
        Returns:
        True if script thread exists (non-null) and Thread.isAlive() returns true.
      • join

        public void join​(long timeoutMillis)
                  throws java.lang.InterruptedException
        Joins script thread (if it exists) via Thread.join(long).
        Parameters:
        timeoutMillis - the time to wait in milliseconds
        Throws:
        java.lang.InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
      • join

        public void join()
                  throws java.lang.InterruptedException
        Joins script thread (if it exists) via Thread.join().
        Throws:
        java.lang.InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
      • setStandardOutputConsumer

        public void setStandardOutputConsumer​(java.util.function.Consumer<java.lang.String> standardOutput)
        Sets the Consumer which will receive standard output while script is running. If null when a script is executed, received messages will be logged by this instance via log4j2.
        Parameters:
        standardOutput - The consumer of running script's standard output
      • setErrorOutputConsumer

        public void setErrorOutputConsumer​(java.util.function.Consumer<java.lang.String> errorOutput)
        Sets the Consumer which will receive error output while script is running. If null when a script is executed, received messages will be logged by this instance via log4j2.
        Parameters:
        errorOutput - The consumer of running script's error output
      • runScriptAsync

        public void runScriptAsync​(java.lang.String script,
                                   java.lang.String nuixVersion,
                                   java.util.Map<java.lang.String,​java.lang.Object> variables)
        Runs a ruby script asynchronously
        Parameters:
        script - A string containing the Ruby script
        nuixVersion - A string containing the Nuix version to be assigned to constant 'NUIX_VERSION'
        variables - A map of variables to inject into the script container. Key is variable name, value is the value to assign to that variable. Prefix name with $ for global variables.
      • runFileAsync

        public void runFileAsync​(java.io.File scriptFile,
                                 java.lang.String nuixVersion,
                                 java.util.Map<java.lang.String,​java.lang.Object> variables)
        Runs a ruby script asynchronously
        Parameters:
        scriptFile - A file containing a Ruby script
        nuixVersion - A string containing the Nuix version to be assigned to constant 'NUIX_VERSION'
        variables - A map of variables to inject into the script container. Key is variable name, value is the value to assign to that variable. Prefix name with $ for global variables.