summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satserver/util/Exec.java
blob: e27340961058a9e6a9cf9523f305da2255adc320 (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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package org.openslx.satserver.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class Exec
{

	/**
	 * Run command, return exit status of process, or -1 on error
	 * 
	 * @param timeoutSec maximum time in seconds to wait for process to finish
	 * @param command Command and arguments
	 * @return exit code
	 */
	public static int sync( int timeoutSec, ExecCallback callback, String... command )
	{
		return syncAt( timeoutSec, callback, "/", command );
	}

	public static int sync( int timeoutSec, String... command )
	{
		return sync( timeoutSec, null, command );
	}

	public static int syncAt( int timeoutSec, ExecCallback callback, String cwd, String... command )
	{
		ProcessBuilder pb = new ProcessBuilder( command );
		pb.directory( new File( cwd ) );
		Process p = null;
		Thread[] list = null;
		try {
			p = pb.start();
			if ( callback != null ) {
				list = setupCallback( p, callback );
			}
			if ( timeoutSec <= 0 ) {
				return p.waitFor();
			} else {
				for ( int i = 0; i < timeoutSec * 10; ++i ) {
					Thread.sleep( 100 );
					try {
						return p.exitValue();
					} catch ( IllegalThreadStateException e ) {
						// Wait...
					}
				}
				// Timeout
				return -1;
			}
		} catch ( IOException | InterruptedException e ) {
			return -2;
		} finally {
			try {
				if ( p != null ) {
					Util.multiClose( p.getOutputStream(), p.getErrorStream() );
					p.destroy();
				}
			} catch ( Exception e ) {
				//
			}
			if ( list != null ) {
				for ( Thread t : list ) {
					try {
						t.interrupt();
					} catch ( Exception e ) {
						//
					}
				}
			}
		}
	}

	public static int syncAt( int timeoutSec, String cwd, String... command )
	{
		return syncAt( timeoutSec, null, cwd, command );
	}

	private static Thread[] setupCallback( final Process p, final ExecCallback cb )
	{
		// Read its stdout
		Thread stdout = new Thread( new Runnable() {
			@Override
			public void run()
			{
				try {
					BufferedReader reader = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
					String line;
					while ( ( line = reader.readLine() ) != null ) {
						synchronized ( p ) {
							cb.processStdOut( line );
						}
					}
				} catch ( Exception e ) {
				}
			}
		} );
		// Read its stderr
		Thread stderr = new Thread( new Runnable() {
			@Override
			public void run()
			{
				try {
					BufferedReader reader = new BufferedReader( new InputStreamReader( p.getErrorStream() ) );
					String line;
					while ( ( line = reader.readLine() ) != null ) {
						synchronized ( p ) {
							cb.processStdErr( line );
						}
					}
				} catch ( Exception e ) {
				}
			}
		} );

		stdout.start();
		stderr.start();

		Thread[] t = new Thread[] { stdout, stderr };
		return t;
	}

	/**/

	public interface ExecCallback
	{
		public void processStdOut( String line );

		public void processStdErr( String line );
	}

}