From 41ac47d732eed8392d60d0f6773e5a279d49b999 Mon Sep 17 00:00:00 2001 From: Adrian Dudau Date: Thu, 12 Dec 2013 13:36:50 +0100 Subject: initial commit of Enea Linux 3.1 Migrated from the internal git server on the dora-enea branch Signed-off-by: Adrian Dudau --- .../cmake/managedbuilder/util/SystemProcess.java | 118 +++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java (limited to 'plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/SystemProcess.java') 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 @@ +/******************************************************************************* + * 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 command = new LinkedList(); + private File workingDirectory; + private ProcessBuilder builder; + private Process process; + private StreamPipe outputRedirector; + + public SystemProcess(LinkedList command) { + this(command, null); + } + + public SystemProcess(LinkedList command, File directory) { + this(command, directory, null); + } + + public SystemProcess(LinkedList command, File directory, Map additionalEnvironmentVariables) { + super(); + this.command = command; + if (directory != null) { + this.workingDirectory = directory; + } + setUpProcessBuilder(additionalEnvironmentVariables); + } + + private void setUpProcessBuilder(Map 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(); + } + } +} -- cgit v1.2.3-54-g00ecf