org.mule.tooling.jubula.cliexecutor.internal.DefaultCliExecutor.java Source code

Java tutorial

Introduction

Here is the source code for org.mule.tooling.jubula.cliexecutor.internal.DefaultCliExecutor.java

Source

/**
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

package org.mule.tooling.jubula.cliexecutor.internal;

import java.io.File;
import java.io.IOException;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteResultHandler;
import org.mule.tooling.jubula.cliexecutor.Callback;

public class DefaultCliExecutor implements CliExecutor {

    @Override
    public int run(final File executable, final String... params) {
        try {
            final DefaultExecutor executor = new DefaultExecutor();
            final CommandLine command = CommandLine.parse(executable.getAbsolutePath());
            command.addArguments(params, true);
            return executor.execute(command);
        } catch (final ExecuteException e) {
            throw new RuntimeException(e);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void runAsync(final String commandString, final Callback callback, final String... params) {
        try {
            final DefaultExecutor executor = new DefaultExecutor();
            final CommandLine command = CommandLine.parse(commandString);
            command.addArguments(params, true);
            executor.execute(command, new ExecuteResultHandler() {

                @Override
                public void onProcessFailed(final ExecuteException returnCode) {
                    callback.failure(returnCode.getExitValue());
                }

                @Override
                public void onProcessComplete(final int returnCode) {
                    callback.success(returnCode);
                }
            });
        } catch (final ExecuteException e) {
            throw new RuntimeException(e);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }

}