Subversion Repositories XServices

Rev

Rev 6 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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