Subversion Repositories XServices

Rev

Rev 58 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 58 Rev 70
Line 15... Line 15...
15
 */
15
 */
Line 16... Line 16...
16
 
16
 
Line 17... Line 17...
17
package net.brutex.xservices.ws.impl;
17
package net.brutex.xservices.ws.impl;
-
 
18
 
-
 
19
import java.io.File;
18
 
20
import java.util.UUID;
19
import java.io.File;
21
 
20
import javax.jws.WebMethod;
22
import javax.jws.WebMethod;
21
import javax.jws.WebParam;
23
import javax.jws.WebParam;
22
import javax.jws.WebService;
24
import javax.jws.WebService;
23
import net.brutex.xservices.types.HostConnection;
25
import net.brutex.xservices.types.HostConnection;
-
 
26
import net.brutex.xservices.types.ReturnCode;
24
import net.brutex.xservices.types.ReturnCode;
27
import net.brutex.xservices.util.BrutexNamespaces;
25
import net.brutex.xservices.util.BrutexNamespaces;
28
import net.brutex.xservices.util.JobWrapper;
-
 
29
import net.brutex.xservices.util.RunTask;
Line 26... Line 30...
26
import net.brutex.xservices.util.RunTask;
30
import net.brutex.xservices.ws.ExecuteService;
27
import net.brutex.xservices.ws.ExecuteService;
31
import net.brutex.xservices.ws.XServicesFault;
28
 
32
 
29
import org.apache.tools.ant.taskdefs.ExecTask;
33
import org.apache.tools.ant.taskdefs.ExecTask;
30
import org.apache.tools.ant.taskdefs.optional.net.RExecTask;
34
import org.apache.tools.ant.taskdefs.optional.net.RExecTask;
-
 
35
import org.apache.tools.ant.taskdefs.optional.net.TelnetTask;
-
 
36
import org.apache.tools.ant.taskdefs.optional.ssh.SSHExec;
-
 
37
import org.apache.tools.ant.types.Commandline;
-
 
38
import org.mozilla.javascript.Context;
-
 
39
import org.mozilla.javascript.Scriptable;
-
 
40
import org.quartz.JobBuilder;
-
 
41
import org.quartz.JobDetail;
-
 
42
import org.quartz.Scheduler;
-
 
43
import org.quartz.SchedulerException;
-
 
44
import org.quartz.Trigger;
-
 
45
import static org.quartz.TriggerBuilder.*;
-
 
46
import static org.quartz.SimpleScheduleBuilder.*;
Line 31... Line 47...
31
import org.apache.tools.ant.taskdefs.optional.net.TelnetTask;
47
import org.quartz.TriggerUtils;
32
import org.apache.tools.ant.taskdefs.optional.ssh.SSHExec;
48
import org.quartz.impl.StdSchedulerFactory;
33
import org.apache.tools.ant.types.Commandline;
49
import org.quartz.impl.triggers.SimpleTriggerImpl;
34
 
50
 
35
/**
-
 
36
 *
-
 
37
 * @author Brian Rosenberger, bru@brutex.de
51
/**
38
 */
-
 
