List of usage examples for org.apache.commons.exec CommandLine addArgument
public CommandLine addArgument(final String argument)
From source file:com.adaptris.hpcc.DfuplusConnection.java
public CommandLine addArguments(CommandLine cmdLine) throws PasswordException { cmdLine.addArgument(String.format("server=%s", getServer())); if (!isBlank(getUsername())) { cmdLine.addArgument(String.format("username=%s", getUsername())); }/*from w ww.j a v a2 s.c o m*/ if (!isBlank(getPassword())) { cmdLine.addArgument( String.format("password=%s", Password.decode(ExternalResolver.resolve(getPassword())))); } if (getSourceIp() != null) { cmdLine.addArgument(String.format("srcip=%s", getSourceIp())); } if (getReplicate() != null) { cmdLine.addArgument(String.format("replicate=%d", getReplicate().booleanValue() ? 1 : 0)); } if (getNoRecover() != null) { cmdLine.addArgument(String.format("norecover=%d", getNoRecover().booleanValue() ? 1 : 0)); } if (getThrottle() != null) { cmdLine.addArgument(String.format("throttle=%d", getThrottle().intValue())); } if (getTransferBufferSize() != null) { cmdLine.addArgument(String.format("transferbuffersize=%d", getTransferBufferSize().intValue())); } return cmdLine; }
From source file:com.blackducksoftware.tools.scmconnector.integrations.subversion.SubversionConnector.java
@Override public int sync() { int exitStatus = -1; try {/*from w w w . j a v a 2 s. c o m*/ File targetDir = new File(getFinalSourceDirectory()); CommandLine command = CommandLine.parse(EXECUTABLE); if (!repoExists(targetDir)) { command.addArgument("co"); command.addArgument(repositoryURL); } else { command.addArgument("up"); } // now add destination // command.addArgument(targetDir.getAbsolutePath()); if (user != null && !user.isEmpty()) { command.addArgument("--username"); command.addArgument(user); if (!password.isEmpty()) { command.addArgument("--password"); command.addArgument(password); } } command.addArgument("--non-interactive"); for (String svnFlag : svnFlagsList) { command.addArgument(svnFlag); } exitStatus = commandLineExecutor.executeCommand(log, command, targetDir); } catch (Exception e) { log.error("Unable to perform sync: " + e.getMessage()); } return exitStatus; }
From source file:com.netflix.genie.core.services.impl.LocalJobKillServiceImpl.java
private void killJobOnUnix(final int pid) throws GenieException { try {//from www .j a v a 2 s .co m // Ensure this process check can't be timed out final Calendar tomorrow = Calendar.getInstance(JobConstants.UTC); tomorrow.add(Calendar.DAY_OF_YEAR, 1); final ProcessChecker processChecker = new UnixProcessChecker(pid, this.executor, tomorrow.getTime()); processChecker.checkProcess(); } catch (final ExecuteException ee) { // This means the job was done already log.debug("Process with pid {} is already done", pid); return; } catch (final IOException ioe) { throw new GenieServerException("Unable to check process status for pid " + pid, ioe); } // TODO: Do we need retries? // This means the job client process is still running try { final CommandLine killCommand; if (this.runAsUser) { killCommand = new CommandLine("sudo"); killCommand.addArgument("kill"); } else { killCommand = new CommandLine("kill"); } killCommand.addArguments(Integer.toString(pid)); this.executor.execute(killCommand); } catch (final IOException ioe) { throw new GenieServerException("Unable to kill process " + pid, ioe); } }
From source file:beans.DeployManagerImpl.java
public WidgetInstance fork(ServerNode server, Widget widget) { File unzippedDir = Utils.downloadAndUnzip(widget.getRecipeURL(), widget.getApiKey()); File recipeDir = unzippedDir; if (widget.getRecipeRootPath() != null) { recipeDir = new File(unzippedDir, widget.getRecipeRootPath()); }/*from w w w .jav a 2 s . co m*/ logger.info("Deploying an instance for recipe at : [{}] ", recipeDir); Recipe.Type recipeType = new Recipe(recipeDir).getRecipeType(); if (alreadyInstalled(server, widget, recipeType)) { logger.info("[{}] [{}] is already installed", recipeType, widget.toInstallName()); WidgetInstance widgetInstance = widget.addWidgetInstance(server, recipeDir); String publicIp = getServicePublicIp(widgetInstance); if (!StringUtils.isEmpty(publicIp)) { logger.info("found service ip at [{}]", publicIp); widgetInstance.setServicePublicIp(publicIp); widgetInstance.save(); } server.createEvent(null, ServerNodeEvent.Type.DONE).save(); return widgetInstance; } else { logger.info("Deploying: [ServerIP={}] [recipe={}] [type={}]", new Object[] { server.getPublicIP(), recipeDir, recipeType.name() }); String recipePath = FilenameUtils.separatorsToSystem(recipeDir.getPath()); CommandLine cmdLine = new CommandLine(conf.cloudify.deployScript); cmdLine.addArgument(server.getPublicIP()); cmdLine.addArgument(recipePath.replaceAll("\\\\", "/")); // support for windows. cmdLine.addArgument(recipeType.commandParam); cmdLine.addArgument(widget.toInstallName()); execute(cmdLine, server); return widget.addWidgetInstance(server, recipeDir); } }
From source file:io.takari.maven.testing.executor.ForkedLauncher.java
public int run(String[] cliArgs, Map<String, String> envVars, File multiModuleProjectDirectory, File workingDirectory, File logFile) throws IOException, LauncherException { String javaHome;/*from w w w .ja v a 2 s. c o m*/ if (envVars == null || envVars.get("JAVA_HOME") == null) { javaHome = System.getProperty("java.home"); } else { javaHome = envVars.get("JAVA_HOME"); } File executable = new File(javaHome, Os.isFamily(Os.FAMILY_WINDOWS) ? "bin/javaw.exe" : "bin/java"); CommandLine cli = new CommandLine(executable); cli.addArgument("-classpath").addArgument(classworldsJar.getAbsolutePath()); cli.addArgument("-Dclassworlds.conf=" + new File(mavenHome, "bin/m2.conf").getAbsolutePath()); cli.addArgument("-Dmaven.home=" + mavenHome.getAbsolutePath()); cli.addArgument("-Dmaven.multiModuleProjectDirectory=" + multiModuleProjectDirectory.getAbsolutePath()); cli.addArgument("org.codehaus.plexus.classworlds.launcher.Launcher"); cli.addArguments(args.toArray(new String[args.size()])); if (extensions != null && !extensions.isEmpty()) { cli.addArgument("-Dmaven.ext.class.path=" + toPath(extensions)); } cli.addArguments(cliArgs); Map<String, String> env = new HashMap<>(); if (mavenHome != null) { env.put("M2_HOME", mavenHome.getAbsolutePath()); } if (envVars != null) { env.putAll(envVars); } if (envVars == null || envVars.get("JAVA_HOME") == null) { env.put("JAVA_HOME", System.getProperty("java.home")); } DefaultExecutor executor = new DefaultExecutor(); executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); executor.setWorkingDirectory(workingDirectory.getAbsoluteFile()); try (OutputStream log = new FileOutputStream(logFile)) { PrintStream out = new PrintStream(log); out.format("Maven Executor implementation: %s\n", getClass().getName()); out.format("Maven home: %s\n", mavenHome); out.format("Build work directory: %s\n", workingDirectory); out.format("Environment: %s\n", env); out.format("Command line: %s\n\n", cli.toString()); out.flush(); PumpStreamHandler streamHandler = new PumpStreamHandler(log); executor.setStreamHandler(streamHandler); return executor.execute(cli, env); // this throws ExecuteException if process return code != 0 } catch (ExecuteException e) { throw new LauncherException("Failed to run Maven: " + e.getMessage() + "\n" + cli, e); } }
From source file:net.ladenthin.snowman.imager.run.streamer.Streamer.java
@Override public void run() { final CImager conf = ConfigurationSingleton.ConfigurationSingleton.getImager(); for (;;) {/*from w w w.j a va2 s . com*/ if (WatchdogSingleton.WatchdogSingleton.getWatchdog().getKillFlag() == true) { LOGGER.trace("killFlag == true"); return; } final CommandLine cmdLine = new CommandLine(streamer); // final CommandLine cmdLine = new CommandLine("sleep"); // cmdLine.addArgument("200"); cmdLine.addArgument("-c"); cmdLine.addArgument(conf.getStreamer().getDevice()); cmdLine.addArgument("-t"); cmdLine.addArgument( String.valueOf(conf.getStreamer().getFramesPerSecond() * conf.getStreamer().getRecordTime())); cmdLine.addArgument("-r"); cmdLine.addArgument(String.valueOf(conf.getStreamer().getFramesPerSecond())); cmdLine.addArgument("-s"); cmdLine.addArgument(conf.getStreamer().getResolutionX() + "x" + conf.getStreamer().getResolutionY()); cmdLine.addArgument("-o"); cmdLine.addArgument(conf.getStreamer().getPath() + File.separator + conf.getSnowmanServer().getCameraname() + "_" + (long) (System.currentTimeMillis() / 1000) + "_" + conf.getStreamer().getFramesPerSecond() + "_00000000.jpeg"); LOGGER.trace("cmdLine: {}", cmdLine); // 10 seconds should be more than enough final long safetyTimeWindow = 10000; final DefaultExecutor executor = new DefaultExecutor(); final long timeout = 1000 * (conf.getStreamer().getRecordTime() + safetyTimeWindow); // final long timeout = 5000; LOGGER.trace("timeout: {}", timeout); final ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); try { LOGGER.debug("start process"); final int exitValue = executor.execute(cmdLine); LOGGER.debug("process executed"); LOGGER.trace("exitValue: {}", exitValue); } catch (IOException e) { if (watchdog.killedProcess()) { LOGGER.warn("Process was killed on purpose by the watchdog "); } else { LOGGER.error("Process exited with an error."); Imager.waitALittleBit(5000); } } LOGGER.trace("loop end"); } }
From source file:de.tu_dresden.psy.fca.ConexpCljBridge.java
public ConexpCljBridge() { this.b = new byte[1]; /**/*from w ww.j ava 2 s. c om*/ * build the command line (see conexp-clj/bin/conexp-clj) */ String java_bin = Launcher.getJavaCommand(); CommandLine conexp_cmd = new CommandLine(java_bin); conexp_cmd.addArgument("-server"); conexp_cmd.addArgument("-cp"); conexp_cmd.addArgument("./conexp-clj/lib/conexp-clj-0.0.7-alpha-SNAPSHOT-standalone.jar"); conexp_cmd.addArgument("clojure.main"); conexp_cmd.addArgument("-e"); conexp_cmd.addArgument(""); conexp_cmd.addArgument("./conexp-clj/lib/conexp-clj.clj"); /** * open the pipes */ this.to_conexp = new PipedOutputStream(); try { this.stream_to_conexp = new PipedInputStream(this.to_conexp, 2048); } catch (IOException e2) { e2.printStackTrace(); } this.stream_error_conexp = new PipedOutputStream(); this.stream_from_conexp = new PipedOutputStream(); try { this.from_conexp = new PipedInputStream(this.stream_from_conexp, 2048); } catch (IOException e1) { e1.printStackTrace(); } try { this.error_conexp = new PipedInputStream(this.stream_error_conexp, 2048); } catch (IOException e1) { e1.printStackTrace(); } /** * setup apache commons exec */ this.result = new DefaultExecuteResultHandler(); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setStreamHandler( new PumpStreamHandler(this.stream_from_conexp, this.stream_error_conexp, this.stream_to_conexp)); /** * run in non-blocking mode */ try { executor.execute(conexp_cmd, this.result); } catch (ExecuteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } this.output_buffer = ""; }
From source file:com.blackducksoftware.tools.scmconnector.integrations.git.GitConnector.java
/** * Checks to see if there is an existing git clone * * @param targetDir//from w w w .j a v a2 s.c om * @return * @throws Exception */ private boolean doesGitCloneExist(File targetDir) throws Exception { validateExecutableInstance(EXECUTABLE); CommandLine command = CommandLine.parse(EXECUTABLE); command.addArgument(GIT_COMMAND_STATUS); command.addArgument(repositoryURL); // Check to see if there are any files in the target directory // If not, return false File[] files = targetDir.listFiles(); if (files == null || files.length == 0) { log.info("Nothing inside the target directory: " + targetDir); log.info("Skipping status check."); return false; } int exitStatus = commandLineExecutor.executeCommand(log, command, targetDir); if (exitStatus == 0) { log.info("repository exists in directory: " + targetDir.getAbsolutePath()); return true; } else { log.info("repository does not exist in directory: " + targetDir.getAbsolutePath()); return false; } }
From source file:com.muk.services.util.BarcodeServiceImpl.java
@Override public File generateBarcode(File path, String giftCardId) { CommandLine cmdLine = null; if (System.getProperty("os.name").equals("Linux")) { cmdLine = new CommandLine("convert"); } else {// ww w.ja va2 s.com cmdLine = new CommandLine("convert.exe"); } cmdLine.addArgument("-font"); cmdLine.addArgument("Free-3-of-9-Extended-Regular"); cmdLine.addArgument("-pointsize"); cmdLine.addArgument("40"); cmdLine.addArgument("-bordercolor"); cmdLine.addArgument("white"); cmdLine.addArgument("-border"); cmdLine.addArgument("10x10"); cmdLine.addArgument("label:${giftCardId}"); cmdLine.addArgument("${outfile}"); Map<String, Object> map = new HashMap<String, Object>(); map.put("giftCardId", giftCardId); map.put("outfile", path); cmdLine.setSubstitutionMap(map); ProcessExecutor executor = new ProcessExecutor(); try { if (0 == executor.runCommandLine(cmdLine, 100000)) { return path; } } catch (IOException ex) { LOG.error("Failed to execute imagemagick", ex); } return null; }
From source file:com.netflix.genie.web.services.impl.LocalJobKillServiceImpl.java
private void killJobOnUnix(final int pid) throws GenieException { try {//ww w . j ava2 s . c o m // Ensure this process check can't be timed out final Instant tomorrow = Instant.now().plus(1, ChronoUnit.DAYS); final ProcessChecker processChecker = new UnixProcessChecker(pid, this.executor, tomorrow); processChecker.checkProcess(); } catch (final ExecuteException ee) { // This means the job was done already log.debug("Process with pid {} is already done", pid); return; } catch (final IOException ioe) { throw new GenieServerException("Unable to check process status for pid " + pid, ioe); } // TODO: Do we need retries? // This means the job client process is still running try { final CommandLine killCommand; if (this.runAsUser) { killCommand = new CommandLine("sudo"); killCommand.addArgument("kill"); } else { killCommand = new CommandLine("kill"); } killCommand.addArguments(Integer.toString(pid)); this.executor.execute(killCommand); } catch (final IOException ioe) { throw new GenieServerException("Unable to kill process " + pid, ioe); } }