Skip to content

Commit 1aaa0b3

Browse files
Adding FileObserverShellMain to androidx.test.services.
PiperOrigin-RevId: 535312414
1 parent 713176f commit 1aaa0b3

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

services/shellexecutor/java/androidx/test/services/shellexecutor/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ kt_android_library(
2424
"Messages.kt",
2525
],
2626
visibility = [
27-
"//services/shellexecutor/javatests/androidx/test/services/shellexecutor:__pkg__",
27+
"//services/shellexecutor/javatests/androidx/test/services/shellexecutor:__subpackages__",
2828
],
2929
)
3030

3131
kt_android_library(
3232
name = "exec_server",
3333
srcs = [
3434
"BlockingPublish.java",
35+
"FileObserverShellMain.kt",
3536
"ShellCommand.java",
3637
"ShellCommandExecutor.java",
3738
"ShellCommandExecutorServer.java",
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (C) 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.test.services.shellexecutor
18+
19+
import android.util.Log
20+
import java.io.IOException
21+
import java.io.InputStream
22+
import java.io.OutputStream
23+
import java.util.concurrent.Executors
24+
import kotlinx.coroutines.CoroutineScope
25+
import kotlinx.coroutines.asCoroutineDispatcher
26+
import kotlinx.coroutines.launch
27+
import kotlinx.coroutines.runBlocking
28+
29+
/** Main runner class for the ShellCommandFileObserverExecutorServer. */
30+
class FileObserverShellMain {
31+
32+
suspend fun run(args: Array<String>): Int {
33+
val scope = CoroutineScope(Executors.newCachedThreadPool().asCoroutineDispatcher())
34+
val server = ShellCommandFileObserverExecutorServer(scope = scope)
35+
server.start()
36+
37+
val processArgs = args.toMutableList()
38+
processArgs.addAll(
39+
processArgs.size - 1,
40+
listOf("-e", ShellExecSharedConstants.BINDER_KEY, server.exchangeDir.toString())
41+
)
42+
val pb = ProcessBuilder(processArgs.toList())
43+
44+
var exitCode: Int
45+
46+
try {
47+
val process = pb.start()
48+
49+
val stdinCopier = scope.launch { copyStream("stdin", System.`in`, process.outputStream) }
50+
val stdoutCopier = scope.launch { copyStream("stdout", process.inputStream, System.out) }
51+
val stderrCopier = scope.launch { copyStream("stderr", process.errorStream, System.err) }
52+
53+
exitCode = process.waitFor()
54+
55+
stdinCopier.cancel() // System.`in`.close() does not force input.read() to return
56+
stdoutCopier.join()
57+
stderrCopier.join()
58+
} finally {
59+
server.stop()
60+
}
61+
return exitCode
62+
}
63+
64+
suspend fun copyStream(name: String, input: InputStream, output: OutputStream) {
65+
val buf = ByteArray(1024)
66+
try {
67+
while (true) {
68+
val size = input.read(buf)
69+
if (size == -1) break
70+
output.write(buf, 0, size)
71+
}
72+
output.flush()
73+
} catch (x: IOException) {
74+
Log.e(TAG, "IOException on $name. Terminating.", x)
75+
}
76+
}
77+
78+
companion object {
79+
private const val TAG = "FileObserverShellMain"
80+
81+
@JvmStatic
82+
public fun main(args: Array<String>) {
83+
System.exit(runBlocking { FileObserverShellMain().run(args) })
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)