Subversion Repositories XServices

Rev

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

Rev 19 Rev 46
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;
1
package net.brutex.xservices.ws;
18
 
-
 
19
import java.io.File;
2
 
20
import javax.jws.WebMethod;
3
import javax.jws.WebMethod;
21
import javax.jws.WebParam;
4
import javax.jws.WebParam;
22
import javax.jws.WebService;
5
import javax.jws.WebService;
-
 
6
 
23
import net.brutex.xservices.types.HostConnection;
7
import net.brutex.xservices.types.HostConnection;
24
import net.brutex.xservices.types.ReturnCode;
8
import net.brutex.xservices.types.ReturnCode;
25
import net.brutex.xservices.util.RunTask;
9
import net.brutex.xservices.util.BrutexNamespaces;
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
 
10
 
32
/**
-
 
33
 *
-
 
34
 * @author Brian Rosenberger, bru@brutex.de
-
 
35
 */
-
 
36
@WebService(targetNamespace="http://ws.xservices.brutex.net", name="ExecuteService")
11
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES)
37
public class ExecuteService {
12
public interface ExecuteService {
38
 
13
 
-
 
14
	@WebMethod(operationName = "runCommand")
39
    @WebMethod(operationName = "runCommand")
15
	public abstract ReturnCode runCommand(
40
    public ReturnCode runCommand(@WebParam(name = "executable") String cmd,
16
			@WebParam(name = "executable") String cmd,
41
            @WebParam(name = "argline") String args,
17
			@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
    }
18
			@WebParam(name = "timeout") long timeout);
-
 
19
 
54
 
20
	@WebMethod(operationName = "runCommandWithArgs")
55
    @WebMethod(operationName = "runCommandWithArgs")
21
	public abstract ReturnCode runCommandWithArgs(
56
    public ReturnCode runCommandWithArgs(@WebParam(name = "executable") String cmd,
22
			@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);
23
			@WebParam(name = "arg") String[] args,
69
    }
24
			@WebParam(name = "timeout") long timeout);
70
 
25
 
71
    @WebMethod(operationName = "runCommandAsync")
-
 
72
    public ReturnCode runCommandAsync(@WebParam(name = "executable") String cmd,
-
 
73
            @WebParam(name = "argline") String args) {
26
	@WebMethod(operationName = "runCommandAsync")
74
 
-
 
75
        return executeCommand(cmd,
-
 
76
                Commandline.translateCommandline(args),
-
 
77
                null,
-
 
78
                true,
-
 
79
                null,
-
 
80
                false,
-
 
81
                true,
-
 
82
                false,
27
	public abstract ReturnCode runCommandAsync(
83
                0);
28
			@WebParam(name = "executable") String cmd,
84
    }
29
			@WebParam(name = "argline") String args);
85
 
-
 
86
    @WebMethod(operationName = "runCommandAsyncWithArgs")
30
 
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,
31
	@WebMethod(operationName = "runCommandAsyncWithArgs")
-
 
32
	public abstract ReturnCode runCommandAsyncWithArgs(
97
                false,
33
			@WebParam(name = "executable") String cmd,
98
                0);
34
			@WebParam(name = "arg") String[] args);
99
    }
35
 
100
 
36
	@WebMethod(operationName = "runCommandWithSSH")
101
    @WebMethod(operationName = "runCommandWithSSH")
37
	public abstract ReturnCode runCommandWithSSH(
102
    public ReturnCode runCommandWithSSH(@WebParam(name = "host") String host,
38
			@WebParam(name = "host") String host,
103
            @WebParam(name = "port") int port,
39
			@WebParam(name = "port") int port,
104
            @WebParam(name = "username") String username,
40
			@WebParam(name = "username") String username,
105
            @WebParam(name = "password") String password,
41
			@WebParam(name = "password") String password,
106
            @WebParam(name = "command") String cmd,
42
			@WebParam(name = "command") String cmd,
107
            @WebParam(name = "timeout") long timeout) {
43
			@WebParam(name = "timeout") long timeout);
108
 
-
 
109
        return sshExec(host, username, password, port, cmd, timeout);
-
 
110
    }
-
 
111
 
44
 
