summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java')
-rw-r--r--plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java118
1 files changed, 118 insertions, 0 deletions
diff --git a/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java
new file mode 100644
index 0000000..9ca6e60
--- /dev/null
+++ b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java
@@ -0,0 +1,118 @@
1/*******************************************************************************
2 * Copyright (c) 2013 BMW Car IT GmbH.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * BMW Car IT - initial API and implementation
10 *******************************************************************************/
11package org.yocto.cmake.managedbuilder.util;
12
13import java.io.File;
14import java.io.IOException;
15import java.io.InputStream;
16import java.io.OutputStream;
17import java.util.LinkedList;
18import java.util.Map;
19
20public class SystemProcess {
21
22 private LinkedList<String> command = new LinkedList<String>();
23 private File workingDirectory;
24 private ProcessBuilder builder;
25 private Process process;
26 private StreamPipe outputRedirector;
27
28 public SystemProcess(LinkedList<String> command) {
29 this(command, null);
30 }
31
32 public SystemProcess(LinkedList<String> command, File directory) {
33 this(command, directory, null);
34 }
35
36 public SystemProcess(LinkedList<String> command, File directory, Map<String, String> additionalEnvironmentVariables) {
37 super();
38 this.command = command;
39 if (directory != null) {
40 this.workingDirectory = directory;
41 }
42 setUpProcessBuilder(additionalEnvironmentVariables);
43 }
44
45 private void setUpProcessBuilder(Map<String, String> additionalEnvironmentVariables) {
46 builder = new ProcessBuilder(command);
47 if (workingDirectory != null) {
48 builder.directory(workingDirectory);
49 }
50 builder.redirectErrorStream(true);
51
52 if(additionalEnvironmentVariables != null && !additionalEnvironmentVariables.isEmpty()) {
53 builder.environment().putAll(additionalEnvironmentVariables);
54 }
55 }
56
57 public void start(OutputStream out) throws IOException {
58 if (builder != null) {
59 process = builder.start();
60 outputRedirector = redirectOutput(process, out);
61 }
62 }
63
64 public int waitForResultAndStop() throws InterruptedException {
65 if (process == null) {
66 return -1;
67 }
68
69 process.waitFor();
70 outputRedirector.interrupt();
71
72 return process.exitValue();
73 }
74
75 public void interrupt() {
76 process.destroy();
77 }
78
79 private StreamPipe redirectOutput(Process process, OutputStream out) {
80 InputStream in = process.getInputStream();
81 StreamPipe stdoutPipe = new StreamPipe(in, out);
82 stdoutPipe.start();
83
84 return stdoutPipe;
85 }
86
87
88 private class StreamPipe extends Thread {
89 private InputStream in;
90 private OutputStream out;
91 boolean shutdown = false;
92
93 public StreamPipe(InputStream in, OutputStream out) {
94 this.in = in;
95 this.out = out;
96 }
97
98 @Override
99 public void run() {
100 byte[] buffer = new byte[1024];
101 int length = 0;
102
103 try {
104 while(!shutdown && ((length = in.read(buffer)) > -1)) {
105 out.write(buffer, 0, length);
106 }
107 } catch (IOException e) {
108 e.printStackTrace();
109 }
110 }
111
112 @Override
113 public void interrupt() {
114 shutdown = true;
115 super.interrupt();
116 }
117 }
118}