Subversion Repositories XServices

Rev

Rev 70 | Details | Compare with Previous | 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;
70 brianR 20
import java.util.UUID;
21
 
6 brianR 22
import javax.jws.WebMethod;
23
import javax.jws.WebParam;
24
import javax.jws.WebService;
19 brianR 25
import net.brutex.xservices.types.HostConnection;
6 brianR 26
import net.brutex.xservices.types.ReturnCode;
46 brianR 27
import net.brutex.xservices.util.BrutexNamespaces;
70 brianR 28
import net.brutex.xservices.util.JobWrapper;
6 brianR 29
import net.brutex.xservices.util.RunTask;
46 brianR 30
import net.brutex.xservices.ws.ExecuteService;
70 brianR 31
import net.brutex.xservices.ws.XServicesFault;
46 brianR 32
 
6 brianR 33
import org.apache.tools.ant.taskdefs.ExecTask;
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;
70 brianR 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.*;
47
import org.quartz.TriggerUtils;
48
import org.quartz.impl.StdSchedulerFactory;
49
import org.quartz.impl.triggers.SimpleTriggerImpl;
6 brianR 50
 
51
/**
70 brianR 52
 *
6 brianR 53
 * @author Brian Rosenberger, bru@brutex.de
54
 */
70 brianR 55
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.ExecuteService", serviceName = "ExecuteService")
46 brianR 56
public class ExecuteServiceImpl implements ExecuteService {
6 brianR 57
 
70 brianR 58
	/*
59
	 * (non-Javadoc)
60
	 *
61
	 * @see
62
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommand(java.lang.String,
63
	 * java.lang.String, long)
46 brianR 64
	 */
65
	@WebMethod(operationName = "runCommand")
70 brianR 66
	public ReturnCode runCommand(@WebParam(name = "executable") String cmd,
67
			@WebParam(name = "argline") String args,
68
			@WebParam(name = "timeout") long timeout) {
6 brianR 69
 
70 brianR 70
		return executeCommand(cmd, Commandline.translateCommandline(args),
71
				null, false, null, false, true, false, timeout);
72
	}
6 brianR 73
 
70 brianR 74
	/*
75
	 * (non-Javadoc)
76
	 *
77
	 * @see
78
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommandWithArgs(java.lang
79
	 * .String, java.lang.String[], long)
46 brianR 80
	 */
70 brianR 81
 
46 brianR 82
	@WebMethod(operationName = "runCommandWithArgs")
70 brianR 83
	public ReturnCode runCommandWithArgs(
84
			@WebParam(name = "executable") String cmd,
85
			@WebParam(name = "arg") String[] args,
86
			@WebParam(name = "timeout") long timeout) {
6 brianR 87
 
70 brianR 88
		return executeCommand(cmd, args, null, false, null, false, true, false,
89
				timeout);
90
	}
6 brianR 91
 
70 brianR 92
	/*
93
	 * (non-Javadoc)
94
	 *
95
	 * @see
96
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommandAsync(java.lang
97
	 * .String, java.lang.String)
46 brianR 98
	 */
70 brianR 99
 
46 brianR 100
	@WebMethod(operationName = "runCommandAsync")
70 brianR 101
	public ReturnCode runCommandAsync(
102
			@WebParam(name = "executable") String cmd,
103
			@WebParam(name = "argline") String args) {
6 brianR 104
 
70 brianR 105
		return executeCommand(cmd, Commandline.translateCommandline(args),
106
				null, true, null, false, true, false, 0);
107
	}
6 brianR 108
 
70 brianR 109
	/*
110
	 * (non-Javadoc)
111
	 *
112
	 * @see
113
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommandAsyncWithArgs(java
114
	 * .lang.String, java.lang.String[])
46 brianR 115
	 */
70 brianR 116
 
46 brianR 117
	@WebMethod(operationName = "runCommandAsyncWithArgs")
70 brianR 118
	public ReturnCode runCommandAsyncWithArgs(
119
			@WebParam(name = "executable") String cmd,
120
			@WebParam(name = "arg") String[] args) {
6 brianR 121
 
70 brianR 122
		return executeCommand(cmd, args, null, true, null, false, true, false,
123
				0);
124
	}
6 brianR 125
 
70 brianR 126
	/*
127
	 * (non-Javadoc)
128
	 *
129
	 * @see
130
	 * net.brutex.xservices.ws.impl.ExecuteService#runCommandWithSSH(java.lang
131
	 * .String, int, java.lang.String, java.lang.String, java.lang.String, long)
46 brianR 132
	 */
70 brianR 133
 
46 brianR 134
	@WebMethod(operationName = "runCommandWithSSH")
70 brianR 135
	public ReturnCode runCommandWithSSH(
136
			@WebParam(name = "host") HostConnection host,
137
			@WebParam(name = "command") String cmd,
138
			@WebParam(name = "timeout") long timeout) {
6 brianR 139
 
70 brianR 140
		return sshExec(host.hostname, host.user, host.password, host.port, cmd,
141
				timeout);
142
	}
6 brianR 143
 
70 brianR 144
	/*
145
	 * (non-Javadoc)
146
	 *
147
	 * @see
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)
46 brianR 151
	 */
70 brianR 152
 
46 brianR 153
	@WebMethod(operationName = "runCommandWithSSHKeyAuth")
70 brianR 154
	public ReturnCode runCommandWithSSHKeyAuth(
155
			@WebParam(name = "host") HostConnection host,
156
			@WebParam(name = "keyfile") String keyfile,
157
			@WebParam(name = "command") String cmd,
158
			@WebParam(name = "timeout") long timeout) {
6 brianR 159
 
70 brianR 160
		return sshExecWithCert(host.hostname, host.user, host.password,
161
				keyfile, host.port, cmd, timeout);
162
	}
6 brianR 163
 
70 brianR 164
	/*
165
	 * (non-Javadoc)
166
	 *
167
	 * @see
168
	 * net.brutex.xservices.ws.impl.ExecuteService#rExec(net.brutex.xservices
169
	 * .types.HostConnection, java.lang.String, long)
46 brianR 170
	 */
70 brianR 171
 
46 brianR 172
	@WebMethod(operationName = "rExec")
70 brianR 173
	public ReturnCode rExec(@WebParam(name = "host") HostConnection host,
174
			@WebParam(name = "command") String cmd,
175
			@WebParam(name = "timeout") long timeout) {
176
		return rexec(host.hostname, host.port, host.user, host.password, cmd,
177
				timeout);
178
	}
6 brianR 179
 
70 brianR 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)
46 brianR 187
	 */
