aboutsummaryrefslogtreecommitdiff
path: root/libraries/process/process.scala
blob: 982c9d06b9f6e5fb89f482f9070147b129d76cf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cbt.process
import cbt.ExitCode
import java.io._

object `package` extends Module

trait Module {
  def runMainForked(
    className: String,
    args:      Seq[String],
    classpath: String,
    directory: Option[File],
    outErrIn:  Option[( OutputStream, OutputStream, InputStream )]
  ): ( Int, () => ExitCode, () => ExitCode ) = {
    // FIXME: Windows support
    val java_exe = new File( System.getProperty( "java.home" ) + "/bin/java" )
    runWithIO(
      java_exe.toString +: "-cp" +: classpath +: className +: args,
      directory,
      outErrIn
    )
  }

  def runWithIO(
    commandLine: Seq[String],
    directory:   Option[File],
    outErrIn:    Option[( OutputStream, OutputStream, InputStream )]
  ): ( Int, () => ExitCode, () => ExitCode ) = {
    val pb = new ProcessBuilder( commandLine: _* )
    outErrIn.map {
      case ( out, err, in ) =>
        val process = directory.map( pb.directory( _ ) ).getOrElse( pb )
          .redirectInput( ProcessBuilder.Redirect.PIPE )
          .redirectOutput( ProcessBuilder.Redirect.PIPE )
          .redirectError( ProcessBuilder.Redirect.PIPE )
          .start

        (
          processId( process ),
          () => {
            val lock = new AnyRef

            val t1 = asyncPipeCharacterStreamSyncLines( process.getErrorStream, err, lock )
            val t2 = asyncPipeCharacterStreamSyncLines( process.getInputStream, out, lock )
            val t3 = asyncPipeCharacterStream( System.in, process.getOutputStream, process.isAlive )

            t1.start
            t2.start
            t3.start

            t1.join
            t2.join

            val e = process.waitFor
            System.err.println( scala.Console.RESET + "Please press ENTER to continue..." )
            t3.join
            ExitCode( e )
          },
          () => {
            process.destroy
            Thread.sleep( 20 )
            ExitCode( process.destroyForcibly.waitFor )
          }
        )
    }.getOrElse {
      val process = pb.inheritIO.start
      (
        processId( process ),
        () => ExitCode( process.waitFor ),
        () => {
          process.destroy
          Thread.sleep( 20 )
          ExitCode( process.destroyForcibly.waitFor )
        }
      )
    }
  }

  private def accessField( cls: Class[_], field: String ): java.lang.reflect.Field = {
    val f = cls.getDeclaredField( field )
    f.setAccessible( true )
    f
  }

  import com.sun.jna.{ Library, Native }
  private trait CLibrary extends Library {
    def getpid: Int
  }
  private val CLibraryInstance: CLibrary = Native.loadLibrary( "c", classOf[CLibrary] ).asInstanceOf[CLibrary]

  def currentProcessId: Int = {
    if ( Option( System.getProperty( "os.name" ) ).exists( _.startsWith( "Windows" ) ) ) {
      com.sun.jna.platform.win32.Kernel32.INSTANCE.GetCurrentProcessId
    } else {
      CLibraryInstance.getpid
    }
  }

  /** process id of given Process */
  def processId( process: Process ): Int = {
    val clsName = process.getClass.getName
    if ( clsName == "java.lang.UNIXProcess" ) {
      accessField( process.getClass, "pid" ).getInt( process )
    } else if ( clsName == "java.lang.Win32Process" || clsName == "java.lang.ProcessImpl" ) {
      import com.sun.jna.platform.win32.{ WinNT, Kernel32 }
      val handle = new WinNT.HANDLE
      handle.setPointer(
        com.sun.jna.Pointer.createConstant(
          accessField( process.getClass, "handle" ).getLong( process )
        )
      )
      Kernel32.INSTANCE.GetProcessId( handle )
    } else {
      throw new Exception( "Unexpected Process sub-class: " + clsName )
    }
  }

  def asyncPipeCharacterStreamSyncLines( inputStream: InputStream, outputStream: OutputStream, lock: AnyRef ): Thread = {
    new Thread(
      new Runnable {
        def run = {
          val b = new BufferedInputStream( inputStream )
          Iterator.continually {
            b.read // block until and read next character
          }.takeWhile( _ != -1 ).map { c =>
            lock.synchronized { // synchronize with other invocations
              outputStream.write( c )
              Iterator
                .continually( b.read )
                .takeWhile( _ != -1 )
                .map { c =>
                  try {
                    outputStream.write( c )
                    outputStream.flush
                    (
                      c != '\n' // release lock when new line was encountered, allowing other writers to slip in
                      && b.available > 0 // also release when nothing is available to not block other outputs
                    )
                  } catch {
                    case e: IOException if e.getMessage == "Stream closed" => false
                  }
                }
                .takeWhile( identity )
                .length // force entire iterator
            }
          }.length // force entire iterator
        }
      }
    )
  }

  def asyncPipeCharacterStream( inputStream: InputStream, outputStream: OutputStream, continue: => Boolean ) = {
    new Thread(
      new Runnable {
        def run = {
          Iterator
            .continually { inputStream.read }
            .takeWhile( _ != -1 )
            .map { c =>
              try {
                outputStream.write( c )
                outputStream.flush
                true
              } catch {
                case e: IOException if e.getMessage == "Stream closed" => false
              }
            }
            .takeWhile( identity )
            .takeWhile( _ => continue )
            .length // force entire iterator
        }
      }
    )
  }
}