From a90b9c48974a59d2d62995ca740f99670ddbffe5 Mon Sep 17 00:00:00 2001 From: Valentin Aitken Date: Fri, 9 Oct 2015 17:54:00 +0300 Subject: [PATCH] Expose single command execute methods - moved to a working version of jaxws-maven-plugin --- pom.xml | 2 +- .../io/cloudsoft/winrm4j/winrm/WinRmTool.java | 43 +++++++++++++++++-- .../winrm/AbstractWinRmToolLiveTest.java | 32 ++++++++------ .../winrm4j/winrm/WinRmToolExecLiveTest.java | 10 ++--- 4 files changed, 63 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index ea23320..035ed82 100644 --- a/pom.xml +++ b/pom.xml @@ -138,7 +138,7 @@ org.jvnet.jax-ws-commons jaxws-maven-plugin - 2.3 + 2.2 org.codehaus.mojo diff --git a/winrm4j/src/main/java/io/cloudsoft/winrm4j/winrm/WinRmTool.java b/winrm4j/src/main/java/io/cloudsoft/winrm4j/winrm/WinRmTool.java index 5158935..4f785b7 100644 --- a/winrm4j/src/main/java/io/cloudsoft/winrm4j/winrm/WinRmTool.java +++ b/winrm4j/src/main/java/io/cloudsoft/winrm4j/winrm/WinRmTool.java @@ -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 "\r\n" 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 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); @@ -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(); @@ -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 "\r\n" 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 commands) { - return executeScript(compilePs(joinPs(commands))); + return executeCommand(compilePs(joinPs(commands))); } private String compilePs(String psScript) { @@ -77,7 +112,7 @@ private String compilePs(String psScript) { * * TODO cover the case where \r appears in the command? */ - private String joinScript(List commands) { + private String joinCommands(List commands) { return join(commands, "\n"); } diff --git a/winrm4j/src/test/java/io/cloudsoft/winrm4j/winrm/AbstractWinRmToolLiveTest.java b/winrm4j/src/test/java/io/cloudsoft/winrm4j/winrm/AbstractWinRmToolLiveTest.java index e3bb858..5cd3ef6 100644 --- a/winrm4j/src/test/java/io/cloudsoft/winrm4j/winrm/AbstractWinRmToolLiveTest.java +++ b/winrm4j/src/test/java/io/cloudsoft/winrm4j/winrm/AbstractWinRmToolLiveTest.java @@ -69,9 +69,9 @@ protected void assertExecFails(List 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 cmds) { Stopwatch stopwatch = Stopwatch.createStarted(); assertFailed(cmds, executePs(cmds), stopwatch); @@ -89,7 +89,7 @@ protected void assertExecSucceeds(List 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 cmds, String stdout, String stderr) { @@ -124,25 +124,29 @@ protected WinRmToolResponse executeScript(final List script) { }}); } - protected WinRmToolResponse executePs(String script) { - return executePs(ImmutableList.of(script)); - } - - protected WinRmToolResponse executePs(final List script) { + protected WinRmToolResponse executePs(final String command) { return callWithRetries(new Callable() { @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 script) { + protected WinRmToolResponse executePs(final List script) { + return callWithRetries(new Callable() { + @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() { @Override public WinRmToolResponse call() throws Exception { - return winRmTool.executePs(script); + return winRmTool.executePs(command); }}); } - + protected WinRmTool connect() throws Exception { return callWithRetries(new Callable() { @Override public WinRmTool call() throws Exception { @@ -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; } } diff --git a/winrm4j/src/test/java/io/cloudsoft/winrm4j/winrm/WinRmToolExecLiveTest.java b/winrm4j/src/test/java/io/cloudsoft/winrm4j/winrm/WinRmToolExecLiveTest.java index d515606..57d7e3b 100644 --- a/winrm4j/src/test/java/io/cloudsoft/winrm4j/winrm/WinRmToolExecLiveTest.java +++ b/winrm4j/src/test/java/io/cloudsoft/winrm4j/winrm/WinRmToolExecLiveTest.java @@ -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); } @@ -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")); @@ -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); } @@ -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;