# File lib/childprocess/jruby/process.rb, line 6 def initialize(args) super(args) @pumps = [] end
# 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 false ensure log(:exit_code => @exit_code) end
# File lib/childprocess/jruby/process.rb, line 12 def io @io ||= JRuby::IO.new end
Only supported in JRuby on a Unix operating system, thanks to limitations in Java's classes
@return [Fixnum] the pid of the process after it has started @raise [NotImplementedError] when trying to access pid on non-Unix platform
# File lib/childprocess/jruby/process.rb, line 51 def pid if @process.getClass.getName != "java.lang.UNIXProcess" raise NotImplementedError, "pid is only supported by JRuby child processes on Unix" end # About the best way we can do this is with a nasty reflection-based impl # Thanks to Martijn Courteaux # http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigter/2951193#2951193 field = @process.getClass.getDeclaredField("pid") field.accessible = true field.get(@process) end
# File lib/childprocess/jruby/process.rb, line 30 def stop(timeout = nil) assert_started @process.destroy wait # no way to actually use the timeout here.. end
# File lib/childprocess/jruby/process.rb, line 37 def wait @process.waitFor stop_pumps @exit_code = @process.exitValue end
# File lib/childprocess/jruby/process.rb, line 66 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
# File lib/childprocess/jruby/process.rb, line 104 def redirect(input, output) if output.nil? input.close return end @pumps << Pump.new(input, output.to_outputstream).run end
# File lib/childprocess/jruby/process.rb, line 117 def set_env(env) ENV.to_hash.merge(@environment).each do |k,v| env.put(k.to_s, v.to_s) if v end end
# File lib/childprocess/jruby/process.rb, line 81 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? stdin = @process.getOutputStream.to_io stdin.sync = true stdin.instance_variable_set(:@java_stream, @process.getOutputStream) def stdin.__flushit; @java_stream.flush; end #The stream provided is a BufferedeOutputStream, so we have to flush it to make the bytes flow to the process def stdin.flush; super; self.__flushit; end def stdin.puts(*args); super(*args); self.__flushit; end io._stdin = stdin else @process.getOutputStream.close end end
# File lib/childprocess/jruby/process.rb, line 113 def stop_pumps @pumps.each { |pump| pump.stop } end