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
|
/*******************************************************************************
* Copyright (c) 2013 BMW Car IT GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BMW Car IT - initial API and implementation
*******************************************************************************/
package org.yocto.cmake.managedbuilder.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.Map;
public class SystemProcess {
private LinkedList<String> command = new LinkedList<String>();
private File workingDirectory;
private ProcessBuilder builder;
private Process process;
private StreamPipe outputRedirector;
public SystemProcess(LinkedList<String> command) {
this(command, null);
}
public SystemProcess(LinkedList<String> command, File directory) {
this(command, directory, null);
}
public SystemProcess(LinkedList<String> command, File directory, Map<String, String> additionalEnvironmentVariables) {
super();
this.command = command;
if (directory != null) {
this.workingDirectory = directory;
}
setUpProcessBuilder(additionalEnvironmentVariables);
}
private void setUpProcessBuilder(Map<String, String> additionalEnvironmentVariables) {
builder = new ProcessBuilder(command);
if (workingDirectory != null) {
builder.directory(workingDirectory);
}
builder.redirectErrorStream(true);
if(additionalEnvironmentVariables != null && !additionalEnvironmentVariables.isEmpty()) {
builder.environment().putAll(additionalEnvironmentVariables);
}
}
public void start(OutputStream out) throws IOException {
if (builder != null) {
process = builder.start();
outputRedirector = redirectOutput(process, out);
}
}
public int waitForResultAndStop() throws InterruptedException {
if (process == null) {
return -1;
}
process.waitFor();
outputRedirector.interrupt();
return process.exitValue();
}
public void interrupt() {
process.destroy();
}
private StreamPipe redirectOutput(Process process, OutputStream out) {
InputStream in = process.getInputStream();
StreamPipe stdoutPipe = new StreamPipe(in, out);
stdoutPipe.start();
return stdoutPipe;
}
private class StreamPipe extends Thread {
private InputStream in;
private OutputStream out;
boolean shutdown = false;
public StreamPipe(InputStream in, OutputStream out) {
this.in = in;
this.out = out;
}
@Override
public void run() {
byte[] buffer = new byte[1024];
int length = 0;
try {
while(!shutdown && ((length = in.read(buffer)) > -1)) {
out.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void interrupt() {
shutdown = true;
super.interrupt();
}
}
}
|