70 brianR 188
 
46 brianR 189
	@WebMethod(operationName = "telnet")
70 brianR 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
	}
6 brianR 198
 
70 brianR 199
 
200
	public void runJScript(String script) throws XServicesFault {
12 brianR 201
 
70 brianR 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
			 */
12 brianR 219
 
70 brianR 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);
6 brianR 230
 
70 brianR 231
			// Execute the script
232
			// cx.evaluateString(scope, "importClass('java.lang.System');\n",
233
			// "head", 1, null);
234
			// cx.evaluateString(scope, "importPackage('java.util');\n", "head",
235
			// 2, null);
236
			Object obj = cx
237
					.evaluateString(scope, script, "TestScript", 1, null);
6 brianR 238
 
70 brianR 239
		} catch (Exception e) {
240
			System.out.println(e.getMessage());
241
		} finally {
242
			// Exit the Context. This removes the association between the
243
			// Context and the current thread and is an
244
			// essential cleanup action. There should be a call to exit for
245
			// every call to enter.
246
			Context.exit();
247
		}
6 brianR 248
 
70 brianR 249
	}
6 brianR 250
 
70 brianR 251
	@WebMethod(exclude = true)
252
	private ReturnCode executeCommand(String executable, String[] args,
253
			File dir, boolean spawn, String inputstring,
254
			boolean newenvironment, boolean vmlauncher, boolean searchpath,
255
			long timeout) {
256
		ExecTask exe = new ExecTask();
257
		RunTask runner = new RunTask(exe);
6 brianR 258
 
70 brianR 259
		/*
260
		 * Commandline cmdl = new Commandline(); cmdl.setExecutable(executable);
261
		 * cmdl.addArguments(args); System.out.println(cmdl.describeCommand());
262
		 */
6 brianR 263
 
70 brianR 264
		exe.setExecutable(executable);
265
		for (String s : args) {
266
			exe.createArg().setValue(s);
267
		}
6 brianR 268
 
70 brianR 269
		exe.setDir(dir);
270
		if (spawn) {
271
			exe.setSpawn(spawn);
272
		} else {
273
			exe.setTimeout(timeout);
274
			exe.setInputString(inputstring);
275
			exe.setOutputproperty("ExecuteService.stdout");
276
			exe.setErrorProperty("ExecuteService.stderr");
277
			exe.setResultProperty("ExecuteService.result");
278
		}
6 brianR 279
 
70 brianR 280
		exe.setNewenvironment(newenvironment);
281
		exe.setVMLauncher(vmlauncher);
282
		exe.setSearchPath(searchpath);
283
 
284
		return runner.postTask();
285
	}
