Skip to content

Commit

Permalink
Drop guava (fixes #74)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmandalidis committed Nov 13, 2019
1 parent 416920c commit 2baa965
Show file tree
Hide file tree
Showing 23 changed files with 197 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@

package org.mandas.docker.client;

import com.google.common.collect.Maps;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

import org.mandas.docker.client.exceptions.DockerException;
import org.mandas.docker.client.messages.ProgressMessage;

import java.io.PrintStream;
import java.util.Map;

/**
* Parses ProgressMessage objects and writes the output to a PrintStream. The output includes ANSI
* escape characters to move the cursor around to nicely print progress bars.
Expand All @@ -46,7 +45,7 @@ public AnsiProgressHandler() {

public AnsiProgressHandler(PrintStream out) {
this.out = out;
idsToLines = Maps.newHashMap();
idsToLines = new HashMap<>();
}

@Override
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/org/mandas/docker/client/CompressedDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
package org.mandas.docker.client;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Collections.reverse;
import static java.util.Collections.unmodifiableList;
import static org.apache.commons.compress.archivers.tar.TarArchiveOutputStream.BIGNUMBER_POSIX;
import static org.apache.commons.compress.archivers.tar.TarArchiveOutputStream.LONGFILE_POSIX;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;

import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -44,7 +43,9 @@
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -104,8 +105,7 @@ public static CompressedDirectory create(final Path directory) throws IOExceptio
final Path file = Files.createTempFile("docker-client-", ".tar.gz");

final Path dockerIgnorePath = directory.resolve(".dockerignore");
final ImmutableList<DockerIgnorePathMatcher> ignoreMatchers =
parseDockerIgnore(dockerIgnorePath);
final List<DockerIgnorePathMatcher> ignoreMatchers = parseDockerIgnore(dockerIgnorePath);

try (final OutputStream fileOut = Files.newOutputStream(file);
final GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(fileOut);
Expand Down Expand Up @@ -137,9 +137,9 @@ public void close() throws IOException {
Files.delete(file);
}

static ImmutableList<DockerIgnorePathMatcher> parseDockerIgnore(Path dockerIgnorePath)
static List<DockerIgnorePathMatcher> parseDockerIgnore(Path dockerIgnorePath)
throws IOException {
final ImmutableList.Builder<DockerIgnorePathMatcher> matchersBuilder = ImmutableList.builder();
final List<DockerIgnorePathMatcher> matchersBuilder = new ArrayList<>();

if (Files.isReadable(dockerIgnorePath) && Files.isRegularFile(dockerIgnorePath)) {
for (final String line : Files.readAllLines(dockerIgnorePath, StandardCharsets.UTF_8)) {
Expand All @@ -158,7 +158,7 @@ static ImmutableList<DockerIgnorePathMatcher> parseDockerIgnore(Path dockerIgnor
}
}

return matchersBuilder.build();
return unmodifiableList(matchersBuilder);
}

private static String createPattern(String line) {
Expand All @@ -172,7 +172,6 @@ private static String createPattern(String line) {
return pattern.replace("/", "\\\\");
}

@VisibleForTesting
static PathMatcher goPathMatcher(FileSystem fs, String pattern) {
// Supposed to work the same way as Go's path.filepath.match.Match:
// http://golang.org/src/path/filepath/match.go#L34
Expand Down Expand Up @@ -253,15 +252,16 @@ private static String getNotSeparatorPattern(String separator) {
private static class Visitor extends SimpleFileVisitor<Path> {

private final Path root;
private final ImmutableList<DockerIgnorePathMatcher> ignoreMatchers;
private final List<DockerIgnorePathMatcher> ignoreMatchers;
private final TarArchiveOutputStream tarStream;

private Visitor(final Path root, ImmutableList<DockerIgnorePathMatcher> ignoreMatchers,
private Visitor(final Path root, List<DockerIgnorePathMatcher> ignoreMatchers,
final TarArchiveOutputStream tarStream) {
this.root = root;
// .dockerignore matchers need to be read from the bottom of the file,
// so the given list should be reversed before using it.
this.ignoreMatchers = ignoreMatchers.reverse();
this.ignoreMatchers = new ArrayList<>(ignoreMatchers);
reverse(this.ignoreMatchers);
this.tarStream = tarStream;
}

Expand Down Expand Up @@ -312,7 +312,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
* @param path the path to match
* @return <code>true</code> if the given path should be excluded, <code>false</code> otherwise
*/
private static boolean exclude(ImmutableList<DockerIgnorePathMatcher> matchers, Path path) {
private static boolean exclude(List<DockerIgnorePathMatcher> matchers, Path path) {
for (final DockerIgnorePathMatcher matcher : matchers) {
if (matcher.matches(path)) {
return matcher.isExclude();
Expand Down
60 changes: 29 additions & 31 deletions src/main/java/org/mandas/docker/client/DefaultDockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.Maps.newHashMap;
import static org.mandas.docker.client.ObjectMapperProvider.objectMapper;
import static org.mandas.docker.client.VersionCompare.compareVersion;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;
import static java.util.concurrent.TimeUnit.SECONDS;
import static javax.ws.rs.HttpMethod.DELETE;
import static javax.ws.rs.HttpMethod.GET;
Expand All @@ -40,6 +39,11 @@
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
import static org.mandas.docker.client.DockerHost.certPathFromEnv;
import static org.mandas.docker.client.DockerHost.configPathFromEnv;
import static org.mandas.docker.client.DockerHost.defaultCertPath;
import static org.mandas.docker.client.ObjectMapperProvider.objectMapper;
import static org.mandas.docker.client.VersionCompare.compareVersion;

import java.io.Closeable;
import java.io.IOException;
Expand All @@ -56,14 +60,15 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -109,21 +114,6 @@
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.CharStreams;
import com.google.common.net.HostAndPort;
import org.mandas.docker.client.auth.ConfigFileRegistryAuthSupplier;
import org.mandas.docker.client.auth.FixedRegistryAuthSupplier;
import org.mandas.docker.client.auth.RegistryAuthSupplier;
Expand Down Expand Up @@ -196,6 +186,14 @@
import org.mandas.docker.client.messages.swarm.Task;
import org.mandas.docker.client.messages.swarm.UnlockKey;
import org.mandas.docker.client.npipe.NpipeConnectionSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Preconditions;
import com.google.common.io.CharStreams;
import com.google.common.net.HostAndPort;

public class DefaultDockerClient implements DockerClient, Closeable {

Expand Down Expand Up @@ -264,7 +262,7 @@ private LoadProgressHandler(ProgressHandler delegate) {
}

private Set<String> getImageNames() {
return ImmutableSet.copyOf(imageNames);
return unmodifiableSet(new HashSet<>(imageNames));
}

@Override
Expand Down Expand Up @@ -633,14 +631,14 @@ public List<Container> listContainers(final ListContainersParam... params)

private WebTarget addParameters(WebTarget resource, final Param... params)
throws DockerException {
final Map<String, List<String>> filters = newHashMap();
final Map<String, List<String>> filters = new HashMap<>();
for (final Param param : params) {
if (param instanceof FilterParam) {
List<String> filterValueList;
if (filters.containsKey(param.name())) {
filterValueList = filters.get(param.name());
} else {
filterValueList = Lists.newArrayList();
filterValueList = new ArrayList<>();
}
filterValueList.add(param.value());
filters.put(param.name(), filterValueList);
Expand All @@ -660,7 +658,7 @@ private WebTarget addParameters(WebTarget resource, final Param... params)

private Map<String, String> getQueryParamMap(final WebTarget resource) {
final String queryParams = resource.getUri().getQuery();
final Map<String, String> paramsMap = Maps.newHashMap();
final Map<String, String> paramsMap = new HashMap<>();
if (queryParams != null) {
for (final String queryParam : queryParams.split("&")) {
final String[] kv = queryParam.split("=");
Expand Down Expand Up @@ -969,7 +967,7 @@ public TopResults topContainer(final String containerId, final String psArgs)
throws DockerException, InterruptedException {
try {
WebTarget resource = resource().path("containers").path(containerId).path("top");
if (!Strings.isNullOrEmpty(psArgs)) {
if (psArgs != null && "".equals(psArgs.trim())) {
resource = resource.queryParam("ps_args", psArgs);
}
return request(GET, TopResults.class, resource, resource.request(APPLICATION_JSON_TYPE));
Expand Down Expand Up @@ -2371,7 +2369,7 @@ public void resizeTty(final String containerId, final Integer height, final Inte
private void checkTtyParams(final Integer height, final Integer width) throws BadParamException {
if ((height == null && width == null) || (height != null && height == 0)
|| (width != null && width == 0)) {
final Map<String, String> paramMap = Maps.newHashMap();
final Map<String, String> paramMap = new HashMap<>();
paramMap.put("h", height == null ? null : height.toString());
paramMap.put("w", width == null ? null : width.toString());
throw new BadParamException(paramMap, "Either width or height must be non-null and > 0");
Expand Down Expand Up @@ -2874,12 +2872,12 @@ public static Builder builder() {
*/
public static Builder fromEnv() throws DockerCertificateException {
final String endpoint = DockerHost.endpointFromEnv();
final Path dockerCertPath = Paths.get(Iterables.find(
Arrays.asList(DockerHost.certPathFromEnv(),
DockerHost.configPathFromEnv(),
DockerHost.defaultCertPath()),
Predicates.notNull()));

final Path dockerCertPath = Paths.get(asList(certPathFromEnv(), configPathFromEnv(), defaultCertPath())
.stream()
.filter(cert -> cert != null)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Cannot find docker certificated path")));
final Builder builder = new Builder();

final Optional<DockerCertificatesStore> certs = DockerCertificates.builder()
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/mandas/docker/client/DefaultLogStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Throwables.throwIfUnchecked;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.common.collect.AbstractIterator;
import com.google.common.io.Closer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import com.google.common.collect.AbstractIterator;
import com.google.common.io.Closer;

class DefaultLogStream extends AbstractIterator<LogMessage> implements LogStream {

private final LogReader reader;
Expand All @@ -41,7 +39,6 @@ private DefaultLogStream(final InputStream stream) {
this(new LogReader(stream));
}

@VisibleForTesting
DefaultLogStream(final LogReader reader) {
this.reader = reader;
}
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/org/mandas/docker/client/DockerCertificates.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

package org.mandas.docker.client;

import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import org.mandas.docker.client.exceptions.DockerCertificateException;
import static java.util.Collections.unmodifiableSet;
import static java.util.stream.Collectors.joining;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -45,7 +43,10 @@
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import javax.net.ssl.HostnameVerifier;
Expand All @@ -56,6 +57,7 @@
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.mandas.docker.client.exceptions.DockerCertificateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -70,7 +72,7 @@ public class DockerCertificates implements DockerCertificatesStore {
public static final String DEFAULT_CLIENT_KEY_NAME = "key.pem";

private static final char[] KEY_STORE_PASSWORD = "docker!!11!!one!".toCharArray();
private static final Set<String> PRIVATE_KEY_ALGS = ImmutableSet.of("RSA", "EC");
private static final Set<String> PRIVATE_KEY_ALGS = unmodifiableSet(new HashSet<>(Arrays.asList("RSA", "EC")));
private static final Logger log = LoggerFactory.getLogger(DockerCertificates.class);

private final SSLContext sslContext;
Expand Down Expand Up @@ -170,7 +172,7 @@ private static PrivateKey tryGeneratePrivateKey(final PKCS8EncodedKeySpec spec,
}

final String error = String.format("Could not generate private key from spec. Tried using %s",
Joiner.on(", ").join(algorithms));
algorithms.stream().collect(joining(", ")));
throw new InvalidKeySpecException(error);
}

Expand Down Expand Up @@ -250,14 +252,14 @@ public Builder sslFactory(final SslContextFactory sslContextFactory) {
public Optional<DockerCertificatesStore> build() throws DockerCertificateException {
if (this.caCertPath == null || this.clientKeyPath == null || this.clientCertPath == null) {
log.debug("caCertPath, clientKeyPath or clientCertPath not specified, not using SSL");
return Optional.absent();
return Optional.empty();
} else if (Files.exists(this.caCertPath) && Files.exists(this.clientKeyPath)
&& Files.exists(this.clientCertPath)) {
return Optional.of((DockerCertificatesStore) new DockerCertificates(this));
} else {
log.debug("{}, {} or {} does not exist, not using SSL", this.caCertPath, this.clientKeyPath,
this.clientCertPath);
return Optional.absent();
return Optional.empty();
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/mandas/docker/client/DockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.mandas.docker.client.exceptions.NotFoundException;
import org.mandas.docker.client.exceptions.PermissionException;
import org.mandas.docker.client.exceptions.ServiceNotFoundException;
import org.mandas.docker.client.exceptions.UnsupportedApiVersionException;
import org.mandas.docker.client.messages.Container;
import org.mandas.docker.client.messages.ContainerChange;
import org.mandas.docker.client.messages.ContainerConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;

public class DockerConfigReader {
private static final Logger LOG = LoggerFactory.getLogger(DockerConfigReader.class);
Expand Down Expand Up @@ -70,7 +69,6 @@ public RegistryAuth anyRegistryAuth() throws IOException {
* @param configPath Path to the docker config file.
* @return Some registry auth value.
*/
@VisibleForTesting
RegistryAuth anyRegistryAuth(final Path configPath) throws IOException {
final Collection<RegistryAuth> registryAuths =
authForAllRegistries(configPath).configs().values();
Expand Down
Loading

0 comments on commit 2baa965

Please sign in to comment.