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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.dockerjava.api.command;

import com.github.dockerjava.api.model.Event;
import com.github.dockerjava.api.model.Filters;

/**
* Get events
Expand All @@ -19,6 +20,10 @@ public interface EventsCmd extends AsyncDockerCmd<EventsCmd, Event, Void> {

public String getUntil();

public Filters getFilters();

public EventsCmd withFilters(Filters filters);

public static interface Exec extends DockerCmdExec<EventsCmd, Void> {
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/github/dockerjava/api/model/EventFilters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.dockerjava.api.model;

import java.util.List;

/**
* Representation of a Docker event filter.
*
* @author Carlos Sanchez <[email protected]>
*
*/
public class EventFilters extends Filters {

/**
* Default constructor for the deserialization.
*/
public EventFilters() {
}

/**
* Constructor.
*
* @param event
* event to filter
*/
public EventFilters(String... event) {
super();
withEvent(event);
}

public Filters withEvent(String... event) {
return withFilter("event", event);
}

public List<String> getEvent() {
return getFilter("event");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.async.ResultCallback;

/**
Expand All @@ -18,6 +21,8 @@
*/
public abstract class ResultCallbackTemplate<T> implements ResultCallback<T> {

private final static Logger LOGGER = LoggerFactory.getLogger(ResultCallbackTemplate.class);

private final CountDownLatch finished = new CountDownLatch(1);

private Closeable stream;
Expand All @@ -34,6 +39,7 @@ public void onNext(T object) {
@Override
public void onError(Throwable throwable) {
try {
LOGGER.error("Error during callback", throwable);
throw new RuntimeException(throwable);
} finally {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.github.dockerjava.core.command;

import static com.google.common.base.Preconditions.*;

import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.EventsCmd;
import com.github.dockerjava.api.model.Event;
import com.github.dockerjava.api.model.Filters;

/**
* Stream docker events
Expand All @@ -13,6 +16,8 @@ public class EventsCmdImpl extends AbstrDockerCmd<EventsCmd, Void> implements Ev

private String until;

private Filters filters;

private ResultCallback<Event> resultCallback;

public EventsCmdImpl(EventsCmd.Exec exec, ResultCallback<Event> resultCallback) {
Expand All @@ -38,6 +43,13 @@ public EventsCmd withResultCallback(ResultCallback<Event> resultCallback) {
return this;
}

@Override
public EventsCmd withFilters(Filters filters) {
checkNotNull(filters, "filters have not been specified");
this.filters = filters;
return this;
}

@Override
public String getSince() {
return since;
Expand All @@ -53,9 +65,15 @@ public ResultCallback<Event> getResultCallback() {
return resultCallback;
}

@Override
public Filters getFilters() {
return filters;
}

@Override
public String toString() {
return new StringBuilder("events").append(since != null ? " --since=" + since : "")
.append(until != null ? " --until=" + until : "").toString();
.append(until != null ? " --until=" + until : "")
.append(filters != null ? " --filters=" + filters : "").toString();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.dockerjava.jaxrs;

import static com.google.common.net.UrlEscapers.*;

import javax.ws.rs.client.WebTarget;

import org.slf4j.Logger;
Expand All @@ -25,6 +27,11 @@ protected Void execute(EventsCmd command) {
WebTarget webTarget = getBaseResource().path("/events").queryParam("since", command.getSince())
.queryParam("until", command.getUntil());

if (command.getFilters() != null) {
webTarget = webTarget.queryParam("filters",
urlPathSegmentEscaper().escape(command.getFilters().toString()));
}

LOGGER.trace("GET: {}", webTarget);

GETCallbackNotifier<Event> callbackNotifier = new GETCallbackNotifier<Event>(new JsonStreamProcessor<Event>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public Void call() throws Exception {
resultCallback.onError(e.getCause());
}
return null;
} catch (Exception e) {
if (resultCallback != null) {
resultCallback.onError(e);
}
return null;
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.EventsCmd;
import com.github.dockerjava.api.model.Event;
import com.github.dockerjava.api.model.EventFilters;
import com.github.dockerjava.client.AbstractDockerClientTest;
import com.github.dockerjava.core.async.ResultCallbackTemplate;

Expand Down Expand Up @@ -94,6 +95,25 @@ public void testEventStreaming1() throws InterruptedException, IOException {
assertTrue(zeroCount, "Received only: " + eventCallback.getEvents());
}

@Test
public void testEventStreamingWithFilter() throws InterruptedException, IOException {
// Don't include other tests events
TimeUnit.SECONDS.sleep(1);

CountDownLatch countDownLatch = new CountDownLatch(1);
EventCallbackTest eventCallback = new EventCallbackTest(countDownLatch);

EventsCmd eventsCmd = dockerClient.eventsCmd(eventCallback).withFilters(new EventFilters().withEvent("start"));
eventsCmd.exec();

generateEvents();

boolean zeroCount = countDownLatch.await(10, TimeUnit.SECONDS);

eventCallback.close();
assertTrue(zeroCount, "Received only: " + eventCallback.getEvents());
}

@Test
public void testEventStreaming2() throws InterruptedException, IOException {
// Don't include other tests events
Expand Down