112
    @WebMethod(operationName = "runCommandWithSSHKeyAuth")
45
	@WebMethod(operationName = "runCommandWithSSHKeyAuth")
-
 
46
	public abstract ReturnCode runCommandWithSSHKeyAuth(
113
    public ReturnCode runCommandWithSSHKeyAuth(@WebParam(name = "host") String host,
47
			@WebParam(name = "host") String host,
114
            @WebParam(name = "port") int port,
48
			@WebParam(name = "port") int port,
115
            @WebParam(name = "username") String username,
49
			@WebParam(name = "username") String username,
116
            @WebParam(name = "passphrase") String passphrase,
50
			@WebParam(name = "passphrase") String passphrase,
117
            @WebParam(name = "keyfile") String keyfile,
51
			@WebParam(name = "keyfile") String keyfile,
118
            @WebParam(name = "command") String cmd,
52
			@WebParam(name = "command") String cmd,
119
            @WebParam(name = "timeout") long timeout) {
53
			@WebParam(name = "timeout") long timeout);
120
 
-
 
121
        return sshExecWithCert(host, username, passphrase, keyfile, port, cmd, timeout);
-
 
122
    }
-
 
123
 
54
 
-
 
55
	@WebMethod(operationName = "rExec")
124
    @WebMethod(operationName = "rExec")
56
	public abstract ReturnCode rExec(
125
    public ReturnCode rExec(@WebParam(name = "host") HostConnection host,
57
			@WebParam(name = "host") HostConnection host,
126
            @WebParam(name = "command") String cmd,
58
			@WebParam(name = "command") String cmd,
127
            @WebParam(name = "timeout") long timeout) {
-
 
128
        return rexec(host.hostname, host.port, host.user, host.password, cmd, timeout);
-
 
129
    }
59
			@WebParam(name = "timeout") long timeout);
-
 
60
 
130
 
61
	@WebMethod(operationName = "telnet")
131
    @WebMethod(operationName = "telnet")
62
	public abstract ReturnCode runTelnet(
132
    public ReturnCode runTelnet(@WebParam(name = "host") HostConnection host,
63
			@WebParam(name = "host") HostConnection host,
133
            @WebParam(name = "prompt") String prompt,
64
			@WebParam(name = "prompt") String prompt,
134
            @WebParam(name = "command") String cmd,
65
			@WebParam(name = "command") String cmd,
135
            @WebParam(name = "expect") String expect,
66
			@WebParam(name = "expect") String expect,
136
            @WebParam(name = "timeout") long timeout) {
67
			@WebParam(name = "timeout") long timeout);
137
        return telnet(host.hostname, host.port, host.user, host.password, cmd, timeout, prompt, expect);
-
 
138
    }
-
 
139
 
-
 
140
    @WebMethod(exclude = true)
-
 
141
    private ReturnCode executeCommand(String executable,
-
 
142
            String[] args,
-
 
143
            File dir,
-
 
144
            boolean spawn,
-
 
145
            String inputstring,
-
 
146
            boolean newenvironment,
-
 
147
            boolean vmlauncher,
-
 
148
            boolean searchpath,
-
 
149
            long timeout) {
-
 
150
        ExecTask exe = new ExecTask();
-
 
151
        RunTask runner = new RunTask(exe);
-
 
152
 
-
 
153
        /*
-
 
154
        Commandline cmdl = new Commandline();
-
 
155
        cmdl.setExecutable(executable);
-
 
156
        cmdl.addArguments(args);
-
 
157
        System.out.println(cmdl.describeCommand());
-
 
158
        */
-
 
159
        
-
 
160
        exe.setExecutable(executable);
-
 
161
        for (String s : args) {
-
 
162
        exe.createArg().setValue(s);
-
 
163
        }
-
 
164
 
-
 
165
        exe.setDir(dir);
-
 
166
        if (spawn) {
-
 
167
            exe.setSpawn(spawn);
-
 
168
        } else {
-
 
169
            exe.setTimeout(timeout);
-
 
170
            exe.setInputString(inputstring);
-
 
171
            exe.setOutputproperty("ExecuteService.stdout");
-
 
172
            exe.setErrorProperty("ExecuteService.stderr");
-
 
173
            exe.setResultProperty("ExecuteService.result");
-
 
174
        }
-
 
175
 
-
 
176
        exe.setNewenvironment(newenvironment);
-
 
177
        exe.setVMLauncher(vmlauncher);
-
 
178
        exe.setSearchPath(searchpath);
-
 
179
 
-
 
180
        return runner.postTask();
-
 
181
    }
