Example usage for org.apache.commons.exec OS isFamilyWindows

List of usage examples for org.apache.commons.exec OS isFamilyWindows

Introduction

In this page you can find the example usage for org.apache.commons.exec OS isFamilyWindows.

Prototype

public static boolean isFamilyWindows() 

Source Link

Usage

From source file:com.tibco.tgdb.test.utils.ProcessCheck.java

/**
 * Check whether a process is running or not
 * @param pid pid of the process to monitor
 * @return true if process is running/*  w w  w  . j  a va2 s . c  o  m*/
 * @throws IOException IO exception
 */
public static boolean isProcessRunning(int pid) throws IOException {
    String line;
    if (OS.isFamilyWindows()) {
        //tasklist exit code is always 0. Parse output
        //findstr exit code 0 if found pid, 1 if it doesn't
        line = "cmd /c \"tasklist /FI \"PID eq " + pid + "\" | findstr " + pid + "\"";
    } else {
        //ps exit code 0 if process exists, 1 if it doesn't
        line = "ps -p " + pid;
    }
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(null, null, null));
    executor.setExitValues(new int[] { 0, 1 });
    int exitValue = executor.execute(cmdLine);
    if (exitValue == 0)
        return true;
    else if (exitValue == 1)
        return false;
    else // should never get to here in theory since execute would throw exception
        return true;
}

From source file:com.github.cshubhamrao.MediaConverter.Library.OSUtils.java

/**
 * This method returns a member of {@code OperatingSystem} representing the
 * current Operating System./*  www .j a  v  a 2 s  .co m*/
 *
 * @since 1.0.2
 * @return Current Operating System
 */
public static OperatingSystem getCurrentOS() {
    OperatingSystem currentOS = OperatingSystem.UNKNOWN;
    if (OS.isFamilyUnix()) {
        currentOS = OperatingSystem.LINUX;
    }
    if (OS.isFamilyMac()) {
        currentOS = OperatingSystem.MAC;
    }
    if (OS.isFamilyWindows()) {
        currentOS = OperatingSystem.WINDOWS;
    }
    return currentOS;
}

From source file:de.pawlidi.openaletheia.utils.AletheiaUtilsTest.java

@Test
public void testGetLinuxMacAddress() {
    String macAdress = AletheiaUtils.getLinuxMacAddress();
    if (OS.isFamilyWindows()) {
        assertTrue(true);//from  ww w .  ja v a2  s .  c  o  m
    } else {
        assertTrue(!macAdress.isEmpty());
    }
}

From source file:com.jaeksoft.searchlib.index.IndexDirectory.java

protected IndexDirectory(File indexDir) throws IOException {
    if (OS.isFamilyWindows())
        directory = FSDirectory.open(indexDir);
    else//from   w w w.j  a  va  2  s  .  c  om
        directory = NIOFSDirectory.open(indexDir);
}

From source file:com.github.genium_framework.appium.support.server.AppiumServer.java

/**
 * Constructs an Appium server instance. Searches automatically for an
 * installed Appium server on your machine using the default installation
 * location according to your operating system.
 *
 * The searched directories are: <br><ul><li>Windows OS: "C:/Program
 * Files/Appium" &amp; "C:/Program Files (x86)/Appium"</li> <li>Mac OS:
 * "/Applications/Appium.app/Contents/Resources" </li></ul>zz
 *
 * @param serverArguments The server arguments to be used when working with
 * the server./* w  w  w .java2  s.c  o  m*/
 */
public AppiumServer(ServerArguments serverArguments) {
    this._serverArguments = serverArguments;

    // search for installed Appium server
    _absoluteServerDirectory = searchForServerDirectory();

    // make sure to get the node executable file path along with the appium.js path too.
    _nodeExecutableFilePath = new File(OS.isFamilyWindows() ? _absoluteServerDirectory + "/node.exe"
            : _absoluteServerDirectory + "/node/bin/node");
    _appiumJavaScriptFilePath = new File(_absoluteServerDirectory + "/node_modules/appium/bin/appium.js");
}

From source file:com.github.genium_framework.appium.support.command.CommandManager.java

/**
 * Constructs a CommandLine object depending on the current running
 * operating system using the number of arguments passed to it.
 *
 * @param command The native OS command to run or an absolute path to an
 * executable to run./*from w w w  .  jav a2  s. c  o  m*/
 * @param parameters The command parameters.
 * @param arguments String arguments of the command to formulate from.
 * @return CommandLine object that represents the command you want to
 * execute.
 */
public static CommandLine createCommandLine(String command, String[] parameters, String... arguments) {
    CommandLine commanddLine = null;

    // add the command to be executed
    if (OS.isFamilyWindows()) {
        commanddLine = new CommandLine("\"" + command + "\"");
    } else if (OS.isFamilyMac() || OS.isFamilyUnix()) {
        commanddLine = new CommandLine(command.contains(" ") ? "'" + command + "'" : command);
    } else {
        throw new UnsupportedOperationException("Unsupported operating system.");
    }

    // add the command parameters
    if (OS.isFamilyWindows()) {
        for (String parameter : parameters) {
            commanddLine.addArgument("\"" + parameter + "\"", false);
        }
    } else if (OS.isFamilyMac() || OS.isFamilyUnix()) {
        for (String parameter : parameters) {
            commanddLine.addArgument(parameter.contains(" ") ? "'" + parameter + "'" : parameter, false);
        }
    }

    // add the command arguments
    for (String argument : arguments) {
        // you have to pass the false value and disable handling quoting
        // otherwise the OS won't be able to run the shell file on MAc OS
        commanddLine.addArgument(argument, false);
    }

    return commanddLine;
}

