Subversion Repositories XServices

Rev

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