This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathSASJavaExec.java
305 lines (271 loc) · 11.5 KB
/
SASJavaExec.java
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* ***************************************************************************
* Copyright (c) 2015 by SAS Institute Inc., Cary, NC 27513 USA
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* <p/>
* ****************************************************************************
*/
package dev;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.logging.Level.SEVERE;
/**
* This class can be instantiated from the Base SAS Java Object and used to
* asynchronously fork a new process.<br/>
* <br/>
* Example:<br/>
* First instantiate the object like this:<br/>
* declare javaobj j("dev.SASJavaExec", "&PYTHON_EXEC_COMMAND", python_call);<br/>
* <br/>
* Optionally, set the working directory like this:<br/>
* j.callVoidMethod("setWorkingDirectory", "c:\\temp\\SASJavaExec\\");<br/>
* <br/>
* Then call the executeProcess method like this:<br/>
* j.callIntMethod("executeProcess", rtn_val);<br/>
* <br/>
* Or the executeProcess method, specifying a timeout in minutes, like this:<br/>
* j.callIntMethod("executeProcess", 6, rtn_val);<br/>
* <br/>
*
* @author <a href="mailto:patrick.hall@sas.com">Patrick Hall, SAS Institute</a>
* @author <a href="mailto:radhikha.myneni@sas.com">Radhikha Myneni, SAS Institute</a>
* @author <a href="mailto:ruiwen.zhang@sas.com">Ruiwen Zhang, SAS Institute</a>
* @author <a href="mailto:tim.haley@sas.com">Tim Haley, SAS Institute</a>
* @version 0.2
* @since 2015-04-01
*/
public class SASJavaExec {
private static final Logger logger = Logger.getLogger(SASJavaExec.class.getName());
/**
* Timeout default in minutes for executeProcess method.
*/
private final static long DEFAULT_TIMEOUT = 1000L;
/**
* Path to executable command.
*/
private final String execCommand;
/**
* Path to Class or script to execute.
*/
private final String script;
/**
* Optional input string to provide on the standard input of the executed process.
*/
private final String inputString;
/**
* Optional directory in which to execute
*/
private String workingDirectory;
public String getWorkingDirectory() {
return workingDirectory;
}
public void setWorkingDirectory(String workingDirectory) {
this.workingDirectory = workingDirectory;
}
/**
* Constructs a SASJavaExec object with NO command line arguments to STDIN.
*
* @param execCommand Path to executable, i.e. C:\Anaconda\python.exe
* @param script Path to Class or script to execute, including any command line arguments,
* i.e. C:\Path\to\digitsdata_svm.py
*/
public SASJavaExec(String execCommand, String script) {
this(execCommand, script, null);
}
/**
* Constructs a SASJavaExec object with command line arguments to STDIN.
*
* @param execCommand Path to executable, i.e. C:\Program Files\R\R-3.1.2\bin\x64\Rscript.exe
* @param script Path to Class or script to execute, including any command line arguments,
* i.e. &WORK_DIR.\digitsdata_svm.R &WORK_DIR
* @param inputString Optional input values for STDIN, i.e. interactive input to command.
*/
public SASJavaExec(String execCommand, String script, String inputString) {
this.execCommand = execCommand.trim();
this.script = script.trim();
this.inputString = inputString;
}
/**
* Executes the process specified by execCommand, className, and optionally,
* inValuesString.
*
* @return the exit code from the process, or <br/>
* -1 if the process failed to start <br/>
* -2 if interrupted and the process was killed <br/>
* -3 if the working directory could not be created. <br/>
* -4 if a file with the working directory name exists but is not a directory. <br/>
*/
public int executeProcess() {
// passing arguments with spaces in them to ProcessBuilder causes extraneous quotes so
// we split all command strings by spaces and pass the array in the original order.
final List<String> processStrings = tokenizeString(execCommand);
processStrings.addAll(tokenizeString(script));
logger.info(MessageFormat.format("Executing {0} ...", processStrings));
final ProcessBuilder processBuilder = new ProcessBuilder(processStrings);
if (workingDirectory != null) {
final Path dir = Paths.get(workingDirectory);
if (Files.notExists(dir)) {
try {
Files.createDirectories(dir);
} catch (IOException e) {
logger.warning("Could not create working directory: " + workingDirectory);
return -3;
}
} else if (!Files.isDirectory(dir)) {
logger.warning(workingDirectory + " already exists but is not a directory.");
return -4;
}
final File directory = dir.toFile();
processBuilder.directory(directory);
}
// Redirect STDERR to STDOUT.
processBuilder.redirectErrorStream(true);
Integer exitValue;
try {
logger.info("Starting external process ...");
final Process process = processBuilder.start();
sendInputStringToProcess(process);
// This handles both STDOUT and STDERR and does not return until the process closes
// both streams, typically when it terminates.
handleProcessOutput(process);
// Wait for the process to return an exit value
try {
exitValue = process.waitFor();
} catch (InterruptedException e) {
logger.warning("Interrupted while waiting, killing external process ...");
process.destroy();
exitValue = -2;
}
logger.info("External process exit value " + exitValue + ".");
} catch (IOException e) {
logger.log(SEVERE, "Failed to start external Process: ", e.getCause());
exitValue = -1;
}
return exitValue;
}
/**
* Executes the process with a time limit of timeoutMin.
*
* @param timeoutMin the maximum number of minutes to wait for the process to complete.
* May be fractional; eg: 0.1 is 6 seconds.
* 0.0 or less will result in the default timeout of 1000 minutes.
*
* @return the exit code from the process, or
* -1 if the process failed to start
* -2 if interrupted and the process was killed
* -3 if the working directory could not be created.
* -4 if a file with the working directory name exists but is not a directory.
* -5 if the process timed out
*/
public int executeProcess(double timeoutMin) {
int exitValue;
final long timeoutInSeconds = Math.round(((timeoutMin > 0) ? timeoutMin : DEFAULT_TIMEOUT) * 60);
logger.info("Using Single Thread Executor.");
final ExecutorService executor = Executors.newSingleThreadExecutor();
final Future<Integer> future = executor.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return executeProcess();
}
});
try {
// Wait for and return the result of the executed process.
exitValue = future.get(timeoutInSeconds, TimeUnit.SECONDS); //timeout is in timeoutMin minutes
logger.info("Process completed with exit value: " + exitValue);
} catch (TimeoutException e) {
logger.log(SEVERE, "Process timed out after " + timeoutMin + " minutes.");
exitValue = -5;
} catch (ExecutionException e) {
logger.log(SEVERE, "Process threw an exception.", e.getCause());
exitValue = -6;
} catch (InterruptedException e) {
logger.warning("Interrupted while waiting for process to complete.");
future.cancel(true);
exitValue = -2;
}
executor.shutdownNow();
return exitValue;
}
/**
* Parse the string into separate tokens, which are separated by spaces in the input.
* Handles strings containing matched pairs of single or double quotes.
* Anything inside of a matched pair of quotes is considered a single token, excluding the quotation marks.
* Single and double quotes can be mixed to support including either single or double quotes in the token.
* Ex: <em>this 'is "an example"' of mixing quotes</em> will be parsed into [this, is "an example", of, mixing, quotes]
*
* @param input the string to parse.
*
* @return the list of tokens found in the string.
*/
private List<String> tokenizeString(String input) {
List<String> matchList = new ArrayList<>();
Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'");
Matcher regexMatcher = regex.matcher(input);
while (regexMatcher.find()) {
if (regexMatcher.group(1) != null) {
// Add double-quoted string without the quotes
matchList.add(regexMatcher.group(1));
} else if (regexMatcher.group(2) != null) {
// Add single-quoted string without the quotes
matchList.add(regexMatcher.group(2));
} else {
// Add unquoted word
matchList.add(regexMatcher.group());
}
}
return matchList;
}
/**
* If an input string was specified, stream it to the process standard input.
*
* @param process the process that is expecting the string.
*/
private void sendInputStringToProcess(Process process) {
if (inputString != null) {
// Try with resources will automatically close the resources, no finally block is needed.
try (PrintStream ps = new PrintStream(process.getOutputStream())) {
ps.print(inputString);
ps.print('\n');
}
}
}
/**
* Read process output line by line and write it to the log.
*
* @param process for which to handle the output.
*/
private void handleProcessOutput(Process process) {
// Try with resources will automatically close the resources, no finally block is needed.
try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = br.readLine()) != null) {
logger.info("PROCESS OUTPUT >>> " + line);
}
} catch (IOException e) {
logger.log(SEVERE, "Failed to read output from external Process: ", e.getCause());
}
}
}