class ChildProcess::JRuby::Process

Public Class Methods

new(args) click to toggle source
Calls superclass method ChildProcess::AbstractProcess::new
# File lib/childprocess/jruby/process.rb, line 6
def initialize(args)
  super(args)

  @pumps = []
end

Public Instance Methods

exited?() click to toggle source
# File lib/childprocess/jruby/process.rb, line 16
def exited?
  return true if @exit_code

  assert_started
  @exit_code = @process.exitValue
  stop_pumps

  true
rescue java.lang.IllegalThreadStateException => ex
  log(ex.class => ex.message)
  false
ensure
  log(:exit_code => @exit_code)
end
io() click to toggle source
# File lib/childprocess/jruby/process.rb, line 12
def io
  @io ||= JRuby::IO.new
end
pid() click to toggle source

On modern Javas, we can simply delegate through to `Process#pid`, which was introduced in Java 9.

@return [Integer] the pid of the process after it has started @raise [NotImplementedError] when trying to access pid on platform for

which it is unsupported in Java
# File lib/childprocess/jruby/process.rb, line 63
def pid
  @process.pid
rescue java.lang.UnsupportedOperationException => e
  raise NotImplementedError, "pid is not supported on this platform: #{e.message}"
end
stop(timeout = nil) click to toggle source
# File lib/childprocess/jruby/process.rb, line 31
def stop(timeout = nil)
  assert_started

  @process.destroy
  wait # no way to actually use the timeout here..
end
wait() click to toggle source
# File lib/childprocess/jruby/process.rb, line 38
def wait
  if exited?
    exit_code
  else
    @process.waitFor

    stop_pumps
    @exit_code = @process.exitValue
  end
end

Private Instance Methods

create_stdin() click to toggle source
# File lib/childprocess/jruby/process.rb, line 157
def create_stdin
  output_stream = @process.getOutputStream

  stdin = output_stream.to_io
  stdin.sync = true
  stdin.instance_variable_set(:@childprocess_java_stream, output_stream)

  class << stdin
    # The stream provided is a BufferedeOutputStream, so we
    # have to flush it to make the bytes flow to the process
    def __childprocess_flush__
      @childprocess_java_stream.flush
    end

    [:flush, :print, :printf, :putc, :puts, :write, :write_nonblock].each do |m|
      define_method(m) do |*args|
        super(*args)
        self.__childprocess_flush__
      end
    end
  end

  stdin
end
launch_process(&blk) click to toggle source
# File lib/childprocess/jruby/process.rb, line 96
def launch_process(&blk)
  pb = java.lang.ProcessBuilder.new(@args)

  pb.directory java.io.File.new(@cwd || Dir.pwd)
  set_env pb.environment

  begin
    @process = pb.start
  rescue java.io.IOException => ex
    raise LaunchError, ex.message
  end

  setup_io
end
redirect(input, output) click to toggle source
# File lib/childprocess/jruby/process.rb, line 127
def redirect(input, output)
  if output.nil?
    input.close
    return
  end

  @pumps << Pump.new(input, output.to_outputstream).run
end
set_env(env) click to toggle source
# File lib/childprocess/jruby/process.rb, line 140
def set_env(env)
  merged = ENV.to_hash

  @environment.each { |k, v| merged[k.to_s] = v }

  merged.each do |k, v|
    if v
      env.put(k, v.to_s)
    elsif env.has_key? k
      env.remove(k)
    end
  end

  removed_keys = env.key_set.to_a - merged.keys
  removed_keys.each { |k| env.remove(k) }
end
setup_io() click to toggle source
# File lib/childprocess/jruby/process.rb, line 111
def setup_io
  if @io
    redirect(@process.getErrorStream, @io.stderr)
    redirect(@process.getInputStream, @io.stdout)
  else
    @process.getErrorStream.close
    @process.getInputStream.close
  end

  if duplex?
    io._stdin = create_stdin
  else
    @process.getOutputStream.close
  end
end
stop_pumps() click to toggle source
# File lib/childprocess/jruby/process.rb, line 136
def stop_pumps
  @pumps.each { |pump| pump.stop }
end