package io.undertow.client;
import org.xnio.FutureResult;
import org.xnio.IoFuture;
import org.xnio.OptionMap;
import io.undertow.connector.ByteBufferPool;
import org.xnio.XnioIoThread;
import org.xnio.XnioWorker;
import org.xnio.ssl.XnioSsl;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
public final class UndertowClient {
private final Map<String, ClientProvider> clientProviders;
private static final UndertowClient INSTANCE = new UndertowClient();
private UndertowClient() {
this(UndertowClient.class.getClassLoader());
}
private UndertowClient(final ClassLoader classLoader) {
ServiceLoader<ClientProvider> providers = ServiceLoader.load(ClientProvider.class, classLoader);
final Map<String, ClientProvider> map = new HashMap<>();
for (ClientProvider provider : providers) {
for (String scheme : provider.handlesSchemes()) {
map.put(scheme, provider);
}
}
this.clientProviders = Collections.unmodifiableMap(map);
}
public IoFuture<ClientConnection> connect(final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
return connect(uri, worker, null, bufferPool, options);
}
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
return connect(bindAddress, uri, worker, null, bufferPool, options);
}
public IoFuture<ClientConnection> connect(final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
return connect((InetSocketAddress) null, uri, worker, ssl, bufferPool, options);
}
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
final FutureResult<ClientConnection> result = new FutureResult<>();
provider.connect(new ClientCallback<ClientConnection>() {
@Override
public void completed(ClientConnection r) {
result.setResult(r);
}
@Override
public void failed(IOException e) {
result.setException(e);
}
}, bindAddress, uri, worker, ssl, bufferPool, options);
return result.getIoFuture();
}
public IoFuture<ClientConnection> connect(final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
return connect((InetSocketAddress) null, uri, ioThread, null, bufferPool, options);
}
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
return connect(bindAddress, uri, ioThread, null, bufferPool, options);
}
public IoFuture<ClientConnection> connect(final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
return connect((InetSocketAddress) null, uri, ioThread, ssl, bufferPool, options);
}
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
final FutureResult<ClientConnection> result = new FutureResult<>();
provider.connect(new ClientCallback<ClientConnection>() {
@Override
public void completed(ClientConnection r) {
result.setResult(r);
}
@Override
public void failed(IOException e) {
result.setException(e);
}
}, bindAddress, uri, ioThread, ssl, bufferPool, options);
return result.getIoFuture();
}
public void connect(final ClientCallback<ClientConnection> listener, final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
connect(listener, uri, worker, null, bufferPool, options);
}
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
connect(listener, bindAddress, uri, worker, null, bufferPool, options);
}
public void connect(final ClientCallback<ClientConnection> listener, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, uri, worker, ssl, bufferPool, options);
}
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, bindAddress, uri, worker, ssl, bufferPool, options);
}
public void connect(final ClientCallback<ClientConnection> listener, final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
connect(listener, uri, ioThread, null, bufferPool, options);
}
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
connect(listener, bindAddress, uri, ioThread, null, bufferPool, options);
}
public void connect(final ClientCallback<ClientConnection> listener, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, uri, ioThread, ssl, bufferPool, options);
}
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, bindAddress, uri, ioThread, ssl, bufferPool, options);
}
private ClientProvider getClientProvider(URI uri) {
ClientProvider provider = clientProviders.get(uri.getScheme());
if (provider == null) {
throw UndertowClientMessages.MESSAGES.unknownScheme(uri);
}
return provider;
}
public static UndertowClient getInstance() {
return INSTANCE;
}
public static UndertowClient getInstance(final ClassLoader classLoader) {
return new UndertowClient(classLoader);
}
}