Subversion Repositories XServices

Rev

Rev 6 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 brianR 1
/*
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
17
package net.brutex.xservices.ws;
18
 
19
import java.io.File;
20
import java.util.Map;
21
import javax.jws.WebMethod;
22
import javax.jws.WebParam;
23
import javax.jws.WebService;
24
import net.brutex.xservices.types.ReturnCode;
25
import net.brutex.xservices.util.RunTask;
26
import org.apache.tools.ant.taskdefs.ExecTask;
27
import org.apache.tools.ant.taskdefs.optional.net.RExecTask;
28
import org.apache.tools.ant.taskdefs.optional.net.TelnetTask;
29
import org.apache.tools.ant.taskdefs.optional.ssh.SSHExec;
30
import org.apache.tools.ant.types.Commandline;
31
 
32
/**
33
 *
34
 * @author Brian Rosenberger, bru@brutex.de
35
 */
36
@WebService(targetNamespace="http://ws.xservices.brutex.net", name="ExecuteService")
37
public class ExecuteService {
38
 
39
    @WebMethod(operationName = "runCommand")
40
    public ReturnCode runCommand(@WebParam(name = "executable") String cmd,
41
            @WebParam(name = "argline") String args,
42
            @WebParam(name = "timeout") long timeout) {
43
 
44
        return executeCommand(cmd,
45
                Commandline.translateCommandline(args),
46
                null,
47
                false,
48
                null,
49
                false,
50
                true,
51
                false,
52
                timeout);
53
    }
54
 
55
    @WebMethod(operationName = "runCommandWithArgs")
56
    public ReturnCode runCommandWithArgs(@WebParam(name = "executable") String cmd,
57
            @WebParam(name = "arg") String[] args,
58
            @WebParam(name = "timeout") long timeout) {
59
 
60
        return executeCommand(cmd,
61
                args,
62
                null,
63
                false,
64
                null,
65
                false,
66
                true,
67
                false,
68
                timeout);
69
    }
70
 
71
    @WebMethod(operationName = "runCommandAsync")
72
    public ReturnCode runCommandAsync(@WebParam(name = "executable") String cmd,
73
            @WebParam(name = "argline") String args) {
74
 
75
        return executeCommand(cmd,
76
                Commandline.translateCommandline(args),
77
                null,
78
                true,
79
                null,
80
                false,
81
                true,
82
                false,
83
                0);
84
    }
85
 
86
    @WebMethod(operationName = "runCommandAsyncWithArgs")
87
    public ReturnCode runCommandAsyncWithArgs(@WebParam(name = "executable") String cmd,
88
            @WebParam(name = "arg") String[] args) {
89
 
90
        return executeCommand(cmd,
91
                args,
92
                null,
93
                true,
94
                null,
95
                false,
96
                true,
97
                false,
98
                0);
99
    }
100
 
101
    @WebMethod(operationName = "runCommandWithSSH")
102
    public ReturnCode runCommandWithSSH(@WebParam(name = "host") String host,
103
            @WebParam(name = "port") int port,
104
            @WebParam(name = "username") String username,
105
            @WebParam(name = "password") String password,
106
            @WebParam(name = "command") String cmd,
107
            @WebParam(name = "timeout") long timeout) {
108
 
109
        return sshExec(host, username, password, port, cmd, timeout);
110
    }
111
 
112
    @WebMethod(operationName = "runCommandWithSSHKeyAuth")
113
    public ReturnCode runCommandWithSSHKeyAuth(@WebParam(name = "host") String host,
114
            @WebParam(name = "port") int port,
115
            @WebParam(name = "username") String username,
116
            @WebParam(name = "passphrase") String passphrase,
117
            @WebParam(name = "keyfile") String keyfile,
118
            @WebParam(name = "command") String cmd,
119
            @WebParam(name = "timeout") long timeout) {
120
 
121
        return sshExecWithCert(host, username, passphrase, keyfile, port, cmd, timeout);
122
    }
123
 
124
    @WebMethod(operationName = "rExec")
125
    public ReturnCode rExec(@WebParam(name = "host") String host,
126
            @WebParam(name = "port") int port,
127
            @WebParam(name = "username") String username,
128
            @WebParam(name = "password") String password,
129
            @WebParam(name = "command") String cmd,
130
            @WebParam(name = "timeout") long timeout) {
131
        return rexec(host, port, username, password, cmd, timeout);
132
    }
133
 
134
    @WebMethod(operationName = "telnet")
135
    public ReturnCode runTelnet(@WebParam(name = "host") String host,
136
            @WebParam(name = "port") int port,
137
            @WebParam(name = "username") String username,
138
            @WebParam(name = "password") String password,
139
            @WebParam(name = "prompt") String prompt,
140
            @WebParam(name = "command") String cmd,
141
            @WebParam(name = "expect") String expect,
142
            @WebParam(name = "timeout") long timeout) {
143
        return telnet(host, port, username, password, cmd, timeout, prompt, expect);
144
    }
145
 
146
    @WebMethod(exclude = true)
147
    private ReturnCode executeCommand(String executable,
148
            String[] args,
149
            File dir,
150
            boolean spawn,
151
            String inputstring,
152
            boolean newenvironment,
153
            boolean vmlauncher,
154
            boolean searchpath,
155
            long timeout) {
156
        ExecTask exe = new ExecTask();
157
        RunTask runner = new RunTask(exe);
12 brianR 158
 
159
        /*
6 brianR 160
        Commandline cmdl = new Commandline();
161
        cmdl.setExecutable(executable);
162
        cmdl.addArguments(args);
163
        System.out.println(cmdl.describeCommand());
12 brianR 164
        */
165
 
166
        exe.setExecutable(executable);
167
        for (String s : args) {
168
        exe.createArg().setValue(s);
169
        }
170
 
6 brianR 171
        exe.setDir(dir);
172
        if (spawn) {
173
            exe.setSpawn(spawn);
174
        } else {
175
            exe.setTimeout(timeout);
176
            exe.setInputString(inputstring);
177
            exe.setOutputproperty("ExecuteService.stdout");
178
            exe.setErrorProperty("ExecuteService.stderr");
179
            exe.setResultProperty("ExecuteService.result");
180
        }
181
 
182
        exe.setNewenvironment(newenvironment);
183
        exe.setVMLauncher(vmlauncher);
184
        exe.setSearchPath(searchpath);
185
 
12 brianR 186
        return runner.postTask();
6 brianR 187
    }
188
 
189
    @WebMethod(exclude = true)
190
    private ReturnCode sshExec(String host,
191
            String username,
192
            String password,
193
            int port,
194
            String command,
195
            long timeout) {
196
        SSHExec sshexec = new SSHExec();
197
        RunTask runner = new RunTask(sshexec);
198
        sshexec.setHost(host);
199
        sshexec.setUsername(username);
200
        sshexec.setPassword(password);
201
        sshexec.setPort(port);
202
        sshexec.setCommand(command);
203
        sshexec.setTrust(true);
204
        sshexec.setTimeout(timeout);
205
        sshexec.setOutputproperty("SSHExec.stdout");
12 brianR 206
        return runner.postTask();
6 brianR 207
    }
208
 
209
    @WebMethod(exclude = true)
210
    private ReturnCode sshExecWithCert(String host,
211
            String username,
212
            String passphrase,
213
            String keyfile,
214
            int port,
215
            String command,
216
            long timeout) {
217
        SSHExec sshexec = new SSHExec();
218
        RunTask runner = new RunTask(sshexec);
219
        sshexec.setHost(host);
220
        sshexec.setUsername(username);
221
        sshexec.setKeyfile(keyfile);
222
        sshexec.setPassphrase(passphrase);
223
        sshexec.setPort(port);
224
        sshexec.setCommand(command);
225
        sshexec.setTrust(true);
226
        sshexec.setTimeout(timeout);
227
        sshexec.setOutputproperty("SSHExec.stdout");
12 brianR 228
        return runner.postTask();
6 brianR 229
    }
230
 
231
    @WebMethod(exclude = true)
232
    private ReturnCode rexec(String host,
233
            int port,
234
            String username,
235
            String password,
236
            String command,
237
            long timeout) {
238
        RExecTask rexec = new RExecTask();
239
        RunTask runner = new RunTask(rexec);
240
        rexec.setServer(host);
241
        rexec.setPort(port);
242
        rexec.setUserid(username);
243
        rexec.setPassword(password);
244
        rexec.setCommand(command);
245
        rexec.setTimeout((int) Math.round(timeout));
246
 
12 brianR 247
        return runner.postTask();
6 brianR 248
    }
249
 
250
    @WebMethod(exclude = true)
251
    private ReturnCode telnet(String host,
252
            int port,
253
            String username,
254
            String password,
255
            String command,
256
            long timeout, String prompt, String expect) {
257
        TelnetTask rexec = new TelnetTask();
258
        RunTask runner = new RunTask(rexec);
259
        rexec.setServer(host);
260
        rexec.setPort(port);
261
        rexec.setUserid(username);
262
        rexec.setPassword(password);
263
        rexec.setTimeout((int) Math.round(timeout));
264
 
265
        rexec.createRead().addText(prompt);
266
        rexec.createWrite().addText(command);
267
        rexec.createRead().addText(expect);
268
 
12 brianR 269
        return runner.postTask();
6 brianR 270
    }
271
}