Subversion Repositories XServices

Rev

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

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