39
@WebService(
-
 
40
		targetNamespace=BrutexNamespaces.WS_XSERVICES, 
52
 * 
Line -... Line 53...
-
 
53
 * @author Brian Rosenberger, bru@brutex.de
41
		endpointInterface="net.brutex.xservices.ws.ExecuteService",
54
 */
-
 
55
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.ExecuteService", serviceName = "ExecuteService")
-
 
56
public class ExecuteServiceImpl implements ExecuteService {
42
		serviceName="ExecuteService"
57
 
-
 
58
	/*
43
			)
59
	 * (non-Javadoc)
44
public class ExecuteServiceImpl implements ExecuteService {
-
 
45
 
60
	 * 
46
    /* (non-Javadoc)
61
	 * @see
47
	 * @see net.brutex.xservices.ws.impl.ExecuteService#runCommand(java.lang.String, java.lang.String, long)
62
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommand(java.lang.String,
48
	 */
63
	 * java.lang.String, long)
Line 49... Line -...
49
    @Override
-
 
50
	@WebMethod(operationName = "runCommand")
64
	 */
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,
65
	@WebMethod(operationName = "runCommand")
58
                false,
66
	public ReturnCode runCommand(@WebParam(name = "executable") String cmd,
Line -... Line 67...
-
 
67
			@WebParam(name = "argline") String args,
59
                null,
68
			@WebParam(name = "timeout") long timeout) {
-
 
69
 
-
 
70
		return executeCommand(cmd, Commandline.translateCommandline(args),
60
                false,
71
				null, false, null, false, true, false, timeout);
-
 
72
	}
61
                true,
73
 
62
                false,
74
	/*
63
                timeout);
75
	 * (non-Javadoc)
-
 
76
	 * 
64
    }
77
	 * @see
65
 
78
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommandWithArgs(java.lang
66
    /* (non-Javadoc)
79
	 * .String, java.lang.String[], long)
Line 67... Line 80...
67
	 * @see net.brutex.xservices.ws.impl.ExecuteService#runCommandWithArgs(java.lang.String, java.lang.String[], long)
80
	 */
68
	 */
-
 
69
    @Override
-
 
70
	@WebMethod(operationName = "runCommandWithArgs")
-
 
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,
81
	
76
                args,
82
	@WebMethod(operationName = "runCommandWithArgs")
Line -... Line 83...
-
 
83
	public ReturnCode runCommandWithArgs(
77
                null,
84
			@WebParam(name = "executable") String cmd,
-
 
85
			@WebParam(name = "arg") String[] args,
-
 
86
			@WebParam(name = "timeout") long timeout) {
78
                false,
87
 
-
 
88
		return executeCommand(cmd, args, null, false, null, false, true, false,
79
                null,
89
				timeout);
80
                false,
90
	}
81
                true,
91
 
-
 
92
	/*
82
                false,
93
	 * (non-Javadoc)
83
                timeout);
94
	 * 
Line 84... Line -...
84
    }
-
 
85
 
95
	 * @see
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")
-
 
91
    public ReturnCode runCommandAsync(@WebParam(name = "executable") String cmd,
96
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommandAsync(java.lang
92
            @WebParam(name = "argline") String args) {
-
 
93
 
97
	 * .String, java.lang.String)
Line -... Line 98...
-
 
98
	 */
94
        return executeCommand(cmd,
99
	
-
 
100
	@WebMethod(operationName = "runCommandAsync")
-
 
101
	public ReturnCode runCommandAsync(
95
                Commandline.translateCommandline(args),
102
			@WebParam(name = "executable") String cmd,
-
 
103
			@WebParam(name = "argline") String args) {
96
                null,
104
 
97
                true,
105
		return executeCommand(cmd, Commandline.translateCommandline(args),
98
                null,
106
				null, true, null, false, true, false, 0);
-
 
107
	}
99
                false,
108
 
100
                true,
109
	/*
Line 101... Line 110...
101
                false,
110
	 * (non-Javadoc)
102
                0);
-
 
103
    }
-
 
104
 
-
 
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")
111
	 * 
110
    public ReturnCode runCommandAsyncWithArgs(@WebParam(name = "executable") String cmd,
112
	 * @see
Line -... Line 113...
-
 
113
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommandAsyncWithArgs(java
111
            @WebParam(name = "arg") String[] args) {
114
	 * .lang.String, java.lang.String[])
-
 
115
	 */
-
 
116
	
-
 
117
	@WebMethod(operationName = "runCommandAsyncWithArgs")
112
 
118
	public ReturnCode runCommandAsyncWithArgs(
113
        return executeCommand(cmd,
119
			@WebParam(name = "executable") String cmd,
114
                args,
120
			@WebParam(name = "arg") String[] args) {
115
                null,
121
 
-
 
122
		return executeCommand(cmd, args, null, true, null, false, true, false,
116
                true,
123
				0);
117
                null,
124
	}
118
                false,
125
 
Line 119... Line 126...
119
                true,
126
	/*
-
 
127
	 * (non-Javadoc)
120
                false,
128
	 * 
Line -... Line 129...
-
 
129
	 * @see
121
                0);
130
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommandWithSSH(java.lang
-
 
131
	 * .String, int, java.lang.String, java.lang.String, java.lang.String, long)
-
 
132
	 */
122
    }
133
	
-
 
134
	@WebMethod(operationName = "runCommandWithSSH")
-
 
135
	public ReturnCode runCommandWithSSH(
123
 
136
			@WebParam(name = "host") HostConnection host,
124
    /* (non-Javadoc)
137
			@WebParam(name = "command") String cmd,
125
	 * @see net.brutex.xservices.ws.impl.ExecuteService#runCommandWithSSH(java.lang.String, int, java.lang.String, java.lang.String, java.lang.String, long)
138
			@WebParam(name = "timeout") long timeout) {
-
 
139
 
126
	 */
140
		return sshExec(host.hostname, host.user, host.password, host.port, cmd,
127
    @Override
141
				timeout);
128
	@WebMethod(operationName = "runCommandWithSSH")
142
	}
129
    public ReturnCode runCommandWithSSH(@WebParam(name = "host") HostConnection host,
143
 
Line 130... Line 144...
130
            @WebParam(name = "command") String cmd,
144
	/*
-
 
145
	 * (non-Javadoc)
131
            @WebParam(name = "timeout") long timeout) {
146
	 * 
Line -... Line 147...
-
 
147
	 * @see
132
 
148
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommandWithSSHKeyAuth(
-
 
149
	 * java.lang.String, int, java.lang.String, java.lang.String,
-
 
150
	 * java.lang.String, java.lang.String, long)
133
        return sshExec(host.hostname, host.user, host.password, host.port, cmd, timeout);
151
	 */
-
 
152
	
134
    }
153
	@WebMethod(operationName = "runCommandWithSSHKeyAuth")
135
 
154
	public ReturnCode runCommandWithSSHKeyAuth(
136
    /* (non-Javadoc)
155
			@WebParam(name = "host") HostConnection host,
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)
156
			@WebParam(name = "keyfile") String keyfile,
138
	 */
157
			@WebParam(name = "command") String cmd,
139
    @Override
158
			@WebParam(name = "timeout") long timeout) {
140
	@WebMethod(operationName = "runCommandWithSSHKeyAuth")
159
 
-
 
160
		return sshExecWithCert(host.hostname, host.user, host.password,
141
    public ReturnCode runCommandWithSSHKeyAuth(@WebParam(name = "host") HostConnection host,
161
				keyfile, host.port, cmd, timeout);
Line -... Line 162...
-
 
162
	}
142
            @WebParam(name = "keyfile") String keyfile,
163
 
-
 
164
	/*
-
 
165
	 * (non-Javadoc)
143
            @WebParam(name = "command") String cmd,
166
	 * 
-
 
167
	 * @see
-
 
168
	 * net.brutex.xservices.ws.impl.ExecuteService#rExec(net.brutex.xservices
144
            @WebParam(name = "timeout") long timeout) {
169
	 * .types.HostConnection, java.lang.String, long)
145
 
170
	 */
146
        return sshExecWithCert(host.hostname, host.user, host.password, keyfile, host.port, cmd, timeout);
171
	
147
    }
172
	@WebMethod(operationName = "rExec")
148
 
173
	public ReturnCode rExec(@WebParam(name = "host") HostConnection host,
149
    /* (non-Javadoc)
174
			@WebParam(name = "command") String cmd,
150
	 * @see net.brutex.xservices.ws.impl.ExecuteService#rExec(net.brutex.xservices.types.HostConnection, java.lang.String, long)
175
			@WebParam(name = "timeout") long timeout) {
151
	 */
176
		return rexec(host.hostname, host.port, host.user, host.password, cmd,
152
    @Override
177
				timeout);
-
 
178
	}
-
 
179
 
-
 
180
	/*
-
 
181
	 * (non-Javadoc)
-
 
182
	 * 
-
 
183
	 * @see
-
 
184
	 * net.brutex.xservices.ws.impl.ExecuteService#runTelnet(net.brutex.xservices
-
 
185
	 * .types.HostConnection, java.lang.String, java.lang.String,
-
 
186
	 * java.lang.String, long)
-
 
187
	 */
-
 
188
	
-
 
189
	@WebMethod(operationName = "telnet")
-
 
190
	public ReturnCode runTelnet(@WebParam(name = "host") HostConnection host,
-
 
191
			@WebParam(name = "prompt") String prompt,
-
 
192
			@WebParam(name = "command") String cmd,
-
 
193
			@WebParam(name = "expect") String expect,
-
 
194
			@WebParam(name = "timeout") long timeout) {
-
 
195
		return telnet(host.hostname, host.port, host.user, host.password, cmd,
-
 
196
				timeout, prompt, expect);
-
 
197
	}
-
 
198
 
-
 
199
	
-
 
200
	public void runJScript(String script) throws XServicesFault {
-
 
201
 
-
 
202
		try {
-
 
203
			// Create and enter a Context. A Context stores information about
-
 
204
			// the execution environment of a script.
-
 
205
			Context cx = Context.enter();
-
 
206
			cx.setOptimizationLevel(0);
-
 
207
			cx.setLanguageVersion(Context.VERSION_1_7);
-
 
208
			// cx is the Context instance you're using to run scripts
-
 
209
			/*
-
 
210
			 * cx.setClassShutter(new ClassShutter() { public boolean
-
 
211
			 * visibleToScripts(String className) {
-
 
212
			 * if(className.startsWith("adapter")) return true;
-
 
213
			 * if(className.startsWith("java.lang.System") ||
-
 
214
			 * className.startsWith
-
 
215
			 * ("org.apache.tomcat.util.log.SystemLogHandler")) return true;
-
 
216
			 * System.out.println(className + " is blocked."); return false; }
-
 
217
			 * });
-
 
218
			 */
-
 
219
 
-
 
220
			// Initialise the standard objects (Object, Function, etc.). This
-
 
221
			// must be done before scripts can be
-
 
222
			// executed. The null parameter tells initStandardObjects
-
 
223
			// to create and return a scope object that we use
-
 
224
			// in later calls.
-
 
225
			Scriptable scope = cx.initStandardObjects();
-
 
226
			// Object wrappedOut = Context.javaToJS(System.out, scope);
-
 
227
			// Object wrappedOut2 = Context.javaToJS(this, scope);
-
 
228
			// scope.put("out", scope, wrappedOut);
-
 
229
			// scope.put("exe", scope, wrappedOut2);
-
 
230
 
153
	@WebMethod(operationName = "rExec")
231
			// Execute the script
Line 154... Line 232...
154
    public ReturnCode rExec(@WebParam(name = "host") HostConnection host,
232
			// cx.evaluateString(scope, "importClass('java.lang.System');\n",
155
            @WebParam(name = "command") String cmd,
233
			// "head", 1, null);
156
            @WebParam(name = "timeout") long timeout) {
-
 
157
        return rexec(host.hostname, host.port, host.user, host.password, cmd, timeout);
-
 
158
    }
-
 
159
 
234
			// cx.evaluateString(scope, "importPackage('java.util');\n", "head",
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
	 */
235
			// 2, null);
163
    @Override
236
			Object obj = cx
164
	@WebMethod(operationName = "telnet")
237
					.evaluateString(scope, script, "TestScript", 1, null);
165
    public ReturnCode runTelnet(@WebParam(name = "host") HostConnection host,
238
 
Line 166... Line 239...
166
            @WebParam(name = "prompt") String prompt,
239
		} catch (Exception e) {
167
            @WebParam(name = "command") String cmd,
-
 
168
            @WebParam(name = "expect") String expect,
240
			System.out.println(e.getMessage());
169
            @WebParam(name = "timeout") long timeout) {
-
 
170
        return telnet(host.hostname, host.port, host.user, host.password, cmd, timeout, prompt, expect);
241
		} finally {
171
    }
242
			// Exit the Context. This removes the association between the
Line 172... Line 243...
172
 
243
			// Context and the current thread and is an
173
    @WebMethod(exclude = true)
244
			// essential cleanup action. There should be a call to exit for
174
    private ReturnCode executeCommand(String executable,
245
			// every call to enter.
Line 212... Line 283...
212
 
283
 
213
        return runner.postTask();
284
		return runner.postTask();
Line 214... Line 285...
214
    }
285
	}
215
 
286
 
216
    @WebMethod(exclude = true)
-
 
217
    private ReturnCode sshExec(String host,
-
 
218
            String username,
-
 
219
            String password,
-
 
220
            int port,
287
	@WebMethod(exclude = true)
221
            String command,
288
	private ReturnCode sshExec(String host, String username, String password,
222
            long timeout) {
289
			int port, String command, long timeout) {
223
        SSHExec sshexec = new SSHExec();
290
		SSHExec sshexec = new SSHExec();
224
        RunTask runner = new RunTask(sshexec);
291
		RunTask runner = new RunTask(sshexec);
225
        sshexec.setHost(host);
292
		sshexec.setHost(host);
Line 232... Line 299...
232
        sshexec.setOutputproperty("SSHExec.stdout");
299
		sshexec.setOutputproperty("SSHExec.stdout");
233
        return runner.postTask();
300
		return runner.postTask();
234
    }
301
	}
Line 235... Line 302...
235
 
302
 
236
    @WebMethod(exclude = true)
303
	@WebMethod(exclude = true)
237
    private ReturnCode sshExecWithCert(String host,
-
 
238
            String username,
-
 
239
            String passphrase,
-
 
240
            String keyfile,
-
 
241
            int port,
304
	private ReturnCode sshExecWithCert(String host, String username,
242
            String command,
305
			String passphrase, String keyfile, int port, String command,
243
            long timeout) {
306
			long timeout) {
244
        SSHExec sshexec = new SSHExec();
307
		SSHExec sshexec = new SSHExec();
245
        RunTask runner = new RunTask(sshexec);
308
		RunTask runner = new RunTask(sshexec);
246
        sshexec.setHost(host);
309
		sshexec.setHost(host);
Line 254... Line 317...
254
        sshexec.setOutputproperty("SSHExec.stdout");
317
		sshexec.setOutputproperty("SSHExec.stdout");
255
        return runner.postTask();
318
		return runner.postTask();
256
    }
319
	}
Line 257... Line 320...
257
 
320
 
258
    @WebMethod(exclude = true)
321
	@WebMethod(exclude = true)
259
    private ReturnCode rexec(String host,
-
 
260
            int port,
-
 
261
            String username,
-
 
262
            String password,
-
 
263
            String command,
322
	private ReturnCode rexec(String host, int port, String username,
264
            long timeout) {
323
			String password, String command, long timeout) {
265
        RExecTask rexec = new RExecTask();
324
		RExecTask rexec = new RExecTask();
266
        RunTask runner = new RunTask(rexec);
325
		RunTask runner = new RunTask(rexec);
267
        rexec.setServer(host);
326
		rexec.setServer(host);
268
        rexec.setPort(port);
327
		rexec.setPort(port);
Line 273... Line 332...
273
 
332
 
274
        return runner.postTask();
333
		return runner.postTask();
Line 275... Line 334...
275
    }
334
	}
276
 
335
 
277
    @WebMethod(exclude = true)
-
 
278
    private ReturnCode telnet(String host,
336
	@WebMethod(exclude = true)
279
            int port,
337
	private ReturnCode telnet(String host, int port, String username,
280
            String username,
-
 
281
            String password,
-
 
282
            String command,
338
			String password, String command, long timeout, String prompt,
283
            long timeout, String prompt, String expect) {
339
			String expect) {
284
        TelnetTask rexec = new TelnetTask();
340
		TelnetTask rexec = new TelnetTask();
285
        RunTask runner = new RunTask(rexec);
341
		RunTask runner = new RunTask(rexec);
286
        rexec.setServer(host);
342
		rexec.setServer(host);