Subversion Repositories XServices

Rev

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

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