List of usage examples for org.apache.commons.exec CommandLine addArgument
public CommandLine addArgument(final String argument, final boolean handleQuoting)
From source file:modules.NativeProcessIterativeDaemonModule.java
protected String extractNative(String command, String options, Path path) throws NativeExecutionException { if (command == null || command.equals("")) { System.err.println("command null at GeneralNativeCommandModule.extractNative()"); return null; }// ww w . j ava 2s. c om CommandLine commandLine = new CommandLine(command); if (options != null && !options.equals("")) { String[] args = options.split(" "); commandLine.addArguments(args); } if (path != null) { commandLine.addArgument(path.toAbsolutePath().toString(), false); } DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); GeneralExecutableModuleConfig generalExecutableModuleConfig = getConfig(); executor.setWatchdog(new ExecuteWatchdog(generalExecutableModuleConfig.timeout)); if (getConfig().workingDirectory != null && getConfig().workingDirectory.exists()) { executor.setWorkingDirectory(getConfig().workingDirectory); } try { // System.out.println("Now execute " + commandLine); executor.execute(commandLine); } catch (ExecuteException xs) { NativeExecutionException n = new NativeExecutionException(); n.initCause(xs); if (path != null) { n.path = path.toAbsolutePath().toString(); } n.executionResult = outputStream.toString(); n.exitCode = xs.getExitValue(); throw n; } catch (IOException xs) { NativeExecutionException n = new NativeExecutionException(); n.initCause(xs); if (path != null) { n.path = path.toAbsolutePath().toString(); } n.executionResult = outputStream.toString(); throw n; } return outputStream.toString().trim(); }
From source file:ch.ivyteam.ivy.maven.engine.EngineControl.java
private CommandLine toEngineCommand(Command command) { String classpath = context.engineClasspathJarPath; if (StringUtils.isNotBlank(context.vmOptions.additionalClasspath)) { classpath += File.pathSeparator + context.vmOptions.additionalClasspath; }/*w w w.jav a 2s. co m*/ CommandLine cli = new CommandLine(new File(getJavaExec())).addArgument("-classpath").addArgument(classpath) .addArgument("-Dosgi.install.area=" + context.engineDirectory); if (StringUtils.isNotBlank(context.vmOptions.additionalVmOptions)) { cli.addArgument(context.vmOptions.additionalVmOptions, false); } cli.addArgument("org.eclipse.equinox.launcher.Main").addArgument("-application") .addArgument("ch.ivyteam.ivy.server.exec.engine").addArgument(command.toString()); return cli; }
From source file:io.selendroid.android.impl.AbstractDevice.java
private CommandLine adbCommand() { CommandLine command = new CommandLine(AndroidSdk.adb()); if (isSerialConfigured()) { command.addArgument("-s", false); command.addArgument(serial, false); }/*from w ww . ja va2 s . co m*/ return command; }
From source file:modules.NativeProcessMonitoringDaemonModule.java
protected void startNativeMonitor(String command, String options, Path path) throws NativeExecutionException { if (command == null || command.equals("")) { System.err.println("command null at GeneralNativeCommandModule.extractNative()"); return;/*w ww. ja v a 2 s . com*/ } CommandLine commandLine = new CommandLine(command); if (options != null && !options.equals("")) { String[] args = options.split(" "); commandLine.addArguments(args); } if (path != null) { commandLine.addArgument(path.toAbsolutePath().toString(), false); } executor = new DefaultExecutor(); esh = new MyExecuteStreamHandler(); executor.setStreamHandler(esh); // GeneralExecutableModuleConfig generalExecutableModuleConfig = // getConfig(); executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT)); if (getConfig().workingDirectory != null && getConfig().workingDirectory.exists()) { executor.setWorkingDirectory(getConfig().workingDirectory); } try { // System.out.println("Now execute: " + commandLine); executor.execute(commandLine, this); } catch (ExecuteException xs) { NativeExecutionException n = new NativeExecutionException(); n.initCause(xs); if (path != null) { n.path = path.toAbsolutePath().toString(); } n.exitCode = xs.getExitValue(); throw n; } catch (IOException xs) { NativeExecutionException n = new NativeExecutionException(); n.initCause(xs); if (path != null) { n.path = path.toAbsolutePath().toString(); } throw n; } return; }
From source file:io.selendroid.standalone.builder.SelendroidServerBuilder.java
AndroidApp signTestServer(File customSelendroidServer, File outputFileName) throws ShellCommandException, AndroidSdkException { if (outputFileName == null) { throw new IllegalArgumentException("outputFileName parameter is null."); }//from w ww .ja v a 2 s. c om File androidKeyStore = androidDebugKeystore(); if (!androidKeyStore.isFile()) { // create a new keystore CommandLine commandline = new CommandLine(JavaSdk.keytool()); commandline.addArgument("-genkey", false); commandline.addArgument("-v", false); commandline.addArgument("-keystore", false); commandline.addArgument(androidKeyStore.toString(), false); commandline.addArgument("-storepass", false); commandline.addArgument(storepass, false); commandline.addArgument("-alias", false); commandline.addArgument(alias, false); commandline.addArgument("-keypass", false); commandline.addArgument(storepass, false); commandline.addArgument("-dname", false); commandline.addArgument("CN=Android Debug,O=Android,C=US", false); commandline.addArgument("-storetype", false); commandline.addArgument("JKS", false); commandline.addArgument("-sigalg", false); commandline.addArgument("MD5withRSA", false); commandline.addArgument("-keyalg", false); commandline.addArgument("RSA", false); commandline.addArgument("-validity", false); commandline.addArgument("9999", false); String output = ShellCommand.exec(commandline, 20000); log.info("A new keystore has been created: " + output); } // Sign the jar CommandLine commandline = new CommandLine(JavaSdk.jarsigner()); commandline.addArgument("-sigalg", false); commandline.addArgument(getSigAlg(), false); commandline.addArgument("-digestalg", false); commandline.addArgument("SHA1", false); commandline.addArgument("-signedjar", false); commandline.addArgument(outputFileName.getAbsolutePath(), false); commandline.addArgument("-storepass", false); commandline.addArgument(storepass, false); commandline.addArgument("-keystore", false); commandline.addArgument(androidKeyStore.toString(), false); commandline.addArgument(customSelendroidServer.getAbsolutePath(), false); commandline.addArgument(alias, false); String output = ShellCommand.exec(commandline, 20000); if (log.isLoggable(Level.INFO)) { log.info("App signing output: " + output); } log.info("The app has been signed: " + outputFileName.getAbsolutePath()); return new DefaultAndroidApp(outputFileName); }
From source file:io.selendroid.standalone.android.impl.DefaultAndroidEmulator.java
private void waitForLauncherToComplete() throws AndroidDeviceException { CommandLine processListCommand = getAdbCommand(); processListCommand.addArgument("shell", false); processListCommand.addArgument("ps", false); String processList = null;/* ww w . j ava2s .c o m*/ do { try { processList = ShellCommand.exec(processListCommand, 20000); } catch (ShellCommandException e) { throw new AndroidDeviceException(e); } //Wait a bit try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } while (processList == null || !processList.contains("S com.android.launcher")); }
From source file:io.selendroid.standalone.android.impl.DefaultAndroidEmulator.java
public void unlockScreen() throws AndroidDeviceException { // Send menu key event CommandLine menuKeyCommand = getAdbCommand(); menuKeyCommand.addArgument("shell", false); menuKeyCommand.addArgument("input", false); menuKeyCommand.addArgument("keyevent", false); menuKeyCommand.addArgument("82", false); try {//from w ww .ja v a 2 s .c o m ShellCommand.exec(menuKeyCommand, 20000); } catch (ShellCommandException e) { throw new AndroidDeviceException(e); } // Send back key event CommandLine backKeyCommand = getAdbCommand(); backKeyCommand.addArgument("shell", false); backKeyCommand.addArgument("input", false); backKeyCommand.addArgument("keyevent", false); backKeyCommand.addArgument("4", false); try { ShellCommand.exec(backKeyCommand, 20000); } catch (ShellCommandException e) { throw new AndroidDeviceException(e); } }
From source file:io.selendroid.standalone.android.impl.DefaultAndroidEmulator.java
private void allAppsGridView() throws AndroidDeviceException { int x = screenSize.width; int y = screenSize.height; if (x > y) { y = y / 2;//from w w w. jav a 2s. c om x = x - 30; } else { x = x / 2; y = y - 30; } List<String> coordinates = new ArrayList<String>(); coordinates.add("3 0 " + x); coordinates.add("3 1 " + y); coordinates.add("1 330 1"); coordinates.add("0 0 0"); coordinates.add("1 330 0"); coordinates.add("0 0 0"); for (String coordinate : coordinates) { CommandLine event1 = getAdbCommand(); event1.addArgument("shell", false); event1.addArgument("sendevent", false); event1.addArgument("dev/input/event0", false); event1.addArgument(coordinate, false); try { ShellCommand.exec(event1); } catch (ShellCommandException e) { throw new AndroidDeviceException(e); } } try { Thread.sleep(750); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
From source file:io.selendroid.android.impl.AbstractDevice.java
public void runAdbCommand(String parameter) { if (parameter == null || parameter.isEmpty() == true) { return;// w w w .ja va 2 s . c o m } CommandLine command = adbCommand(); String[] params = parameter.split(" "); for (int i = 0; i < params.length; i++) { command.addArgument(params[i], false); } executeCommand(command, 20000); }
From source file:com.oneops.inductor.ProcessRunner.java
/** * Creates a process and logs the output * * @param cmd//ww w . java 2s. c o m * @param logKey * @param result */ private void executeProcess(String[] cmd, String logKey, ProcessResult result) { Map<String, String> env = getEnvVars(logKey, cmd); logger.info(logKey + " Cmd: " + String.join(" ", cmd) + ", Env: " + env); // run the cmd try { CommandLine cmdLine = new CommandLine(cmd[0]); // add rest of cmd string[] as arguments for (int i = 1; i < cmd.length; i++) { // needs the quote handling=false or else doesn't work // http://www.techques.com/question/1-5080109/How-to-execute--bin-sh-with-commons-exec? cmdLine.addArgument(cmd[i], false); } DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setWatchdog(new ExecuteWatchdog(timeoutInSeconds * 1000)); executor.setStreamHandler(new OutputHandler(logger, logKey, result)); result.setResultCode(executor.execute(cmdLine, env)); // set fault to last error if fault map is empty if (result.getResultCode() != 0 && result.getFaultMap().keySet().size() < 1) { result.getFaultMap().put("ERROR", result.getLastError()); } } catch (ExecuteException ee) { logger.error(logKey + ee); result.setResultCode(ee.getExitValue()); } catch (IOException e) { logger.error(e); result.setResultCode(1); } }