@Override public boolean isFinished() { // if process is marked as finished, consider it so if (isMarkedAsFinished()) { return true; } try { if (!processReference.isInitialized()) { return false; } processReference.getProcess().exitValue(); return true; } catch (IllegalThreadStateException e) { return false; } }
@Deprecated public CommandTool workingDir(String workingDirectory) throws IllegalArgumentException { return workingDirectory(workingDirectory); }
@Override public boolean hasFailed() { if (!isFinished()) { throw new IllegalStateException("Process " + processName + " is not yet finished, cannot determine whether it failed."); } // check whether we have specified exit value and if not adhere to defaults if (allowedExitCodes.isEmpty()) { return processReference.getProcess().exitValue() != 0; } return !allowedExitCodes.contains(processReference.getProcess().exitValue()); }
@Override protected ProcessResult process(Object input) throws Exception { Validate.executionNotNull(commandBuilder, "Command must not be null"); .redirectErrorStream(true) .shouldExitWith(allowedExitCodes) .command(command) .workingDirectory(workingDirectory) .addEnvironment(environment) .runAsDaemon(isDaemon) .execute(); processRef.setProcess(process); .programName(command.getProgramName()).interaction(interaction).execute();
@Override public void startRecording(String remoteFilePath, ScreenrecordOptions options) throws Exception { if (isRecording()) { throw new IllegalStateException("Android device is already recording the video."); } Validate.notNullOrEmpty(remoteFilePath, "remoteFilePath for taken video is a null object"); recordedVideoRemote = remoteFilePath; Validate.notNull(options, "options for recording of a video is a null object"); CommandTool screenRecorderTool = new ScreenRecordToolBuilder() .androidSdk(androidSdk) .options(options) .remoteFilePath(recordedVideoRemote) .build(); screenrecorderExecution = screenRecorderTool.execute(); }
@Override public Execution<Process> execute() throws ExecutionException { // here we rewrap future based execution into process based execution to get better details about execution Execution<Process> processFutureExecution = super.execute(); Process process = processFutureExecution.await(); ProcessReference ref = new ProcessReference(command.getProgramName()); ref.setProcess(process); ProcessBasedExecution<Process> execution = new ProcessBasedExecution<Process>(processFutureExecution, ref, command.getProgramName(), allowedExitCodes); // register shutdown hook if (!runsAsDaemon) { execution.registerShutdownHook(); } return execution; }
@Override public Execution<RESULT> terminate() throws ExecutionException { // if process has not yet started, terminate Future that would lead to its creation if (!processReference.isInitialized()) { processFutureExecution.terminate(); return markAsFinished(); } processReference.getProcess().destroy(); try { processReference.getProcess().waitFor(); } catch (InterruptedException e) { log.log(Level.WARNING, "Ignoring Interuption Exception while terminating the process {0}", processName); } // close STDIN of the process, if any OutputStream ostream = processReference.getProcess().getOutputStream(); try { if (ostream != null) { ostream.flush(); ostream.close(); } } catch (IOException e) { log.log(Level.WARNING, "Ignoring IO exception while terminating the process {0}", processName); } return this; }
@Override public Execution<ProcessResult> execute() throws ExecutionException { // here we rewrap future based execution into process based execution to get better details about execution // and ability to terminate the process this.processRef = new ProcessReference(commandBuilder.build().getProgramName()); Execution<ProcessResult> processFutureExecution = super.execute(); ProcessBasedExecution<ProcessResult> execution = new ProcessBasedExecution<ProcessResult>(processFutureExecution, processRef, commandBuilder.build().getProgramName(), allowedExitCodes); return execution; }
/** * Adds a map of key, value environment variables to the default process environment * * @param envVariables * environment variables. Value might be null. * * @throws IllegalArgumentException */ public CommandTool addEnvironment(Map<? extends CharSequence, ? extends CharSequence> envVariables) throws IllegalArgumentException { Validate.notNull(envVariables, "Environment variables must not be null"); for (Map.Entry<? extends CharSequence, ? extends CharSequence> entry : envVariables.entrySet()) { Validate.notNull(entry.getKey(), "Environment variable name must not be null nor empty"); CharSequence value = entry.getValue(); environment.put(entry.getKey().toString(), value != null ? value.toString() : null); } return this; }
/** * Sets executable to be executed. This can either a an absolute path or executed on path of underlying file system * * @param programName * program name * * @throws IllegalArgumentException * If program name is null or empty */ public CommandTool programName(CharSequence programName) throws IllegalArgumentException { Validate.notNullOrEmpty(programName, "Program name must not be empty nor null"); this.commandBuilder = new CommandBuilder(programName); return this; }
public synchronized Process getProcess() throws ExecutionException { if (!isInitialized()) { throw new ExecutionException( "Unable to get {0} process identifier in ref {1}. This is a bug in Arquillian Spacelift.", processName, hashCode()); } return process; }
@Override protected Process process(Object input) throws Exception { Validate.executionNotNull(command, "Command must not be null"); Validate.executionNotNull(command.getProgramName(), "Command program name must not be null"); ProcessBuilder builder = new ProcessBuilder(command.getFullCommand()); builder.directory(workingDirectory); builder.environment().putAll(environment); builder.redirectErrorStream(redirectErrorStream); return builder.start(); } }
@Override public RESULT until(CountDownWatch timeout, ExecutionCondition<RESULT> condition) throws ExecutionException, TimeoutExecutionException { return until(timeout.timeout(), timeout.getTimeUnit(), condition); } }
@Override public RESULT awaitAtMost(CountDownWatch timeout) throws ExecutionException, TimeoutExecutionException { return awaitAtMost(timeout.timeout(), timeout.getTimeUnit()); }
/** * Sets working directory for the command * * @param workingDirectory * working directory, can be {@code null} to use current directory of running Java process * * @throws IllegalArgumentException * if working directory does not exist */ public CommandTool workingDirectory(String workingDirectory) throws IllegalArgumentException { if (workingDirectory == null) { this.workingDirectory = null; return this; } return workingDirectory(new File(workingDirectory)); }
/** * Adds a sequence of key, value environment variables to the default process environment * * @param envVariables * environment variables. Value might be null. * * @throws IllegalArgumentException * If values do not form complete pairs or if key is null */ public CommandTool addEnvironment(CharSequence... envVariables) throws IllegalArgumentException { if (envVariables.length % 2 == 1) { throw new IllegalArgumentException("Environment variables must be a sequence of key, value pairs."); } for (int i = 0; i < (envVariables.length / 2); i += 2) { CharSequence key = envVariables[i]; CharSequence value = envVariables[i + 1]; Validate.notNull(key, "Environment variable name must not be null nor empty"); environment.put(key.toString(), value != null ? value.toString() : null); } return this; }