Subversion Repositories XServices

Rev

Rev 12 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.brutex.xservices.ws;

import java.io.File;
import java.util.Map;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import net.brutex.xservices.types.ReturnCode;
import net.brutex.xservices.util.RunTask;
import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.taskdefs.optional.net.RExecTask;
import org.apache.tools.ant.taskdefs.optional.net.TelnetTask;
import org.apache.tools.ant.taskdefs.optional.ssh.SSHExec;
import org.apache.tools.ant.types.Commandline;

/**
 *
 * @author Brian Rosenberger, bru@brutex.de
 */
@WebService(targetNamespace="http://ws.xservices.brutex.net", name="ExecuteService")
public class ExecuteService {

    @WebMethod(operationName = "runCommand")
    public ReturnCode runCommand(@WebParam(name = "executable") String cmd,
            @WebParam(name = "argline") String args,
            @WebParam(name = "timeout") long timeout) {

        return executeCommand(cmd,
                Commandline.translateCommandline(args),
                null,
                false,
                null,
                false,
                true,
                false,
                timeout);
    }

    @WebMethod(operationName = "runCommandWithArgs")
    public ReturnCode runCommandWithArgs(@WebParam(name = "executable") String cmd,
            @WebParam(name = "arg") String[] args,
            @WebParam(name = "timeout") long timeout) {

        return executeCommand(cmd,
                args,
                null,
                false,
                null,
                false,
                true,
                false,
                timeout);
    }

    @WebMethod(operationName = "runCommandAsync")
    public ReturnCode runCommandAsync(@WebParam(name = "executable") String cmd,
            @WebParam(name = "argline") String args) {

        return executeCommand(cmd,
                Commandline.translateCommandline(args),
                null,
                true,
                null,
                false,
                true,
                false,
                0);
    }

    @WebMethod(operationName = "runCommandAsyncWithArgs")
    public ReturnCode runCommandAsyncWithArgs(@WebParam(name = "executable") String cmd,
            @WebParam(name = "arg") String[] args) {

        return executeCommand(cmd,
                args,
                null,
                true,
                null,
                false,
                true,
                false,
                0);
    }

    @WebMethod(operationName = "runCommandWithSSH")
    public ReturnCode runCommandWithSSH(@WebParam(name = "host") String host,
            @WebParam(name = "port") int port,
            @WebParam(name = "username") String username,
            @WebParam(name = "password") String password,
            @WebParam(name = "command") String cmd,
            @WebParam(name = "timeout") long timeout) {

        return sshExec(host, username, password, port, cmd, timeout);
    }

    @WebMethod(operationName = "runCommandWithSSHKeyAuth")
    public ReturnCode runCommandWithSSHKeyAuth(@WebParam(name = "host") String host,
            @WebParam(name = "port") int port,
            @WebParam(name = "username") String username,
            @WebParam(name = "passphrase") String passphrase,
            @WebParam(name = "keyfile") String keyfile,
            @WebParam(name = "command") String cmd,
            @WebParam(name = "timeout") long timeout) {

        return sshExecWithCert(host, username, passphrase, keyfile, port, cmd, timeout);
    }

    @WebMethod(operationName = "rExec")
    public ReturnCode rExec(@WebParam(name = "host") String host,
            @WebParam(name = "port") int port,
            @WebParam(name = "username") String username,
            @WebParam(name = "password") String password,
            @WebParam(name = "command") String cmd,
            @WebParam(name = "timeout") long timeout) {
        return rexec(host, port, username, password, cmd, timeout);
    }

    @WebMethod(operationName = "telnet")
    public ReturnCode runTelnet(@WebParam(name = "host") String host,
            @WebParam(name = "port") int port,
            @WebParam(name = "username") String username,
            @WebParam(name = "password") String password,
            @WebParam(name = "prompt") String prompt,
            @WebParam(name = "command") String cmd,
            @WebParam(name = "expect") String expect,
            @WebParam(name = "timeout") long timeout) {
        return telnet(host, port, username, password, cmd, timeout, prompt, expect);
    }