286
 
287
	@WebMethod(exclude = true)
288
	private ReturnCode sshExec(String host, String username, String password,
289
			int port, String command, long timeout) {
290
		SSHExec sshexec = new SSHExec();
291
		RunTask runner = new RunTask(sshexec);
292
		sshexec.setHost(host);
293
		sshexec.setUsername(username);
294
		sshexec.setPassword(password);
295
		sshexec.setPort(port);
296
		sshexec.setCommand(command);
297
		sshexec.setTrust(true);
298
		sshexec.setTimeout(timeout);
299
		sshexec.setOutputproperty("SSHExec.stdout");
300
		return runner.postTask();
301
	}
302
 
303
	@WebMethod(exclude = true)
304
	private ReturnCode sshExecWithCert(String host, String username,
305
			String passphrase, String keyfile, int port, String command,
306
			long timeout) {
307
		SSHExec sshexec = new SSHExec();
308
		RunTask runner = new RunTask(sshexec);
309
		sshexec.setHost(host);
310
		sshexec.setUsername(username);
311
		sshexec.setKeyfile(keyfile);
312
		sshexec.setPassphrase(passphrase);
313
		sshexec.setPort(port);
314
		sshexec.setCommand(command);
315
		sshexec.setTrust(true);
316
		sshexec.setTimeout(timeout);
317
		sshexec.setOutputproperty("SSHExec.stdout");
318
		return runner.postTask();
319
	}
320
 
321
	@WebMethod(exclude = true)
322
	private ReturnCode rexec(String host, int port, String username,
323
			String password, String command, long timeout) {
324
		RExecTask rexec = new RExecTask();
325
		RunTask runner = new RunTask(rexec);
326
		rexec.setServer(host);
327
		rexec.setPort(port);
328
		rexec.setUserid(username);
329
		rexec.setPassword(password);
330
		rexec.setCommand(command);
331
		rexec.setTimeout((int) Math.round(timeout));
332
 
333
		return runner.postTask();
334
	}
335
 
336
	@WebMethod(exclude = true)
337
	private ReturnCode telnet(String host, int port, String username,
338
			String password, String command, long timeout, String prompt,
339
			String expect) {
340
		TelnetTask rexec = new TelnetTask();
341
		RunTask runner = new RunTask(rexec);
342
		rexec.setServer(host);
343
		rexec.setPort(port);
344
		rexec.setUserid(username);
345
		rexec.setPassword(password);
346
		rexec.setTimeout((int) Math.round(timeout));
347
 
348
		rexec.createRead().addText(prompt);
349
		rexec.createWrite().addText(command);
350
		rexec.createRead().addText(expect);
351
 
352
		return runner.postTask();
353
	}
6 brianR 354
}