|
| 1 | +package com.github.dockerjava.jaxrs; |
| 2 | + |
| 3 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | + |
| 5 | +import java.io.InputStream; |
| 6 | +import java.util.concurrent.Callable; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | + |
| 10 | +import javax.ws.rs.client.WebTarget; |
| 11 | +import javax.ws.rs.core.Response; |
| 12 | + |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +import com.fasterxml.jackson.core.JsonFactory; |
| 17 | +import com.fasterxml.jackson.core.JsonParser; |
| 18 | +import com.fasterxml.jackson.core.JsonToken; |
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import com.github.dockerjava.api.command.StatsCallback; |
| 21 | +import com.github.dockerjava.api.command.StatsCmd; |
| 22 | +import com.github.dockerjava.api.model.Statistics; |
| 23 | +import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream; |
| 24 | + |
| 25 | +public class StatsCmdExec extends AbstrDockerCmdExec<StatsCmd, ExecutorService> implements StatsCmd.Exec { |
| 26 | + private static final Logger LOGGER = LoggerFactory.getLogger(StatsCmdExec.class); |
| 27 | + |
| 28 | + public StatsCmdExec(WebTarget baseResource) { |
| 29 | + super(baseResource); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + protected ExecutorService execute(StatsCmd command) { |
| 34 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 35 | + |
| 36 | + WebTarget webResource = getBaseResource().path("/containers/{id}/stats") |
| 37 | + .resolveTemplate("id", command.getContainerId()); |
| 38 | + |
| 39 | + LOGGER.trace("GET: {}", webResource); |
| 40 | + StatsNotifier eventNotifier = StatsNotifier.create(command.getStatsCallback(), webResource); |
| 41 | + executorService.submit(eventNotifier); |
| 42 | + return executorService; |
| 43 | + } |
| 44 | + |
| 45 | + private static class StatsNotifier implements Callable<Void> { |
| 46 | + private static final JsonFactory JSON_FACTORY = new JsonFactory(); |
| 47 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 48 | + |
| 49 | + private final StatsCallback statsCallback; |
| 50 | + private final WebTarget webTarget; |
| 51 | + |
| 52 | + private StatsNotifier(StatsCallback statsCallback, WebTarget webTarget) { |
| 53 | + this.statsCallback = statsCallback; |
| 54 | + this.webTarget = webTarget; |
| 55 | + } |
| 56 | + |
| 57 | + public static StatsNotifier create(StatsCallback statsCallback, WebTarget webTarget) { |
| 58 | + checkNotNull(statsCallback, "An StatsCallback must be provided"); |
| 59 | + checkNotNull(webTarget, "An WebTarget must be provided"); |
| 60 | + return new StatsNotifier(statsCallback, webTarget); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Void call() throws Exception { |
| 65 | + int numEvents = 0; |
| 66 | + Response response = null; |
| 67 | + try { |
| 68 | + response = webTarget.request().get(Response.class); |
| 69 | + InputStream inputStream = new WrappedResponseInputStream( |
| 70 | + response); |
| 71 | + JsonParser jp = JSON_FACTORY.createParser(inputStream); |
| 72 | + // The following condition looks strange but jp.nextToken() will block until there is an |
| 73 | + // event from the docker server or the connection is terminated. |
| 74 | + // therefore we want to check before getting an event (to prevent a blocking operation |
| 75 | + // and after the event to make sure that the eventCallback is still interested in getting notified. |
| 76 | + while (statsCallback.isReceiving() && |
| 77 | + jp.nextToken() != JsonToken.END_OBJECT && !jp.isClosed() && |
| 78 | + statsCallback.isReceiving()) { |
| 79 | + try { |
| 80 | + statsCallback.onStats(OBJECT_MAPPER.readValue(jp, |
| 81 | + Statistics.class)); |
| 82 | + } catch (Exception e) { |
| 83 | + statsCallback.onException(e); |
| 84 | + } |
| 85 | + numEvents++; |
| 86 | + } |
| 87 | + } catch (Exception e) { |
| 88 | + statsCallback.onException(e); |
| 89 | + } finally { |
| 90 | + if (response != null) { |
| 91 | + response.close(); |
| 92 | + } |
| 93 | + try { |
| 94 | + statsCallback.onCompletion(numEvents); |
| 95 | + } catch (Exception e) { |
| 96 | + statsCallback.onException(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments