Example usage for java.lang ProcessBuilder environment

List of usage examples for java.lang ProcessBuilder environment

Introduction

In this page you can find the example usage for java.lang ProcessBuilder environment.

Prototype

Map environment

To view the source code for java.lang ProcessBuilder environment.

Click Source Link

Usage

From source file:org.kyasuda.docwaza.office.OfficeProcess.java

private void addBasisAndUrePaths(ProcessBuilder processBuilder) throws IOException {
    File ureBin = null;//from ww w  .j  a va  2  s .c om
    File basisProgram = null;
    // see
    // http://wiki.services.openoffice.org/wiki/ODF_Toolkit/Efforts/Three-Layer_OOo
    File basisLink = new File(officeHome, "basis-link");
    if (!basisLink.isFile()) {
        // check the case with LibreOffice 3.5+ home
        File ureLink = new File(officeHome, "ure-link");
        if (!ureLink.isFile()) {
            logger.fine(
                    "no %OFFICE_HOME%/basis-link found; assuming it's OOo 2.x and we don't need to append URE and Basic paths");
            return;
        }
        ureBin = new File(new File(officeHome, FileUtils.readFileToString(ureLink).trim()), "bin");
    } else {
        String basisLinkText = FileUtils.readFileToString(basisLink).trim();
        File basisHome = new File(officeHome, basisLinkText);
        basisProgram = new File(basisHome, "program");
        File ureLink = new File(basisHome, "ure-link");
        String ureLinkText = FileUtils.readFileToString(ureLink).trim();
        File ureHome = new File(basisHome, ureLinkText);
        ureBin = new File(ureHome, "bin");
    }
    Map<String, String> environment = processBuilder.environment();
    // Windows environment variables are case insensitive but Java maps are
    // not :-/
    // so let's make sure we modify the existing key
    String pathKey = "PATH";
    for (String key : environment.keySet()) {
        if ("PATH".equalsIgnoreCase(key)) {
            pathKey = key;
        }
    }
    String path = environment.get(pathKey) + ";" + ureBin.getAbsolutePath();
    if (basisProgram != null) {
        path += ";" + basisProgram.getAbsolutePath();
    }
    logger.fine(String.format("setting %s to \"%s\"", pathKey, path));
    environment.put(pathKey, path);
}

From source file:net.rim.ejde.internal.packaging.PackagingManager.java

private void runRapcCommand() throws CoreException {
    try {/*from  www . j  a  va 2s  . com*/
        File workDir = _bbProject.getProject().getLocation().toFile();
        if (writeToFile) {
            File outputFile = null;
            String outputFileName = _bbProject.getProject().getName() + ".files";
            outputFile = new File(workDir, outputFileName);
            _rapcCommandsHead.add("@" + outputFileName);
            flushToFile(outputFile);
        } else {
            _rapcCommandsHead.addAll(_rapcCommands);
        }
        String command = getStringCommand(_rapcCommandsHead);
        _log.trace("Execute rapc command: " + command + "; Working Directory: " + workDir.getPath());
        ProcessBuilder rapcBuilder = new ProcessBuilder(_rapcCommandsHead);

        String javaHome = System.getenv("JAVA_HOME");
        if (javaHome != null) {
            Map<String, String> env = rapcBuilder.environment();
            String pathName = "Path";
            for (String s : env.keySet()) {
                if (s.equalsIgnoreCase("Path"))
                    pathName = s;
            }
            String path = env.get(pathName);
            path = path == null ? javaHome : (path + File.pathSeparator + javaHome);
            path = path + File.pathSeparator + javaHome + File.separator + "bin";
            env.put(pathName, path);
            _log.trace("PATH=" + path);
        }

        rapcBuilder.directory(workDir);
        rapcBuilder.redirectErrorStream(true);
        long startTime = System.currentTimeMillis();
        _consoleOutputStream.println(
                NLS.bind(Messages.PackagingManager_PACKAGING_PROJECT_MSG, _bbProject.getProject().getName()));
        _consoleOutputStream.println(command);
        Process process = rapcBuilder.start();
        InputStream inStream = process.getInputStream();
        InputStreamHandler inputHandler = new InputStreamHandler(_bbProject.getProject(), _consoleOutputStream,
                inStream);
        inputHandler.start();
        int result = process.waitFor();
        inputHandler.join();
        float spendTime = ((float) (System.currentTimeMillis() - startTime)) / 1000;
        if (result == 0) {

            _consoleOutputStream.println(NLS.bind(Messages.PackagingManager_PACKAGING_SUCCEED_MSG,
                    new String[] { _bbProject.getProject().getName(), String.valueOf(spendTime) }));
        } else {
            _consoleOutputStream.println(NLS.bind(Messages.PackagingManager_PACKAGING_FAILED_MSG,
                    new String[] { _bbProject.getProject().getName(), String.valueOf(spendTime) }));
        }
    } catch (IOException e) {
        throw new CoreException(StatusFactory.createErrorStatus(e.getMessage()));
    } catch (InterruptedException e) {
        throw new CoreException(StatusFactory.createErrorStatus(e.getMessage()));
    }
}

From source file:org.cloudifysource.shell.installer.LocalhostGridAgentBootstrapper.java

private void runCommand(final String[] command, final String[] args, final String securityProfile,
        final String securityFilePath, final String keystoreFilePath, final String keystorePassword)
        throws CLIException, InterruptedException {

    final File directory = new File(Environment.getHomeDirectory(), "/bin").getAbsoluteFile();

    // gs-agent.sh/bat need full path
    command[command.length - 1] = new File(directory, command[command.length - 1]).getAbsolutePath();

    final List<String> commandLine = new ArrayList<String>();
    commandLine.addAll(Arrays.asList(command));
    commandLine.addAll(Arrays.asList(args));

    final String commandString = StringUtils.collectionToDelimitedString(commandLine, " ");
    final File filename = createScript(commandString);
    final ProcessBuilder pb = new ProcessBuilder().command(filename.getAbsolutePath()).directory(directory);

    String localCloudOptions = "-Xmx" + CloudifyConstants.DEFAULT_LOCALCLOUD_GSA_GSM_ESM_LUS_MEMORY_IN_MB + "m"
            + " -D" + CloudifyConstants.LUS_PORT_CONTEXT_PROPERTY + "=" + lusPort + " -D"
            + GSM_EXCLUDE_GSC_ON_FAILED_INSTANCE + "=" + GSM_EXCLUDE_GSC_ON_FAILED_INSTACE_BOOL + " "
            + GSM_PENDING_REQUESTS_DELAY + " -D" + ZONES_PROPERTY + "=" + LOCALCLOUD_GSA_ZONES + " -D"
            + CloudifyConstants.SYSTEM_PROPERTY_ESM_DISCOVERY_POLLING_INTERVAL_SECONDS + "=1";

    final Map<String, String> environment = pb.environment();
    if (lookupGroups != null) {
        environment.put("LOOKUPGROUPS", lookupGroups);
    }/*from  ww  w  .  j  a v a  2  s .  c o  m*/

    if (lookupLocators != null) {
        final String disableMulticast = "-Dcom.gs.multicast.enabled=false";
        environment.put("LOOKUPLOCATORS", lookupLocators);
        localCloudOptions += " " + disableMulticast;
    }

    if (isLocalCloud) {
        logger.fine("Setting env vars COMPONENT_JAVA_OPTIONS: " + localCloudOptions);
        environment.put("COMPONENT_JAVA_OPTIONS", localCloudOptions);
        environment.put(CloudifyConstants.GIGASPACES_CLOUD_HARDWARE_ID, "localcloud");
        environment.put(CloudifyConstants.GIGASPACES_CLOUD_IMAGE_ID, "localcloud");
        environment.put(CloudifyConstants.GIGASPACES_CLOUD_TEMPLATE_NAME, "localcloud");
        environment.put(CloudifyConstants.GIGASPACES_CLOUD_MACHINE_ID, "localcloud");
        final String springProfiles = createSpringProfilesList(securityProfile);
        environment.put(CloudifyConstants.SPRING_ACTIVE_PROFILE_ENV_VAR, springProfiles);
        if (ShellUtils.isSecureConnection(securityProfile)) {
            environment.put(CloudifyConstants.KEYSTORE_FILE_ENV_VAR, keystoreFilePath);
            environment.put(CloudifyConstants.KEYSTORE_PASSWORD_ENV_VAR, keystorePassword);
        }
        environment.put(CloudifyConstants.SPRING_SECURITY_CONFIG_FILE_ENV_VAR, securityFilePath);
        if (nicAddress != null) {
            environment.put(CloudifyConstants.GIGASPACES_AGENT_ENV_PRIVATE_IP, nicAddress);
            environment.put(CloudifyConstants.GIGASPACES_AGENT_ENV_PUBLIC_IP, nicAddress);

            environment.put(CloudifyConstants.CLOUDIFY_AGENT_ENV_PRIVATE_IP, nicAddress);
            environment.put(CloudifyConstants.CLOUDIFY_AGENT_ENV_PUBLIC_IP, nicAddress);
            environment.put("NIC_ADDR", nicAddress);

        }
    }

    // start process
    // there is no need to redirect output, since the process suppresses
    // output
    try {
        logger.fine("Executing command: " + commandString);
        final Process proc = pb.start();
        Thread.sleep(MIN_PROC_ERROR_TIME);
        try {
            // The assumption is that if the script contains errors,
            // the processBuilder will finish by the end of the above sleep
            // period.
            if (proc.exitValue() != 0) {
                String errorMessage = "Error while starting agent. "
                        + "Please make sure that another agent is not already running. ";
                if (verbose) {
                    errorMessage = errorMessage.concat("Command executed: " + commandString);
                }
                throw new CLIException(errorMessage);
            }
            // ProcessBuilder is still running. We assume the agent script
            // is running fine.
        } catch (final IllegalThreadStateException e) {
            logger.fine("agent is starting...");
        }
    } catch (final IOException e) {
        throw new CLIException("Error while starting agent", e);
    }
}

From source file:edu.uci.ics.pregelix.example.util.TestExecutor.java

public void executeTest(String actualPath, TestCaseContext testCaseCtx, ProcessBuilder pb,
        boolean isDmlRecoveryTest, TestGroup failedGroup) throws Exception {

    File testFile;/*from  w w  w  . j a  v a  2  s . c  o  m*/
    File expectedResultFile;
    String statement;
    List<TestFileContext> expectedResultFileCtxs;
    List<TestFileContext> testFileCtxs;
    File qbcFile = null;
    File qarFile = null;
    int queryCount = 0;

    List<CompilationUnit> cUnits = testCaseCtx.getTestCase().getCompilationUnit();
    for (CompilationUnit cUnit : cUnits) {
        LOGGER.info("Starting [TEST]: " + testCaseCtx.getTestCase().getFilePath() + "/" + cUnit.getName()
                + " ... ");
        testFileCtxs = testCaseCtx.getTestFiles(cUnit);
        expectedResultFileCtxs = testCaseCtx.getExpectedResultFiles(cUnit);
        for (TestFileContext ctx : testFileCtxs) {
            testFile = ctx.getFile();
            statement = readTestFile(testFile);
            boolean failed = false;
            try {
                switch (ctx.getType()) {
                case "ddl":
                    if (ctx.getFile().getName().endsWith("aql")) {
                        executeDDL(statement, "http://localhost:19002/ddl");
                    } else {
                        executeDDL(statement, "http://localhost:19002/ddl/sqlpp");
                    }
                    break;
                case "update":
                    //isDmlRecoveryTest: set IP address
                    if (isDmlRecoveryTest && statement.contains("nc1://")) {
                        statement = statement.replaceAll("nc1://",
                                "127.0.0.1://../../../../../../asterix-app/");
                    }
                    if (ctx.getFile().getName().endsWith("aql")) {
                        executeUpdate(statement, "http://localhost:19002/update");
                    } else {
                        executeUpdate(statement, "http://localhost:19002/update/sqlpp");
                    }
                    break;
                case "query":
                case "async":
                case "asyncdefer":
                    // isDmlRecoveryTest: insert Crash and Recovery
                    if (isDmlRecoveryTest) {
                        executeScript(pb, pb.environment().get("SCRIPT_HOME") + File.separator + "dml_recovery"
                                + File.separator + "kill_cc_and_nc.sh");
                        executeScript(pb, pb.environment().get("SCRIPT_HOME") + File.separator + "dml_recovery"
                                + File.separator + "stop_and_start.sh");
                    }
                    InputStream resultStream = null;
                    OutputFormat fmt = OutputFormat.forCompilationUnit(cUnit);
                    if (ctx.getFile().getName().endsWith("aql")) {
                        if (ctx.getType().equalsIgnoreCase("query")) {
                            resultStream = executeQuery(statement, fmt, "http://localhost:19002/query",
                                    cUnit.getParameter());
                        } else if (ctx.getType().equalsIgnoreCase("async")) {
                            resultStream = executeAnyAQLAsync(statement, false, fmt,
                                    "http://localhost:19002/aql");
                        } else if (ctx.getType().equalsIgnoreCase("asyncdefer")) {
                            resultStream = executeAnyAQLAsync(statement, true, fmt,
                                    "http://localhost:19002/aql");
                        }
                    } else {
                        if (ctx.getType().equalsIgnoreCase("query")) {
                            resultStream = executeQuery(statement, fmt, "http://localhost:19002/query/sqlpp",
                                    cUnit.getParameter());
                        } else if (ctx.getType().equalsIgnoreCase("async")) {
                            resultStream = executeAnyAQLAsync(statement, false, fmt,
                                    "http://localhost:19002/sqlpp");
                        } else if (ctx.getType().equalsIgnoreCase("asyncdefer")) {
                            resultStream = executeAnyAQLAsync(statement, true, fmt,
                                    "http://localhost:19002/sqlpp");
                        }
                    }

                    if (queryCount >= expectedResultFileCtxs.size()) {
                        throw new IllegalStateException(
                                "no result file for " + testFile.toString() + "; queryCount: " + queryCount
                                        + ", filectxs.size: " + expectedResultFileCtxs.size());
                    }
                    expectedResultFile = expectedResultFileCtxs.get(queryCount).getFile();

                    File actualResultFile = testCaseCtx.getActualResultFile(cUnit, new File(actualPath));
                    actualResultFile.getParentFile().mkdirs();
                    writeOutputToFile(actualResultFile, resultStream);

                    runScriptAndCompareWithResult(testFile, new PrintWriter(System.err), expectedResultFile,
                            actualResultFile);
                    LOGGER.info("[TEST]: " + testCaseCtx.getTestCase().getFilePath() + "/" + cUnit.getName()
                            + " PASSED ");

                    queryCount++;
                    break;
                case "mgx":
                    executeManagixCommand(statement);
                    break;
                case "txnqbc": //qbc represents query before crash
                    resultStream = executeQuery(statement, OutputFormat.forCompilationUnit(cUnit),
                            "http://localhost:19002/query", cUnit.getParameter());
                    qbcFile = new File(actualPath + File.separator
                            + testCaseCtx.getTestCase().getFilePath().replace(File.separator, "_") + "_"
                            + cUnit.getName() + "_qbc.adm");
                    qbcFile.getParentFile().mkdirs();
                    writeOutputToFile(qbcFile, resultStream);
                    break;
                case "txnqar": //qar represents query after recovery
                    resultStream = executeQuery(statement, OutputFormat.forCompilationUnit(cUnit),
                            "http://localhost:19002/query", cUnit.getParameter());
                    qarFile = new File(actualPath + File.separator
                            + testCaseCtx.getTestCase().getFilePath().replace(File.separator, "_") + "_"
                            + cUnit.getName() + "_qar.adm");
                    qarFile.getParentFile().mkdirs();
                    writeOutputToFile(qarFile, resultStream);
                    runScriptAndCompareWithResult(testFile, new PrintWriter(System.err), qbcFile, qarFile);

                    LOGGER.info("[TEST]: " + testCaseCtx.getTestCase().getFilePath() + "/" + cUnit.getName()
                            + " PASSED ");
                    break;
                case "txneu": //eu represents erroneous update
                    try {
                        executeUpdate(statement, "http://localhost:19002/update");
                    } catch (Exception e) {
                        //An exception is expected.
                        failed = true;
                        e.printStackTrace();
                    }
                    if (!failed) {
                        throw new Exception(
                                "Test \"" + testFile + "\" FAILED!\n  An exception" + "is expected.");
                    }
                    System.err.println("...but that was expected.");
                    break;
                case "script":
                    try {
                        String output = executeScript(pb, getScriptPath(testFile.getAbsolutePath(),
                                pb.environment().get("SCRIPT_HOME"), statement.trim()));
                        if (output.contains("ERROR")) {
                            throw new Exception(output);
                        }
                    } catch (Exception e) {
                        throw new Exception("Test \"" + testFile + "\" FAILED!\n", e);
                    }
                    break;
                case "sleep":
                    Thread.sleep(Long.parseLong(statement.trim()));
                    break;
                case "errddl": // a ddlquery that expects error
                    try {
                        executeDDL(statement, "http://localhost:19002/ddl");
                    } catch (Exception e) {
                        // expected error happens
                        failed = true;
                        e.printStackTrace();
                    }
                    if (!failed) {
                        throw new Exception(
                                "Test \"" + testFile + "\" FAILED!\n  An exception" + "is expected.");
                    }
                    System.err.println("...but that was expected.");
                    break;
                default:
                    throw new IllegalArgumentException("No statements of type " + ctx.getType());
                }

            } catch (Exception e) {

                System.err.println("testFile " + testFile.toString() + " raised an exception:");

                e.printStackTrace();
                if (cUnit.getExpectedError().isEmpty()) {
                    System.err.println("...Unexpected!");
                    if (failedGroup != null) {
                        failedGroup.getTestCase().add(testCaseCtx.getTestCase());
                    }
                    throw new Exception("Test \"" + testFile + "\" FAILED!", e);
                } else {
                    LOGGER.info("[TEST]: " + testCaseCtx.getTestCase().getFilePath() + "/" + cUnit.getName()
                            + " failed as expected: " + e.getMessage());
                    System.err.println("...but that was expected.");
                }

            }
        }
    }
}

From source file:io.snappydata.hydra.cluster.SnappyTest.java

protected void executeSnappyStreamingJobUsingJobScript(Vector jobClassNames, String logFileName) {
    String snappyJobScript = getScriptLocation("snappy-job.sh");
    ProcessBuilder pb = null;
    File log = null;//from   w w w  .jav  a  2  s  . c  o  m
    File logFile = null;
    userAppJar = SnappyPrms.getUserAppJar();
    snappyTest.verifyDataForJobExecution(jobClassNames, userAppJar);
    leadHost = getLeadHost();
    String leadPort = (String) SnappyBB.getBB().getSharedMap().get("primaryLeadPort");
    try {
        for (int i = 0; i < jobClassNames.size(); i++) {
            String userJob = (String) jobClassNames.elementAt(i);
            pb = new ProcessBuilder(snappyJobScript, "submit", "--lead", leadHost + ":" + leadPort,
                    "--app-name", "myapp", "--class", userJob, "--app-jar",
                    snappyTest.getUserAppJarLocation(userAppJar, jarPath), "--stream");
            java.util.Map<String, String> env = pb.environment();
            if (SnappyPrms.getCommaSepAPPProps() == null) {
                env.put("APP_PROPS", "shufflePartitions=" + SnappyPrms.getShufflePartitions());
            } else {
                env.put("APP_PROPS", SnappyPrms.getCommaSepAPPProps() + ",shufflePartitions="
                        + SnappyPrms.getShufflePartitions());
            }
            log = new File(".");
            String dest = log.getCanonicalPath() + File.separator + logFileName;
            logFile = new File(dest);
            snappyTest.executeProcess(pb, logFile);
        }
        snappyTest.getSnappyJobsStatus(snappyJobScript, logFile, leadPort);
    } catch (IOException e) {
        throw new TestException("IOException occurred while retriving destination logFile path " + log
                + "\nError Message:" + e.getMessage());
    }
}

From source file:org.ramadda.repository.Repository.java

/**
 * Excecute a command//w  ww . j a  v  a2 s . co  m
 *
 * @param commands     command parameters
 * @param envVars      enviroment variables
 * @param workingDir   the working directory
 *
 * @return the input and output streams
 *
 * @throws Exception  problem with execution
 */
public String[] executeCommand(List<String> commands, Map<String, String> envVars, File workingDir)
        throws Exception {
    ProcessBuilder pb = new ProcessBuilder(commands);
    if (envVars != null) {
        Map<String, String> env = pb.environment();
        //env.clear();
        env.putAll(envVars);
    }
    pb.directory(workingDir);
    Process process = pb.start();
    String errorMsg = new String(IOUtil.readBytes(process.getErrorStream()));
    String outMsg = new String(IOUtil.readBytes(process.getInputStream()));
    int result = process.waitFor();

    return new String[] { outMsg, errorMsg };
}

From source file:org.apache.asterix.test.aql.TestExecutor.java

public void executeTest(TestCaseContext testCaseCtx, TestFileContext ctx, String statement,
        boolean isDmlRecoveryTest, ProcessBuilder pb, CompilationUnit cUnit, MutableInt queryCount,
        List<TestFileContext> expectedResultFileCtxs, File testFile, String actualPath) throws Exception {
    File qbcFile;/*w w  w . ja  v  a  2s . c  o  m*/
    boolean failed = false;
    File expectedResultFile;
    switch (ctx.getType()) {
    case "ddl":
        if (ctx.getFile().getName().endsWith("aql")) {
            executeDDL(statement, getEndpoint(Servlets.AQL_DDL));
        } else {
            InputStream resultStream = executeQueryService(statement, getEndpoint(Servlets.QUERY_SERVICE));
            ResultExtractor.extract(resultStream);
        }
        break;
    case "update":
        // isDmlRecoveryTest: set IP address
        if (isDmlRecoveryTest && statement.contains("nc1://")) {
            statement = statement.replaceAll("nc1://", "127.0.0.1://../../../../../../asterix-app/");
        }
        if (ctx.getFile().getName().endsWith("aql")) {
            executeUpdate(statement, getEndpoint(Servlets.AQL_UPDATE));
        } else {
            InputStream resultStream = executeQueryService(statement, getEndpoint(Servlets.QUERY_SERVICE));
            ResultExtractor.extract(resultStream);
        }
        break;
    case "pollquery":
        // polltimeoutsecs=nnn, polldelaysecs=nnn
        final Matcher timeoutMatcher = POLL_TIMEOUT_PATTERN.matcher(statement);
        int timeoutSecs;
        if (timeoutMatcher.find()) {
            timeoutSecs = Integer.parseInt(timeoutMatcher.group(1));
        } else {
            throw new IllegalArgumentException("ERROR: polltimeoutsecs=nnn must be present in poll file");
        }
        final Matcher retryDelayMatcher = POLL_DELAY_PATTERN.matcher(statement);
        int retryDelaySecs = retryDelayMatcher.find() ? Integer.parseInt(timeoutMatcher.group(1)) : 1;
        long startTime = System.currentTimeMillis();
        long limitTime = startTime + TimeUnit.SECONDS.toMillis(timeoutSecs);
        ctx.setType(ctx.getType().substring("poll".length()));
        Exception finalException;
        LOGGER.fine("polling for up to " + timeoutSecs + " seconds w/ " + retryDelaySecs + " second(s) delay");
        while (true) {
            try {
                executeTest(testCaseCtx, ctx, statement, isDmlRecoveryTest, pb, cUnit, queryCount,
                        expectedResultFileCtxs, testFile, actualPath);
                finalException = null;
                break;
            } catch (Exception e) {
                if ((System.currentTimeMillis() > limitTime)) {
                    finalException = e;
                    break;
                }
                LOGGER.fine("sleeping " + retryDelaySecs + " second(s) before polling again");
                Thread.sleep(TimeUnit.SECONDS.toMillis(retryDelaySecs));
            }
        }
        if (finalException != null) {
            throw new Exception("Poll limit (" + timeoutSecs + "s) exceeded without obtaining expected result",
                    finalException);
        }
        break;
    case "query":
    case "async":
    case "asyncdefer":
        // isDmlRecoveryTest: insert Crash and Recovery
        if (isDmlRecoveryTest) {
            executeScript(pb, pb.environment().get("SCRIPT_HOME") + File.separator + "dml_recovery"
                    + File.separator + "kill_cc_and_nc.sh");
            executeScript(pb, pb.environment().get("SCRIPT_HOME") + File.separator + "dml_recovery"
                    + File.separator + "stop_and_start.sh");
        }
        InputStream resultStream = null;
        OutputFormat fmt = OutputFormat.forCompilationUnit(cUnit);
        if (ctx.getFile().getName().endsWith("aql")) {
            if (ctx.getType().equalsIgnoreCase("query")) {
                resultStream = executeQuery(statement, fmt, getEndpoint(Servlets.AQL_QUERY),
                        cUnit.getParameter());
            } else if (ctx.getType().equalsIgnoreCase("async")) {
                resultStream = executeAnyAQLAsync(statement, false, fmt, getEndpoint(Servlets.AQL));
            } else if (ctx.getType().equalsIgnoreCase("asyncdefer")) {
                resultStream = executeAnyAQLAsync(statement, true, fmt, getEndpoint(Servlets.AQL));
            }
        } else {
            if (ctx.getType().equalsIgnoreCase("query")) {
                resultStream = executeQueryService(statement, fmt, getEndpoint(Servlets.QUERY_SERVICE),
                        cUnit.getParameter(), true);
                resultStream = ResultExtractor.extract(resultStream);
            } else if (ctx.getType().equalsIgnoreCase("async")) {
                resultStream = executeAnyAQLAsync(statement, false, fmt, getEndpoint(Servlets.SQLPP));
            } else if (ctx.getType().equalsIgnoreCase("asyncdefer")) {
                resultStream = executeAnyAQLAsync(statement, true, fmt, getEndpoint(Servlets.SQLPP));
            }
        }
        if (queryCount.intValue() >= expectedResultFileCtxs.size()) {
            throw new IllegalStateException("no result file for " + testFile.toString() + "; queryCount: "
                    + queryCount + ", filectxs.size: " + expectedResultFileCtxs.size());
        }
        expectedResultFile = expectedResultFileCtxs.get(queryCount.intValue()).getFile();

        File actualResultFile = testCaseCtx.getActualResultFile(cUnit, expectedResultFile,
                new File(actualPath));
        writeOutputToFile(actualResultFile, resultStream);

        runScriptAndCompareWithResult(testFile, new PrintWriter(System.err), expectedResultFile,
                actualResultFile);
        queryCount.increment();

        // Deletes the matched result file.
        actualResultFile.getParentFile().delete();
        break;
    case "mgx":
        executeManagixCommand(statement);
        break;
    case "txnqbc": // qbc represents query before crash
        resultStream = executeQuery(statement, OutputFormat.forCompilationUnit(cUnit),
                getEndpoint(Servlets.AQL_QUERY), cUnit.getParameter());
        qbcFile = getTestCaseQueryBeforeCrashFile(actualPath, testCaseCtx, cUnit);
        writeOutputToFile(qbcFile, resultStream);
        break;
    case "txnqar": // qar represents query after recovery
        resultStream = executeQuery(statement, OutputFormat.forCompilationUnit(cUnit),
                getEndpoint(Servlets.AQL_QUERY), cUnit.getParameter());
        File qarFile = new File(actualPath + File.separator
                + testCaseCtx.getTestCase().getFilePath().replace(File.separator, "_") + "_" + cUnit.getName()
                + "_qar.adm");
        writeOutputToFile(qarFile, resultStream);
        qbcFile = getTestCaseQueryBeforeCrashFile(actualPath, testCaseCtx, cUnit);
        runScriptAndCompareWithResult(testFile, new PrintWriter(System.err), qbcFile, qarFile);
        break;
    case "txneu": // eu represents erroneous update
        try {
            executeUpdate(statement, getEndpoint(Servlets.AQL_UPDATE));
        } catch (Exception e) {
            // An exception is expected.
            failed = true;
            e.printStackTrace();
        }
        if (!failed) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n  An exception" + "is expected.");
        }
        System.err.println("...but that was expected.");
        break;
    case "script":
        try {
            String output = executeScript(pb, getScriptPath(testFile.getAbsolutePath(),
                    pb.environment().get("SCRIPT_HOME"), statement.trim()));
            if (output.contains("ERROR")) {
                throw new Exception(output);
            }
        } catch (Exception e) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n", e);
        }
        break;
    case "sleep":
        String[] lines = statement.split("\n");
        Thread.sleep(Long.parseLong(lines[lines.length - 1].trim()));
        break;
    case "errddl": // a ddlquery that expects error
        try {
            executeDDL(statement, getEndpoint(Servlets.AQL_DDL));
        } catch (Exception e) {
            // expected error happens
            failed = true;
            e.printStackTrace();
        }
        if (!failed) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n  An exception is expected.");
        }
        System.err.println("...but that was expected.");
        break;
    case "vscript": // a script that will be executed on a vagrant virtual node
        try {
            String[] command = statement.trim().split(" ");
            if (command.length != 2) {
                throw new Exception("invalid vagrant script format");
            }
            String nodeId = command[0];
            String scriptName = command[1];
            String output = executeVagrantScript(pb, nodeId, scriptName);
            if (output.contains("ERROR")) {
                throw new Exception(output);
            }
        } catch (Exception e) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n", e);
        }
        break;
    case "vmgx": // a managix command that will be executed on vagrant cc node
        String output = executeVagrantManagix(pb, statement);
        if (output.contains("ERROR")) {
            throw new Exception(output);
        }
        break;
    case "get":
    case "post":
        if (!"http".equals(ctx.extension())) {
            throw new IllegalArgumentException(
                    "Unexpected format for method " + ctx.getType() + ": " + ctx.extension());
        }
        fmt = OutputFormat.forCompilationUnit(cUnit);
        String endpoint = stripJavaComments(statement).trim();
        switch (ctx.getType()) {
        case "get":
            resultStream = executeJSONGet(fmt, "http://" + host + ":" + port + endpoint);
            break;
        case "post":
            resultStream = executeJSONPost(fmt, "http://" + host + ":" + port + endpoint);
            break;
        default:
            throw new IllegalStateException("NYI: " + ctx.getType());
        }
        expectedResultFile = expectedResultFileCtxs.get(queryCount.intValue()).getFile();
        actualResultFile = testCaseCtx.getActualResultFile(cUnit, expectedResultFile, new File(actualPath));
        writeOutputToFile(actualResultFile, resultStream);
        runScriptAndCompareWithResult(testFile, new PrintWriter(System.err), expectedResultFile,
                actualResultFile);
        queryCount.increment();
        break;
    case "server": // (start <test server name> <port>
                   // [<arg1>][<arg2>][<arg3>]...|stop (<port>|all))
        try {
            lines = statement.trim().split("\n");
            String[] command = lines[lines.length - 1].trim().split(" ");
            if (command.length < 2) {
                throw new Exception("invalid server command format. expected format ="
                        + " (start <test server name> <port> [<arg1>][<arg2>][<arg3>]"
                        + "...|stop (<port>|all))");
            }
            String action = command[0];
            if (action.equals("start")) {
                if (command.length < 3) {
                    throw new Exception("invalid server start command. expected format ="
                            + " (start <test server name> <port> [<arg1>][<arg2>][<arg3>]...");
                }
                String name = command[1];
                Integer port = new Integer(command[2]);
                if (runningTestServers.containsKey(port)) {
                    throw new Exception("server with port " + port + " is already running");
                }
                ITestServer server = TestServerProvider.createTestServer(name, port);
                server.configure(Arrays.copyOfRange(command, 3, command.length));
                server.start();
                runningTestServers.put(port, server);
            } else if (action.equals("stop")) {
                String target = command[1];
                if (target.equals("all")) {
                    for (ITestServer server : runningTestServers.values()) {
                        server.stop();
                    }
                    runningTestServers.clear();
                } else {
                    Integer port = new Integer(command[1]);
                    ITestServer server = runningTestServers.get(port);
                    if (server == null) {
                        throw new Exception("no server is listening to port " + port);
                    }
                    server.stop();
                    runningTestServers.remove(port);
                }
            } else {
                throw new Exception("unknown server action");
            }
        } catch (Exception e) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n", e);
        }
        break;
    case "lib": // expected format <dataverse-name> <library-name>
                // <library-directory>
                // TODO: make this case work well with entity names containing spaces by
                // looking for \"
        lines = statement.split("\n");
        String lastLine = lines[lines.length - 1];
        String[] command = lastLine.trim().split(" ");
        if (command.length < 3) {
            throw new Exception("invalid library format");
        }
        String dataverse = command[1];
        String library = command[2];
        switch (command[0]) {
        case "install":
            if (command.length != 4) {
                throw new Exception("invalid library format");
            }
            String libPath = command[3];
            librarian.install(dataverse, library, libPath);
            break;
        case "uninstall":
            if (command.length != 3) {
                throw new Exception("invalid library format");
            }
            librarian.uninstall(dataverse, library);
            break;
        default:
            throw new Exception("invalid library format");
        }
        break;
    default:
        throw new IllegalArgumentException("No statements of type " + ctx.getType());
    }
}

From source file:org.apache.asterix.test.common.TestExecutor.java

public void executeTestFile(TestCaseContext testCaseCtx, TestFileContext ctx, Map<String, Object> variableCtx,
        String statement, boolean isDmlRecoveryTest, ProcessBuilder pb, CompilationUnit cUnit,
        MutableInt queryCount, List<TestFileContext> expectedResultFileCtxs, File testFile, String actualPath)
        throws Exception {
    File qbcFile;//from  ww  w. jav  a 2  s  . c o  m
    boolean failed = false;
    File expectedResultFile;
    switch (ctx.getType()) {
    case "ddl":
        if (ctx.getFile().getName().endsWith("aql")) {
            executeDDL(statement, getEndpoint(Servlets.AQL_DDL));
        } else {
            InputStream resultStream = executeQueryService(statement, getEndpoint(Servlets.QUERY_SERVICE),
                    OutputFormat.CLEAN_JSON);
            ResultExtractor.extract(resultStream);
        }
        break;
    case "update":
        // isDmlRecoveryTest: set IP address
        if (isDmlRecoveryTest && statement.contains("nc1://")) {
            statement = statement.replaceAll("nc1://", "127.0.0.1://../../../../../../asterix-app/");
        }
        if (ctx.getFile().getName().endsWith("aql")) {
            executeUpdate(statement, getEndpoint(Servlets.AQL_UPDATE));
        } else {
            InputStream resultStream = executeQueryService(statement, getEndpoint(Servlets.QUERY_SERVICE),
                    OutputFormat.forCompilationUnit(cUnit));
            ResultExtractor.extract(resultStream);
        }
        break;
    case "pollget":
    case "pollquery":
        // polltimeoutsecs=nnn, polldelaysecs=nnn
        int timeoutSecs = getTimeoutSecs(statement);
        int retryDelaySecs = getRetryDelaySecs(statement);
        long startTime = System.currentTimeMillis();
        long limitTime = startTime + TimeUnit.SECONDS.toMillis(timeoutSecs);
        ctx.setType(ctx.getType().substring("poll".length()));
        boolean expectedException = false;
        Exception finalException;
        LOGGER.fine("polling for up to " + timeoutSecs + " seconds w/ " + retryDelaySecs + " second(s) delay");
        while (true) {
            try {
                executeTestFile(testCaseCtx, ctx, variableCtx, statement, isDmlRecoveryTest, pb, cUnit,
                        queryCount, expectedResultFileCtxs, testFile, actualPath);
                finalException = null;
                break;
            } catch (Exception e) {
                if (isExpected(e, cUnit)) {
                    expectedException = true;
                    finalException = e;
                    break;
                }
                if ((System.currentTimeMillis() > limitTime)) {
                    finalException = e;
                    break;
                }
                LOGGER.fine("sleeping " + retryDelaySecs + " second(s) before polling again");
                Thread.sleep(TimeUnit.SECONDS.toMillis(retryDelaySecs));
            }
        }
        if (expectedException) {
            throw finalException;
        } else if (finalException != null) {
            throw new Exception("Poll limit (" + timeoutSecs + "s) exceeded without obtaining expected result",
                    finalException);
        }
        break;
    case "query":
    case "async":
    case "deferred":
        // isDmlRecoveryTest: insert Crash and Recovery
        if (isDmlRecoveryTest) {
            executeScript(pb, pb.environment().get("SCRIPT_HOME") + File.separator + "dml_recovery"
                    + File.separator + "kill_cc_and_nc.sh");
            executeScript(pb, pb.environment().get("SCRIPT_HOME") + File.separator + "dml_recovery"
                    + File.separator + "stop_and_start.sh");
        }
        InputStream resultStream = null;
        OutputFormat fmt = OutputFormat.forCompilationUnit(cUnit);
        final String reqType = ctx.getType();
        final List<CompilationUnit.Parameter> params = cUnit.getParameter();
        if (ctx.getFile().getName().endsWith("aql")) {
            if (reqType.equalsIgnoreCase("query")) {
                resultStream = executeQuery(statement, fmt, getEndpoint(Servlets.AQL_QUERY), params);
            } else {
                final URI endpoint = getEndpoint(Servlets.AQL);
                if (reqType.equalsIgnoreCase("async")) {
                    resultStream = executeAnyAQLAsync(statement, false, fmt, endpoint, variableCtx);
                } else if (reqType.equalsIgnoreCase("deferred")) {
                    resultStream = executeAnyAQLAsync(statement, true, fmt, endpoint, variableCtx);
                }
                Assert.assertNotNull("no handle for " + reqType + " test " + testFile.toString(), resultStream);
            }
        } else {
            String delivery = DELIVERY_IMMEDIATE;
            if (reqType.equalsIgnoreCase("async")) {
                delivery = DELIVERY_ASYNC;
            } else if (reqType.equalsIgnoreCase("deferred")) {
                delivery = DELIVERY_DEFERRED;
            }
            final URI uri = getEndpoint(Servlets.QUERY_SERVICE);
            if (DELIVERY_IMMEDIATE.equals(delivery)) {
                resultStream = executeQueryService(statement, fmt, uri, params, true, true);
                resultStream = ResultExtractor.extract(resultStream);
            } else {
                String handleVar = getHandleVariable(statement);
                resultStream = executeQueryService(statement, fmt, uri, upsertParam(params, "mode", delivery),
                        true);
                String handle = ResultExtractor.extractHandle(resultStream);
                Assert.assertNotNull("no handle for " + reqType + " test " + testFile.toString(), handleVar);
                variableCtx.put(handleVar, handle);
            }
        }
        if (queryCount.intValue() >= expectedResultFileCtxs.size()) {
            Assert.fail("no result file for " + testFile.toString() + "; queryCount: " + queryCount
                    + ", filectxs.size: " + expectedResultFileCtxs.size());
        }
        expectedResultFile = expectedResultFileCtxs.get(queryCount.intValue()).getFile();

        File actualResultFile = testCaseCtx.getActualResultFile(cUnit, expectedResultFile,
                new File(actualPath));
        writeOutputToFile(actualResultFile, resultStream);

        runScriptAndCompareWithResult(testFile, new PrintWriter(System.err), expectedResultFile,
                actualResultFile);
        queryCount.increment();

        // Deletes the matched result file.
        actualResultFile.getParentFile().delete();
        break;
    case "mgx":
        executeManagixCommand(stripLineComments(statement).trim());
        break;
    case "txnqbc": // qbc represents query before crash
        resultStream = executeQuery(statement, OutputFormat.forCompilationUnit(cUnit),
                getEndpoint(Servlets.AQL_QUERY), cUnit.getParameter());
        qbcFile = getTestCaseQueryBeforeCrashFile(actualPath, testCaseCtx, cUnit);
        writeOutputToFile(qbcFile, resultStream);
        break;
    case "txnqar": // qar represents query after recovery
        resultStream = executeQuery(statement, OutputFormat.forCompilationUnit(cUnit),
                getEndpoint(Servlets.AQL_QUERY), cUnit.getParameter());
        File qarFile = new File(actualPath + File.separator
                + testCaseCtx.getTestCase().getFilePath().replace(File.separator, "_") + "_" + cUnit.getName()
                + "_qar.adm");
        writeOutputToFile(qarFile, resultStream);
        qbcFile = getTestCaseQueryBeforeCrashFile(actualPath, testCaseCtx, cUnit);
        runScriptAndCompareWithResult(testFile, new PrintWriter(System.err), qbcFile, qarFile);
        break;
    case "txneu": // eu represents erroneous update
        try {
            executeUpdate(statement, getEndpoint(Servlets.AQL_UPDATE));
        } catch (Exception e) {
            // An exception is expected.
            failed = true;
            System.err.println("testFile " + testFile.toString() + " raised an exception: " + e);
        }
        if (!failed) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n  An exception" + "is expected.");
        }
        System.err.println("...but that was expected.");
        break;
    case "script":
        try {
            String output = executeScript(pb, getScriptPath(testFile.getAbsolutePath(),
                    pb.environment().get("SCRIPT_HOME"), stripLineComments(statement).trim()));
            if (output.contains("ERROR")) {
                throw new Exception(output);
            }
        } catch (Exception e) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n", e);
        }
        break;
    case "sleep":
        String[] lines = stripLineComments(statement).trim().split("\n");
        Thread.sleep(Long.parseLong(lines[lines.length - 1].trim()));
        break;
    case "errddl": // a ddlquery that expects error
        try {
            executeDDL(statement, getEndpoint(Servlets.AQL_DDL));
        } catch (Exception e) {
            // expected error happens
            failed = true;
            System.err.println("testFile " + testFile.toString() + " raised an exception: " + e);
        }
        if (!failed) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n  An exception is expected.");
        }
        System.err.println("...but that was expected.");
        break;
    case "vscript": // a script that will be executed on a vagrant virtual node
        try {
            String[] command = stripLineComments(statement).trim().split(" ");
            if (command.length != 2) {
                throw new Exception("invalid vagrant script format");
            }
            String nodeId = command[0];
            String scriptName = command[1];
            String output = executeVagrantScript(pb, nodeId, scriptName);
            if (output.contains("ERROR")) {
                throw new Exception(output);
            }
        } catch (Exception e) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n", e);
        }
        break;
    case "vmgx": // a managix command that will be executed on vagrant cc node
        String output = executeVagrantManagix(pb, stripLineComments(statement).trim());
        if (output.contains("ERROR")) {
            throw new Exception(output);
        }
        break;
    case "get":
    case "post":
        fmt = OutputFormat.forCompilationUnit(cUnit);
        String handleVar = getHandleVariable(statement);
        final String trimmedPathAndQuery = stripLineComments(stripJavaComments(statement)).trim();
        final String variablesReplaced = replaceVarRef(trimmedPathAndQuery, variableCtx);
        if ("http".equals(ctx.extension())) {
            resultStream = executeHttp(ctx.getType(), variablesReplaced, fmt);
        } else if ("uri".equals(ctx.extension())) {
            resultStream = executeURI(ctx.getType(), URI.create(variablesReplaced), fmt);
        } else {
            throw new IllegalArgumentException(
                    "Unexpected format for method " + ctx.getType() + ": " + ctx.extension());
        }
        if (handleVar != null) {
            String handle = ResultExtractor.extractHandle(resultStream);
            if (handle != null) {
                variableCtx.put(handleVar, handle);
            } else {
                throw new Exception("no handle for test " + testFile.toString());
            }
        } else {
            expectedResultFile = expectedResultFileCtxs.get(queryCount.intValue()).getFile();
            actualResultFile = testCaseCtx.getActualResultFile(cUnit, expectedResultFile, new File(actualPath));
            writeOutputToFile(actualResultFile, resultStream);
            runScriptAndCompareWithResult(testFile, new PrintWriter(System.err), expectedResultFile,
                    actualResultFile);
        }
        queryCount.increment();
        break;
    case "server": // (start <test server name> <port>
                   // [<arg1>][<arg2>][<arg3>]...|stop (<port>|all))
        try {
            lines = statement.trim().split("\n");
            String[] command = lines[lines.length - 1].trim().split(" ");
            if (command.length < 2) {
                throw new Exception("invalid server command format. expected format ="
                        + " (start <test server name> <port> [<arg1>][<arg2>][<arg3>]"
                        + "...|stop (<port>|all))");
            }
            String action = command[0];
            if (action.equals("start")) {
                if (command.length < 3) {
                    throw new Exception("invalid server start command. expected format ="
                            + " (start <test server name> <port> [<arg1>][<arg2>][<arg3>]...");
                }
                String name = command[1];
                Integer port = new Integer(command[2]);
                if (runningTestServers.containsKey(port)) {
                    throw new Exception("server with port " + port + " is already running");
                }
                ITestServer server = TestServerProvider.createTestServer(name, port);
                server.configure(Arrays.copyOfRange(command, 3, command.length));
                server.start();
                runningTestServers.put(port, server);
            } else if (action.equals("stop")) {
                String target = command[1];
                if (target.equals("all")) {
                    for (ITestServer server : runningTestServers.values()) {
                        server.stop();
                    }
                    runningTestServers.clear();
                } else {
                    Integer port = new Integer(command[1]);
                    ITestServer server = runningTestServers.get(port);
                    if (server == null) {
                        throw new Exception("no server is listening to port " + port);
                    }
                    server.stop();
                    runningTestServers.remove(port);
                }
            } else {
                throw new Exception("unknown server action");
            }
        } catch (Exception e) {
            throw new Exception("Test \"" + testFile + "\" FAILED!\n", e);
        }
        break;
    case "lib": // expected format <dataverse-name> <library-name>
                // <library-directory>
                // TODO: make this case work well with entity names containing spaces by
                // looking for \"
        lines = statement.split("\n");
        String lastLine = lines[lines.length - 1];
        String[] command = lastLine.trim().split(" ");
        if (command.length < 3) {
            throw new Exception("invalid library format");
        }
        String dataverse = command[1];
        String library = command[2];
        switch (command[0]) {
        case "install":
            if (command.length != 4) {
                throw new Exception("invalid library format");
            }
            String libPath = command[3];
            librarian.install(dataverse, library, libPath);
            break;
        case "uninstall":
            if (command.length != 3) {
                throw new Exception("invalid library format");
            }
            librarian.uninstall(dataverse, library);
            break;
        default:
            throw new Exception("invalid library format");
        }
        break;
    case "node":
        command = stripJavaComments(statement).trim().split(" ");
        String commandType = command[0];
        String nodeId = command[1];
        if (commandType.equals("kill")) {
            killNC(nodeId, cUnit);
        }
        break;
    default:
        throw new IllegalArgumentException("No statements of type " + ctx.getType());
    }
}

From source file:org.taverna.server.localworker.impl.WorkerCore.java

/**
 * Fire up the workflow. This causes a transition into the operating state.
 * /*from   w  w w . j  av a 2  s. co m*/
 * @param executeWorkflowCommand
 *            The command to run to execute the workflow.
 * @param workflow
 *            The workflow document to execute.
 * @param workingDir
 *            What directory to use as the working directory.
 * @param inputBaclava
 *            The baclava file to use for inputs, or <tt>null</tt> to use
 *            the other <b>input*</b> arguments' values.
 * @param inputFiles
 *            A mapping of input names to files that supply them. Note that
 *            we assume that nothing mapped here will be mapped in
 *            <b>inputValues</b>.
 * @param inputValues
 *            A mapping of input names to values to supply to them. Note
 *            that we assume that nothing mapped here will be mapped in
 *            <b>inputFiles</b>.
 * @param outputBaclava
 *            What baclava file to write the output from the workflow into,
 *            or <tt>null</tt> to have it written into the <tt>out</tt>
 *            subdirectory.
 * @param token
 *            The name of the workflow run.
 * @throws IOException
 *             If any of quite a large number of things goes wrong.
 */
@Override
public void initWorker(String executeWorkflowCommand, String workflow, File workingDir, File inputBaclava,
        Map<String, File> inputFiles, Map<String, String> inputValues, File outputBaclava, File securityDir,
        char[] password, Map<String, String> environment, String token) throws IOException {
    ProcessBuilder pb = new ProcessBuilder();
    /*
     * WARNING! HERE THERE BE DRAGONS! BE CAREFUL HERE!
     * 
     * Work around _Maven_ bug with permissions in zip files! The executable
     * bit is stripped by Maven's handling of file permissions, and there's
     * no practical way to work around it without massively increasing the
     * pain in other ways. Only want this on Unix - Windows isn't affected
     * by this - so we use the file separator as a proxy for whether this is
     * a true POSIX system. Ugly! Ugly ugly ugly...
     * 
     * http://jira.codehaus.org/browse/MASSEMBLY-337 is relevant, but not
     * the whole story as we don't want to use a non-standard packaging
     * method as there's a real chance of it going wrong in an unexpected
     * way then. Other parts of the story are that the executable bit isn't
     * preserved when unpacking with the dependency plugin, and there's no
     * way to be sure that the servlet container will preserve the bit
     * either (as that's probably using a Java-based ZIP engine).
     */
    if (File.separatorChar == '/')
        pb.command().add("/bin/sh");
    pb.command().add(executeWorkflowCommand);

    // Enable verbose logging
    pb.command().add("-logfile");
    pb.command().add(new File(new File(workingDir, "logs"), "detail.log").getAbsolutePath());

    if (securityDir != null) {
        pb.command().add(CREDENTIAL_MANAGER_DIRECTORY);
        pb.command().add(securityDir.getAbsolutePath());
        out.println("security dir location: " + securityDir);
    }
    if (password != null) {
        pb.command().add(CREDENTIAL_MANAGER_PASSWORD);
        out.println("password of length " + password.length + " will be written to subprocess stdin");
    }

    // Add arguments denoting inputs
    if (inputBaclava != null) {
        pb.command().add("-inputdoc");
        pb.command().add(inputBaclava.getAbsolutePath());
        if (!inputBaclava.exists())
            throw new IOException("input baclava file doesn't exist");
    } else {
        for (Entry<String, File> port : inputFiles.entrySet()) {
            if (port.getValue() != null) {
                pb.command().add("-inputfile");
                pb.command().add(port.getKey());
                pb.command().add(port.getValue().getAbsolutePath());
                if (!port.getValue().exists())
                    throw new IOException("input file for port \"" + port + "\" doesn't exist");
            }
        }
        for (Entry<String, String> port : inputValues.entrySet()) {
            if (port.getValue() != null) {
                pb.command().add("-inputvalue");
                pb.command().add(port.getKey());
                pb.command().add(port.getValue());
            }
        }
    }

    // Add arguments denoting outputs
    if (outputBaclava != null) {
        pb.command().add("-outputdoc");
        pb.command().add(outputBaclava.getAbsolutePath());
        if (!outputBaclava.getParentFile().exists())
            throw new IOException("parent directory of output baclava file does not exist");
        if (outputBaclava.exists())
            throw new IOException("output baclava file exists");
    } else {
        File out = new File(workingDir, "out");
        if (!out.mkdir()) {
            throw new IOException("failed to make output directory \"out\"");
        }
        if (!out.delete()) {
            // Taverna needs the dir to *not* exist now
            throw new IOException("failed to delete output directory \"out\"");
        }
        pb.command().add("-outputdir");
        pb.command().add(out.getAbsolutePath());
    }

    // Add an argument holding the workflow
    File tmp = createTempFile("taverna", ".t2flow");
    Writer w = new OutputStreamWriter(new FileOutputStream(tmp), "UTF-8");
    try {
        w.write(workflow);
    } finally {
        w.close();
    }
    tmp.deleteOnExit();
    pb.command().add(tmp.getAbsolutePath());

    // Indicate what working directory to use
    pb.directory(workingDir);
    wd = workingDir;

    Map<String, String> env = pb.environment();

    // Merge any options we have had imposed on us from outside
    env.putAll(environment);

    // Patch the environment to deal with TAVUTILS-17
    assert env.get("PATH") != null;
    env.put("PATH", new File(System.getProperty("java.home"), "bin") + pathSeparator + env.get("PATH"));
    // Patch the environment to deal with TAVSERV-189
    env.put("RAVEN_APPHOME", workingDir.getCanonicalPath());
    // Patch the environment to deal with TAVSERV-224
    env.put("TAVERNA_RUN_ID", token);
    if (interactionHost != null) {
        env.put("INTERACTION_HOST", interactionHost);
        env.put("INTERACTION_PORT", interactionPort);
        env.put("INTERACTION_WEBDAV", interactionWebdavPath);
        env.put("INTERACTION_FEED", interactionFeedPath);
    }

    // Start the subprocess
    out.println("starting " + pb.command() + " in directory " + workingDir);
    subprocess = pb.start();
    if (subprocess == null)
        throw new IOException("unknown failure creating process");
    start = new Date();

    // Capture its stdout and stderr
    new AsyncCopy(subprocess.getInputStream(), stdout);
    new AsyncCopy(subprocess.getErrorStream(), stderr);
    if (password != null)
        new AsyncPrint(subprocess.getOutputStream(), password);
}