-
 
182
 
-
 
183
    @WebMethod(exclude = true)
-
 
184
    private ReturnCode sshExec(String host,
-
 
185
            String username,
-
 
186
            String password,
-
 
187
            int port,
-
 
188
            String command,
-
 
189
            long timeout) {
-
 
190
        SSHExec sshexec = new SSHExec();
-
 
191
        RunTask runner = new RunTask(sshexec);
-
 
192
        sshexec.setHost(host);
-
 
193
        sshexec.setUsername(username);
-
 
194
        sshexec.setPassword(password);
-
 
195
        sshexec.setPort(port);
-
 
196
        sshexec.setCommand(command);
-
 
197
        sshexec.setTrust(true);
-
 
198
        sshexec.setTimeout(timeout);
-
 
199
        sshexec.setOutputproperty("SSHExec.stdout");
-
 
200
        return runner.postTask();
-
 
201
    }
-
 
202
 
-
 
203
    @WebMethod(exclude = true)
-
 
204
    private ReturnCode sshExecWithCert(String host,
-
 
205
            String username,
-
 
206
            String passphrase,
-
 
207
            String keyfile,
-
 
208
            int port,
-
 
209
            String command,
-
 
210
            long timeout) {
-
 
211
        SSHExec sshexec = new SSHExec();
-
 
212
        RunTask runner = new RunTask(sshexec);
-
 
213
        sshexec.setHost(host);
-
 
214
        sshexec.setUsername(username);
-
 
215
        sshexec.setKeyfile(keyfile);
-
 
216
        sshexec.setPassphrase(passphrase);
-
 
217
        sshexec.setPort(port);
-
 
218
        sshexec.setCommand(command);
-
 
219
        sshexec.setTrust(true);
-
 
220
        sshexec.setTimeout(timeout);
-
 
221
        sshexec.setOutputproperty("SSHExec.stdout");
-
 
222
        return runner.postTask();
-
 
223
    }
-
 
224
 
-
 
225
    @WebMethod(exclude = true)
-
 
226
    private ReturnCode rexec(String host,
-
 
227
            int port,
-
 
228
            String username,
-
 
229
            String password,
-
 
230
            String command,
-
 
231
            long timeout) {
-
 
232
        RExecTask rexec = new RExecTask();
-
 
233
        RunTask runner = new RunTask(rexec);
-
 
234
        rexec.setServer(host);
-
 
235
        rexec.setPort(port);
-
 
236
        rexec.setUserid(username);
-
 
237
        rexec.setPassword(password);
-
 
238
        rexec.setCommand(command);
-
 
239
        rexec.setTimeout((int) Math.round(timeout));
-
 
240
 
-
 
241
        return runner.postTask();
-
 
242
    }
-
 
243
 
-
 
244
    @WebMethod(exclude = true)
-
 
245
    private ReturnCode telnet(String host,
-
 
246
            int port,
-
 
247
            String username,
-
 
248
            String password,
-
 
249
            String command,
-
 
250
            long timeout, String prompt, String expect) {
-
 
251
        TelnetTask rexec = new TelnetTask();
-
 
252
        RunTask runner = new RunTask(rexec);
-
 
253
        rexec.setServer(host);
-
 
254
        rexec.setPort(port);
-
 
255
        rexec.setUserid(username);
-
 
256
        rexec.setPassword(password);
-
 
257
        rexec.setTimeout((int) Math.round(timeout));
-
 
258
        
-
 
259
        rexec.createRead().addText(prompt);
-
 
260
        rexec.createWrite().addText(command);
-
 
261
        rexec.createRead().addText(expect);
-
 
262
 
-
 
263
        return runner.postTask();
-
 
264
    }
68
 
265
}
69
}
-
 
70
 
-
 
71
Generated by GNU Enscript 1.6.5.90.
-
 
72
 
-
 
73