Subversion Repositories XServices

Rev

Go to most recent revision | 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
 
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")
58 brianR 129
    public ReturnCode runCommandWithSSH(@WebParam(name = "host") HostConnection host,
6 brianR 130
            @WebParam(name = "command") String cmd,
131
            @WebParam(name = "timeout") long timeout) {
132
 
58 brianR 133
        return sshExec(host.hostname, host.user, host.password, host.port, cmd, timeout);
6 brianR 134
    }
135
 
46 brianR 136
    /* (non-Javadoc)
137
	 * @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)
138
	 */
139
    @Override
140
	@WebMethod(operationName = "runCommandWithSSHKeyAuth")
58 brianR 141
    public ReturnCode runCommandWithSSHKeyAuth(@WebParam(name = "host") HostConnection host,
6 brianR 142
            @WebParam(name = "keyfile") String keyfile,
143
            @WebParam(name = "command") String cmd,
144
            @WebParam(name = "timeout") long timeout) {
145
 
58 brianR 146
        return sshExecWithCert(host.hostname, host.user, host.password, keyfile, host.port, cmd, timeout);
6 brianR 147
    }
148
 
46 brianR 149
    /* (non-Javadoc)
150
	 * @see net.brutex.xservices.ws.impl.ExecuteService#rExec(net.brutex.xservices.types.HostConnection, java.lang.String, long)
151
	 */
152
    @Override
153
	@WebMethod(operationName = "rExec")
19 brianR 154
    public ReturnCode rExec(@WebParam(name = "host") HostConnection host,
6 brianR 155
            @WebParam(name = "command") String cmd,
156
            @WebParam(name = "timeout") long timeout) {
19 brianR 157
        return rexec(host.hostname, host.port, host.user, host.password, cmd, timeout);
6 brianR 158
    }
159
 
46 brianR 160
    /* (non-Javadoc)
161
	 * @see net.brutex.xservices.ws.impl.ExecuteService#runTelnet(net.brutex.xservices.types.HostConnection, java.lang.String, java.lang.String, java.lang.String, long)
162
	 */
163
    @Override
164
	@WebMethod(operationName = "telnet")
19 brianR 165
    public ReturnCode runTelnet(@WebParam(name = "host") HostConnection host,
6 brianR 166
            @WebParam(name = "prompt") String prompt,
167
            @WebParam(name = "command") String cmd,
168
            @WebParam(name = "expect") String expect,
169
            @WebParam(name = "timeout") long timeout) {
19 brianR 170
        return telnet(host.hostname, host.port, host.user, host.password, cmd, timeout, prompt, expect);
6 brianR 171
    }
172
 
173
    @WebMethod(exclude = true)
174
    private ReturnCode executeCommand(String executable,
175
            String[] args,
176
            File dir,
177
            boolean spawn,
178
            String inputstring,
179
            boolean newenvironment,
180
            boolean vmlauncher,
181
            boolean searchpath,
182
            long timeout) {
183
        ExecTask exe = new ExecTask();
184
        RunTask runner = new RunTask(exe);
12 brianR 185
 
186
        /*
6 brianR 187
        Commandline cmdl = new Commandline();
188
        cmdl.setExecutable(executable);
189
        cmdl.addArguments(args);
190
        System.out.println(cmdl.describeCommand());
12 brianR 191
        */
192
 
193
        exe.setExecutable(executable);
194
        for (String s : args) {
195
        exe.createArg().setValue(s);
196
        }
197
 
6 brianR 198
        exe.setDir(dir);
199
        if (spawn) {
200
            exe.setSpawn(spawn);
201
        } else {
202
            exe.setTimeout(timeout);
203
            exe.setInputString(inputstring);
204
            exe.setOutputproperty("ExecuteService.stdout");
205
            exe.setErrorProperty("ExecuteService.stderr");
206
            exe.setResultProperty("ExecuteService.result");
207
        }
208
 
209
        exe.setNewenvironment(newenvironment);
210
        exe.setVMLauncher(vmlauncher);
211
        exe.setSearchPath(searchpath);
212
 
12 brianR 213
        return runner.postTask();
6 brianR 214
    }
215
 
216
    @WebMethod(exclude = true)
217
    private ReturnCode sshExec(String host,
218
            String username,
219
            String password,
220
            int port,
221
            String command,
222
            long timeout) {
223
        SSHExec sshexec = new SSHExec();
224
        RunTask runner = new RunTask(sshexec);
225
        sshexec.setHost(host);
226
        sshexec.setUsername(username);
227
        sshexec.setPassword(password);
228
        sshexec.setPort(port);
229
        sshexec.setCommand(command);
230
        sshexec.setTrust(true);
231
        sshexec.setTimeout(timeout);
232
        sshexec.setOutputproperty("SSHExec.stdout");
12 brianR 233
        return runner.postTask();
6 brianR 234
    }
235
 
236
    @WebMethod(exclude = true)
237
    private ReturnCode sshExecWithCert(String host,
238
            String username,
239
            String passphrase,
240
            String keyfile,
241
            int port,
242
            String command,
243
            long timeout) {
244
        SSHExec sshexec = new SSHExec();
245
        RunTask runner = new RunTask(sshexec);
246
        sshexec.setHost(host);
247
        sshexec.setUsername(username);
248
        sshexec.setKeyfile(keyfile);
249
        sshexec.setPassphrase(passphrase);
250
        sshexec.setPort(port);
251
        sshexec.setCommand(command);
252
        sshexec.setTrust(true);
253
        sshexec.setTimeout(timeout);
254
        sshexec.setOutputproperty("SSHExec.stdout");
12 brianR 255
        return runner.postTask();
6 brianR 256
    }
257
 
258
    @WebMethod(exclude = true)
259
    private ReturnCode rexec(String host,
260
            int port,
261
            String username,
262
            String password,
263
            String command,
264
            long timeout) {
265
        RExecTask rexec = new RExecTask();
266
        RunTask runner = new RunTask(rexec);
267
        rexec.setServer(host);
268
        rexec.setPort(port);
269
        rexec.setUserid(username);
270
        rexec.setPassword(password);
271
        rexec.setCommand(command);
272
        rexec.setTimeout((int) Math.round(timeout));
273
 
12 brianR 274
        return runner.postTask();
6 brianR 275
    }
276
 
277
    @WebMethod(exclude = true)
278
    private ReturnCode telnet(String host,
279
            int port,
280
            String username,
281
            String password,
282
            String command,
283
            long timeout, String prompt, String expect) {
284
        TelnetTask rexec = new TelnetTask();
285
        RunTask runner = new RunTask(rexec);
286
        rexec.setServer(host);
287
        rexec.setPort(port);
288
        rexec.setUserid(username);
289
        rexec.setPassword(password);
290
        rexec.setTimeout((int) Math.round(timeout));
291
 
292
        rexec.createRead().addText(prompt);
293
        rexec.createWrite().addText(command);
294
        rexec.createRead().addText(expect);
295
 
12 brianR 296
        return runner.postTask();
6 brianR 297
    }
298
}