    @WebMethod(exclude = true)
    private ReturnCode executeCommand(String executable,
            String[] args,
            File dir,
            boolean spawn,
            String inputstring,
            boolean newenvironment,
            boolean vmlauncher,
            boolean searchpath,
            long timeout) {
        ExecTask exe = new ExecTask();
        RunTask runner = new RunTask(exe);
        Commandline cmdl = new Commandline();
        cmdl.setExecutable(executable);
        cmdl.addArguments(args);
        System.out.println(cmdl.describeCommand());
        exe.setCommand(cmdl);
        exe.setDir(dir);
        if (spawn) {
            exe.setSpawn(spawn);
        } else {
            exe.setTimeout(timeout);
            exe.setInputString(inputstring);
            exe.setOutputproperty("ExecuteService.stdout");
            exe.setErrorProperty("ExecuteService.stderr");
            exe.setResultProperty("ExecuteService.result");
        }

        exe.setNewenvironment(newenvironment);
        exe.setVMLauncher(vmlauncher);
        exe.setSearchPath(searchpath);

        Map<String, String> result = runner.postTask();
        ReturnCode res = null;
        if (spawn) {
            res = new ReturnCode(0, null, null);
        } else {
            res = new ReturnCode(Integer.valueOf(result.get("ExecuteService.result")),
                    result.get("ExecuteService.stdout"), result.get("ExecuteService.stderr"));
        }
        return res;
    }

    @WebMethod(exclude = true)
    private ReturnCode sshExec(String host,
            String username,
            String password,
            int port,
            String command,
            long timeout) {
        SSHExec sshexec = new SSHExec();
        RunTask runner = new RunTask(sshexec);
        sshexec.setHost(host);
        sshexec.setUsername(username);
        sshexec.setPassword(password);
        sshexec.setPort(port);
        sshexec.setCommand(command);
        sshexec.setTrust(true);
        sshexec.setTimeout(timeout);
        sshexec.setOutputproperty("SSHExec.stdout");
        Map<String, String> result = runner.postTask();
        ReturnCode res = null;
        res = new ReturnCode(0,
                result.get("SSHExec.stdout"), "");

        return res;
    }

    @WebMethod(exclude = true)
    private ReturnCode sshExecWithCert(String host,
            String username,
            String passphrase,
            String keyfile,
            int port,
            String command,
            long timeout) {
        SSHExec sshexec = new SSHExec();
        RunTask runner = new RunTask(sshexec);
        sshexec.setHost(host);
        sshexec.setUsername(username);
        sshexec.setKeyfile(keyfile);
        sshexec.setPassphrase(passphrase);
        sshexec.setPort(port);
        sshexec.setCommand(command);
        sshexec.setTrust(true);
        sshexec.setTimeout(timeout);
        sshexec.setOutputproperty("SSHExec.stdout");
        Map<String, String> result = runner.postTask();
        ReturnCode res = null;
        res = new ReturnCode(0,
                result.get("SSHExec.stdout"), "");

        return res;
    }

    @WebMethod(exclude = true)
    private ReturnCode rexec(String host,
            int port,
            String username,
            String password,
            String command,
            long timeout) {
        RExecTask rexec = new RExecTask();
        RunTask runner = new RunTask(rexec);
        rexec.setServer(host);
        rexec.setPort(port);
        rexec.setUserid(username);
        rexec.setPassword(password);
        rexec.setCommand(command);
        rexec.setTimeout((int) Math.round(timeout));

        Map<String, String> result = runner.postTask();
        ReturnCode res = null;
        res = new ReturnCode(0,
                "", "");

        return res;
    }

    @WebMethod(exclude = true)
    private ReturnCode telnet(String host,
            int port,
            String username,
            String password,
            String command,
            long timeout, String prompt, String expect) {
        TelnetTask rexec = new TelnetTask();
        RunTask runner = new RunTask(rexec);
        rexec.setServer(host);
        rexec.setPort(port);
        rexec.setUserid(username);
        rexec.setPassword(password);
        rexec.setTimeout((int) Math.round(timeout));
        
        rexec.createRead().addText(prompt);
        rexec.createWrite().addText(command);
        rexec.createRead().addText(expect);

        Map<String, String> result = runner.postTask();
        ReturnCode res = null;
        res = new ReturnCode(0,
                "", "");
        return res;
    }
}