Skip to content

Commit

Permalink
Expose single command execute methods
Browse files Browse the repository at this point in the history
- moved to a working version of jaxws-maven-plugin
  • Loading branch information
bostko committed Jan 15, 2016
1 parent 9475ba2 commit a90b9c4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
43 changes: 39 additions & 4 deletions winrm4j/src/main/java/io/cloudsoft/winrm4j/winrm/WinRmTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,29 @@ private WinRmTool(String address, String username, String password) {
this.password = password;
}

/**
* Execute a list of Windows Native commands as one command.
* The method translates the list of commands to a single String command with a <code>"\r\n"</code> delimiter and a terminating one.
* @deprecated since 0.2; Use the {@link #executeCommand(String)} instead and transform your commands list explicitly
*/
@Deprecated
public WinRmToolResponse executeScript(List<String> commands) {
return executeScript(joinScript(commands));
return executeCommand(joinCommands(commands));
}

/**
* @deprecated will be removed in the next version
*/
public WinRmToolResponse executeScript(String commands) {
return executeCommand(commands);
}

/**
* Executes a Native Windows command.
* It is creating a new Shell on the destination host each time it is being called.
* @since 0.2
*/
public WinRmToolResponse executeCommand(String command) {
Builder builder = WinRmClient.builder(getEndpointUrl());
if (username != null && password != null) {
builder.credentials(username, password);
Expand All @@ -40,7 +58,7 @@ public WinRmToolResponse executeScript(String commands) {
StringWriter err = new StringWriter();

try {
int code = client.command(commands, out, err);
int code = client.command(command, out, err);
return new WinRmToolResponse(out.toString(), err.toString(), code);
} finally {
client.disconnect();
Expand All @@ -58,8 +76,25 @@ private String getEndpointUrl() {
}
}

/**
* Executes a Power Shell command.
* It is creating a new Shell on the destination host each time it is being called.
* @param psCommand
* @since 0.2
*/
public WinRmToolResponse executePs(String psCommand) {
return executeCommand(compilePs(psCommand));
}

/**
* Execute a list of Power Shell commands as one command.
* The method translates the list of commands to a single String command with a <code>"\r\n"</code> delimiter and a terminating one.
* @param commands
* @deprecated since 0.2; Use the {@link #executePs(String)} instead and transform your commands list explicitly
*/
@Deprecated
public WinRmToolResponse executePs(List<String> commands) {
return executeScript(compilePs(joinPs(commands)));
return executeCommand(compilePs(joinPs(commands)));
}

private String compilePs(String psScript) {
Expand All @@ -77,7 +112,7 @@ private String compilePs(String psScript) {
*
* TODO cover the case where \r appears in the command?
*/
private String joinScript(List<String> commands) {
private String joinCommands(List<String> commands) {
return join(commands, "\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ protected void assertExecFails(List<String> cmds) {

protected void assertExecPsFails(String cmd) {
Stopwatch stopwatch = Stopwatch.createStarted();
assertFailed(cmd, executePs(ImmutableList.of(cmd)), stopwatch);
assertFailed(cmd, executePs(cmd), stopwatch);
}

protected void assertExecPsFails(List<String> cmds) {
Stopwatch stopwatch = Stopwatch.createStarted();
assertFailed(cmds, executePs(cmds), stopwatch);
Expand All @@ -89,7 +89,7 @@ protected void assertExecSucceeds(List<String> cmds, String stdout, String stder

protected void assertExecPsSucceeds(String cmd, String stdout, String stderr) {
Stopwatch stopwatch = Stopwatch.createStarted();
assertSucceeded(cmd, executePs(ImmutableList.of(cmd)), stdout, stderr, stopwatch);
assertSucceeded(cmd, executePs(cmd), stdout, stderr, stopwatch);
}

protected void assertExecPsSucceeds(List<String> cmds, String stdout, String stderr) {
Expand Down Expand Up @@ -124,25 +124,29 @@ protected WinRmToolResponse executeScript(final List<String> script) {
}});
}

protected WinRmToolResponse executePs(String script) {
return executePs(ImmutableList.of(script));
}

protected WinRmToolResponse executePs(final List<String> script) {
protected WinRmToolResponse executePs(final String command) {
return callWithRetries(new Callable<WinRmToolResponse>() {
@Override public WinRmToolResponse call() throws Exception {
WinRmTool winRmTool = WinRmTool.connect(VM_HOST + ":" + VM_PORT, VM_USER, VM_PASSWORD);
return winRmTool.executePs(script);
return winRmTool.executePs(command);
}});
}

protected WinRmToolResponse executePs(final WinRmTool winRmTool, final List<String> script) {
protected WinRmToolResponse executePs(final List<String> script) {
return callWithRetries(new Callable<WinRmToolResponse>() {
@Override public WinRmToolResponse call() throws Exception {
WinRmTool winRmTool = WinRmTool.connect(VM_HOST + ":" + VM_PORT, VM_USER, VM_PASSWORD);
return winRmTool.executePs(script);
}});
}

protected WinRmToolResponse executePs(final WinRmTool winRmTool, final String command) {
return callWithRetries(new Callable<WinRmToolResponse>() {
@Override public WinRmToolResponse call() throws Exception {
return winRmTool.executePs(script);
return winRmTool.executePs(command);
}});
}

protected WinRmTool connect() throws Exception {
return callWithRetries(new Callable<WinRmTool>() {
@Override public WinRmTool call() throws Exception {
Expand Down Expand Up @@ -184,9 +188,9 @@ protected void copyTo(InputStream source, String destination) throws Exception {
} else {
chunk = Arrays.copyOf(inputData, bytesRead);
}
executePs(ImmutableList.of("If ((!(Test-Path " + destination + ")) -or ((Get-Item '" + destination + "').length -eq " +
executePs("If ((!(Test-Path " + destination + ")) -or ((Get-Item '" + destination + "').length -eq " +
expectedFileSize + ")) {Add-Content -Encoding Byte -path " + destination +
" -value ([System.Convert]::FromBase64String(\"" + new String(BaseEncoding.base64().encode(chunk)) + "\"))}"));
" -value ([System.Convert]::FromBase64String(\"" + new String(BaseEncoding.base64().encode(chunk)) + "\"))}");
expectedFileSize += bytesRead;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void testExecPsBatchFileExit3() throws Exception {
String scriptPath = "C:\\myscript-"+makeRandomString(8)+".bat";
copyTo(new ByteArrayInputStream(script.getBytes()), scriptPath);

WinRmToolResponse response = executePs(ImmutableList.of("& '"+scriptPath+"'"));
WinRmToolResponse response = executePs("& '"+scriptPath+"'");
String msg = "statusCode="+response.getStatusCode()+"; out="+response.getStdOut()+"; err="+response.getStdErr();
assertEquals(response.getStatusCode(), 3, msg);
}
Expand Down Expand Up @@ -347,7 +347,7 @@ public void testConfirmUseOfErrorActionPreferenceDoesNotCauseErr() throws Except
public void testExecPsExit1() throws Exception {
// Single commands
assertExecPsFails("exit 1");
assertExecPsFails(ImmutableList.of("exit 1"));
assertExecPsFails("exit 1");

// Multi-part
assertExecPsFails(ImmutableList.of(PS_ERR_ACTION_PREF_EQ_STOP, "Write-Host myline", "exit 1"));
Expand All @@ -372,11 +372,11 @@ public void testToolReuse() throws Exception {
WinRmTool winRmTool = connect();

Stopwatch stopwatch = Stopwatch.createStarted();
WinRmToolResponse response = executePs(winRmTool, ImmutableList.of("echo myline"));
WinRmToolResponse response = executePs(winRmTool, "echo myline");
assertSucceeded("echo myline", response, "myline", "", stopwatch);

stopwatch = Stopwatch.createStarted();
WinRmToolResponse response2 = executePs(winRmTool, ImmutableList.of("echo myline"));
WinRmToolResponse response2 = executePs(winRmTool, "echo myline");
assertSucceeded("echo myline", response2, "myline", "", stopwatch);
}

Expand All @@ -397,7 +397,7 @@ public Void call() throws Exception {
String line = "myline" + makeRandomString(8);
Stopwatch stopwatch = Stopwatch.createStarted();
try {
WinRmToolResponse response = executePs(winRmTool, ImmutableList.of("echo " + line));
WinRmToolResponse response = executePs(winRmTool, "echo " + line);
assertSucceeded("echo " + line, response, line, "", stopwatch);
LOG.info("Executed `echo "+line+"` in "+makeTimeStringRounded(stopwatch)+", in thread "+Thread.currentThread()+"; total "+counter.incrementAndGet()+" methods done");
return null;
Expand Down

0 comments on commit a90b9c4

Please sign in to comment.