Subversion Repositories XServices

Rev

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

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