From source file:com.github.genium_framework.appium.support.server.AppiumServer.java

/**
 * Constructs an Appium server instance. You specify the custom directory to
 * your Appium server./*from   w  w w .  j av  a 2  s.com*/
 *
 * @param absoluteServerDirectory The custom directory to your Appium
 * server. The directory that contains the "node_modules" directory &amp;
 * the NodeJS executable.
 * @param serverArguments The server arguments to be used when working with
 * the server.
 */
public AppiumServer(File absoluteServerDirectory, ServerArguments serverArguments) {
    this._absoluteServerDirectory = absoluteServerDirectory;
    this._serverArguments = serverArguments;

    // make sure to get the node executable file path along with the appium.js path too.
    _nodeExecutableFilePath = new File(OS.isFamilyWindows() ? _absoluteServerDirectory + "/node.exe"
            : _absoluteServerDirectory + "/node/bin/node");
    _appiumJavaScriptFilePath = new File(_absoluteServerDirectory + "/node_modules/appium/bin/appium.js");
}

From source file:com.github.genium_framework.appium.support.server.AppiumServer.java

/**
 * Search the operating system for an Appium server installation directory.
 *
 * @return A File representation to the Appium server installation
 * directory./*w ww  .java  2  s  .  c  o  m*/
 */
private File searchForServerDirectory() {
    if (OS.isFamilyWindows()) {
        if (getArch().equals("32")) {
            return doesDirectoryExists(System.getenv("ProgramFiles") + "/Appium");
        } else {
            // must be the x86_64
            return doesDirectoryExists(System.getenv("ProgramFiles") + " (x86)/Appium");
        }
    } else if (OS.isFamilyMac()) {
        return doesDirectoryExists("/Applications/Appium.app/Contents/Resources");
    }

    // server directrory was not found.
    throw new ServerDirectoryNotFoundException();
}

From source file:com.github.genium_framework.appium.support.server.AppiumServer.java

/**
 * Stops an already running Appium server.
 *//*  w  w w.  ja v a2  s.  c om*/
@Override
public void stopServer() {
    String[] stopServerCommand = null;

    try {
        if (OS.isFamilyWindows()) {
            stopServerCommand = new String[] { "cmd", "/c",
                    "echo off & FOR /F \"usebackq tokens=5\" %a in (`netstat -nao ^| findstr /R /C:\""
                            + _serverArguments.get(AppiumCommonArgs.PORT_NUMBER)
                            + " \"`) do (FOR /F \"usebackq\" %b in (`TASKLIST /FI \"PID eq %a\" ^| findstr /I node.exe`) do taskkill /F /PID %a)" };
        } else if (OS.isFamilyMac()) {
            // Using command substitution
            stopServerCommand = new String[] { "/bin/sh", "-c",
                    "PID=\"$(lsof -i -P | pgrep -f node)\";kill -9 $PID" };
        } else if (OS.isFamilyUnix()) {
            // Using command substitution
            stopServerCommand = new String[] { "/bin/env",
                    "PID=\"$(lsof -i -P | pgrep -f node)\";kill -9 $PID" };
        } else {
            throw new UnsupportedOperationException("Not supported yet for this operating system...");
        }

        LOGGER.log(Level.INFO, "Stopping the server with the command: {0}",
                CommandManager.convertCommandStringArrayToString(stopServerCommand));
        String stopServerOutput = CommandManager.executeCommandUsingJavaRuntime(stopServerCommand, true);
        LOGGER.log(Level.INFO, "Server stopping output: {0}", stopServerOutput);
    } catch (UnsupportedOperationException ex) {
        throw ex;
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "An exception was thrown.", ex);
    }
}

From source file:net.hasor.maven.ExecMojoTest.java

public void testGetExecutablePath() throws IOException {

    ExecMojo realMojo = new ExecMojo();

    File workdir = new File(System.getProperty("user.dir"));
    Map enviro = new HashMap();

    String myJavaPath = "target" + File.separator + "javax";
    File f = new File(myJavaPath);
    assertTrue("file created...", f.createNewFile());
    assertTrue("file exists...", f.exists());

    realMojo.setExecutable(myJavaPath);/*from  ww  w.  j a va2 s.  c  om*/

    CommandLine cmd = realMojo.getExecutablePath(enviro, workdir);
    assertTrue("File exists so path is absolute",
            cmd.getExecutable().startsWith(System.getProperty("user.dir")));

    f.delete();
    assertFalse("file deleted...", f.exists());
    cmd = realMojo.getExecutablePath(enviro, workdir);
    assertEquals("File doesn't exist. Let the system find it (in that PATH?)", myJavaPath, cmd.getExecutable());

    if (OS.isFamilyWindows()) //how to make this part of the test run on other platforms as well??
    {

        myJavaPath = "target" + File.separator + "javax.bat";
        f = new File(myJavaPath);
        assertTrue("file created...", f.createNewFile());
        assertTrue("file exists...", f.exists());

        realMojo.setExecutable("javax.bat");
        cmd = realMojo.getExecutablePath(enviro, workdir);
        assertTrue("is bat file on windows, execute using cmd.", cmd.getExecutable().equals("cmd"));

        enviro.put("PATH", workdir.getAbsolutePath() + File.separator + "target");
        cmd = realMojo.getExecutablePath(enviro, workdir);
        assertTrue("is bat file on windows' PATH, execute using cmd.", cmd.getExecutable().equals("cmd"));
        f.delete();
        assertFalse("file deleted...", f.exists());
    }
}