List of usage examples for org.apache.commons.vfs2 FileObject setExecutable
boolean setExecutable(boolean executable, boolean ownerOnly) throws FileSystemException;
From source file:sf.net.experimaestro.connectors.UnixScriptProcessBuilder.java
@Override final public XPMProcess start() throws LaunchException, IOException { final FileObject runFile = connector.resolveFile(path); final FileObject basepath = runFile.getParent(); final String baseName = runFile.getName().getBaseName(); try (CommandContext env = new CommandContext.FolderContext(connector, basepath, baseName)) { // Prepare the commands commands().prepare(env);/* ww w. j a v a 2 s. c om*/ // First generate the run file PrintWriter writer = new PrintWriter(runFile.getContent().getOutputStream()); writer.format("#!%s%n", shPath); writer.format("# Experimaestro generated task: %s%n", path); writer.println(); // A command fails if any of the piped commands fail writer.println("set -o pipefail"); writer.println(); writer.println(); if (environment() != null) { for (Map.Entry<String, String> pair : environment().entrySet()) writer.format("export %s=\"%s\"%n", pair.getKey(), protect(pair.getValue(), QUOTED_SPECIAL)); } if (directory() != null) { writer.format("cd \"%s\"%n", protect(env.resolve(directory()), QUOTED_SPECIAL)); } if (!lockFiles.isEmpty()) { writer.format("%n# Checks that the locks are set%n"); for (String lockFile : lockFiles) { writer.format("test -f %s || exit 017%n", lockFile); } } writer.format( "%n%n# Set traps to cleanup (remove locks and temporary files, kill remaining processes) when exiting%n%n"); writer.format("trap cleanup EXIT SIGINT SIGTERM%n"); writer.format("cleanup() {%n"); for (String file : lockFiles) { writer.format(" rm -f %s;%n", file); } commands().forEachCommand(Streams.propagate(c -> { final CommandContext.NamedPipeRedirections namedRedirections = env.getNamedRedirections(c, false); for (FileObject file : Iterables.concat(namedRedirections.outputRedirections, namedRedirections.errorRedirections)) { writer.format(" rm -f %s;%n", env.resolve(file)); } })); // Kills remaining processes writer.println(" jobs -pr | xargs kill"); writer.format("}%n%n"); // Write the command final StringWriter sw = new StringWriter(); PrintWriter exitWriter = new PrintWriter(sw); exitWriter.format("code=$?; if test $code -ne 0; then%n"); if (exitCodePath != null) exitWriter.format(" echo $code > \"%s\"%n", protect(exitCodePath, QUOTED_SPECIAL)); exitWriter.format(" exit $code%n"); exitWriter.format("fi%n"); String exitScript = sw.toString(); writer.format("%n%n"); switch (input.type()) { case INHERIT: break; case READ: writer.format("cat \"%s\" | ", connector.resolve(input.file())); break; default: throw new UnsupportedOperationException("Unsupported input redirection type: " + input.type()); } writer.println("("); // The prepare all the commands writeCommands(env, writer, commands()); writer.print(") "); writeRedirection(writer, output, 1); writeRedirection(writer, error, 2); writer.println(); writer.print(exitScript); if (exitCodePath != null) writer.format("echo 0 > \"%s\"%n", protect(exitCodePath, QUOTED_SPECIAL)); if (donePath != null) writer.format("touch \"%s\"%n", protect(donePath, QUOTED_SPECIAL)); writer.close(); // Set the file as executable runFile.setExecutable(true, false); processBuilder.command(protect(path, SHELL_SPECIAL)); processBuilder.detach(true); processBuilder.redirectOutput(output); processBuilder.redirectError(error); processBuilder.job(job); return processBuilder.start(); } catch (Exception e) { throw new LaunchException(e); } }