CommandLine injector added.

This commit is contained in:
Zhen Tang 2013-06-26 00:39:13 +08:00
parent c999aba8af
commit 57a22f98b6
4 changed files with 120 additions and 3 deletions

View File

@ -6,8 +6,6 @@ public @interface Function {
Class<?> plugin(); Class<?> plugin();
int number();
Parameter[] parameters(); Parameter[] parameters();
String help(); String help();

View File

@ -1,7 +1,7 @@
package org.bench4q.agent.plugin.annotation; package org.bench4q.agent.plugin.annotation;
public @interface Plugin { public @interface Plugin {
String id(); String uuid();
String name(); String name();

View File

@ -0,0 +1,16 @@
package org.bench4q.agent.plugin.commandline;
import java.util.Map;
import org.bench4q.agent.plugin.AbstractPlugin;
import org.bench4q.agent.plugin.annotation.Plugin;
@Plugin(help = "This plug-in supports execution of command lines", uuid = "13b46059-559d-c483-93c1-bd008d92fab8", name = "Command Line Injector", parameters = {})
public class CommandLinePlugin extends AbstractPlugin {
@Override
public void init(Map<String, String> parameters) {
}
}

View File

@ -0,0 +1,103 @@
package org.bench4q.agent.plugin.commandline;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.bench4q.agent.plugin.AbstractSampleFunction;
import org.bench4q.agent.plugin.FunctionResult;
import org.bench4q.agent.plugin.annotation.Function;
import org.bench4q.agent.plugin.annotation.Parameter;
@Function(help = "Executes the provided command line.", name = "Execute", parameters = {
@Parameter(name = "command", type = "String"),
@Parameter(name = "comment", type = "String"),
@Parameter(name = "iteration", type = "String") }, plugin = CommandLinePlugin.class)
public class ExecuteFunction extends AbstractSampleFunction {
private String stderr;
private String stdout;
@Override
public FunctionResult execute(Map<String, String> parameters) {
try {
List<String> commandLine = new ArrayList<String>();
final String commandLineStr = parameters.get("command");
Runtime runtime = Runtime.getRuntime();
if (System.getProperty("os.name").equalsIgnoreCase("linux")) {
commandLine.add("/bin/sh");
commandLine.add("-c");
commandLine.add(commandLineStr);
} else if (System.getProperty("os.name").toLowerCase()
.startsWith("windows")) {
commandLine.add("cmd.exe");
commandLine.add("/c");
commandLine.add("\"" + commandLineStr + "\"");
} else {
}
// report.setDate(System.currentTimeMillis());
final Process process = runtime.exec(commandLine
.toArray(new String[commandLine.size()]));
// standard output reading
new Thread() {
public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String streamline = "";
try {
stdout = "";
while ((streamline = reader.readLine()) != null)
stdout += streamline
+ System.getProperty("line.separator");
} finally {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
// standard error output reading
new Thread() {
public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String streamline = "";
try {
stderr = "";
while ((streamline = reader.readLine()) != null)
stderr += streamline
+ System.getProperty("line.separator");
} finally {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
// wait until the end of command execution
process.waitFor();
// get return code of command execution
int result = process.exitValue();
// retcode = String.valueOf(result);
// fill sample report
// report.duration = (int) (System.currentTimeMillis() -
// report.getDate());
// report.type = "COMMAND LINE";
// report.comment = (String)params.get(SAMPLE_EXECUTE_COMMENT);
// report.iteration =
// Long.parseLong((String)params.get(SAMPLE_EXECUTE_ITERATION));
// report.result = retcode;
// report.successful = (result == 0);
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}