Subversion Repositories XServices

Rev

Details | 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);
158
        Commandline cmdl = new Commandline();
159
        cmdl.setExecutable(executable);
160
        cmdl.addArguments(args);
161
        System.out.println(cmdl.describeCommand());
162
        exe.setCommand(cmdl);
163
        exe.setDir(dir);
164
        if (spawn) {
165
            exe.setSpawn(spawn);
166
        } else {
167
            exe.setTimeout(timeout);
168
            exe.setInputString(inputstring);
169
            exe.setOutputproperty("ExecuteService.stdout");
170
            exe.setErrorProperty("ExecuteService.stderr");
171
            exe.setResultProperty("ExecuteService.result");
172
        }
173
 
174
        exe.setNewenvironment(newenvironment);
175
        exe.setVMLauncher(vmlauncher);
176
        exe.setSearchPath(searchpath);
177
 
178
        Map<String, String> result = runner.postTask();
179
        ReturnCode res = null;
180
        if (spawn) {
181
            res = new ReturnCode(0, null, null);
182
        } else {
183
            res = new ReturnCode(Integer.valueOf(result.get("ExecuteService.result")),
184
                    result.get("ExecuteService.stdout"), result.get("ExecuteService.stderr"));
185
        }
186
        return res;
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");
206
        Map<String, String> result = runner.postTask();
207
        ReturnCode res = null;
208
        res = new ReturnCode(0,
209
                result.get("SSHExec.stdout"), "");
210
 
211
        return res;
212
    }
213
 
214
    @WebMethod(exclude = true)
215
    private ReturnCode sshExecWithCert(String host,
216
            String username,
217
            String passphrase,
218
            String keyfile,
219
            int port,
220
            String command,
221
            long timeout) {
222
        SSHExec sshexec = new SSHExec();
223
        RunTask runner = new RunTask(sshexec);
224
        sshexec.setHost(host);
225
        sshexec.setUsername(username);
226
        sshexec.setKeyfile(keyfile);
227
        sshexec.setPassphrase(passphrase);
228
        sshexec.setPort(port);
229
        sshexec.setCommand(command);
230
        sshexec.setTrust(true);
231
        sshexec.setTimeout(timeout);
232
        sshexec.setOutputproperty("SSHExec.stdout");
233
        Map<String, String> result = runner.postTask();
234
        ReturnCode res = null;
235
        res = new ReturnCode(0,
236
                result.get("SSHExec.stdout"), "");
237
 
238
        return res;
239
    }
240
 
241
    @WebMethod(exclude = true)
242
    private ReturnCode rexec(String host,
243
            int port,
244
            String username,
245
            String password,
246
            String command,
247
            long timeout) {
248
        RExecTask rexec = new RExecTask();
249
        RunTask runner = new RunTask(rexec);
250
        rexec.setServer(host);
251
        rexec.setPort(port);
252
        rexec.setUserid(username);
253
        rexec.setPassword(password);
254
        rexec.setCommand(command);
255
        rexec.setTimeout((int) Math.round(timeout));
256
 
257
        Map<String, String> result = runner.postTask();
258
        ReturnCode res = null;
259
        res = new ReturnCode(0,
260
                "", "");
261
 
262
        return res;
263
    }
264
 
265
    @WebMethod(exclude = true)
266
    private ReturnCode telnet(String host,
267
            int port,
268
            String username,
269
            String password,
270
            String command,
271
            long timeout, String prompt, String expect) {
272
        TelnetTask rexec = new TelnetTask();
273
        RunTask runner = new RunTask(rexec);
274
        rexec.setServer(host);
275
        rexec.setPort(port);
276
        rexec.setUserid(username);
277
        rexec.setPassword(password);
278
        rexec.setTimeout((int) Math.round(timeout));
279
 
280
        rexec.createRead().addText(prompt);
281
        rexec.createWrite().addText(command);
282
        rexec.createRead().addText(expect);
283
 
284
        Map<String, String> result = runner.postTask();
285
        ReturnCode res = null;
286
        res = new ReturnCode(0,
287
                "", "");
288
        return res;
289
    }
290
}