com.github.dockerjava.core.InvocationBuilder Maven / Gradle / Ivy
The newest version!
package com.github.dockerjava.core;
import com.fasterxml.jackson.core.type.TypeReference;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch;
public interface InvocationBuilder {
InvocationBuilder accept(MediaType mediaType);
InvocationBuilder header(String name, String value);
void delete();
void get(ResultCallback resultCallback);
T get(TypeReference typeReference);
void get(TypeReference typeReference, ResultCallback resultCallback);
InputStream post(Object entity);
void post(Object entity, InputStream stdin, ResultCallback resultCallback);
T post(Object entity, TypeReference typeReference);
void post(Object entity, TypeReference typeReference, ResultCallback resultCallback);
T post(TypeReference typeReference, InputStream body);
void post(TypeReference typeReference, ResultCallback resultCallback, InputStream body);
void postStream(InputStream body);
InputStream get();
void put(InputStream body, MediaType mediaType);
/**
* Implementation of {@link ResultCallback} with the single result event expected.
*/
class AsyncResultCallback
extends ResultCallbackTemplate, A_RES_T> {
private A_RES_T result = null;
private final CountDownLatch resultReady = new CountDownLatch(1);
@Override
public void onNext(A_RES_T object) {
onResult(object);
}
private void onResult(A_RES_T object) {
if (resultReady.getCount() == 0) {
throw new IllegalStateException("Result has already been set");
}
try {
result = object;
} finally {
resultReady.countDown();
}
}
@Override
public void close() throws IOException {
try {
super.close();
} finally {
resultReady.countDown();
}
}
/**
* Blocks until {@link ResultCallback#onNext(Object)} was called for the first time
*/
@SuppressWarnings("unchecked")
public A_RES_T awaitResult() {
try {
resultReady.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
throwFirstError();
return result;
}
}
}