diff options
author | Adrian Dudau <adrian.dudau@enea.com> | 2013-12-12 13:36:50 +0100 |
---|---|---|
committer | Adrian Dudau <adrian.dudau@enea.com> | 2013-12-12 15:25:03 +0100 |
commit | 41ac47d732eed8392d60d0f6773e5a279d49b999 (patch) | |
tree | cf19d099db9cfdb8d73aa21c31e7aa1cc86ff860 /plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util | |
download | eclipse-poky-juno-master.tar.gz |
Migrated from the internal git server on the dora-enea branch
Signed-off-by: Adrian Dudau <adrian.dudau@enea.com>
Diffstat (limited to 'plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util')
2 files changed, 167 insertions, 0 deletions
diff --git a/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/ConsoleUtility.java b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/ConsoleUtility.java new file mode 100644 index 0000000..9fb31e5 --- /dev/null +++ b/plugins/org.yocto.cmake.managedbuilder/src/org/yocto/cmake/managedbuilder/util/ConsoleUtility.java | |||
@@ -0,0 +1,49 @@ | |||
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 | *******************************************************************************/ | ||
11 | package org.yocto.cmake.managedbuilder.util; | ||
12 | |||
13 | import org.eclipse.ui.console.ConsolePlugin; | ||
14 | import org.eclipse.ui.console.IConsole; | ||
15 | import org.eclipse.ui.console.IOConsoleOutputStream; | ||
16 | import org.eclipse.ui.console.MessageConsole; | ||
17 | |||
18 | |||
19 | public class ConsoleUtility { | ||
20 | |||
21 | public static IOConsoleOutputStream getConsoleOutput(String consoleName) { | ||
22 | return getConsole(consoleName).newOutputStream(); | ||
23 | } | ||
24 | |||
25 | public static MessageConsole getConsole(String consoleName) { | ||
26 | MessageConsole foundConsole = findConsole(consoleName); | ||
27 | if (foundConsole != null) { | ||
28 | foundConsole.clearConsole(); | ||
29 | } else { | ||
30 | foundConsole = new MessageConsole(consoleName, null); | ||
31 | ConsolePlugin.getDefault(). | ||
32 | getConsoleManager().addConsoles(new IConsole[] { foundConsole }); | ||
33 | } | ||
34 | |||
35 | return foundConsole; | ||
36 | } | ||
37 | |||
38 | public static MessageConsole findConsole(String consoleName) { | ||
39 | IConsole[] consoles = | ||
40 | ConsolePlugin.getDefault().getConsoleManager().getConsoles(); | ||
41 | for (IConsole console : consoles) { | ||
42 | if (console.getName().equals(consoleName)) { | ||
43 | return (MessageConsole) console; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | return null; | ||
48 | } | ||
49 | } | ||
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 | *******************************************************************************/ | ||
11 | package org.yocto.cmake.managedbuilder.util; | ||
12 | |||
13 | import java.io.File; | ||
14 | import java.io.IOException; | ||
15 | import java.io.InputStream; | ||
16 | import java.io.OutputStream; | ||
17 | import java.util.LinkedList; | ||
18 | import java.util.Map; | ||
19 | |||
20 | public 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 | } | ||