/** * Add a single argument. * * @param argument The argument to add * @param handleQuoting Add the argument with/without handling quoting * @return The command line itself */ public CommandLine addArgument(final String argument, boolean handleQuoting) { if (argument == null) { return this; } // check if we can really quote the argument - if not throw an // IllegalArgumentException if (handleQuoting) { StringUtils.quoteArgument(argument); } arguments.add(new Argument(argument, handleQuoting)); return this; }
/** * Execute a command on the operating system using Java's built-in Process * class * * @param command A string array representation of the command to execute. * @param getOutput Whether or not to get the output/error streams of the * process you forked. This is helpful for debugging reasons. * @return A string representation of output/error streams of the process. */ public static String executeCommandUsingJavaRuntime(String[] command, boolean getOutput) { String output = ""; try { Process p = Runtime.getRuntime().exec(command); // read the output from the command if requested by the user if (getOutput) { List<String> outErrStr = IOUtils.readLines(p.getInputStream()); output = StringUtils.toString(outErrStr.toArray(new String[outErrStr.size()]), "\n"); } } catch (Exception ex) { LOGGER.log(Level.SEVERE, "An exception was thrown while executing the command (" + command + ")", ex); } return output; }
public int unload(String loadedInputPath) throws Exception { String unloaderDir = platformConfig.getUnloaderPath(); commandLine = new CommandLine(Paths.get(unloaderDir).toFile()); commandLine.addArgument("--graph-name"); commandLine.addArgument(formattedGraph.getName()); commandLine.addArgument("--output-path"); commandLine.addArgument(loadedInputPath); String commandString = StringUtils.toString(commandLine.toStrings(), " "); LOG.info(String.format("Execute graph unloader with command-line: [%s]", commandString)); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(System.out, System.err)); executor.setExitValue(0); return executor.execute(commandLine); }
@Override public String toString() { String command = StringUtils.toString(toStrings(), " "); if (workingDirectory.equals(new File("."))) { return "[" + command + "]"; } else { return "[" + command + "] in [" + workingDirectory.getAbsolutePath() + "]"; } }
/** * Returns the executable. * * @return The executable */ public String getExecutable() { // Expand the executable and replace '/' and '\\' with the platform // specific file separator char. This is safe here since we know // that this is a platform specific command. return StringUtils.fixFileSeparatorChar(expandArgument(executable)); }
/** * Returns the expanded and quoted command line arguments. * * @return The quoted arguments */ public String[] getArguments() { Argument currArgument; String expandedArgument; String[] result = new String[arguments.size()]; for(int i=0; i<result.length; i++) { currArgument = (Argument) arguments.get(i); expandedArgument = expandArgument(currArgument.getValue()); result[i] = (currArgument.isHandleQuoting() ? StringUtils.quoteArgument(expandedArgument) : expandedArgument); } return result; }
/** * Get the executable - the argument is trimmed and '/' and '\\' are * replaced with the platform specific file separator char * * @param executable the executable * @return the platform-specific executable string */ private String getExecutable(final String executable) { if (executable == null) { throw new IllegalArgumentException("Executable can not be null"); } else if(executable.trim().length() == 0) { throw new IllegalArgumentException("Executable can not be empty"); } else { return StringUtils.fixFileSeparatorChar(executable); } }
private CommandLine newJavaCommandLine() { String javaExecPath = null; Toolchain toolchain = getToolchain(); if (toolchain != null) { log.info("Toolchain: " + toolchain); javaExecPath = toolchain.findTool("java"); } else if (OS.isFamilyWindows()) { String exec = "java.exe"; String path = System.getenv("PATH"); if (path != null) { for (String elem : StringUtils.split(path, File.pathSeparator)) { File file = new File(elem, exec); if (file.exists()) { javaExecPath = file.getAbsolutePath(); break; } } } } if (null == javaExecPath) { javaExecPath = "java"; } return new CommandLine(javaExecPath); }
public int load(String loadedInputPath) throws Exception { String loaderDir = platformConfig.getLoaderPath(); commandLine = new CommandLine(Paths.get(loaderDir).toFile()); commandLine.addArgument("--graph-name"); commandLine.addArgument(formattedGraph.getName()); commandLine.addArgument("--input-vertex-path"); commandLine.addArgument(formattedGraph.getVertexFilePath()); commandLine.addArgument("--input-edge-path"); commandLine.addArgument(formattedGraph.getEdgeFilePath()); commandLine.addArgument("--output-path"); commandLine.addArgument(loadedInputPath); commandLine.addArgument("--directed"); commandLine.addArgument(formattedGraph.isDirected() ? "true" : "false"); commandLine.addArgument("--weighted"); commandLine.addArgument(formattedGraph.hasEdgeProperties() ? "true" : "false"); String commandString = StringUtils.toString(commandLine.toStrings(), " "); LOG.info(String.format("Execute graph loader with command-line: [%s]", commandString)); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(System.out, System.err)); executor.setExitValue(0); return executor.execute(commandLine); }
/** * Executes the platform job with the pre-defined parameters. * * @return the exit code * @throws IOException if the platform failed to run */ public int execute() throws Exception { String executableDir = platformConfig.getExecutablePath(); commandLine = new CommandLine(Paths.get(executableDir).toFile()); // List of benchmark parameters. String jobId = getJobId(); String logDir = getLogPath(); // List of dataset parameters. String inputPath = getInputPath(); String outputPath = getOutputPath(); // List of platform parameters. int numMachines = platformConfig.getNumMachines(); int numThreads = platformConfig.getNumThreads(); String homeDir = platformConfig.getHomePath(); appendBenchmarkParameters(jobId, logDir); appendAlgorithmParameters(); appendDatasetParameters(inputPath, outputPath); appendPlatformConfigurations(homeDir, numMachines, numThreads); String commandString = StringUtils.toString(commandLine.toStrings(), " "); LOG.info(String.format("Execute benchmark job with command-line: [%s]", commandString)); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(System.out, System.err)); executor.setExitValue(0); return executor.execute(commandLine); }
/** * Returns the classpath for executable jar. */ protected String getClasspath() throws IOException { List<String> classpath = new ArrayList<>(); classpath.add(getBundleJarPath()); classpath.addAll(getPluginsPath()); return StringUtils.toString(classpath.toArray(new String[classpath.size()]), " "); }
private File createCommandFile(final CommandLine cmd, final Map env) throws IOException { File script = File.createTempFile("EXEC", ".TMP"); script.deleteOnExit(); PrintWriter out = null; try { out = new PrintWriter(new FileWriter(script.getAbsolutePath(),true)); // add the environment as global symbols for the DCL script if (env != null) { Set entries = env.entrySet(); for (Iterator iter = entries.iterator(); iter.hasNext();) { Entry entry = (Entry) iter.next(); out.print("$ "); out.print(entry.getKey()); out.print(" == "); // define as global symbol out.println('\"'); String value = (String) entry.getValue(); // Any embedded " values need to be doubled if (value.indexOf('\"') > 0){ StringBuffer sb = new StringBuffer(); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); if (c == '\"') { sb.append('\"'); } sb.append(c); } value=sb.toString(); } out.print(value); out.println('\"'); } } final String command = cmd.getExecutable(); if (cmd.isFile()){// We assume it is it a script file out.print("$ @"); // This is a bit crude, but seems to work String parts[] = StringUtils.split(command,"/"); out.print(parts[0]); // device out.print(":["); out.print(parts[1]); // top level directory final int lastPart = parts.length-1; for(int i=2; i< lastPart; i++){ out.print("."); out.print(parts[i]); } out.print("]"); out.print(parts[lastPart]); } else { out.print("$ "); out.print(command); } String[] args = cmd.getArguments(); for (int i = 0; i < args.length; i++) { out.println(" -"); out.print(args[i]); } out.println(); } finally { if (out != null) { out.close(); } } return script; }
/** * Stringify operator returns the command line as a string. * Parameters are correctly quoted when containing a space or * left untouched if the are already quoted. * * @return the command line as single string */ public String toString() { return StringUtils.toString(toStrings(), " "); }
/** * Expand variables in a command line argument. * * @param argument the argument * @return the expanded string */ private String expandArgument(final String argument) { StringBuffer stringBuffer = StringUtils.stringSubstitution(argument, this.getSubstitutionMap(), true); return stringBuffer.toString(); }