Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
<slf4j-api.version>1.7.5</slf4j-api.version>
<jsr305.version>1.3.9</jsr305.version>
<jnr.unixsocket.version>0.3</jnr.unixsocket.version>
<guava.version>18.0</guava.version>
<bouncycastle.version>1.51</bouncycastle.version>
<unix-socket-factory.version>2014-11-16T14-41-27</unix-socket-factory.version>

Expand Down Expand Up @@ -150,12 +149,6 @@
<version>${slf4j-api.version}</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
Expand Down
145 changes: 145 additions & 0 deletions src/main/java/com/github/dockerjava/Preconditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* Copyright (C) 2008 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.dockerjava;


public final class Preconditions {
private Preconditions() {}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* @param expression a boolean expression
* @param errorMessageTemplate a template for the exception message should the check fail. The
* message is formed by replacing each {@code %s} placeholder in the template with an
* argument. These are matched by position - the first {@code %s} gets {@code
* errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message
* in square braces. Unmatched placeholders will be left as-is.
* @param errorMessageArgs the arguments to be substituted into the message template. Arguments
* are converted to strings using {@link String#valueOf(Object)}.
* @throws IllegalArgumentException if {@code expression} is false
* @throws NullPointerException if the check fails and either {@code errorMessageTemplate} or
* {@code errorMessageArgs} is null (don't let this happen)
*/
public static void checkArgument(boolean expression,
String errorMessageTemplate,
Object... errorMessageArgs) {
if (!expression) {
throw new IllegalArgumentException(Preconditions.format(errorMessageTemplate, errorMessageArgs));
}
}

/**
* Ensures that an object reference passed as a parameter to the calling method is not null.
*
* @param reference an object reference
* @param errorMessage the exception message to use if the check fails; will be converted to a
* string using {@link String#valueOf(Object)}
* @return the non-null reference that was validated
* @throws NullPointerException if {@code reference} is null
*/
public static <T> T checkNotNull(T reference, Object errorMessage) {
if (reference == null) {
throw new NullPointerException(String.valueOf(errorMessage));
}
return reference;
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* @param expression a boolean expression
* @param errorMessage the exception message to use if the check fails; will be converted to a
* string using {@link String#valueOf(Object)}
* @throws IllegalArgumentException if {@code expression} is false
*/
public static void checkArgument(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}

/**
* Ensures that an object reference passed as a parameter to the calling method is not null.
*
* @param reference an object reference
* @return the non-null reference that was validated
* @throws NullPointerException if {@code reference} is null
*/
public static <T> T checkNotNull(T reference) {
if (reference == null) {
throw new NullPointerException();
}
return reference;
}

/**
* Ensures the truth of an expression involving the state of the calling instance, but not
* involving any parameters to the calling method.
*
* @param expression a boolean expression
* @param errorMessage the exception message to use if the check fails; will be converted to a
* string using {@link String#valueOf(Object)}
* @throws IllegalStateException if {@code expression} is false
*/
public static void checkState(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalStateException(String.valueOf(errorMessage));
}
}

/**
* Substitutes each {@code %s} in {@code template} with an argument. These are matched by
* position: the first {@code %s} gets {@code args[0]}, etc. If there are more arguments than
* placeholders, the unmatched arguments will be appended to the end of the formatted message in
* square braces.
*
* @param template a non-null string containing 0 or more {@code %s} placeholders.
* @param args the arguments to be substituted into the message template. Arguments are converted
* to strings using {@link String#valueOf(Object)}. Arguments can be null.
*/
// Note that this is somewhat-improperly used from Verify.java as well.
public static String format(String template, Object... args) {
template = String.valueOf(template); // null -> "null"

// start substituting the arguments into the '%s' placeholders
StringBuilder builder = new StringBuilder(template.length() + 16 * args.length);
int templateStart = 0;
int i = 0;
while (i < args.length) {
int placeholderStart = template.indexOf("%s", templateStart);
if (placeholderStart == -1) {
break;
}
builder.append(template.substring(templateStart, placeholderStart));
builder.append(args[i++]);
templateStart = placeholderStart + 2;
}
builder.append(template.substring(templateStart));

// if we run out of placeholders, append the extra args in square braces
if (i < args.length) {
builder.append(" [");
builder.append(args[i++]);
while (i < args.length) {
builder.append(", ");
builder.append(args[i++]);
}
builder.append(']');
}

return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.dockerjava.api.command;

import static com.github.dockerjava.jaxrs.util.guava.Guava.join;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Joiner;

/**
*
Expand All @@ -28,17 +29,15 @@ public String[][] getProcesses() {

@Override
public String toString() {
Joiner joiner = Joiner.on("; ").skipNulls();

StringBuffer buffer = new StringBuffer();
buffer.append("[");
for(String[] fields: processes) {
buffer.append("[" + joiner.join(fields) + "]");
buffer.append("[" + join(fields, "; ", true) + "]");
}
buffer.append("]");

return "TopContainerResponse{" +
"titles=" + joiner.join(titles) +
"titles=" + join(titles, "; ", true) +
", processes=" + buffer.toString() +
'}';
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/github/dockerjava/api/model/Device.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.dockerjava.api.model;

import static com.github.dockerjava.Preconditions.checkNotNull;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;

public class Device {

Expand All @@ -22,10 +23,10 @@ public Device() {

public Device(String cGroupPermissions, String pathInContainer,
String pathOnHost) {
Preconditions.checkNotNull(cGroupPermissions,
checkNotNull(cGroupPermissions,
"cGroupPermissions is null");
Preconditions.checkNotNull(pathInContainer, "pathInContainer is null");
Preconditions.checkNotNull(pathOnHost, "pathOnHost is null");
checkNotNull(pathInContainer, "pathInContainer is null");
checkNotNull(pathOnHost, "pathOnHost is null");
this.cGroupPermissions = cGroupPermissions;
this.pathInContainer = pathInContainer;
this.pathOnHost = pathOnHost;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.dockerjava.api.model;

import static com.github.dockerjava.Preconditions.checkNotNull;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;

/**
* Container restart policy
Expand Down Expand Up @@ -36,7 +37,7 @@ public RestartPolicy() {
}

private RestartPolicy(int maximumRetryCount, String name) {
Preconditions.checkNotNull(name, "name is null");
checkNotNull(name, "name is null");
this.maximumRetryCount = maximumRetryCount;
this.name = name;
}
Expand Down
45 changes: 26 additions & 19 deletions src/main/java/com/github/dockerjava/core/DockerClientConfig.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.github.dockerjava.core;

import com.github.dockerjava.api.DockerClientException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.core.NameParser.HostnameReposName;
import com.github.dockerjava.core.NameParser.ReposTag;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import static com.github.dockerjava.Preconditions.checkNotNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.github.dockerjava.api.DockerClientException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.core.NameParser.HostnameReposName;
import com.github.dockerjava.core.NameParser.ReposTag;

public class DockerClientConfig implements Serializable {

private static final long serialVersionUID = -4307357472441531489L;
Expand All @@ -39,18 +41,23 @@ public class DockerClientConfig implements Serializable {
/**
* A map from the environment name to the interval name.
*/
private static final Map<String, String> ENV_NAME_TO_IO_NAME = ImmutableMap.<String, String>builder()
.put("DOCKER_URL", DOCKER_IO_URL_PROPERTY)
.put("DOCKER_VERSION", DOCKER_IO_VERSION_PROPERTY)
.put("DOCKER_USERNAME", DOCKER_IO_USERNAME_PROPERTY)
.put("DOCKER_PASSWORD", DOCKER_IO_PASSWORD_PROPERTY)
.put("DOCKER_EMAIL", DOCKER_IO_EMAIL_PROPERTY)
.put("DOCKER_SERVER_ADDRESS", DOCKER_IO_SERVER_ADDRESS_PROPERTY)
.put("DOCKER_READ_TIMEOUT", DOCKER_IO_READ_TIMEOUT_PROPERTY)
.put("DOCKER_LOGGING_FILTER_ENABLED", DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY)
.put(DOCKER_CERT_PATH_PROPERTY, DOCKER_IO_DOCKER_CERT_PATH_PROPERTY)
.put("DOCKER_CFG_PATH", DOCKER_IO_DOCKER_CFG_PATH_PROPERTY)
.build();
// Immutable ish
private static final Map<String, String> ENV_NAME_TO_IO_NAME;
static {
Map<String, String> m = new HashMap<String, String>();
m.put("DOCKER_URL", DOCKER_IO_URL_PROPERTY);
m.put("DOCKER_VERSION", DOCKER_IO_VERSION_PROPERTY);
m.put("DOCKER_USERNAME", DOCKER_IO_USERNAME_PROPERTY);
m.put("DOCKER_PASSWORD", DOCKER_IO_PASSWORD_PROPERTY);
m.put("DOCKER_EMAIL", DOCKER_IO_EMAIL_PROPERTY);
m.put("DOCKER_SERVER_ADDRESS", DOCKER_IO_SERVER_ADDRESS_PROPERTY);
m.put("DOCKER_READ_TIMEOUT", DOCKER_IO_READ_TIMEOUT_PROPERTY);
m.put("DOCKER_LOGGING_FILTER_ENABLED", DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY);
m.put(DOCKER_CERT_PATH_PROPERTY, DOCKER_IO_DOCKER_CERT_PATH_PROPERTY);
m.put("DOCKER_CFG_PATH", DOCKER_IO_DOCKER_CFG_PATH_PROPERTY);
ENV_NAME_TO_IO_NAME = Collections.unmodifiableMap(m);
}

private static final String DOCKER_IO_PROPERTIES_PROPERTY = "docker.io.properties";
private URI uri;
private final String version, username, password, email, serverAddress, dockerCfgPath;
Expand Down Expand Up @@ -370,7 +377,7 @@ public DockerClientConfigBuilder withProperties(Properties p) {
}

public final DockerClientConfigBuilder withUri(String uri) {
Preconditions.checkNotNull(uri, "uri was not specified");
checkNotNull(uri, "uri was not specified");
this.uri = URI.create(uri);
return this;
}
Expand Down
Loading