blob: 1ed5947a8b398c232e97e82ca96cdcf4298fb328 (
plain) (
blame)
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
|
package org.openslx.runvirt.viewer;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
/**
* Utils for viewing displays of virtual machines.
*
* @author Manuel Bentele
* @version 1.0
*/
public class ViewerUtils
{
/**
* Synchronously executes a viewer program specified by a command line call.
* <p>
* The command line call of the viewer program consists of the program name and an optional list
* of submitted command line arguments for the viewer program. The result of the executed viewer
* program from the standard output is returned after the program has exited.
*
* @param viewerProgram name of the viewer program.
* @param viewerArguments optional command line arguments for the viewer program.
* @return result of the executed viewer program from the standard output.
* @throws ViewerException failed to execute the viewer program.
*/
@SuppressWarnings( "deprecation" )
public static String executeViewer( String viewerProgram, String[] viewerArguments ) throws ViewerException
{
final CommandLine viewerCommandLine = new CommandLine( viewerProgram );
final DefaultExecutor viewerExecutor = new DefaultExecutor();
// prepare viewer command to execute
viewerCommandLine.addArguments( viewerArguments );
// set up temporary working directory for the viewer process
viewerExecutor.setWorkingDirectory( FileUtils.getTempDirectory() );
// set expected exit value of the viewer process indicating a successful operation
viewerExecutor.setExitValue( 0 );
// set up output stream handler to retrieve the content from the viewer's standard output
final ByteArrayOutputStream viewerOutputStream = new ByteArrayOutputStream();
final PumpStreamHandler viewerOutputStreamHandler = new PumpStreamHandler( viewerOutputStream );
viewerExecutor.setStreamHandler( viewerOutputStreamHandler );
// execute the viewer command as blocking process
try {
viewerExecutor.execute( viewerCommandLine );
} catch ( IOException e ) {
throw new ViewerException( "Failed to execute '" + viewerProgram + "': " + e.getLocalizedMessage() );
}
final String viewerOuput = viewerOutputStream.toString( StandardCharsets.UTF_8 );
IOUtils.closeQuietly( viewerOutputStream );
return viewerOuput;
}
}
|