summaryrefslogtreecommitdiffstats
path: root/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java
blob: ea71ea5cb91308531b78234da8284e7b215887da (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package org.openslx.taskmanager.api;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import org.apache.log4j.Logger;

public abstract class SystemCommandTask extends AbstractTask
{

	private static final Logger log = Logger.getLogger( SystemCommandTask.class );

	private String[] command = null;
	
	private Process process = null;


	@Override
	protected final boolean execute()
	{
		command = initCommandLine();
		if ( command == null || command.length == 0 ) {
			return processEnded( -1 );
		}

		ProcessBuilder pb = new ProcessBuilder( command );
		pb.directory( new File( "/" ) );

		try {

			// Create process
			process = pb.start();
			final Process p = process;
			processStarted();
			p.getOutputStream();

			// 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 ) {
								processStdOut( line );
							}
						}
					} catch ( IOException 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 ) {
								processStdErr( line );
							}
						}
					} catch ( IOException e ) {
					}
				}
			} );

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

			// Wait for everything
			stdout.join();
			stderr.join();
			process.waitFor();

			return processEnded( process.exitValue() );

		} catch ( IOException e ) {
			log.warn( "Process of task " + getId() + " died." );
			processStdErr( e.toString() );
			return processEnded( -2 );
		} catch ( InterruptedException e ) {
			Thread.currentThread().interrupt();
			return false;
		} catch ( Exception e ) {
			log.warn( "Unexpected exception when executing " + getId() + ": " + e.toString() );
			processStdErr( e.toString() );
			return processEnded( -3 );
		} finally {
			if ( process != null )
				process.destroy();
		}
	}
	
	/**
	 * Write data to the process's stdin.
	 * 
	 * @param data stuff to write
	 * @return success or failure mapped to a boolean in a really complicated way
	 */
	protected final boolean toStdIn(byte[] data)
	{
		try {
			process.getOutputStream().write( data );
		} catch ( IOException e ) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * Write text to the process's stdin.
	 * 
	 * @param text stuff to write
	 * @return success or failure mapped to a boolean in a really complicated way
	 */
	protected final boolean toStdIn(String text)
	{
		return toStdIn( text.getBytes( StandardCharsets.UTF_8 ) );
	}

	/**
	 * Called to get the command line. Each argument should be a separate array
	 * element. Returning null means the task should not run (as the arguments
	 * were probably faulty).
	 * 
	 * @return List of arguments. First element is the command itself.
	 */
	protected abstract String[] initCommandLine();

	/**
	 * Called when the process has been successfully started.
	 */
	protected void processStarted()
	{
	}

	/**
	 * Called when the process has finished running
	 * 
	 * @param exitCode the process' exit code
	 * @return 
	 */
	protected abstract boolean processEnded( int exitCode );

	/**
	 * Called when a line has been read from the process' stdout.
	 * 
	 * @param line The line read from the process, without any newline characters
	 */
	protected abstract void processStdOut( String line );

	/**
	 * Called when a line has been read from the process' stderr.
	 * Trailing newline is removed.
	 * 
	 * @param line The line read from the process, without any newline characters
	 */
	protected abstract void processStdErr( String line );

}