From 77b4c5b9745bf211ceadad1e8fb6328f6e74a29d Mon Sep 17 00:00:00 2001 From: hierynomus Date: Mon, 3 Nov 2014 16:39:53 +0100 Subject: [PATCH] Removed compile time dependency on guava --- build.gradle | 7 +- gradle/wrapper/gradle-wrapper.properties | 2 +- .../java/com/xebialabs/overthere/CmdLine.java | 22 +++-- .../xebialabs/overthere/CmdLineArgument.java | 2 +- .../overthere/ConnectionOptions.java | 20 +++-- .../overthere/cifs/CifsConnectionBuilder.java | 4 +- .../xebialabs/overthere/cifs/CifsFile.java | 4 +- .../xebialabs/overthere/cifs/PathMapper.java | 53 ++++++------ .../cifs/telnet/CifsTelnetConnection.java | 4 +- .../cifs/winrm/CifsWinRmConnection.java | 4 +- .../overthere/cifs/winrm/soap/OptionSet.java | 13 +-- .../cifs/winrs/CifsWinrsConnection.java | 4 +- .../overthere/local/LocalConnection.java | 4 +- .../xebialabs/overthere/local/LocalFile.java | 4 +- .../spi/BaseOverthereConnection.java | 9 +- .../overthere/spi/BaseOverthereFile.java | 2 +- .../overthere/ssh/SshConnection.java | 9 +- .../ssh/SshElevatedUserConnection.java | 4 +- .../com/xebialabs/overthere/ssh/SshFile.java | 34 ++++---- .../xebialabs/overthere/ssh/SshProcess.java | 2 +- .../overthere/ssh/SshScpConnection.java | 2 +- .../xebialabs/overthere/ssh/SshScpFile.java | 13 +-- .../overthere/ssh/SshSftpConnection.java | 2 +- .../ssh/SshSftpCygwinConnection.java | 2 +- .../xebialabs/overthere/ssh/SshSftpFile.java | 30 +++---- .../overthere/ssh/SshSftpUnixConnection.java | 2 +- .../ssh/SshSftpWinSshdConnection.java | 2 +- .../overthere/ssh/SshTunnelConnection.java | 27 +++--- .../overthere/util/ByteArrayFile.java | 4 +- ...turingOverthereExecutionOutputHandler.java | 12 +-- ...apturingOverthereProcessOutputHandler.java | 14 +-- .../overthere/util/OverthereFileCopier.java | 41 +++++---- .../overthere/util/OverthereUtils.java | 85 ++++++++++++++----- .../overthere/UnixCloudHostListener.java | 2 +- .../overthere/WindowsCloudHostListener.java | 2 +- 35 files changed, 241 insertions(+), 206 deletions(-) diff --git a/build.gradle b/build.gradle index 82769ff2..6d19a290 100644 --- a/build.gradle +++ b/build.gradle @@ -47,7 +47,6 @@ configurations { dependencies { // General dependencies - compile 'com.google.guava:guava:16.0.1' compile 'nl.javadude.scannit:scannit:1.2.1' compile 'org.slf4j:slf4j-api:1.6.3' compile 'org.slf4j:jcl-over-slf4j:1.6.3' @@ -88,6 +87,8 @@ dependencies { testCompile 'org.mockito:mockito-core:1.8.5' testCompile 'org.testng:testng:5.14.10' testCompile 'nl.javadude.assumeng:assumeng:1.2.2' + testCompile 'com.google.guava:guava:16.0.1' + testRuntime 'ch.qos.logback:logback-classic:1.0.6' testRuntime('org.uncommons:reportng:1.1.2') { @@ -113,8 +114,8 @@ task itest(type: Test) { } includes = ['**/*Itest.*', '**/LocalConnectionTest.*'] - testResultsDir = file("${buildDir}/itest-results") - testReportDir = file("${buildDir}/reports/itests") + reports.junitXml.destination = file("${buildDir}/itest-results") + reports.html.destination = file("${buildDir}/reports/itests") maxHeapSize = "512m" copyProjectPropertyToSystemProperty(project, systemProperties, 'itests') diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e43a90dc..ed639640 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-bin.zip +distributionUrl=http\://services.gradle.org/distributions/gradle-2.1-bin.zip diff --git a/src/main/java/com/xebialabs/overthere/CmdLine.java b/src/main/java/com/xebialabs/overthere/CmdLine.java index 83520d01..85b29ace 100644 --- a/src/main/java/com/xebialabs/overthere/CmdLine.java +++ b/src/main/java/com/xebialabs/overthere/CmdLine.java @@ -24,13 +24,11 @@ import java.io.Serializable; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.List; -import com.google.common.base.Function; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; -import static com.google.common.collect.Lists.newArrayList; -import static com.google.common.collect.Lists.transform; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; +import static com.xebialabs.overthere.util.OverthereUtils.checkState; import static com.xebialabs.overthere.CmdLineArgument.arg; import static com.xebialabs.overthere.CmdLineArgument.nested; import static com.xebialabs.overthere.CmdLineArgument.password; @@ -44,7 +42,7 @@ @SuppressWarnings("serial") public class CmdLine implements Serializable { - List arguments = newArrayList(); + List arguments = new ArrayList(); /** * Adds {@link CmdLineArgument#arg(String) a regular argument} to the command line. @@ -154,12 +152,12 @@ public List getArguments() { */ public String[] toCommandArray(final OperatingSystemFamily os, final boolean forLogging) { checkState(arguments.size() > 0, "Cannot encode empty command line"); - return transform(arguments, new Function() { - @Override - public String apply(CmdLineArgument from) { - return from.toString(os, forLogging); - } - }).toArray(new String[arguments.size()]); + String[] args = new String[arguments.size()]; + for (int i = 0; i < arguments.size(); i++) { + args[i] = arguments.get(i).toString(os, forLogging); + + } + return args; } /** diff --git a/src/main/java/com/xebialabs/overthere/CmdLineArgument.java b/src/main/java/com/xebialabs/overthere/CmdLineArgument.java index 633dc1b0..f03786ab 100644 --- a/src/main/java/com/xebialabs/overthere/CmdLineArgument.java +++ b/src/main/java/com/xebialabs/overthere/CmdLineArgument.java @@ -24,7 +24,7 @@ import java.io.Serializable; -import static com.google.common.base.Preconditions.checkNotNull; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; import static com.xebialabs.overthere.OperatingSystemFamily.UNIX; /** diff --git a/src/main/java/com/xebialabs/overthere/ConnectionOptions.java b/src/main/java/com/xebialabs/overthere/ConnectionOptions.java index 2e504831..37692537 100644 --- a/src/main/java/com/xebialabs/overthere/ConnectionOptions.java +++ b/src/main/java/com/xebialabs/overthere/ConnectionOptions.java @@ -22,12 +22,8 @@ */ package com.xebialabs.overthere; -import java.util.Map; -import java.util.Set; +import java.util.*; -import com.google.common.collect.ImmutableSet; - -import static com.google.common.collect.Maps.newHashMap; import static com.xebialabs.overthere.ssh.SshConnectionBuilder.PASSPHRASE; /** @@ -172,20 +168,28 @@ public class ConnectionOptions { private final Map options; - private static final ImmutableSet filteredKeys = ImmutableSet.of(PASSWORD, PASSPHRASE); + private static final Set filteredKeys = getFilteredKeys(); + + private static Set getFilteredKeys() { + HashSet strings = new HashSet(); + strings.add(PASSWORD); + strings.add(PASSPHRASE); + return Collections.unmodifiableSet(strings); + } /** * Creates an empty options object. */ public ConnectionOptions() { - options = newHashMap(); + options = new HashMap(); } /** * Creates a copy of an existing options object. */ public ConnectionOptions(ConnectionOptions options) { - this.options = newHashMap(options.options); + this(); + this.options.putAll(options.options); } /** diff --git a/src/main/java/com/xebialabs/overthere/cifs/CifsConnectionBuilder.java b/src/main/java/com/xebialabs/overthere/cifs/CifsConnectionBuilder.java index fd734e90..1266ca2d 100644 --- a/src/main/java/com/xebialabs/overthere/cifs/CifsConnectionBuilder.java +++ b/src/main/java/com/xebialabs/overthere/cifs/CifsConnectionBuilder.java @@ -22,8 +22,8 @@ */ package com.xebialabs.overthere.cifs; +import java.util.Collections; import java.util.Map; -import com.google.common.collect.ImmutableMap; import com.xebialabs.overthere.ConnectionOptions; import com.xebialabs.overthere.OverthereConnection; @@ -88,7 +88,7 @@ public class CifsConnectionBuilder implements OverthereConnectionBuilder { /** * See the online documentation */ - public static final Map PATH_SHARE_MAPPINGS_DEFAULT = ImmutableMap.of(); + public static final Map PATH_SHARE_MAPPINGS_DEFAULT = Collections.emptyMap(); /** * See the online documentation diff --git a/src/main/java/com/xebialabs/overthere/cifs/CifsFile.java b/src/main/java/com/xebialabs/overthere/cifs/CifsFile.java index 7bdeec8f..66373624 100644 --- a/src/main/java/com/xebialabs/overthere/cifs/CifsFile.java +++ b/src/main/java/com/xebialabs/overthere/cifs/CifsFile.java @@ -26,6 +26,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; +import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +38,6 @@ import jcifs.smb.SmbException; import jcifs.smb.SmbFile; -import static com.google.common.collect.Lists.newArrayList; import static java.lang.String.format; class CifsFile extends BaseOverthereFile { @@ -177,7 +177,7 @@ public List listFiles() throws RuntimeIOException { try { upgradeToDirectorySmbFile(); - List files = newArrayList(); + List files = new ArrayList(); for (String name : smbFile.list()) { files.add(getFile(name)); } diff --git a/src/main/java/com/xebialabs/overthere/cifs/PathMapper.java b/src/main/java/com/xebialabs/overthere/cifs/PathMapper.java index 9c2ed1c0..c5d2ce93 100644 --- a/src/main/java/com/xebialabs/overthere/cifs/PathMapper.java +++ b/src/main/java/com/xebialabs/overthere/cifs/PathMapper.java @@ -22,16 +22,11 @@ */ package com.xebialabs.overthere.cifs; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.SortedMap; import java.util.regex.Pattern; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Predicate; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSortedMap; -import com.google.common.collect.Iterables; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; import static java.lang.String.format; import static java.util.regex.Pattern.quote; @@ -43,19 +38,24 @@ class PathMapper { private final SortedMap sharesForPaths; private final Map pathsForShares; - @VisibleForTesting PathMapper(final Map mappings) { // longest first, so reverse lexicographical order - ImmutableSortedMap.Builder sharesForPath = ImmutableSortedMap.reverseOrder(); - ImmutableMap.Builder pathsForShare = ImmutableMap.builder(); + SortedMap sharesForPath = new TreeMap(new Comparator() { + @Override + public int compare(String o1, String o2) { + return o2.compareTo(o1); + } + }); + Map pathsForShare = new HashMap(); + for (Entry mapping : mappings.entrySet()) { String pathPrefixToMatch = mapping.getKey(); String shareForPathPrefix = mapping.getValue(); - sharesForPath.put(pathPrefixToMatch.toLowerCase(), shareForPathPrefix); - pathsForShare.put(shareForPathPrefix.toLowerCase(), pathPrefixToMatch); + checkArgument(sharesForPath.put(pathPrefixToMatch.toLowerCase(), shareForPathPrefix) == null, "path prefix is not unique in mapping %s -> %s", pathPrefixToMatch, shareForPathPrefix); + checkArgument(pathsForShare.put(shareForPathPrefix.toLowerCase(), pathPrefixToMatch) == null, "share is not unique in mapping %s -> %s", shareForPathPrefix, pathPrefixToMatch); } - this.sharesForPaths = sharesForPath.build(); - this.pathsForShares = pathsForShare.build(); + this.sharesForPaths = Collections.unmodifiableSortedMap(sharesForPath); + this.pathsForShares = Collections.unmodifiableMap(pathsForShare); } /** @@ -67,16 +67,16 @@ class PathMapper { * @param path the local path to convert * @return the remotely accessible path (using shares) at which the local path can be accessed using SMB */ - @VisibleForTesting String toSharedPath(String path) { final String lowerCasePath = path.toLowerCase(); // assumes correct format drive: or drive:\path - String mappedPathPrefix = Iterables.find(sharesForPaths.keySet(), new Predicate() { - @Override - public boolean apply(String input) { - return lowerCasePath.startsWith(input); + String mappedPathPrefix = null; + for (String s : sharesForPaths.keySet()) { + if (lowerCasePath.startsWith(s)) { + mappedPathPrefix = s; + break; } - }, null); + } // the share + the remainder of the path if found, otherwise the path with ':' replaced by '$' return ((mappedPathPrefix != null) ? sharesForPaths.get(mappedPathPrefix) + path.substring(mappedPathPrefix.length()) : path.substring(0, 1) + ADMIN_SHARE_DESIGNATOR @@ -87,16 +87,17 @@ public boolean apply(String input) { * @param path the remotely accessible path to convert (minus the host name, i.e. beginning with the share) * @return the local path (using drive letters) corresponding to the path that is remotely accessible using SMB */ - @VisibleForTesting String toLocalPath(String path) { final String lowerCasePath = path.toLowerCase(); // assumes correct format share or share\path - String mappedShare = Iterables.find(pathsForShares.keySet(), new Predicate() { - @Override - public boolean apply(String input) { - return lowerCasePath.startsWith(input); + String mappedShare = null; + for (String s : pathsForShares.keySet()) { + if (lowerCasePath.startsWith(s)) { + mappedShare = s; + break; } - }, null); + } + if (mappedShare != null) { return pathsForShares.get(mappedShare) + path.substring(mappedShare.length()); } else if ((path.length() >= 2) && ADMIN_SHARE_PATTERN.matcher(path.substring(0, 2)).matches()) { diff --git a/src/main/java/com/xebialabs/overthere/cifs/telnet/CifsTelnetConnection.java b/src/main/java/com/xebialabs/overthere/cifs/telnet/CifsTelnetConnection.java index 37919dc1..5e6d1455 100644 --- a/src/main/java/com/xebialabs/overthere/cifs/telnet/CifsTelnetConnection.java +++ b/src/main/java/com/xebialabs/overthere/cifs/telnet/CifsTelnetConnection.java @@ -43,8 +43,8 @@ import com.xebialabs.overthere.cifs.CifsConnection; import com.xebialabs.overthere.spi.AddressPortMapper; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS; import static com.xebialabs.overthere.cifs.CifsConnectionBuilder.CIFS_PROTOCOL; import static com.xebialabs.overthere.util.OverthereUtils.closeQuietly; diff --git a/src/main/java/com/xebialabs/overthere/cifs/winrm/CifsWinRmConnection.java b/src/main/java/com/xebialabs/overthere/cifs/winrm/CifsWinRmConnection.java index cd53b40d..bc633d79 100644 --- a/src/main/java/com/xebialabs/overthere/cifs/winrm/CifsWinRmConnection.java +++ b/src/main/java/com/xebialabs/overthere/cifs/winrm/CifsWinRmConnection.java @@ -42,8 +42,8 @@ import com.xebialabs.overthere.cifs.WinrmHttpsHostnameVerificationStrategy; import com.xebialabs.overthere.spi.AddressPortMapper; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS; import static com.xebialabs.overthere.cifs.CifsConnectionBuilder.CIFS_PROTOCOL; import static com.xebialabs.overthere.cifs.CifsConnectionBuilder.WINRM_CONTEXT_DEFAULT; diff --git a/src/main/java/com/xebialabs/overthere/cifs/winrm/soap/OptionSet.java b/src/main/java/com/xebialabs/overthere/cifs/winrm/soap/OptionSet.java index 010195ba..9a1a908f 100644 --- a/src/main/java/com/xebialabs/overthere/cifs/winrm/soap/OptionSet.java +++ b/src/main/java/com/xebialabs/overthere/cifs/winrm/soap/OptionSet.java @@ -22,20 +22,23 @@ */ package com.xebialabs.overthere.cifs.winrm.soap; +import java.util.ArrayList; import java.util.List; -import com.google.common.collect.Lists; /** */ public enum OptionSet { - OPEN_SHELL(Lists.newArrayList(new KeyValuePair("WINRS_NOPROFILE", "FALSE"), new KeyValuePair("WINRS_CODEPAGE", "437"))), - RUN_COMMAND(Lists.newArrayList(new KeyValuePair("WINRS_CONSOLEMODE_STDIN", "TRUE"))); + OPEN_SHELL(new KeyValuePair("WINRS_NOPROFILE", "FALSE"), new KeyValuePair("WINRS_CODEPAGE", "437")), + RUN_COMMAND(new KeyValuePair("WINRS_CONSOLEMODE_STDIN", "TRUE")); private final List keyValuePairs; - OptionSet(List keyValuePairs) { - this.keyValuePairs = keyValuePairs; + OptionSet(KeyValuePair... keyValuePairs) { + this.keyValuePairs = new ArrayList(); + for (KeyValuePair keyValuePair : keyValuePairs) { + this.keyValuePairs.add(keyValuePair); + } } public List getKeyValuePairs() { diff --git a/src/main/java/com/xebialabs/overthere/cifs/winrs/CifsWinrsConnection.java b/src/main/java/com/xebialabs/overthere/cifs/winrs/CifsWinrsConnection.java index 610d3df2..628f7495 100755 --- a/src/main/java/com/xebialabs/overthere/cifs/winrs/CifsWinrsConnection.java +++ b/src/main/java/com/xebialabs/overthere/cifs/winrs/CifsWinrsConnection.java @@ -34,8 +34,8 @@ import com.xebialabs.overthere.spi.AddressPortMapper; import com.xebialabs.overthere.util.DefaultAddressPortMapper; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS; import static com.xebialabs.overthere.cifs.CifsConnectionBuilder.CIFS_PROTOCOL; import static com.xebialabs.overthere.cifs.CifsConnectionBuilder.WINRM_ENABLE_HTTPS_DEFAULT; diff --git a/src/main/java/com/xebialabs/overthere/local/LocalConnection.java b/src/main/java/com/xebialabs/overthere/local/LocalConnection.java index d03cd398..b8a3be26 100644 --- a/src/main/java/com/xebialabs/overthere/local/LocalConnection.java +++ b/src/main/java/com/xebialabs/overthere/local/LocalConnection.java @@ -40,8 +40,8 @@ import com.xebialabs.overthere.spi.Protocol; import com.xebialabs.overthere.util.DefaultAddressPortMapper; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; import static com.xebialabs.overthere.ConnectionOptions.OPERATING_SYSTEM; import static com.xebialabs.overthere.ConnectionOptions.TEMPORARY_DIRECTORY_PATH; import static com.xebialabs.overthere.OperatingSystemFamily.getLocalHostOperatingSystemFamily; diff --git a/src/main/java/com/xebialabs/overthere/local/LocalFile.java b/src/main/java/com/xebialabs/overthere/local/LocalFile.java index bff6645a..968988bd 100644 --- a/src/main/java/com/xebialabs/overthere/local/LocalFile.java +++ b/src/main/java/com/xebialabs/overthere/local/LocalFile.java @@ -29,6 +29,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; +import java.util.ArrayList; import java.util.List; import com.xebialabs.overthere.ConnectionOptions; @@ -36,7 +37,6 @@ import com.xebialabs.overthere.RuntimeIOException; import com.xebialabs.overthere.spi.BaseOverthereFile; -import static com.google.common.collect.Lists.newArrayList; import static com.xebialabs.overthere.local.LocalConnection.LOCAL_PROTOCOL; /** @@ -162,7 +162,7 @@ public void mkdirs() { @Override public List listFiles() { - List list = newArrayList(); + List list = new ArrayList(); for (File each : file.listFiles()) { list.add(new LocalFile(connection, each)); } diff --git a/src/main/java/com/xebialabs/overthere/spi/BaseOverthereConnection.java b/src/main/java/com/xebialabs/overthere/spi/BaseOverthereConnection.java index eafe19dd..95b4765e 100644 --- a/src/main/java/com/xebialabs/overthere/spi/BaseOverthereConnection.java +++ b/src/main/java/com/xebialabs/overthere/spi/BaseOverthereConnection.java @@ -25,6 +25,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; @@ -42,9 +43,7 @@ import com.xebialabs.overthere.OverthereProcessOutputHandler; import com.xebialabs.overthere.RuntimeIOException; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Strings.isNullOrEmpty; -import static com.google.common.collect.Lists.newArrayList; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; import static com.xebialabs.overthere.ConnectionOptions.*; import static com.xebialabs.overthere.util.ConsoleOverthereExecutionOutputHandler.syserrHandler; import static com.xebialabs.overthere.util.ConsoleOverthereExecutionOutputHandler.sysoutHandler; @@ -72,7 +71,7 @@ public abstract class BaseOverthereConnection implements OverthereConnection { protected final boolean deleteTemporaryDirectoryOnDisconnect; protected final int temporaryFileCreationRetries; protected final String temporaryFileHolderDirectoryNamePrefix; - protected final List temporaryFileHolderDirectories = newArrayList(); + protected final List temporaryFileHolderDirectories = new ArrayList(); protected final int streamBufferSize; protected int temporaryFileHolderDirectoryNameSuffix = 0; protected OverthereFile workingDirectory; @@ -167,7 +166,7 @@ public final OverthereFile getTempFile(String prefix, String suffix) throws Runt */ @Override public final synchronized OverthereFile getTempFile(String name) { - if (isNullOrEmpty(name)) { + if (name == null || name.trim().isEmpty()) { name = "tmp"; } diff --git a/src/main/java/com/xebialabs/overthere/spi/BaseOverthereFile.java b/src/main/java/com/xebialabs/overthere/spi/BaseOverthereFile.java index 291ab57b..5aca1445 100644 --- a/src/main/java/com/xebialabs/overthere/spi/BaseOverthereFile.java +++ b/src/main/java/com/xebialabs/overthere/spi/BaseOverthereFile.java @@ -35,7 +35,7 @@ import com.xebialabs.overthere.util.CapturingOverthereExecutionOutputHandler; import com.xebialabs.overthere.util.OverthereFileCopier; -import static com.google.common.base.Preconditions.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; import static com.xebialabs.overthere.ConnectionOptions.DIRECTORY_COPY_COMMAND_FOR_UNIX; import static com.xebialabs.overthere.ConnectionOptions.DIRECTORY_COPY_COMMAND_FOR_UNIX_DEFAULT; import static com.xebialabs.overthere.ConnectionOptions.DIRECTORY_COPY_COMMAND_FOR_WINDOWS; diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshConnection.java index 118230a5..e7659ac7 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshConnection.java @@ -48,8 +48,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.annotations.VisibleForTesting; - import com.xebialabs.overthere.CmdLine; import com.xebialabs.overthere.CmdLineArgument; import com.xebialabs.overthere.ConnectionOptions; @@ -59,9 +57,9 @@ import com.xebialabs.overthere.spi.AddressPortMapper; import com.xebialabs.overthere.spi.BaseOverthereConnection; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; +import static com.xebialabs.overthere.util.OverthereUtils.checkState; import static com.xebialabs.overthere.ConnectionOptions.ADDRESS; import static com.xebialabs.overthere.ConnectionOptions.PASSWORD; import static com.xebialabs.overthere.ConnectionOptions.PORT; @@ -125,7 +123,6 @@ abstract class SshConnection extends BaseOverthereConnection { private static final Config config = new DefaultConfig(); - @VisibleForTesting protected Factory sshClientFactory = new Factory() { @Override public SSHClient create() { diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshElevatedUserConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshElevatedUserConnection.java index 197d2bd0..21cb1216 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshElevatedUserConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshElevatedUserConnection.java @@ -25,7 +25,6 @@ import java.io.InputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.annotations.VisibleForTesting; import com.xebialabs.overthere.CmdLine; import com.xebialabs.overthere.CmdLineArgument; @@ -38,7 +37,7 @@ import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.transport.TransportException; -import static com.google.common.base.Preconditions.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; import static com.xebialabs.overthere.util.OverthereUtils.constructPath; /** @@ -119,7 +118,6 @@ protected CmdLine processCommandLine(final CmdLine cmd) { return processedCmd; } - @VisibleForTesting CmdLine prefixWithElevationCommand(final CmdLine commandLine) { CmdLine commandLineWithSudo = new CmdLine(); if (quoteCommand) { diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshFile.java b/src/main/java/com/xebialabs/overthere/ssh/SshFile.java index a62176da..fa0b712d 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshFile.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshFile.java @@ -22,20 +22,15 @@ */ package com.xebialabs.overthere.ssh; -import java.util.List; -import com.google.common.base.CharMatcher; -import com.google.common.base.Joiner; -import com.google.common.base.Splitter; - -import com.xebialabs.overthere.CmdLine; -import com.xebialabs.overthere.OperatingSystemFamily; -import com.xebialabs.overthere.OverthereExecutionOutputHandler; -import com.xebialabs.overthere.OverthereFile; -import com.xebialabs.overthere.RuntimeIOException; +import com.xebialabs.overthere.*; import com.xebialabs.overthere.spi.BaseOverthereFile; -import static com.google.common.collect.Lists.newArrayList; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS; +import static com.xebialabs.overthere.util.OverthereUtils.mkString; /** * A file on a host connected through SSH. @@ -138,8 +133,13 @@ public String toString() { } static List splitPath(String path, OperatingSystemFamily os) { - Splitter s = os == WINDOWS ? WINDOWS_PATH_SPLITTER : UNIX_PATH_SPLITTER; - return newArrayList(s.split(path)); + Pattern s = os == WINDOWS ? WINDOWS_PATH_SPLITTER : UNIX_PATH_SPLITTER; + List l = new ArrayList(); + for (String p : s.split(path)) { + if (p.isEmpty()) continue; + l.add(p); + } + return l; } static String joinPath(List pathComponents, OperatingSystemFamily os) { @@ -150,13 +150,13 @@ static String joinPath(List pathComponents, OperatingSystemFamily os) { } if (os == WINDOWS) { - String path = Joiner.on(fileSep).join(pathComponents); + String path = mkString(pathComponents, fileSep); if (pathComponents.size() == 1) { path += fileSep; } return path; } else { - return fileSep + Joiner.on(fileSep).join(pathComponents); + return fileSep + mkString(pathComponents, fileSep); } } @@ -166,8 +166,8 @@ static void checkWindowsPath(List pathComponents, OperatingSystemFamily } } - private static final Splitter UNIX_PATH_SPLITTER = Splitter.on('/').omitEmptyStrings(); + private static final Pattern UNIX_PATH_SPLITTER = Pattern.compile("/"); - private static final Splitter WINDOWS_PATH_SPLITTER = Splitter.on(CharMatcher.anyOf("/\\")).omitEmptyStrings(); + private static final Pattern WINDOWS_PATH_SPLITTER = Pattern.compile("[/\\\\]"); } diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshProcess.java b/src/main/java/com/xebialabs/overthere/ssh/SshProcess.java index afe068dd..c4627131 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshProcess.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshProcess.java @@ -38,7 +38,7 @@ import net.schmizz.sshj.connection.channel.direct.Signal; import net.schmizz.sshj.transport.TransportException; -import static com.google.common.base.Preconditions.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; import static java.lang.String.format; class SshProcess implements OverthereProcess { diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshScpConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshScpConnection.java index c8c60f9a..f7c3d50a 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshScpConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshScpConnection.java @@ -27,7 +27,7 @@ import com.xebialabs.overthere.RuntimeIOException; import com.xebialabs.overthere.spi.AddressPortMapper; -import static com.google.common.base.Preconditions.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS; import static com.xebialabs.overthere.OperatingSystemFamily.ZOS; import static com.xebialabs.overthere.ssh.SshConnectionBuilder.DELETE_DIRECTORY_COMMAND; diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshScpFile.java b/src/main/java/com/xebialabs/overthere/ssh/SshScpFile.java index 6898b684..09c1c795 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshScpFile.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshScpFile.java @@ -28,13 +28,12 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import com.xebialabs.overthere.CmdLine; import com.xebialabs.overthere.OverthereFile; @@ -45,8 +44,6 @@ import net.schmizz.sshj.xfer.LocalSourceFile; import net.schmizz.sshj.xfer.scp.SCPUploadClient; -import static com.google.common.base.Predicates.equalTo; -import static com.google.common.collect.Lists.newArrayList; import static com.xebialabs.overthere.CmdLine.build; import static com.xebialabs.overthere.ssh.SshConnection.NOCD_PSEUDO_COMMAND; import static com.xebialabs.overthere.util.CapturingOverthereExecutionOutputHandler.capturingHandler; @@ -61,8 +58,6 @@ */ class SshScpFile extends SshFile { - private static final Predicate NOT_SELF_OR_PARENT_DIR = Predicates.not(Predicates.or(equalTo("."), equalTo(".."))); - private static final String PERMISSIONS_TOKEN_PATTERN = "[dl\\-]([r\\-][w\\-][xsStT\\-]){3}[@\\.\\+]*"; private static Pattern permissionsTokenPattern = Pattern.compile(PERMISSIONS_TOKEN_PATTERN); @@ -274,10 +269,10 @@ public List listFiles() { throw new RuntimeIOException("Cannot list directory " + this + ": " + capturedStderr.getOutput() + " (errno=" + errno + ")"); } - List files = newArrayList(); + List files = new ArrayList(); for (String lsLine : capturedStdout.getOutputLines()) { // Filter out the '.' and '..' - if (NOT_SELF_OR_PARENT_DIR.apply(lsLine)) { + if (!(".".equals(lsLine) || "..".equals(lsLine))) { files.add(connection.getFile(this, lsLine)); } } @@ -428,7 +423,7 @@ public boolean isDirectory() { @Override public Iterable getChildren(LocalFileFilter filter) throws IOException { - List files = newArrayList(); + List files = new ArrayList(); for (OverthereFile each : f.listFiles()) { files.add(new OverthereFileLocalSourceFile(each)); } diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshSftpConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshSftpConnection.java index 9e6a7834..a590568a 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshSftpConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshSftpConnection.java @@ -33,7 +33,7 @@ import net.schmizz.sshj.sftp.SFTPClient; -import static com.google.common.base.Preconditions.checkState; +import static com.xebialabs.overthere.util.OverthereUtils.checkState; import static com.xebialabs.overthere.util.OverthereUtils.closeQuietly; import static java.lang.String.format; diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshSftpCygwinConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshSftpCygwinConnection.java index 56efe435..9619dce7 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshSftpCygwinConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshSftpCygwinConnection.java @@ -36,7 +36,7 @@ import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.transport.TransportException; -import static com.google.common.base.Preconditions.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; import static com.xebialabs.overthere.ConnectionOptions.FILE_COPY_COMMAND_FOR_WINDOWS; import static com.xebialabs.overthere.ConnectionOptions.FILE_COPY_COMMAND_FOR_WINDOWS_DEFAULT; import static com.xebialabs.overthere.OperatingSystemFamily.UNIX; diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshSftpFile.java b/src/main/java/com/xebialabs/overthere/ssh/SshSftpFile.java index 3e756720..758b8f58 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshSftpFile.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshSftpFile.java @@ -22,26 +22,20 @@ */ package com.xebialabs.overthere.ssh; +import com.xebialabs.overthere.OverthereFile; +import com.xebialabs.overthere.RuntimeIOException; +import net.schmizz.sshj.sftp.*; +import net.schmizz.sshj.xfer.FilePermission; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.EnumSet; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.xebialabs.overthere.OverthereFile; -import com.xebialabs.overthere.RuntimeIOException; - -import net.schmizz.sshj.sftp.FileAttributes; -import net.schmizz.sshj.sftp.FileMode; -import net.schmizz.sshj.sftp.OpenMode; -import net.schmizz.sshj.sftp.RemoteFile; -import net.schmizz.sshj.sftp.RemoteResourceInfo; -import net.schmizz.sshj.sftp.SFTPClient; -import net.schmizz.sshj.xfer.FilePermission; -import static com.google.common.collect.Lists.newArrayList; -import static com.google.common.collect.Sets.newHashSet; import static com.xebialabs.overthere.util.OverthereUtils.closeQuietly; import static java.lang.String.format; @@ -127,7 +121,7 @@ public List listFiles() { List ls = connection.getSharedSftpClient().ls(getSftpPath()); // copy files to list, skipping . and .. - List files = newArrayList(); + List files = new ArrayList(); for (RemoteResourceInfo l : ls) { String filename = l.getName(); if (filename.equals(".") || filename.equals("..")) { @@ -231,7 +225,7 @@ public InputStream getInputStream() { try { final SFTPClient sftp = connection.connectSftp(); - final RemoteFile remoteFile = sftp.open(getSftpPath(), newHashSet(OpenMode.READ)); + final RemoteFile remoteFile = sftp.open(getSftpPath(), EnumSet.of(OpenMode.READ)); final InputStream wrapped = remoteFile.new RemoteFileInputStream(); return asBuffered(new InputStream() { @@ -298,7 +292,7 @@ public OutputStream getOutputStream() { try { final SFTPClient sftp = connection.connectSftp(); - final RemoteFile remoteFile = sftp.open(getSftpPath(), newHashSet(OpenMode.CREAT, OpenMode.WRITE, OpenMode.TRUNC)); + final RemoteFile remoteFile = sftp.open(getSftpPath(), EnumSet.of(OpenMode.CREAT, OpenMode.WRITE, OpenMode.TRUNC)); final OutputStream wrapped = remoteFile.new RemoteFileOutputStream(); return asBuffered(new OutputStream() { diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshSftpUnixConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshSftpUnixConnection.java index 62ce19e8..1560ce2d 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshSftpUnixConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshSftpUnixConnection.java @@ -25,7 +25,7 @@ import com.xebialabs.overthere.ConnectionOptions; import com.xebialabs.overthere.spi.AddressPortMapper; -import static com.google.common.base.Preconditions.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS; import static com.xebialabs.overthere.ssh.SshConnectionBuilder.SSH_PROTOCOL; diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshSftpWinSshdConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshSftpWinSshdConnection.java index 1071291b..d4956f99 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshSftpWinSshdConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshSftpWinSshdConnection.java @@ -29,7 +29,7 @@ import com.xebialabs.overthere.RuntimeIOException; import com.xebialabs.overthere.spi.AddressPortMapper; -import static com.google.common.base.Preconditions.checkArgument; +import static com.xebialabs.overthere.util.OverthereUtils.checkArgument; import static com.xebialabs.overthere.OperatingSystemFamily.WINDOWS; import static com.xebialabs.overthere.ssh.SshConnectionBuilder.SSH_PROTOCOL; import static java.lang.Character.toUpperCase; diff --git a/src/main/java/com/xebialabs/overthere/ssh/SshTunnelConnection.java b/src/main/java/com/xebialabs/overthere/ssh/SshTunnelConnection.java index 398f89d7..d188fb54 100644 --- a/src/main/java/com/xebialabs/overthere/ssh/SshTunnelConnection.java +++ b/src/main/java/com/xebialabs/overthere/ssh/SshTunnelConnection.java @@ -26,15 +26,17 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.ReentrantLock; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.util.concurrent.Monitor; import com.xebialabs.overthere.CmdLine; import com.xebialabs.overthere.ConnectionOptions; @@ -50,9 +52,7 @@ import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.transport.TransportException; -import static com.google.common.base.Preconditions.checkState; -import static com.google.common.collect.Lists.newArrayList; -import static com.google.common.collect.Maps.newHashMap; +import static com.xebialabs.overthere.util.OverthereUtils.checkState; import static com.xebialabs.overthere.ssh.SshConnectionBuilder.PORT_ALLOCATION_RANGE_START; import static com.xebialabs.overthere.ssh.SshConnectionBuilder.PORT_ALLOCATION_RANGE_START_DEFAULT; import static com.xebialabs.overthere.util.OverthereUtils.closeQuietly; @@ -68,13 +68,13 @@ public class SshTunnelConnection extends SshConnection implements AddressPortMap private static final int MAX_PORT = 65535; - private Map localPortForwards = newHashMap(); + private Map localPortForwards = new HashMap(); - private List portForwarders = newArrayList(); + private List portForwarders = new ArrayList(); private int startPortRange; - private final Monitor M = new Monitor(); + private final ReentrantLock lock = new ReentrantLock(); public SshTunnelConnection(final String protocol, final ConnectionOptions options, final AddressPortMapper mapper) { super(protocol, options, mapper); @@ -99,7 +99,7 @@ public void doClose() { @Override public InetSocketAddress map(InetSocketAddress address) { - M.enter(); + lock.lock(); try { if (localPortForwards.containsKey(address)) { return localPortForwards.get(address); @@ -112,7 +112,7 @@ public InetSocketAddress map(InetSocketAddress address) { localPortForwards.put(address, localAddress); return localAddress; } finally { - M.leave(); + lock.unlock(); } } @@ -206,10 +206,10 @@ public void close() throws IOException { static class TunnelPortManager { private AtomicInteger lastBoundPort = new AtomicInteger(0); - private Monitor M = new Monitor(); + private ReentrantLock lock = new ReentrantLock(); ServerSocket bindToNextFreePort(int startFrom) { - M.enter(); + lock.lock(); try { int firstPort = Math.max(startFrom, lastBoundPort.get() + 1); int port = firstPort; @@ -233,11 +233,10 @@ ServerSocket bindToNextFreePort(int startFrom) { } } } finally { - M.leave(); + lock.unlock(); } } - @VisibleForTesting protected ServerSocket tryBind(int localPort) { try { ServerSocket ss = new ServerSocket(); diff --git a/src/main/java/com/xebialabs/overthere/util/ByteArrayFile.java b/src/main/java/com/xebialabs/overthere/util/ByteArrayFile.java index e0749b79..498be133 100644 --- a/src/main/java/com/xebialabs/overthere/util/ByteArrayFile.java +++ b/src/main/java/com/xebialabs/overthere/util/ByteArrayFile.java @@ -25,6 +25,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; import java.util.List; import com.xebialabs.overthere.ConnectionOptions; @@ -32,7 +33,6 @@ import com.xebialabs.overthere.OverthereFile; import com.xebialabs.overthere.spi.BaseOverthereFile; -import static com.google.common.collect.Lists.newArrayList; import static com.xebialabs.overthere.ConnectionOptions.OPERATING_SYSTEM; import static com.xebialabs.overthere.ConnectionOptions.TEMPORARY_DIRECTORY_PATH; import static com.xebialabs.overthere.OperatingSystemFamily.getLocalHostOperatingSystemFamily; @@ -152,7 +152,7 @@ public void deleteRecursively() { @Override public List listFiles() { - return newArrayList(); + return new ArrayList(); } @Override diff --git a/src/main/java/com/xebialabs/overthere/util/CapturingOverthereExecutionOutputHandler.java b/src/main/java/com/xebialabs/overthere/util/CapturingOverthereExecutionOutputHandler.java index fe09612e..d1e9142e 100644 --- a/src/main/java/com/xebialabs/overthere/util/CapturingOverthereExecutionOutputHandler.java +++ b/src/main/java/com/xebialabs/overthere/util/CapturingOverthereExecutionOutputHandler.java @@ -22,13 +22,13 @@ */ package com.xebialabs.overthere.util; +import com.xebialabs.overthere.OverthereExecutionOutputHandler; + +import java.util.ArrayList; import java.util.Collections; import java.util.List; -import com.google.common.collect.Lists; - -import com.xebialabs.overthere.OverthereExecutionOutputHandler; -import static com.google.common.base.Joiner.on; +import static com.xebialabs.overthere.util.OverthereUtils.mkString; import static java.util.Collections.unmodifiableList; /** @@ -36,7 +36,7 @@ */ public class CapturingOverthereExecutionOutputHandler implements OverthereExecutionOutputHandler { - private List outputLines = Collections.synchronizedList(Lists.newArrayList()); + private List outputLines = Collections.synchronizedList(new ArrayList()); private CapturingOverthereExecutionOutputHandler() { } @@ -66,7 +66,7 @@ public List getOutputLines() { * @return the captured regular output as one string. */ public String getOutput() { - return on('\n').join(outputLines); + return mkString(outputLines, '\n'); } /** diff --git a/src/main/java/com/xebialabs/overthere/util/CapturingOverthereProcessOutputHandler.java b/src/main/java/com/xebialabs/overthere/util/CapturingOverthereProcessOutputHandler.java index 81632de1..8c5f0b7c 100644 --- a/src/main/java/com/xebialabs/overthere/util/CapturingOverthereProcessOutputHandler.java +++ b/src/main/java/com/xebialabs/overthere/util/CapturingOverthereProcessOutputHandler.java @@ -22,12 +22,12 @@ */ package com.xebialabs.overthere.util; +import com.xebialabs.overthere.OverthereProcessOutputHandler; + import java.util.ArrayList; import java.util.List; -import com.xebialabs.overthere.OverthereProcessOutputHandler; - -import static com.google.common.base.Joiner.on; +import static com.xebialabs.overthere.util.OverthereUtils.mkString; import static java.util.Collections.unmodifiableList; /** @@ -79,7 +79,7 @@ public List getOutputLines() { * @return the captured regular output as one string. */ public String getOutput() { - return on('\n').join(outputLines); + return mkString(outputLines, '\n'); } /** @@ -97,7 +97,7 @@ public List getErrorLines() { * @return the captured error output as one string. */ public String getError() { - return on('\n').join(errorLines); + return mkString(errorLines, '\n'); } /** @@ -115,7 +115,7 @@ public List getAllLines() { * @return the captured regular and error output as one string. */ public String getAll() { - return on('\n').join(allLines); + return mkString(allLines, '\n'); } /** @@ -129,4 +129,6 @@ public static CapturingOverthereProcessOutputHandler capturingHandler() { return new CapturingOverthereProcessOutputHandler(); } + + } diff --git a/src/main/java/com/xebialabs/overthere/util/OverthereFileCopier.java b/src/main/java/com/xebialabs/overthere/util/OverthereFileCopier.java index c8146874..acd0fd30 100644 --- a/src/main/java/com/xebialabs/overthere/util/OverthereFileCopier.java +++ b/src/main/java/com/xebialabs/overthere/util/OverthereFileCopier.java @@ -22,23 +22,16 @@ */ package com.xebialabs.overthere.util; +import com.xebialabs.overthere.OverthereFile; +import com.xebialabs.overthere.RuntimeIOException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Stack; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.io.ByteSink; -import com.google.common.io.ByteSource; -import com.google.common.io.ByteStreams; - -import com.xebialabs.overthere.ConnectionOptions; -import com.xebialabs.overthere.OverthereFile; -import com.xebialabs.overthere.RuntimeIOException; -import static com.xebialabs.overthere.ConnectionOptions.REMOTE_COPY_BUFFER_SIZE; -import static com.xebialabs.overthere.ConnectionOptions.REMOTE_COPY_BUFFER_SIZE_DEFAULT; import static com.xebialabs.overthere.util.OverthereUtils.closeQuietly; /** @@ -147,17 +140,21 @@ private static void copyFile(final OverthereFile srcFile, final OverthereFile ds logger.debug("About to overwrite existing file {}", dstFile); try { - new ByteSource() { - @Override - public InputStream openStream() throws IOException { - return srcFile.getInputStream(); - } - }.copyTo(new ByteSink() { - @Override - public OutputStream openStream() throws IOException { - return dstFile.getOutputStream(); + InputStream is = srcFile.getInputStream(); + try { + OutputStream os = dstFile.getOutputStream(); + try { + byte[] bytes = new byte[1024]; + int nRead; + while ((nRead = is.read(bytes, 0, bytes.length)) != -1) { + os.write(bytes, 0, nRead); + } + } finally { + closeQuietly(os); } - }); + } finally { + closeQuietly(is); + } } catch (IOException exc) { throw new RuntimeIOException("Cannot copy " + srcFile + " to " + dstFile, exc); } diff --git a/src/main/java/com/xebialabs/overthere/util/OverthereUtils.java b/src/main/java/com/xebialabs/overthere/util/OverthereUtils.java index 82625dd7..c426ebe3 100644 --- a/src/main/java/com/xebialabs/overthere/util/OverthereUtils.java +++ b/src/main/java/com/xebialabs/overthere/util/OverthereUtils.java @@ -22,20 +22,22 @@ */ package com.xebialabs.overthere.util; -import java.io.*; -import com.google.common.io.ByteStreams; -import com.google.common.io.CharStreams; -import com.google.common.io.Closeables; -import com.google.common.io.InputSupplier; - import com.xebialabs.overthere.OverthereFile; import com.xebialabs.overthere.RuntimeIOException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.util.List; + +import static java.lang.String.format; /** * Contains a number of static helper methods. */ public class OverthereUtils { + private static final Logger logger = LoggerFactory.getLogger(OverthereUtils.class); /** * Reads the contents of an {@link OverthereFile} into a byte array. * @@ -43,15 +45,20 @@ public class OverthereUtils { * @returns the byte array. */ public static byte[] read(final OverthereFile from) { + InputStream is = from.getInputStream(); try { - return ByteStreams.toByteArray(new InputSupplier() { - @Override - public InputStream getInput() throws IOException { - return from.getInputStream(); - } - }); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + int nRead; + byte[] bytes = new byte[1024]; + while ((nRead = is.read(bytes, 0, bytes.length)) != -1) { + os.write(bytes, 0, nRead); + } + + return os.toByteArray(); } catch (IOException exc) { throw new RuntimeException(exc); + } finally { + closeQuietly(is); } } @@ -63,15 +70,21 @@ public InputStream getInput() throws IOException { * @returns the string. */ public static String read(final OverthereFile from, final String charsetName) { + InputStream is = from.getInputStream(); try { - return CharStreams.toString(new InputSupplier() { - @Override - public Reader getInput() throws IOException { - return new InputStreamReader(from.getInputStream(), charsetName); - } - }); + InputStreamReader isr = new InputStreamReader(is, charsetName); + StringBuilder b = new StringBuilder(); + int nRead; + char[] chars = new char[1024]; + while ((nRead = isr.read(chars, 0, chars.length)) != -1) { + b.append(chars, 0, nRead); + } + + return b.toString(); } catch (IOException exc) { throw new RuntimeException(exc); + } finally { + closeQuietly(is); } } @@ -161,11 +174,45 @@ public static String constructPath(final OverthereFile parent, final String chil } public static void closeQuietly(Closeable c) { + if (c == null) return; try { - Closeables.close(c, true); + c.close(); } catch (IOException e) { + logger.warn("IOException while closing closeable", e); // Will not happen because of true... } } + public static void checkArgument(boolean expression, String errorMessage, Object... messageParams) { + if (!expression) { + throw new IllegalArgumentException(format(errorMessage, messageParams)); + } + } + + public static void checkState(boolean expression, String errorMessage, Object... messageParams) { + if (!expression) { + throw new IllegalStateException(format(errorMessage, messageParams)); + } + } + + public static T checkNotNull(T t, String errorMessage, Object... messageParams) { + if (t == null) { + throw new NullPointerException(format(errorMessage, messageParams)); + } + return t; + } + + public static String mkString(List strings, char sep) { + return mkString(strings, String.valueOf(sep)); + } + + public static String mkString(List strings, String sep) { + if (strings.isEmpty()) return ""; + + StringBuilder b = new StringBuilder(strings.get(0)); + for (int i = 1; i < strings.size(); i++) { + b.append(sep).append(strings.get(i)); + } + return b.toString(); + } } diff --git a/src/test/java/com/xebialabs/overthere/UnixCloudHostListener.java b/src/test/java/com/xebialabs/overthere/UnixCloudHostListener.java index 0402444d..98eddf5e 100644 --- a/src/test/java/com/xebialabs/overthere/UnixCloudHostListener.java +++ b/src/test/java/com/xebialabs/overthere/UnixCloudHostListener.java @@ -26,7 +26,7 @@ import com.xebialabs.overcast.host.CloudHost; -import static com.google.common.base.Preconditions.checkNotNull; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; public class UnixCloudHostListener extends CloudHostListener { diff --git a/src/test/java/com/xebialabs/overthere/WindowsCloudHostListener.java b/src/test/java/com/xebialabs/overthere/WindowsCloudHostListener.java index ee2f0fb3..5b6fd246 100644 --- a/src/test/java/com/xebialabs/overthere/WindowsCloudHostListener.java +++ b/src/test/java/com/xebialabs/overthere/WindowsCloudHostListener.java @@ -26,7 +26,7 @@ import com.xebialabs.overcast.host.CloudHost; -import static com.google.common.base.Preconditions.checkNotNull; +import static com.xebialabs.overthere.util.OverthereUtils.checkNotNull; public class WindowsCloudHostListener extends CloudHostListener {